-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.test.js
109 lines (94 loc) · 3.19 KB
/
server.test.js
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
const supertest = require("supertest");
const { app } = require("./server");
const cookieSession = require("cookie-session");
// console.log("app in server.test.js");
//test takes 2 arguments
// 1st arg string that describes the test we want to run
// 2nd arg is a callback function that contains the actual test
// test("GET / register sends a 200 status code as a response", () => {
// return supertest(app)
// .get("/register")
// .then((res) => {
// // console.log("status code: ", res.statusCode);
// expect(res.statusCode).toBe(200);
// });
// });
// test.only("POST / register redirects to /profile", () => {
// return supertest(app)
// .post("/register")
// .then((res) => {
// expect(res.statusCode).toBe(302);
// expect(res.header.location).toBe("/profile");
// });
// });
// NOT working for now
// test("GET / petition sends 302 when there is no cookie", () => {
// cookieSession.mockSessionOnce({});
// return supertest(app)
// .get("/petition")
// .then((res) => {
// expect(res.statusCode).toBe(302);
// });
// });
// test("GET / petition sends 200 if there is a cookie", () => {
// cookieSession.mockSessionOnce({
// userId: true,
// loggedIn: true,
// signatureId: true,
// });
// return supertest(app)
// .get("/petition")
// .then((res) => {
// expect(res.statusCode).toBe(200);
// });
// });
test("Logged out users redirected to register when they attempt to go to petition", () => {
cookieSession.mockSessionOnce({
userId: false,
loggedIn: false,
});
return supertest(app)
.get("/petition")
.then((res) => {
expect(res.statusCode).toBe(302);
expect(res.header.location).toBe("/register");
});
});
test("logged in users redirected to petition when they attempt to go to register or login", () => {
cookieSession.mockSessionOnce({
userId: true,
loggedIn: true,
});
return supertest(app)
.get("/register", "/login")
.then((res) => {
expect(res.statusCode).toBe(302);
expect(res.header.location).toBe("/petition");
});
});
test("users logged in that signed are redirected to thanks when they try to access petition or submit signature", () => {
cookieSession.mockSessionOnce({
signatureId: true,
userId: true,
loggedIn: true,
});
return supertest(app)
.get("/petition")
.then((res) => {
expect(res.statusCode).toBe(302);
expect(res.header.location).toBe("/thanks");
});
});
test("Users who are logged in and have not signed the petition are redirected to the petition page when they attempt to go to either the thank you page or the signers page", () => {
cookieSession.mockSessionOnce({
signatureId: false,
userId: true,
loggedIn: true,
});
return supertest(app)
.get("/thanks", "/signers")
.then((res) => {
expect(res.statusCode).toBe(302);
expect(res.header.location).toBe("/petition");
});
});