-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.rs
299 lines (253 loc) · 10.7 KB
/
tests.rs
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// If we run tests, current all DB contents will be deleted.
// If you won't, please set environment variable for example:
// `export ROCKET_DATABASES='{sqlite_database={url="db/mydb.sqlite"}}'`
extern crate parking_lot;
extern crate rand;
use super::task::Task;
use self::parking_lot::Mutex;
use self::rand::{Rng, thread_rng, distributions::Alphanumeric};
use rocket::local::Client;
use rocket::http::{Status, ContentType};
use chrono::Local;
static DB_LOCK: Mutex<()> = Mutex::new(());
macro_rules! run_test {
(|$client:ident, $conn:ident| $block:expr) => ({
let _lock = DB_LOCK.lock();
let rocket = super::rocket();
let db = super::DbConn::get_one(&rocket);
let $client = Client::new(rocket).expect("Rocket client");
let $conn = db.expect("failed to get database connection for testing");
assert!(Task::delete_all(&$conn), "failed to delete all tasks for testing");
$block
})
}
#[test]
fn index_page() {
run_test!(|client, _conn| {
// Ensure we can access index page
let mut res = client.get("/").dispatch();
assert_eq!(res.status(), Status::Ok);
// Ensure index shows correct task table.
let body = res.body_string().unwrap();
assert!(body.contains("name"));
assert!(body.contains("Last updated"));
assert!(body.contains("Update to today"));
// TODO: Ensure the number of table row reflects the number of tasks.
})
}
#[test]
fn detail_page() {
run_test!(|client, conn| {
// Create new task and get its ID.
let task_name: String = "detailpagetest".to_string();
client.post("/")
.header(ContentType::Form)
.body(format!("name={}", task_name))
.dispatch();
let inserted_id = Task::all(&conn)[0].id.unwrap();
// Ensure we can access detail page.
let mut res = client.get(format!("/{}", inserted_id)).dispatch();
assert_eq!(res.status(), Status::Ok);
// Ensure detail page shows required fields.
let body = res.body_string().unwrap();
assert!(body.contains("Task name"));
assert!(body.contains("Description"));
assert!(body.contains("Last updated"));
assert!(body.contains(r#"<button class="button is-primary is-light" type="submit">Update</button>"#));
assert!(body.contains(
r#"<button class="button is-link is-light" onclick="location.href='../'">Back to index page</button>"#
));
})
}
#[test]
fn confirm_page() {
run_test!(|client, conn| {
// Create new task and get its ID.
let task_name: String = "confirmpagetest".to_string();
client.post("/")
.header(ContentType::Form)
.body(format!("name={}", task_name))
.dispatch();
let inserted_id = Task::all(&conn)[0].id.unwrap();
// Ensure we can access detail page.
let mut res = client.get(format!("/{}/confirm", inserted_id)).dispatch();
assert_eq!(res.status(), Status::Ok);
// Ensure confirm page shows buttons
let body = res.body_string().unwrap();
assert!(body.contains(
r#"<button class="button is-danger is-light" type="submit">Delete</button>"#
));
// If I write full button HTML, `cargo test` hangs up. I don't know why.
assert!(body.contains("Back to task</button>"));
assert!(body.contains(
r#"<button class="button is-link is-light" onclick="location.href='/'">Back to index page</button>"#
));
})
}
#[test]
fn test_insertion_deletion() {
run_test!(|client, conn| {
// Get the tasks before making changes.
let init_tasks = Task::all(&conn);
// insert new task
client.post("/")
.header(ContentType::Form)
.body("name=test+task")
.dispatch();
// Ensure we have one more task in the DB.
let new_tasks = Task::all(&conn);
assert_eq!(new_tasks.len(), init_tasks.len() + 1);
// Ensure the task is what we expect.
assert_eq!(new_tasks[0].name, "test task");
assert_eq!(new_tasks[0].description, "");
assert_eq!(new_tasks[0].updated_at, Local::today().naive_local().to_string());
// Delete task.
let id = new_tasks[0].id.unwrap();
client.delete(format!("/{}", id)).dispatch();
// Ensure task was deleted.
let final_tasks = Task::all(&conn);
assert_eq!(final_tasks.len(), init_tasks.len());
if final_tasks.len() > 0 {
assert_ne!(final_tasks[0].name, "test task");
}
})
}
#[test]
fn test_many_insertions() {
const ITER: usize = 100;
let mut rng = thread_rng();
run_test!(|client, conn| {
let init_num = Task::all(&conn).len();
let mut descs = Vec::new();
for i in 0..ITER {
// Insert new task with random name.
let desc: String = rng.sample_iter(&Alphanumeric).take(6).collect();
client.post("/")
.header(ContentType::Form)
.body(format!("name={}", desc))
.dispatch();
// Record the name we choose for this iteration.
descs.insert(0, desc);
// Ensure the task was inserted properly and all other tasks remain.
let tasks = Task::all_by_id(&conn);
assert_eq!(tasks.len(), init_num + i + 1);
for j in 0..i {
assert_eq!(descs[j], tasks[j].name);
}
}
})
}
#[test]
fn test_bad_new_task_form_submissions() {
run_test!(|client, _conn| {
// Submit an **empty** form. This is an unexpected pattern
// because task form in index page has `name` field.
let res = client.post("/")
.header(ContentType::Form)
.dispatch();
let mut cookies = res.headers().get("Set-Cookie");
assert_eq!(res.status(), Status::UnprocessableEntity);
assert!(!cookies.any(|value| value.contains("warning")));
// Submit a form without a name field. This is same as just above pattern.
let res = client.post("/")
.header(ContentType::Form)
.dispatch();
let mut cookies = res.headers().get("Set-Cookie");
assert_eq!(res.status(), Status::UnprocessableEntity);
assert!(!cookies.any(|value| value.contains("warning")));
// Submit a form with an empty name. We look for `warning` in the
// cookies which corresponds to flash message being set as a warning.
let res = client.post("/")
.header(ContentType::Form)
.body("name=")
.dispatch();
let mut cookies = res.headers().get("Set-Cookie");
assert_eq!(res.status(), Status::SeeOther);
assert!(cookies.any(|value| value.contains("warning")));
})
}
#[test]
fn test_bad_update_form_submissions() {
run_test!(|client, conn| {
// Create new task and get its ID.
let task_name: String = "detailformtest".to_string();
client.post("/")
.header(ContentType::Form)
.body(format!("name={}", task_name))
.dispatch();
let inserted_id = Task::all(&conn)[0].id.unwrap();
let post_url = format!("/{}", inserted_id);
// Submit an **empty** form. This is an unexpected pattern
// because task form in detail page has some fields.
let res = client.post(&post_url)
.header(ContentType::Form)
.dispatch();
let mut cookies = res.headers().get("Set-Cookie");
assert_eq!(res.status(), Status::UnprocessableEntity);
assert!(!cookies.any(|value| value.contains("warning")));
// Submit a form without a name field. This is same as just above pattern.
let res = client.post(&post_url)
.header(ContentType::Form)
.body("description=hello")
.dispatch();
let mut cookies = res.headers().get("Set-Cookie");
assert_eq!(res.status(), Status::UnprocessableEntity);
assert!(!cookies.any(|value| value.contains("warning")));
// Submit a form with an empty name. We look for `warning` in the
// cookies which corresponds to flash message being set as a warning.
let res = client.post(&post_url)
.header(ContentType::Form)
.body("name=&description=hello&updated_at=2020-04-28")
.dispatch();
let mut cookies = res.headers().get("Set-Cookie");
assert_eq!(res.status(), Status::SeeOther);
assert!(cookies.any(|value| value.contains("warning")));
})
}
#[test]
fn test_update_date() {
run_test!(|client, conn| {
// Create new task with old `updated_at`.
let mut rng = thread_rng();
let rng_name: String = rng.sample_iter(&Alphanumeric).take(7).collect();
let t = Task::insert_with_old_date(&rng_name, &conn);
assert!(t);
// Ensure `updated_at` of created task is updated to today.
let new_tasks = Task::all(&conn);
let today_str = Local::today().naive_local().to_string();
// First, ensure current task date is not today.
assert_ne!(new_tasks[0].updated_at, today_str);
let inserted_id = new_tasks[0].id.unwrap(); // `id` is `Nullable`
let res = client.post(format!("/{}/date", inserted_id)).dispatch();
let mut cookies = res.headers().get("Set-Cookie");
let final_tasks = Task::all(&conn);
assert_eq!(res.status(), Status::SeeOther);
assert!(cookies.any(|value| value.contains("success")));
assert_eq!(final_tasks[0].updated_at, today_str);
})
}
#[test]
fn test_update_task() {
run_test!(|client, conn| {
// Create new task and get its ID.
let task_name = "updatetasktest".to_string();
let t = Task::insert_with_old_date(&task_name, &conn);
assert!(t);
// Submit valid update form. Note that `updated_at` field isn't updated.
let inserted_id = Task::all(&conn)[0].id.unwrap();
let task_description = "newdescription".to_string();
let dt = Local::today().naive_local().to_string();
let form_data = format!("name={}&description={}&updated_at={}", task_name, task_description, dt);
let res = client.post(format!("/{}", inserted_id))
.header(ContentType::Form)
.body(form_data)
.dispatch();
let mut cookies = res.headers().get("Set-Cookie");
assert_eq!(res.status(), Status::SeeOther);
assert!(cookies.any(|value| value.contains("success")));
let updated_task = Task::task_by_id(inserted_id, &conn);
assert_eq!(updated_task.name, task_name);
assert_eq!(updated_task.description, task_description);
assert_ne!(updated_task.updated_at, dt);
})
}