forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasserts.rs
More file actions
128 lines (104 loc) · 4.62 KB
/
asserts.rs
File metadata and controls
128 lines (104 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// code-review: should we use macros to return the exact line where the assert fails?
use reqwest::Response;
use torrust_tracker::apis::resources::auth_key::AuthKey;
use torrust_tracker::apis::resources::stats::Stats;
use torrust_tracker::apis::resources::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_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(), "application/json");
assert_eq!(response.text().await.unwrap(), "{\"status\":\"ok\"}");
}
// Error responses
pub async fn assert_bad_request(response: Response, body: &str) {
assert_eq!(response.status(), 400);
assert_eq!(response.headers().get("content-type").unwrap(), "text/plain; charset=utf-8");
assert_eq!(response.text().await.unwrap(), body);
}
pub async fn assert_not_found(response: Response) {
assert_eq!(response.status(), 404);
// todo: missing header in the response
//assert_eq!(response.headers().get("content-type").unwrap(), "text/plain; charset=utf-8");
assert_eq!(response.text().await.unwrap(), "");
}
pub async fn assert_torrent_not_known(response: Response) {
assert_eq!(response.status(), 200);
assert_eq!(response.headers().get("content-type").unwrap(), "application/json");
assert_eq!(response.text().await.unwrap(), "\"torrent not known\"");
}
pub async fn assert_invalid_infohash_param(response: Response, invalid_infohash: &str) {
assert_bad_request(
response,
&format!(
"Invalid URL: invalid infohash param: string \"{}\", expected a 40 character long string",
invalid_infohash
),
)
.await;
}
pub async fn assert_invalid_auth_key_param(response: Response, invalid_auth_key: &str) {
assert_bad_request(response, &format!("Invalid auth key id param \"{}\"", &invalid_auth_key)).await;
}
pub async fn assert_invalid_key_duration_param(response: Response, invalid_key_duration: &str) {
assert_bad_request(
response,
&format!("Invalid URL: Cannot parse `\"{invalid_key_duration}\"` to a `u64`"),
)
.await;
}
pub async fn assert_token_not_valid(response: Response) {
assert_unhandled_rejection(response, "token not valid").await;
}
pub async fn assert_unauthorized(response: Response) {
assert_unhandled_rejection(response, "unauthorized").await;
}
pub async fn assert_failed_to_remove_torrent_from_whitelist(response: Response) {
assert_unhandled_rejection(response, "failed to remove torrent from whitelist").await;
}
pub async fn assert_failed_to_whitelist_torrent(response: Response) {
assert_unhandled_rejection(response, "failed to whitelist torrent").await;
}
pub async fn assert_failed_to_reload_whitelist(response: Response) {
assert_unhandled_rejection(response, "failed to reload whitelist").await;
}
pub async fn assert_failed_to_generate_key(response: Response) {
assert_unhandled_rejection(response, "failed to generate key").await;
}
pub async fn assert_failed_to_delete_key(response: Response) {
assert_unhandled_rejection(response, "failed to delete key").await;
}
pub async fn assert_failed_to_reload_keys(response: Response) {
assert_unhandled_rejection(response, "failed to reload keys").await;
}
async fn assert_unhandled_rejection(response: Response, reason: &str) {
assert_eq!(response.status(), 500);
assert_eq!(response.headers().get("content-type").unwrap(), "text/plain; charset=utf-8");
assert_eq!(
response.text().await.unwrap(),
format!("Unhandled rejection: Err {{ reason: \"{reason}\" }}")
);
}