1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-07-29 22:01:14 +03:00

Database testing

This commit is contained in:
Quentin Gliech
2022-08-04 15:32:10 +02:00
parent 3cfd0f1553
commit 646a0f26d6
2 changed files with 45 additions and 0 deletions

View File

@ -259,12 +259,20 @@ jobs:
target
key: ${{ runner.os }}-cargo-build-${{ steps.toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }}
- name: Start PostgreSQL database
run: |
sudo systemctl start postgresql.service
pg_isready
sudo -u postgres psql --command="CREATE USER test PASSWORD 'test'" --command="\du"
- name: Test
id: test
uses: actions-rs/cargo@v1
with:
command: test
args: --offline --workspace
env:
DATABASE_URL: postgresql://test:test@localhost/postgres
# Ignore errors on the nightly toolchain
continue-on-error: "${{ matrix.toolchain == 'nightly' }}"
@ -328,6 +336,12 @@ jobs:
curl -sL https://github.com/mozilla/grcov/releases/download/v0.8.7/grcov-x86_64-unknown-linux-gnu.tar.bz2 | tar jxf - -C "${HOME}/.local/bin"
echo "$HOME/.local/bin" >> $GITHUB_PATH
- name: Start PostgreSQL database
run: |
sudo systemctl start postgresql.service
pg_isready
sudo -u postgres psql --command="CREATE USER test PASSWORD 'test'" --command="\du"
- name: Run test suite with profiling enabled
uses: actions-rs/cargo@v1
with:
@ -337,6 +351,7 @@ jobs:
CARGO_INCREMENTAL: '0'
RUSTFLAGS: '-Cinstrument-coverage'
LLVM_PROFILE_FILE: "cargo-test-%p-%m.profraw"
DATABASE_URL: postgresql://test:test@localhost/postgres
- name: Build grcov report
run: |

View File

@ -841,3 +841,33 @@ pub async fn add_user_email_verification_code(
Ok(verification)
}
#[cfg(test)]
mod tests {
use super::*;
#[sqlx::test(migrator = "crate::MIGRATOR")]
async fn test_user_registration_and_login(pool: sqlx::PgPool) -> anyhow::Result<()> {
let mut txn = pool.begin().await?;
let exists = username_exists(&mut txn, "john").await?;
assert!(!exists);
let hasher = Argon2::default();
let user = register_user(&mut txn, hasher, "john", "hunter2").await?;
assert_eq!(user.username, "john");
let exists = username_exists(&mut txn, "john").await?;
assert!(exists);
let session = login(&mut txn, "john", "hunter2").await?;
assert_eq!(session.user.data, user.data);
let user2 = lookup_user_by_username(&mut txn, "john").await?;
assert_eq!(user.data, user2.data);
txn.commit().await?;
Ok(())
}
}