You've already forked authentication-service
mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-11-23 11:02:35 +03:00
storage-pg: add tests for user locking
This commit is contained in:
@@ -161,10 +161,11 @@ impl<'c> UserRepository for PgUserRepository<'c> {
|
||||
let id = Ulid::from_datetime_with_source(created_at.into(), rng);
|
||||
tracing::Span::current().record("user.id", tracing::field::display(id));
|
||||
|
||||
sqlx::query!(
|
||||
let res = sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO users (user_id, username, created_at)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (username) DO NOTHING
|
||||
"#,
|
||||
Uuid::from(id),
|
||||
username,
|
||||
@@ -174,6 +175,10 @@ impl<'c> UserRepository for PgUserRepository<'c> {
|
||||
.execute(&mut *self.conn)
|
||||
.await?;
|
||||
|
||||
// If the user already exists, want to return an error but not poison the
|
||||
// transaction
|
||||
DatabaseError::ensure_affected_rows(&res, 1)?;
|
||||
|
||||
Ok(User {
|
||||
id,
|
||||
username,
|
||||
|
||||
@@ -63,12 +63,38 @@ async fn test_user_repo(pool: PgPool) {
|
||||
assert!(repo.user().lookup(user.id).await.unwrap().is_some());
|
||||
|
||||
// Adding a second time should give a conflict
|
||||
// It should not poison the transaction though
|
||||
assert!(repo
|
||||
.user()
|
||||
.add(&mut rng, &clock, USERNAME.to_owned())
|
||||
.await
|
||||
.is_err());
|
||||
|
||||
// Try locking a user
|
||||
assert!(user.is_valid());
|
||||
let user = repo.user().lock(&clock, user).await.unwrap();
|
||||
assert!(!user.is_valid());
|
||||
|
||||
// Check that the property is retrieved on lookup
|
||||
let user = repo.user().lookup(user.id).await.unwrap().unwrap();
|
||||
assert!(!user.is_valid());
|
||||
|
||||
// Locking a second time should not fail
|
||||
let user = repo.user().lock(&clock, user).await.unwrap();
|
||||
assert!(!user.is_valid());
|
||||
|
||||
// Try unlocking a user
|
||||
let user = repo.user().unlock(user).await.unwrap();
|
||||
assert!(user.is_valid());
|
||||
|
||||
// Check that the property is retrieved on lookup
|
||||
let user = repo.user().lookup(user.id).await.unwrap().unwrap();
|
||||
assert!(user.is_valid());
|
||||
|
||||
// Unlocking a second time should not fail
|
||||
let user = repo.user().unlock(user).await.unwrap();
|
||||
assert!(user.is_valid());
|
||||
|
||||
repo.save().await.unwrap();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user