Skip to content
This repository was archived by the owner on Jan 2, 2026. It is now read-only.

Commit cb41484

Browse files
committed
feat: more coverage
fix: add postgresql db to workflow fix: apply db migrations asdfasdfasfd
1 parent d1fe08f commit cb41484

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

.github/workflows/build-test-coverage.yaml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ jobs:
2121
timeout-minutes: 30
2222
steps:
2323
- uses: actions/checkout@v4
24+
- uses: ikalnytskyi/action-setup-postgres@v6
25+
with:
26+
username: sonata
27+
password: sljkdhfghjklsdfghjkldfsghjklsdfghjklsdfg
28+
database: sonata
29+
port: 5432
30+
id: postgres
2431
- uses: dtolnay/rust-toolchain@nightly
2532
with:
2633
components: "llvm-tools-preview"
@@ -32,8 +39,14 @@ jobs:
3239
tool: cargo-nextest,cargo-binstall
3340
- name: Install dependencies
3441
run: |
35-
cargo binstall --no-confirm --force --only-signed cargo-llvm-cov
42+
cargo binstall --no-confirm --force --only-signed cargo-llvm-cov sqlx-cli
43+
- name: Apply migrations
44+
env:
45+
DATABASE_URL: ${{ steps.postgres.outputs.connection-uri }}
46+
run: cargo sqlx migrate run
3647
- name: Build, Test, Publish Coverage
48+
env:
49+
DATABASE_URL: ${{ steps.postgres.outputs.connection-uri }}
3750
run: |
3851
if [ -n "${{ secrets.COVERALLS_REPO_TOKEN }}" ]; then
3952
mkdir -p coverage

src/api/admin/invitations.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
use poem::{IntoResponse, handler};
66

77
#[handler]
8+
#[cfg_attr(coverage_nightly, coverage(off))]
89
pub(crate) async fn create_invite() -> impl IntoResponse {}

src/api/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub(super) mod admin;
1919
pub(crate) mod middlewares;
2020

2121
#[allow(clippy::expect_used)]
22+
#[cfg_attr(coverage_nightly, coverage(off))]
2223
/// Build the API [Route]s and start a `tokio::task`, which is a poem [Server] processing incoming
2324
/// HTTP API requests.
2425
pub(super) fn start_api(api_config: ApiConfig, db: Database) -> tokio::task::JoinHandle<()> {

src/database/api_keys.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ use crate::StdError;
1212
use crate::database::Database;
1313
use crate::errors::SonataDbError;
1414

15+
/// Constant used to determine how long auto-generated tokens are supposed to be.
16+
pub const STANDARD_TOKEN_LENGTH: usize = 128;
17+
1518
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
1619
pub struct ApiKey {
1720
token: String,
1821
}
1922

23+
#[cfg_attr(coverage_nightly, coverage(off))]
2024
impl Deref for ApiKey {
2125
type Target = str;
2226

@@ -41,13 +45,15 @@ impl ApiKey {
4145
Ok(Self { token: token.to_owned() })
4246
}
4347

48+
#[cfg_attr(coverage_nightly, coverage(off))]
49+
/// Getter for the internal token.
4450
pub fn token(&self) -> &str {
4551
&self.token
4652
}
4753

48-
/// Generates a new, random [ApiKey] which is 128 characters in length.
54+
/// Generates a new, random [ApiKey] which is [STANDARD_TOKEN_LENGTH] characters in length.
4955
pub fn new_random(rng: &mut ThreadRng) -> Self {
50-
Self { token: Alphanumeric.sample_string(rng, 128) }
56+
Self { token: Alphanumeric.sample_string(rng, STANDARD_TOKEN_LENGTH) }
5157
}
5258
}
5359

@@ -69,3 +75,30 @@ pub(crate) async fn add_api_key_to_database(
6975
.map_err(SonataDbError::Sqlx)?;
7076
Ok(key)
7177
}
78+
79+
#[cfg(test)]
80+
mod test {
81+
use rand::rng;
82+
use sqlx::{Pool, Postgres};
83+
84+
use super::*;
85+
86+
#[test]
87+
fn token_length() {
88+
assert!(ApiKey::new("").is_err());
89+
assert!(ApiKey::new("token").is_err());
90+
assert!(ApiKey::new(['c'; 256].iter().collect::<String>().as_str()).is_err());
91+
assert!(ApiKey::new("transrightsarehumanrights_thefirstpridewasariot").is_ok());
92+
}
93+
94+
#[test]
95+
fn auto_gen_token() {
96+
assert_eq!(ApiKey::new_random(&mut rng()).len(), STANDARD_TOKEN_LENGTH);
97+
}
98+
99+
#[sqlx::test]
100+
async fn insert_key_into_db(db: Pool<Postgres>) {
101+
let key = ApiKey::new_random(&mut rng());
102+
assert!(add_api_key_to_database(key.token(), &Database { pool: db }).await.is_ok());
103+
}
104+
}

0 commit comments

Comments
 (0)