Skip to content

Commit

Permalink
Merge pull request #104 from lajp/integration-tests
Browse files Browse the repository at this point in the history
Add integration tests
  • Loading branch information
lajp authored Dec 27, 2023
2 parents 9e3cdd8 + cdc1594 commit 292a146
Show file tree
Hide file tree
Showing 15 changed files with 813 additions and 36 deletions.
30 changes: 27 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,40 @@ jobs:
test:
name: Test Suite
runs-on: ubuntu-latest

services:
postgres:
image: postgres
env:
POSTGRES_DB: testaustime
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
- uses: actions-rs/cargo@v1
with:
command: test
- name: Install Diesel
run: cargo install diesel_cli --features=postgres
- name: Create Test DB
env:
DATABASE_URL: postgres://postgres:postgres@localhost/testaustime
run: diesel migration run

- name: Run tests
env:
TEST_DATABASE: postgres://postgres:postgres@localhost/testaustime
run: cargo test

fmt:
name: Rustfmt
Expand Down
21 changes: 0 additions & 21 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ regex = "1.5"
tracing = "0.1.37"
tracing-actix-web = "0.6.2"

r2d2 = "0.8"

log = "0.4"
env_logger = "0.9"
thiserror = "1.0"
Expand Down
6 changes: 3 additions & 3 deletions src/api/leaderboards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use actix_web::{
};
use dashmap::DashMap;
use diesel::result::DatabaseErrorKind;
use serde::Deserialize;
use serde::{Deserialize, Serialize};

use crate::{
api::auth::SecuredUserIdentity,
Expand All @@ -14,12 +14,12 @@ use crate::{
models::{PrivateLeaderboard, UserId},
};

#[derive(Deserialize)]
#[derive(Deserialize, Serialize)]
pub struct LeaderboardName {
pub name: String,
}

#[derive(Deserialize)]
#[derive(Deserialize, Serialize)]
pub struct LeaderboardInvite {
pub invite: String,
}
Expand Down
2 changes: 0 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use thiserror::Error;

#[derive(Debug, Error)]
pub enum TimeError {
#[error("Failed to connect to database connection pool")]
R2d2Error(#[from] r2d2::Error),
#[error("Failed to connect to database connection pool")]
DeadpoolError(#[from] diesel_async::pooled_connection::deadpool::PoolError),
#[error("Diesel transaction failed `{0}`")]
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ mod requests;
mod schema;
mod utils;

#[cfg(test)]
mod tests;

use std::{num::NonZeroU32, sync::Arc};

use actix_cors::Cors;
Expand Down
8 changes: 4 additions & 4 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub struct NewTestaustimeUser {
pub identity: i32,
}

#[derive(Clone, Debug, Serialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SelfUser {
pub id: i32,
pub auth_token: String,
Expand Down Expand Up @@ -107,7 +107,7 @@ pub struct TestausIdUser {

use crate::schema::user_identities;

#[derive(Insertable, Serialize, Clone)]
#[derive(Insertable, Serialize, Clone, Deserialize)]
#[diesel(table_name = user_identities)]
pub struct NewUserIdentity {
pub auth_token: String,
Expand Down Expand Up @@ -202,15 +202,15 @@ pub struct NewLeaderboardMember {
pub admin: bool,
}

#[derive(Serialize, Clone, Debug)]
#[derive(Serialize, Clone, Debug, Deserialize)]
pub struct PrivateLeaderboardMember {
pub id: i32,
pub username: String,
pub admin: bool,
pub time_coded: i32,
}

#[derive(Serialize, Clone, Debug)]
#[derive(Serialize, Clone, Debug, Deserialize)]
pub struct PrivateLeaderboard {
pub name: String,
pub invite: String,
Expand Down
2 changes: 1 addition & 1 deletion src/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct DataRequest {
pub project_name: Option<String>,
}

#[derive(Deserialize)]
#[derive(Deserialize, Debug)]
pub struct RegisterRequest {
pub username: String,
pub password: String,
Expand Down
67 changes: 67 additions & 0 deletions src/tests/account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use actix_web::test::{self, TestRequest};
use serde_json::json;

use super::{macros::*, *};
use crate::models::{NewUserIdentity, SecuredAccessTokenResponse};

#[actix_web::test]
async fn public_accounts() {
let app = test::init_service(App::new().configure(init_test_services)).await;

let body = json!({"username": "celebrity", "password": "password"});
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 80u16);
let resp = request!(app, addr, post, "/auth/register", body);
assert!(resp.status().is_success(), "Creating user failed");

let user: NewUserIdentity = test::read_body_json(resp).await;
let resp = request_auth!(app, addr, get, "/users/@me", user.auth_token);

assert!(resp.status().is_success(), "Getting profile failed");

let profile: serde_json::Value = test::read_body_json(resp).await;
assert!(
!profile["is_public"].as_bool().unwrap(),
"New account should be private"
);

let resp = request!(app, addr, get, "/users/celebrity/activity/data");
assert!(
resp.status().is_client_error(),
"Data should be private for private accounts"
);

let resp = request!(app, addr, post, "/auth/securedaccess", body);
assert!(
resp.status().is_success(),
"Getting secured access token failed"
);
let sat: SecuredAccessTokenResponse = test::read_body_json(resp).await;

let change = json!({"public_profile": true});
let resp = request_auth!(app, addr, post, "/account/settings", sat.token, change);

assert!(resp.status().is_success(), "Changing settings failed");

let resp = request_auth!(app, addr, get, "/users/@me", user.auth_token);

assert!(resp.status().is_success(), "Getting profile failed");

let profile: serde_json::Value = test::read_body_json(resp).await;
assert!(
profile["is_public"].as_bool().unwrap(),
"Setting account public failed"
);

let resp = request!(app, addr, get, "/users/celebrity/activity/data");
assert!(
resp.status().is_success(),
"Data should be public for public accounts"
);

let resp = request!(app, addr, delete, "/users/@me/delete", body);
assert!(resp.status().is_success(), "Failed to delete user");
}

// TODO: add test for searching public accounts
Loading

0 comments on commit 292a146

Please sign in to comment.