-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest.ts
144 lines (122 loc) · 6.63 KB
/
test.ts
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
/*
Behaviors to test :
| | No credentials | Invalid credentials | Valid credentials |
|-------------------------------------|--------------------------------------------------------|--------------------------------------------------------|-------------------|
| GET / | 200 | 200 | 200 |
| GET /index.html | 200 | 200 | 200 |
| GET /index.js | 200 | 200 | 200 |
| GET /about.html | 200 | 200 | 200 |
| GET /_assets/ic_home.svg | 200 | 200 | 200 |
| GET /_assets/ic_about.svg | 200 | 200 | 200 |
| GET /_styles/app.css | 200 | 200 | 200 |
| | | | |
| GET /admin | 401, body=Restricted area, please login (user:pass). | 401, body=Restricted area, please login (user:pass). | 200 |
| GET /admin/index.html | 401, body=Restricted area, please login (user:pass). | 401, body=Restricted area, please login (user:pass). | 200 |
| GET /admin/index.js | 401, body=Restricted area, please login (user:pass). | 401, body=Restricted area, please login (user:pass). | 200 |
| GET /admin/users.html | 401, body=Restricted area, please login (user:pass). | 401, body=Restricted area, please login (user:pass). | 200 |
| GET /admin/_assets/ic_dashboard.svg | 401, body=Restricted area, please login (user:pass). | 401, body=Restricted area, please login (user:pass). | 200 |
| GET /admin/_assets/ic_users.svg | 401, body=Restricted area, please login (user:pass). | 401, body=Restricted area, please login (user:pass). | 200 |
| GET /admin/_styles/admin.css | 401, body=Restricted area, please login (user:pass). | 401, body=Restricted area, please login (user:pass). | 200 |
| | | | |
| GET /foo | 404 | 404 | 404 |
| GET /admin/foo | 401, body=Restricted area, please login (user:pass). | 401, body=Restricted area, please login (user:pass). | 404 |
*/
if (!process.env.TEST_URL)
throw 'The env variable `TEST_URL` must be set.';
if (!process.env.TEST_VARIANT)
throw 'The env variable `TEST_VARIANT` must be set to one of the following: no-credentials, invalid-credentials, valid-credentials';
//
// Imports
//
import nodeFetch from 'node-fetch';
//
// Test variants
//
const testVariants = {
'no-credentials': {
name: 'No credentials',
credentials: {
username: null,
password: null,
},
expectedAdminAreaResponseCode: 401,
expectedAdminAreaNonExistentUrlResponseCode: 401,
},
'invalid-credentials': {
name: 'Invalid credentials',
credentials: {
username: 'foo',
password: 'bar',
},
expectedAdminAreaResponseCode: 401,
expectedAdminAreaNonExistentUrlResponseCode: 401,
},
'valid-credentials': {
name: 'Valid credentials',
credentials: {
username: 'user',
password: 'pass',
},
expectedAdminAreaResponseCode: 200,
expectedAdminAreaNonExistentUrlResponseCode: 404,
},
};
const testUrl = process.env.TEST_URL
const testVariant = testVariants[process.env.TEST_VARIANT];
//
// Helpers
//
function adminAreaTest(url: string, expectedResponseCode: number) {
describe('GET ' + url, () => {
it(expectedResponseCode.toString(), async () => {
const res = await fetch(url);
expect(res.status).toBe(expectedResponseCode);
// If not authorized, check response body and realm too
if (res.status === 401) {
const body = await res.text();
expect(body).toBe('Restricted area, please login (user:pass).');
expect(res.headers.get('www-authenticate')).toMatch(/Basic realm="vercel-basic-auth\.[a-z-]+"/);
}
});
});
}
function fetch(relativeUrl: string) {
return nodeFetch(testUrl + relativeUrl, {
headers: {
'Authorization': 'Basic ' + Buffer.from(testVariant.credentials.username + ':' + testVariant.credentials.password).toString('base64')
}
});
}
function publicAreaTest(url: string, expectedResponseCode: number) {
describe('GET ' + url, () => {
it(expectedResponseCode.toString(), async () => {
const res = await fetch(url);
expect(res.status).toBe(expectedResponseCode);
});
});
}
//
// Tests
//
describe(testUrl + ': ' + testVariant.name, () => {
// Public area
publicAreaTest('/', 200);
publicAreaTest('/index.html', 200);
publicAreaTest('/index.js', 200);
publicAreaTest('/about.html', 200);
publicAreaTest('/_assets/ic_home.svg', 200);
publicAreaTest('/_assets/ic_about.svg', 200);
publicAreaTest('/_styles/app.css', 200);
// Admin area
const expectedResponseCode = testVariant.expectedAdminAreaResponseCode;
adminAreaTest('/admin', expectedResponseCode);
adminAreaTest('/admin/index.html', expectedResponseCode);
adminAreaTest('/admin/index.js', expectedResponseCode);
adminAreaTest('/admin/users.html', expectedResponseCode);
adminAreaTest('/admin/_assets/ic_dashboard.svg', expectedResponseCode);
adminAreaTest('/admin/_assets/ic_users.svg', expectedResponseCode);
adminAreaTest('/admin/_styles/admin.css', expectedResponseCode);
// Non-existent URL
publicAreaTest('/foo', 404);
adminAreaTest('/admin/foo', testVariant.expectedAdminAreaNonExistentUrlResponseCode);
});