You've already forked authentication-service
mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-08-09 04:22:45 +03:00
storage-pg: add tests for user locking
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "\n INSERT INTO users (user_id, username, created_at)\n VALUES ($1, $2, $3)\n ",
|
"query": "\n INSERT INTO users (user_id, username, created_at)\n VALUES ($1, $2, $3)\n ON CONFLICT (username) DO NOTHING\n ",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [],
|
"columns": [],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
@@ -12,5 +12,5 @@
|
|||||||
},
|
},
|
||||||
"nullable": []
|
"nullable": []
|
||||||
},
|
},
|
||||||
"hash": "b26ae7dd28f8a756b55a76e80cdedd7be9ba26435ea4a914421483f8ed832537"
|
"hash": "7f4c4634ada4dc2745530dcca8eee92abf78dfbdf1a25e58a2bc9c14be8035f0"
|
||||||
}
|
}
|
@@ -161,10 +161,11 @@ impl<'c> UserRepository for PgUserRepository<'c> {
|
|||||||
let id = Ulid::from_datetime_with_source(created_at.into(), rng);
|
let id = Ulid::from_datetime_with_source(created_at.into(), rng);
|
||||||
tracing::Span::current().record("user.id", tracing::field::display(id));
|
tracing::Span::current().record("user.id", tracing::field::display(id));
|
||||||
|
|
||||||
sqlx::query!(
|
let res = sqlx::query!(
|
||||||
r#"
|
r#"
|
||||||
INSERT INTO users (user_id, username, created_at)
|
INSERT INTO users (user_id, username, created_at)
|
||||||
VALUES ($1, $2, $3)
|
VALUES ($1, $2, $3)
|
||||||
|
ON CONFLICT (username) DO NOTHING
|
||||||
"#,
|
"#,
|
||||||
Uuid::from(id),
|
Uuid::from(id),
|
||||||
username,
|
username,
|
||||||
@@ -174,6 +175,10 @@ impl<'c> UserRepository for PgUserRepository<'c> {
|
|||||||
.execute(&mut *self.conn)
|
.execute(&mut *self.conn)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
// If the user already exists, want to return an error but not poison the
|
||||||
|
// transaction
|
||||||
|
DatabaseError::ensure_affected_rows(&res, 1)?;
|
||||||
|
|
||||||
Ok(User {
|
Ok(User {
|
||||||
id,
|
id,
|
||||||
username,
|
username,
|
||||||
|
@@ -63,12 +63,38 @@ async fn test_user_repo(pool: PgPool) {
|
|||||||
assert!(repo.user().lookup(user.id).await.unwrap().is_some());
|
assert!(repo.user().lookup(user.id).await.unwrap().is_some());
|
||||||
|
|
||||||
// Adding a second time should give a conflict
|
// Adding a second time should give a conflict
|
||||||
|
// It should not poison the transaction though
|
||||||
assert!(repo
|
assert!(repo
|
||||||
.user()
|
.user()
|
||||||
.add(&mut rng, &clock, USERNAME.to_owned())
|
.add(&mut rng, &clock, USERNAME.to_owned())
|
||||||
.await
|
.await
|
||||||
.is_err());
|
.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();
|
repo.save().await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user