Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
on:
push:
branches: [main]
pull_request:
branches: [main]
name: Docker
env:
REGISTRY: ghcr.io
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
on:
push:
branches: [main]
pull_request:
branches: [main]
name: Server
jobs:
fmt:
Expand Down Expand Up @@ -65,7 +67,7 @@ jobs:
with:
key: coverage
- name: Generate code coverage
run: cargo llvm-cov --locked --lcov --output-path lcov.info
run: cargo llvm-cov --locked --workspace --exclude entity --lcov --output-path lcov.info
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/svelte.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
on:
push:
branches: [main]
pull_request:
branches: [main]
name: Client
env:
NODE_VERSION: 18
Expand Down
48 changes: 48 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license-file = "LICENSE"
build = "src/build.rs"

[workspace]
members = [".", "entity", "migration", "deezer-rs", "api-macro"]
members = [".", "entity", "migration", "api-macro"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down Expand Up @@ -52,6 +52,8 @@ https = ["dep:axum-server"]

[dev-dependencies]
serde_test = "1"
axum-test-helper = "0.3"
tokio = { version = "1", features = ["test-util"] }

# TODO: fix caching with cargo-chef when optimizing
# or not cache for docker build
Expand Down
2 changes: 1 addition & 1 deletion api-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl ErrorVariant {
fn to_variant(&self) -> syn::Variant {
match self {
ErrorVariant::InternalError => parse_quote!(
#[doc = "Internal error"]
#[doc = "Internal error: {0}"]
#[status(axum::http::status::StatusCode::INTERNAL_SERVER_ERROR)]
InternalError(
#[from]
Expand Down
3 changes: 3 additions & 0 deletions migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ features = [
"runtime-tokio-rustls", # `ASYNC_RUNTIME` feature
"sqlx-sqlite", # `DATABASE_DRIVER` feature
]

[dev-dependencies]
tokio = { version = "1", features = ["test-util"] }
21 changes: 21 additions & 0 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,24 @@ impl MigratorTrait for Migrator {
vec![Box::new(m20220101_000001_create_table::Migration)]
}
}

#[cfg(test)]
mod tests {
use super::*;
use sea_orm::Database;

#[tokio::test]
async fn test_migrations() {
let db = Database::connect("sqlite::memory:")
.await
.expect("Failed to connect to database");

Migrator::up(&db, None)
.await
.expect("Failed to migrate database");

Migrator::down(&db, None)
.await
.expect("Failed to migrate database");
}
}
2 changes: 2 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use self::state::ApiState;
mod admin;
mod room;
mod search;
#[cfg(test)]
mod test;
mod websocket;

pub mod state;
Expand Down
105 changes: 105 additions & 0 deletions src/api/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use axum::http::StatusCode;
use axum_test_helper::TestClient;
use chrono::{Duration, Utc};
use migration::MigratorTrait;
use sea_orm::Database;
use serde_json::json;

use crate::utils::jwt;

use super::{router, state::ApiState};

#[tokio::test]
async fn test_admin_login() {
jwt::set_jwt_secret("secret");
let db = Database::connect("sqlite::memory:").await.unwrap();
let username = "admin";
// password: admin
let password = "$argon2id$v=19$m=19456,t=2,p=1$bjFCSXBGR3pJclBraDFOSA$Aiqx8jvWC8UT8Xj9K37DqA";
let state = ApiState::new(db, username.to_string(), password.to_string());
let api = router(state);

let client = TestClient::new(api);

// Successful login

let body = json!( {
"username": "admin",
"password": "admin"
});

let res = client.post("/admin/login").json(&body).send().await;
assert_eq!(res.status(), StatusCode::OK, "Successful login");

// Wrong password

let body = json!( {
"username": "admin",
"password": "wrong"
});
let res = client.post("/admin/login").json(&body).send().await;
assert_eq!(res.status(), StatusCode::UNAUTHORIZED, "Wrong password");

// Wrong username

let body = json!( {
"username": "wrong",
"password": "admin"
});
let res = client.post("/admin/login").json(&body).send().await;
assert_eq!(res.status(), StatusCode::UNAUTHORIZED, "Wrong username");

// Missing username

let body = json!( {
"password": "admin"
});
let res = client.post("/admin/login").json(&body).send().await;
assert_ne!(res.status(), StatusCode::OK, "Missing username");
}

#[tokio::test]
async fn test_room_create_and_join() {
let admin_token = jwt::admin_token();

let db = Database::connect("sqlite::memory:").await.unwrap();
migration::Migrator::up(&db, None).await.unwrap();
let username = "admin";
// password: admin
let password = "$argon2id$v=19$m=19456,t=2,p=1$bjFCSXBGR3pJclBraDFOSA$Aiqx8jvWC8UT8Xj9K37DqA";
let state = ApiState::new(db, username.to_string(), password.to_string());
let client = TestClient::new(router(state));

// Create room

let res = client
.post("/room")
.json(&json!({
"id": "AAAAAA",
"expiration": Utc::now() + Duration::hours(5)
}))
.header("Authorization", admin_token)
.send()
.await;
assert_eq!(res.status(), StatusCode::OK, "Create rooom");

// Join the room

let res = client.get("/room/AAAAAA/join").send().await;
let status = res.status();
eprintln!("{}", res.text().await);
assert_eq!(status, StatusCode::OK, "Join room");

// Join error

let res = client.get("/room/ZZZZZZ/join").send().await;
assert_eq!(res.status(), StatusCode::NOT_FOUND, "Room does not exist");

// Room id error

let res = client.get("/room/ZZZZ/join").send().await;
assert_eq!(res.status(), StatusCode::BAD_REQUEST, "Id to short");

let res = client.get("/room/ZZZZZZZZZ/join").send().await;
assert_eq!(res.status(), StatusCode::BAD_REQUEST, "Id to short");
}
14 changes: 14 additions & 0 deletions src/utils/cors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,17 @@ fn parse_allow_origin(var: &str) -> AllowOrigin {
origins.into()
}
}

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

#[test]
fn test_parse_allow_origin() {
let var = "http://localhost:3000,http://localhost:3001";
let _ = parse_allow_origin(var);

let var = "*";
let _ = parse_allow_origin(var);
}
}
Loading