Skip to content

Commit

Permalink
refactor(api): extract asserts in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jan 11, 2023
1 parent 03ba166 commit 5d9dd9d
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 98 deletions.
4 changes: 2 additions & 2 deletions src/apis/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub enum ActionStatus<'a> {
fn response_ok() -> Response {
(
StatusCode::OK,
[(header::CONTENT_TYPE, "text/plain; charset=utf-8")],
format!("{:?}", ActionStatus::Ok),
[(header::CONTENT_TYPE, "application/json")],
serde_json::to_string(&ActionStatus::Ok).unwrap(),
)
.into_response()
}
Expand Down
46 changes: 44 additions & 2 deletions tests/api/asserts.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,55 @@
// code-review: should we use macros to return the exact line where the assert fails?

use reqwest::Response;
use torrust_tracker::api::resource::auth_key::AuthKey;
use torrust_tracker::api::resource::stats::Stats;
use torrust_tracker::api::resource::torrent::{ListItem, Torrent};

// Resource responses

pub async fn assert_stats(response: Response, stats: Stats) {
assert_eq!(response.status(), 200);
assert_eq!(response.headers().get("content-type").unwrap(), "application/json");
assert_eq!(response.json::<Stats>().await.unwrap(), stats);
}

pub async fn assert_torrent_list(response: Response, torrents: Vec<ListItem>) {
assert_eq!(response.status(), 200);
assert_eq!(response.headers().get("content-type").unwrap(), "application/json");
assert_eq!(response.json::<Vec<ListItem>>().await.unwrap(), torrents);
}

pub async fn assert_torrent_info(response: Response, torrent: Torrent) {
assert_eq!(response.status(), 200);
assert_eq!(response.headers().get("content-type").unwrap(), "application/json");
assert_eq!(response.json::<Torrent>().await.unwrap(), torrent);
}

pub async fn assert_auth_key(response: Response) -> AuthKey {
assert_eq!(response.status(), 200);
assert_eq!(response.headers().get("content-type").unwrap(), "application/json");
response.json::<AuthKey>().await.unwrap()
}

pub async fn assert_auth_key_utf8(response: Response) -> AuthKey {
assert_eq!(response.status(), 200);
assert_eq!(
response.headers().get("content-type").unwrap(),
"application/json; charset=utf-8"
);
response.json::<AuthKey>().await.unwrap()
}

// OK response

pub async fn assert_ok(response: Response) {
assert_eq!(response.status(), 200);
assert_eq!(response.headers().get("content-type").unwrap(), "text/plain; charset=utf-8");
assert_eq!(response.text().await.unwrap(), "Ok");
assert_eq!(response.headers().get("content-type").unwrap(), "application/json");
assert_eq!(response.text().await.unwrap(), "{\"status\":\"ok\"}");
}

// Error responses

pub async fn assert_torrent_not_known(response: Response) {
assert_eq!(response.status(), 200);
assert_eq!(response.headers().get("content-type").unwrap(), "application/json");
Expand Down
Loading

0 comments on commit 5d9dd9d

Please sign in to comment.