-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
161 lines (141 loc) · 4.59 KB
/
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
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
const axios = require('axios');
const assert=require('assert');
const baseURI = "https://blooming-mountain-10766.herokuapp.com/";
const config = {
headers: {
'Content-Type': 'application/json'
}
}
const registerData = [
{
"username": "orange_guy10",
"password": "ackabacka95",
"email": "[email protected]",
"name": "Donald Trump",
"expected": 200
},
{
"username": "uncle_joe2005",
"password": "ackabacka95",
"email": "[email protected]",
"name": "Joe Biden",
"expected": 200
},
{
"username": "cool_guy10",
"password": "ackabacka10",
"email": "[email protected]",
"expected": 400
},
{
"username": "cool_guy11",
"name": "Teddy Roosevelt",
"password": "[email protected]",
"expected": 400
},
{
"username": "helloWorld10",
"name": "George Washington",
"password": "ackabacka10",
"email": "[email protected]",
"expected": 400
},
{
"name": "Karen",
"password": "supermarketlady23",
"email": "[email protected]",
"expected": 400
},
{
"email": "[email protected]",
"password": "12345678",
"expected": 400
}
]
// Testing if backend is connected
axios.get(baseURI)
.then(res => {
assert.deepEqual(res.status, 200, "Not connected right");
console.log('Backend is connected...');
})
.catch(err => {
console.error(err.message);
exit(1);
});
// GET api/srcollins
axios.get(baseURI+"api/srcollins")
.then(res => {
let lotName = res.data.lotName;
let responseStatus = res.status;
assert.deepEqual(lotName, "srcollins", `Expected name to be srcollins but returned ${lotName}`);
assert.deepEqual(responseStatus, 200, `Expected a 200 Status Code but returned ${responseStatus}`);
console.log("api/srcollins works...");
})
.catch(err => {
console.error(err.message);
exit(1);
});
// GET api/newscience
axios.get(baseURI+"api/newscience")
.then(res => {
let lotName = res.data.lotName;
let responseStatus = res.status;
assert.deepEqual(lotName, "newscience", `Expected name to be newscience but returned ${lotName}`);
assert.deepEqual(responseStatus, 200, `Expected a 200 Status Code but returned ${responseStatus}`);
console.log("api/newscience works...");
})
.catch(err => {
console.error(err.message);
exit(1);
});
// GET api/businessag
axios.get(baseURI+"api/businessag")
.then(res => {
let lotName = res.data.lotName;
let responseStatus = res.status;
assert.deepEqual(lotName, "businessag", `Expected name to be businessag but returned ${lotName}`);
assert.deepEqual(responseStatus, 200, `Expected a 200 Status Code but returned ${responseStatus}`);
console.log("api/businessag works...");
})
.catch(err => {
console.error(err.message);
exit(1);
});
// GET api/msc
axios.get(baseURI+"api/msc")
.then(res => {
let lotName = res.data.lotName;
let responseStatus = res.status;
assert.deepEqual(lotName, 'memorial student center', `Expected name to be msc but returned ${lotName}`);
assert.deepEqual(responseStatus, 200, `Expected a 200 Status Code but returned ${responseStatus}`);
console.log("api/msc works...");
})
.catch(err => {
console.error(err.message);
exit(1);
});
/**
** This for-loop wil iterate through the registerData JSON and automatically
** register each user through the "api/users" endpoint.
*/
for (let i = 0; i < registerData.length; i++) {
// Destructure each key from JSON registerData
let {username, name, password, email, expected} = registerData[i];
let body = JSON.stringify({username, name, password, email});
// Use axios to make an HTTP POST request with the body and config settings
axios.post(baseURI + 'api/users', body, config)
.then(res => {
let responseStatus = res.status;
assert.deepEqual(responseStatus, expected, `Expected ${expected} but received ${responseStatus}`);
console.log(`api/users... Test Case [${i}] passing`);
})
.catch(err => {
if (err.response.status == registerData[i].expected) {
console.log(`api/users... Negative Test Case [${i}] passing`);
}
else {
console.error(`api/users... Failing at Test Case [${i}]`);
exit(1);
}
})
}