-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
211 lines (167 loc) · 5.57 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
198
199
200
201
202
203
204
205
206
207
208
const axios = require("axios");
const formData = {
firstname: "Test",
lastname: "Test",
email: "[email protected]",
password: "123456",
admin: "false",
remember_me: "false"
};
const model = "SJ-200 Vintage";
const brand = "Gibson";
const product = {
model: "SJ-200 Vintage",
brand: "TEST",
specs: "The King of the Flat-tops! Played by more legendary performers and fea...",
price: 5499,
type: "Acoustic Guitar",
pic1: "https://static.gibson.com/product-images/Acoustic/ACC73T218/Vintage%20...",
pic2: "https://static.gibson.com/product-images/Acoustic/ACC73T218/Vintage%20...",
pic3: "https://static.gibson.com/product-images/Acoustic/ACC73T218/Vintage%20...",
quantity: 1
};
const register = async formData => {
const config = {
headers: {
'Content-Type': 'application/json'
}
};
try {
//we don't need to enter localhost 'cause we set it as a proxy
const res = await axios.post('http://localhost:5000/api/users', formData, config);
console.log("User was successfully registered");
return res.data;
} catch (err) {
console.error(err.response.data.msg);
}
};
// Add product to users cart
const addProduct = async (token) => {
const product = {
model: "SJ-200 Vintage",
brand: "Gibson",
specs: "The King of the Flat-tops! Played by more legendary performers and fea...",
price: 5499,
type: "Acoustic Guitar",
pic1: "https://static.gibson.com/product-images/Acoustic/ACC73T218/Vintage%20...",
pic2: "https://static.gibson.com/product-images/Acoustic/ACC73T218/Vintage%20...",
pic3: "https://static.gibson.com/product-images/Acoustic/ACC73T218/Vintage%20...",
quantity: 1
};
const config = {
headers: {
'Content-Type': 'application/json',
'x-auth-token': token.token
}
};
try {
const res = await axios.post('http://localhost:5000/api/shopping-cart', product, config);
console.log("Product was added to cart");
return res.data;
} catch (err) {
console.error(err.response.data.msg);
}
};
const loadAdmin = async () => {
try {
const config = {
headers: {
'Content-Type': 'application/json'
}
};
const user = {
email: "[email protected]",
password: "123456"
};
const res = await axios.post('http://localhost:5000/api/auth', user, config);
return res.data;
} catch (err) {
console.error(err);
}
};
//Add product
const addProductToStore = async token => {
const product = {
model: "SJ-200 Vintage",
brand: "TEST",
specs: "The King of the Flat-tops! Played by more legendary performers and fea...",
price: 666,
type: "Acoustic Guitar",
pic1: "https://static.gibson.com/product-images/Acoustic/ACC73T218/Vintage%20...",
pic2: "https://static.gibson.com/product-images/Acoustic/ACC73T218/Vintage%20...",
pic3: "https://static.gibson.com/product-images/Acoustic/ACC73T218/Vintage%20...",
quantity: 1
};
const config = {
headers: {
'Content-Type': 'application/json',
'x-auth-token': token.token
}
};
const newToken = token;
try {
const res = await axios.post('http://localhost:5000/api/products', product, config);
console.log("Product was successfully added");
//console.log(res.data._id);
return [newToken, res.data];
} catch (err) {
console.error(err.response.msg);
}
};
//Update product
const updateProduct = async tokenAndProduct => {
const newProduct = {
brand: "Test",
quantity: 13
};
const config = {
headers: {
'Content-Type': 'application/json',
'x-auth-token': tokenAndProduct[0].token
}
};
const newToken = tokenAndProduct[0];
try {
const res = await axios.put(`http://localhost:5000/api/products/${tokenAndProduct[1]._id}`, newProduct, config);
console.log('Product was successfully updated');
return [newToken, res.data._id];
} catch (err) {
console.error(err);
}
};
//Delete product
const deleteProduct = async tokenAndId => {
const config = {
headers: {
'Content-Type': 'application/json',
'x-auth-token': tokenAndId[0].token
}
};
try {
await axios.delete(`http://localhost:5000/api/products/${tokenAndId[1]}`, config);
//dispatch({ type: DELETE_PRODUCT, payload: id });
console.log("Product was successfully deleted");
} catch (err) {
console.error(err);
}
};
/*
register user, and add an item to his/her shopping cart
*/
const registrationTest = () => register(formData)
.then((token) => addProduct(token))
.then((product) => {
if (product.brand === brand && product.model === model) {
console.log("Registration test ended.");
console.log("**************************************\n");
}
})
.catch((err) => console.log(err));
//Test description: laoad admin user, add product, update it, remove it, be happy.
const adminTest = () => loadAdmin()
.then((token) => addProductToStore(token))
.then((tokenAndProduct) => updateProduct(tokenAndProduct))
.then((tokenAndId) => deleteProduct(tokenAndId))
//returns a token and an object that was added to the store
.then(() => console.log("Test ended."));
registrationTest().then(adminTest);