-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-api.mjs
More file actions
396 lines (361 loc) Β· 8.33 KB
/
Copy pathtest-api.mjs
File metadata and controls
396 lines (361 loc) Β· 8.33 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
const API_URL = 'http://localhost:3000/graphql';
const gql = async (query, token = null) => {
const headers = { 'Content-Type': 'application/json' };
if (token) headers['Authorization'] = `Bearer ${token}`;
const res = await fetch(API_URL, {
method: 'POST',
headers,
body: JSON.stringify({ query }),
});
return res.json();
};
const log = (emoji, name, success, data = null) => {
console.log(`\n${emoji} ${name}`);
if (success) {
console.log('β
SUCCESS');
if (data) console.log(JSON.stringify(data, null, 2));
} else {
console.log('β FAILED');
if (data) console.log(JSON.stringify(data, null, 2));
}
};
(async () => {
console.log('π Testing All GraphQL APIs\n');
// 1. Employee Login (SuperAdmin)
console.log('=== 1. Employee Authentication ===');
const loginRes = await gql(`
mutation {
employeeLogin(email: "superadmin@dima.com", password: "admin123") {
accessToken
employee { id firstname lastname email role { id name } }
}
}
`);
if (loginRes.errors) {
log('π', 'Employee Login', false, loginRes.errors);
process.exit(1);
}
const token = loginRes.data.employeeLogin.accessToken;
log('π', 'Employee Login', true, loginRes.data.employeeLogin.employee);
console.log(`Token: ${token.substring(0, 30)}...`);
// 2. Employee Me
const meRes = await gql(
`
query {
employeeMe {
id firstname lastname email
role { id name }
lastConnectionDate
}
}
`,
token,
);
log(
'π€',
'Employee Me',
!meRes.errors,
meRes.data?.employeeMe || meRes.errors,
);
// 3. Roles CRUD
console.log('\n=== 2. Roles Management ===');
// List Roles
const rolesRes = await gql(
'query { roles { id name description isSystem } }',
token,
);
log(
'π',
'List Roles',
!rolesRes.errors,
rolesRes.data?.roles || rolesRes.errors,
);
// Create Role
const createRoleRes = await gql(
`
mutation {
createRole(input: { name: "MANAGER", description: "Manager access" }) {
id name description isSystem
}
}
`,
token,
);
log(
'β',
'Create Role',
!createRoleRes.errors,
createRoleRes.data?.createRole || createRoleRes.errors,
);
const roleId = createRoleRes.data?.createRole?.id;
if (roleId) {
// Update Role
const updateRoleRes = await gql(
`
mutation {
updateRole(id: ${roleId}, input: { description: "Updated Manager" }) {
id name description
}
}
`,
token,
);
log(
'βοΈ',
'Update Role',
!updateRoleRes.errors,
updateRoleRes.data?.updateRole || updateRoleRes.errors,
);
// Delete Role
const deleteRoleRes = await gql(
`
mutation {
deleteRole(id: ${roleId}) { id name }
}
`,
token,
);
log(
'ποΈ',
'Delete Role',
!deleteRoleRes.errors,
deleteRoleRes.data?.deleteRole || deleteRoleRes.errors,
);
}
// 4. Employee CRUD
console.log('\n=== 3. Employee Management ===');
// List Employees
const employeesRes = await gql(
`
query {
employees {
id firstname lastname email
role { name }
active
}
}
`,
token,
);
log(
'π',
'List Employees',
!employeesRes.errors,
employeesRes.data?.employees || employeesRes.errors,
);
// Get Sales Role ID
const salesRole = rolesRes.data?.roles?.find((r) => r.name === 'SALES');
if (salesRole) {
// Create Employee
const createEmpRes = await gql(
`
mutation {
createEmployee(input: {
email: "test@dima.com"
password: "test123"
firstname: "Test"
lastname: "User"
roleId: ${salesRole.id}
}) {
id email role { name }
}
}
`,
token,
);
log(
'β',
'Create Employee',
!createEmpRes.errors,
createEmpRes.data?.createEmployee || createEmpRes.errors,
);
const empId = createEmpRes.data?.createEmployee?.id;
if (empId) {
// Get One Employee
const getEmpRes = await gql(
`
query {
employee(id: ${empId}) {
id firstname lastname email role { name } active
}
}
`,
token,
);
log(
'π',
'Get Employee',
!getEmpRes.errors,
getEmpRes.data?.employee || getEmpRes.errors,
);
// Update Employee
const updateEmpRes = await gql(
`
mutation {
updateEmployee(id: ${empId}, input: { active: false }) {
id active
}
}
`,
token,
);
log(
'βοΈ',
'Update Employee',
!updateEmpRes.errors,
updateEmpRes.data?.updateEmployee || updateEmpRes.errors,
);
// Delete Employee
const deleteEmpRes = await gql(
`
mutation {
deleteEmployee(id: ${empId}) { id email }
}
`,
token,
);
log(
'ποΈ',
'Delete Employee',
!deleteEmpRes.errors,
deleteEmpRes.data?.deleteEmployee || deleteEmpRes.errors,
);
}
}
// 5. Customer Authentication
console.log('\n=== 4. Customer Authentication ===');
const customerLoginRes = await gql(`
mutation {
customerLogin(email: "customer@example.com", password: "customer123") {
accessToken
customer { id firstname lastname email }
}
}
`);
log(
'π€',
'Customer Login',
!customerLoginRes.errors,
customerLoginRes.data?.customerLogin?.customer || customerLoginRes.errors,
);
const customerToken = customerLoginRes.data?.customerLogin?.accessToken;
if (customerToken) {
// Customer Me
const customerMeRes = await gql(
`
query {
customerMe { id firstname lastname email active }
}
`,
customerToken,
);
log(
'π€',
'Customer Me',
!customerMeRes.errors,
customerMeRes.data?.customerMe || customerMeRes.errors,
);
}
// Customer Register
const registerRes = await gql(`
mutation {
customerRegister(input: {
email: "newcust@test.com"
password: "pass123"
firstname: "New"
lastname: "Customer"
groupId: 1
}) {
accessToken
customer { id email }
}
}
`);
log(
'π',
'Customer Register',
!registerRes.errors,
registerRes.data?.customerRegister?.customer || registerRes.errors,
);
// 6. Customer Groups
console.log('\n=== 5. Customer Groups ===');
// List Groups
const groupsRes = await gql(
'query { customerGroups { id name reduction showPrices } }',
token,
);
log(
'π',
'List Groups',
!groupsRes.errors,
groupsRes.data?.customerGroups || groupsRes.errors,
);
// Create Group
const createGroupRes = await gql(
`
mutation {
createCustomerGroup(input: { name: "VIP", reduction: 10.0, showPrices: true }) {
id name reduction
}
}
`,
token,
);
log(
'β',
'Create Group',
!createGroupRes.errors,
createGroupRes.data?.createCustomerGroup || createGroupRes.errors,
);
const groupId = createGroupRes.data?.createCustomerGroup?.id;
if (groupId) {
// Get One Group
const getGroupRes = await gql(
`
query {
customerGroup(id: ${groupId}) { id name reduction showPrices }
}
`,
token,
);
log(
'π',
'Get Group',
!getGroupRes.errors,
getGroupRes.data?.customerGroup || getGroupRes.errors,
);
// Update Group
const updateGroupRes = await gql(
`
mutation {
updateCustomerGroup(id: ${groupId}, input: { reduction: 15.0 }) {
id name reduction
}
}
`,
token,
);
log(
'βοΈ',
'Update Group',
!updateGroupRes.errors,
updateGroupRes.data?.updateCustomerGroup || updateGroupRes.errors,
);
// Delete Group
const deleteGroupRes = await gql(
`
mutation {
deleteCustomerGroup(id: ${groupId}) { id name }
}
`,
token,
);
log(
'ποΈ',
'Delete Group',
!deleteGroupRes.errors,
deleteGroupRes.data?.deleteCustomerGroup || deleteGroupRes.errors,
);
}
console.log('\nπ All tests completed!\n');
})();