Skip to content

Commit

Permalink
Add tests for self-check
Browse files Browse the repository at this point in the history
  • Loading branch information
mbr committed Apr 9, 2024
1 parent 58b804d commit dfb7962
Showing 3 changed files with 143 additions and 11 deletions.
112 changes: 101 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@ uuid = { version = "1.6.1", features = [ "v4", "serde" ] }

[dev-dependencies]
http-body-util = "0.1.0"
pgdb = "0.3.0"
tempdir = "0.3.7"
tower = "0.4.13"

41 changes: 41 additions & 0 deletions src/postgres.rs
Original file line number Diff line number Diff line change
@@ -124,3 +124,44 @@ fn assert_valid_ident(input: &str) -> Result<(), Error> {
Err(Error::InvalidIdent(input.to_owned()))
} else {Ok(())}
}

#[cfg(test)]
mod tests {
use super::PostgresDb;

fn mk_pg() -> pgdb::Postgres {
let pg = pgdb::Postgres::build()
.start()
.expect("could not build database");

pg
}

#[tokio::test]
async fn selfcheck_works_for_superuser() {
let pg = mk_pg();

let db = PostgresDb::new(pg.as_superuser().uri("postgres"));
let con = db.connect().await.expect("failed to connect");
con.run_self_check().await.expect("self-check should work");
}

#[tokio::test]
async fn selfcheck_fails_for_regular_user() {
let pg = mk_pg();
let su = pg.as_superuser();
su.create_user("unittest", "unittest")
.expect("should be able to create user");

su.create_database("unittest", "unittest")
.expect("should be able to create db");

let db = dbg!(PostgresDb::new(
pg.as_user("unittest", "unittest").uri("postgres")
));
let con = db.connect().await.expect("failed to connect");
con.run_self_check()
.await
.expect_err("self-check should fail");
}
}

0 comments on commit dfb7962

Please sign in to comment.