forked from mrvautin/expressCart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
197 lines (179 loc) · 4.83 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
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
const test = require('ava');
const fs = require('fs');
const app = require('../app');
const common = require('../lib/common');
const request = require('supertest');
const agent = request.agent(app);
// Get test data to compare in tests
const rawTestData = fs.readFileSync('./bin/testdata.json', 'utf-8');
const testData = JSON.parse(rawTestData);
// Setup some global DB objects for comparison
let db;
let config;
let products;
let customers;
let users;
// Start up app and wait for it to be ready
test.before.cb(t => {
app.on('appStarted', async () => {
// Set some stuff now we have the app started
config = app.config;
db = app.db;
// Get some data from DB to use in compares
await common.testData(app);
products = await db.products.find({}).toArray();
customers = await db.customers.find({}).toArray();
users = await db.users.find({}).toArray();
agent
.post('/admin/login_action')
.send({
email: users[0].userEmail,
password: 'test'
})
.expect(200)
.end((err, res) => {
if(err){
t.fail();
t.end();
}
t.end();
});
});
});
test.cb('[Success] Get products JSON', t => {
agent
.get('?json=true')
.expect(200)
.end((err, res) => {
if(err){
t.fail();
t.end();
}
if(res.body.length < config.productsPerPage){
t.is(res.body.length, testData.products.length);
}else{
t.is(res.body.length, config.productsPerPage);
}
t.pass();
t.end();
});
});
test.cb('[Success] User Login', t => {
agent
.post('/admin/login_action')
.send({
email: users[0].userEmail,
password: 'test'
})
.expect(200)
.end((err, res) => {
if(err){
t.fail();
t.end();
}
t.deepEqual(res.body.message, 'Login successful');
t.end();
});
});
test.cb('[Fail] Incorrect user password', t => {
agent
.post('/admin/login_action')
.send({
email: users[0].userEmail,
password: 'test1'
})
.expect(400)
.end((err, res) => {
if(err){
t.fail();
t.end();
}
t.deepEqual(res.body.message, 'Access denied. Check password and try again.');
t.end();
});
});
test.cb('[Fail] Customer login with incorrect email', t => {
agent
.post('/customer/login_action')
.send({
loginEmail: 'test1@test.com',
loginPassword: 'test'
})
.expect(400)
.end((err, res) => {
if(err){
t.fail();
t.end();
}
t.deepEqual(res.body.message, 'A customer with that email does not exist.');
t.end();
});
});
test.cb('[Success] Customer login with correct email', t => {
agent
.post('/customer/login_action')
.send({
loginEmail: 'test@test.com',
loginPassword: 'test'
})
.expect(200)
.end((err, res) => {
if(err){
t.fail();
t.end();
}
t.deepEqual(res.body.message, 'Successfully logged in');
t.end();
});
});
test.cb('[Success] Add product to cart', t => {
agent
.post('/product/addtocart')
.send({
productId: products[0]._id,
productQuantity: 1,
productOptions: JSON.stringify(products[0].productOptions)
})
.expect(200)
.end((err, res) => {
if(err){
t.fail();
t.end();
}
t.deepEqual(res.body.message, 'Cart successfully updated');
t.end();
});
});
test.cb('[Fail] Add incorrect product to cart', t => {
agent
.post('/product/addtocart')
.send({
id: 'fake_product_id',
state: false
})
.expect(400)
.end((err, res) => {
if(err){
t.fail();
t.end();
}
t.deepEqual(res.body.message, 'Error updating cart. Please try again.');
t.end();
});
});
test.cb('[Sucess] Change product publish status', t => {
agent
.post('/admin/product/published_state')
.send({
id: products[0]._id,
state: false
})
.expect(200)
.end((err, res) => {
if(err){
t.fail();
t.end();
}
t.end();
});
});