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
167 lines (136 loc) · 6.21 KB
/
asserts.rs
File metadata and controls
167 lines (136 loc) · 6.21 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// code-review: should we use macros to return the exact line where the assert fails?
use reqwest::Response;
use torrust_tracker::servers::apis::v1::context::auth_key::resources::AuthKey;
use torrust_tracker::servers::apis::v1::context::stats::resources::Stats;
use torrust_tracker::servers::apis::v1::context::torrent::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) {
let response_status = response.status();
let response_headers = response.headers().get("content-type").cloned().unwrap();
let response_text = response.text().await.unwrap();
let details = format!(
r#"
status: ´{response_status}´
headers: ´{response_headers:?}´
text: ´"{response_text}"´"#
);
assert_eq!(response_status, 200, "details:{details}.");
assert_eq!(response_headers, "application/json", "\ndetails:{details}.");
assert_eq!(response_text, "{\"status\":\"ok\"}", "\ndetails:{details}.");
}
// 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_bad_request_with_text(response: Response, text: &str) {
assert_eq!(response.status(), 400);
assert_eq!(response.headers().get("content-type").unwrap(), "text/plain; charset=utf-8");
assert!(response.text().await.unwrap().contains(text));
}
pub async fn assert_unprocessable_content(response: Response, text: &str) {
assert_eq!(response.status(), 422);
assert_eq!(response.headers().get("content-type").unwrap(), "text/plain; charset=utf-8");
assert!(response.text().await.unwrap().contains(text));
}
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 \"{invalid_infohash}\", expected a 40 character long string"),
)
.await;
}
pub async fn assert_invalid_auth_key_get_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_auth_key_post_param(response: Response, invalid_auth_key: &str) {
assert_bad_request_with_text(
response,
&format!("Invalid URL: invalid auth key: string \"{}\"", &invalid_auth_key),
)
.await;
}
pub async fn assert_unprocessable_auth_key_duration_param(response: Response, _invalid_value: &str) {
assert_unprocessable_content(
response,
"Failed to deserialize the JSON body into the target type: seconds_valid: invalid type",
)
.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");
let reason_text = format!("Unhandled rejection: Err {{ reason: \"{reason}");
let response_text = response.text().await.unwrap();
assert!(
response_text.contains(&reason_text),
":\n response: `\"{response_text}\"`\n does not contain: `\"{reason_text}\"`."
);
}