|
1
|
import { MedusaContainer } from "@medusajs/types" |
|
2
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
3
|
import { |
|
4
|
adminHeaders, |
|
5
|
createAdminUser, |
|
6
|
generatePublishableKey, |
|
7
|
generateStoreHeaders, |
|
8
|
} from "../../../../helpers/create-admin-user" |
|
9
|
import { createAuthenticatedCustomer } from "../../../../modules/helpers/create-authenticated-customer" |
|
10
|
|
|
11
|
jest.setTimeout(50000) |
|
12
|
|
|
13
|
medusaIntegrationTestRunner({ |
|
14
|
testSuite: ({ dbConnection, api, getContainer, dbUtils }) => { |
|
15
|
let appContainer: MedusaContainer |
|
16
|
let storeHeaders |
|
17
|
|
|
18
|
beforeAll(async () => { |
|
19
|
appContainer = getContainer() |
|
20
|
const publishableKey = await generatePublishableKey(appContainer) |
|
21
|
storeHeaders = generateStoreHeaders({ publishableKey }) |
|
22
|
await createAdminUser(dbConnection, adminHeaders, appContainer) |
|
23
|
|
|
24
|
await dbUtils.snapshot() |
|
25
|
}) |
|
26
|
|
|
27
|
describe("POST /store/customers", () => { |
|
28
|
it("should fails to create a customer without an identity", async () => { |
|
29
|
const customer = await api |
|
30
|
.post( |
|
31
|
"/store/customers", |
|
32
|
{ |
|
33
|
email: "newcustomer@medusa.js", |
|
34
|
first_name: "John", |
|
35
|
last_name: "Doe", |
|
36
|
}, |
|
37
|
storeHeaders |
|
38
|
) |
|
39
|
.catch((e) => e) |
|
40
|
|
|
41
|
expect(customer.response.status).toEqual(401) |
|
42
|
}) |
|
43
|
|
|
44
|
it("should successfully create a customer with an identity", async () => { |
|
45
|
const signup = await api.post("/auth/customer/emailpass/register", { |
|
46
|
email: "newcustomer@medusa.js", |
|
47
|
password: "secret_password", |
|
48
|
}) |
|
49
|
|
|
50
|
expect(signup.status).toEqual(200) |
|
51
|
expect(signup.data).toEqual({ token: expect.any(String) }) |
|
52
|
|
|
53
|
const customer = await api.post( |
|
54
|
"/store/customers", |
|
55
|
{ |
|
56
|
email: "newcustomer@medusa.js", |
|
57
|
first_name: "John", |
|
58
|
last_name: "Doe", |
|
59
|
metadata: { |
|
60
|
loyalty_level: "gold", |
|
61
|
preferences: { |
|
62
|
newsletter: true, |
|
63
|
}, |
|
64
|
}, |
|
65
|
}, |
|
66
|
{ |
|
67
|
headers: { |
|
68
|
authorization: `Bearer ${signup.data.token}`, |
|
69
|
...storeHeaders.headers, |
|
70
|
}, |
|
71
|
} |
|
72
|
) |
|
73
|
|
|
74
|
expect(customer.status).toEqual(200) |
|
75
|
expect(customer.data).toEqual({ |
|
76
|
customer: expect.objectContaining({ |
|
77
|
email: "newcustomer@medusa.js", |
|
78
|
first_name: "John", |
|
79
|
last_name: "Doe", |
|
80
|
has_account: true, |
|
81
|
metadata: { |
|
82
|
loyalty_level: "gold", |
|
83
|
preferences: { |
|
84
|
newsletter: true, |
|
85
|
}, |
|
86
|
}, |
|
87
|
}), |
|
88
|
}) |
|
89
|
}) |
|
90
|
|
|
91
|
it("should successfully create a customer with an identity even if the email is already taken by a non-registered customer", async () => { |
|
92
|
const nonRegisteredCustomer = await api.post( |
|
93
|
"/admin/customers", |
|
94
|
{ |
|
95
|
email: "newcustomer@medusa.js", |
|
96
|
first_name: "John", |
|
97
|
last_name: "Doe", |
|
98
|
}, |
|
99
|
adminHeaders |
|
100
|
) |
|
101
|
|
|
102
|
expect(nonRegisteredCustomer.status).toEqual(200) |
|
103
|
expect(nonRegisteredCustomer.data).toEqual({ |
|
104
|
customer: expect.objectContaining({ |
|
105
|
email: "newcustomer@medusa.js", |
|
106
|
first_name: "John", |
|
107
|
last_name: "Doe", |
|
108
|
has_account: false, |
|
109
|
}), |
|
110
|
}) |
|
111
|
|
|
112
|
const signup = await api.post("/auth/customer/emailpass/register", { |
|
113
|
email: "newcustomer@medusa.js", |
|
114
|
password: "secret_password", |
|
115
|
}) |
|
116
|
|
|
117
|
expect(signup.status).toEqual(200) |
|
118
|
expect(signup.data).toEqual({ token: expect.any(String) }) |
|
119
|
|
|
120
|
const customer = await api.post( |
|
121
|
"/store/customers", |
|
122
|
{ |
|
123
|
email: "newcustomer@medusa.js", |
|
124
|
first_name: "Jane", |
|
125
|
last_name: "Doe", |
|
126
|
}, |
|
127
|
{ |
|
128
|
headers: { |
|
129
|
authorization: `Bearer ${signup.data.token}`, |
|
130
|
...storeHeaders.headers, |
|
131
|
}, |
|
132
|
} |
|
133
|
) |
|
134
|
|
|
135
|
expect(customer.status).toEqual(200) |
|
136
|
expect(customer.data).toEqual({ |
|
137
|
customer: expect.objectContaining({ |
|
138
|
email: "newcustomer@medusa.js", |
|
139
|
first_name: "Jane", |
|
140
|
last_name: "Doe", |
|
141
|
has_account: true, |
|
142
|
}), |
|
143
|
}) |
|
144
|
|
|
145
|
// Check that customers co-exist |
|
146
|
const customers = await api.get("/admin/customers", adminHeaders) |
|
147
|
|
|
148
|
expect(customers.status).toEqual(200) |
|
149
|
expect(customers.data.customers).toHaveLength(2) |
|
150
|
expect(customers.data.customers).toEqual( |
|
151
|
expect.arrayContaining([ |
|
152
|
expect.objectContaining({ |
|
153
|
first_name: "Jane", |
|
154
|
last_name: "Doe", |
|
155
|
email: "newcustomer@medusa.js", |
|
156
|
has_account: true, |
|
157
|
}), |
|
158
|
expect.objectContaining({ |
|
159
|
first_name: "John", |
|
160
|
last_name: "Doe", |
|
161
|
email: "newcustomer@medusa.js", |
|
162
|
has_account: false, |
|
163
|
}), |
|
164
|
]) |
|
165
|
) |
|
166
|
}) |
|
167
|
|
|
168
|
it("should fail to create a customer with an identity when the email is already taken by a registered customer", async () => { |
|
169
|
const firstSignup = await api.post( |
|
170
|
"/auth/customer/emailpass/register", |
|
171
|
{ |
|
172
|
email: "newcustomer@medusa.js", |
|
173
|
password: "secret_password", |
|
174
|
} |
|
175
|
) |
|
176
|
|
|
177
|
expect(firstSignup.status).toEqual(200) |
|
178
|
expect(firstSignup.data).toEqual({ token: expect.any(String) }) |
|
179
|
|
|
180
|
await api.post( |
|
181
|
"/store/customers", |
|
182
|
{ |
|
183
|
email: "newcustomer@medusa.js", |
|
184
|
first_name: "John", |
|
185
|
last_name: "Doe", |
|
186
|
}, |
|
187
|
{ |
|
188
|
headers: { |
|
189
|
authorization: `Bearer ${firstSignup.data.token}`, |
|
190
|
...storeHeaders.headers, |
|
191
|
}, |
|
192
|
} |
|
193
|
) |
|
194
|
|
|
195
|
const firstSignin = await api.post("/auth/customer/emailpass", { |
|
196
|
email: "newcustomer@medusa.js", |
|
197
|
password: "secret_password", |
|
198
|
}) |
|
199
|
|
|
200
|
const customer = await api |
|
201
|
.post( |
|
202
|
"/store/customers", |
|
203
|
{ |
|
204
|
email: "newcustomer@medusa.js", |
|
205
|
first_name: "Jane", |
|
206
|
last_name: "Doe", |
|
207
|
}, |
|
208
|
{ |
|
209
|
headers: { |
|
210
|
authorization: `Bearer ${firstSignin.data.token}`, |
|
211
|
...storeHeaders.headers, |
|
212
|
}, |
|
213
|
} |
|
214
|
) |
|
215
|
.catch((e) => e) |
|
216
|
|
|
217
|
expect(customer.response.status).toEqual(400) |
|
218
|
expect(customer.response.data.message).toEqual( |
|
219
|
"Request already authenticated as a customer." |
|
220
|
) |
|
221
|
}) |
|
222
|
|
|
223
|
describe("With ensurePublishableApiKey middleware", () => { |
|
224
|
it("should fail when no publishable key is passed in the header", async () => { |
|
225
|
const { response } = await api |
|
226
|
.post( |
|
227
|
"/store/customers", |
|
228
|
{ |
|
229
|
email: "newcustomer@medusa.js", |
|
230
|
first_name: "John", |
|
231
|
last_name: "Doe", |
|
232
|
}, |
|
233
|
{ |
|
234
|
headers: {}, |
|
235
|
} |
|
236
|
) |
|
237
|
.catch((e) => e) |
|
238
|
|
|
239
|
expect(response.data).toEqual({ |
|
240
|
message: |
|
241
|
"Publishable API key required in the request header: x-publishable-api-key. You can manage your keys in settings in the dashboard.", |
|
242
|
type: "not_allowed", |
|
243
|
}) |
|
244
|
}) |
|
245
|
|
|
246
|
it("should fail when publishable keys are invalid", async () => { |
|
247
|
const { response } = await api |
|
248
|
.post( |
|
249
|
"/store/customers", |
|
250
|
{ |
|
251
|
email: "newcustomer@medusa.js", |
|
252
|
first_name: "John", |
|
253
|
last_name: "Doe", |
|
254
|
}, |
|
255
|
{ |
|
256
|
headers: { |
|
257
|
"x-publishable-api-key": ["test1", "test2"], |
|
258
|
}, |
|
259
|
} |
|
260
|
) |
|
261
|
.catch((e) => e) |
|
262
|
|
|
263
|
expect(response.data).toEqual({ |
|
264
|
message: |
|
265
|
"A valid publishable key is required to proceed with the request", |
|
266
|
type: "not_allowed", |
|
267
|
}) |
|
268
|
}) |
|
269
|
}) |
|
270
|
}) |
|
271
|
|
|
272
|
describe("POST /store/customers/me", () => { |
|
273
|
it("should successfully update a customer", async () => { |
|
274
|
const { customer, jwt } = await createAuthenticatedCustomer( |
|
275
|
api, |
|
276
|
storeHeaders |
|
277
|
) |
|
278
|
|
|
279
|
const response = await api.post( |
|
280
|
`/store/customers/me`, |
|
281
|
{ |
|
282
|
first_name: "John2", |
|
283
|
last_name: "Doe2", |
|
284
|
}, |
|
285
|
{ |
|
286
|
headers: { |
|
287
|
authorization: `Bearer ${jwt}`, |
|
288
|
...storeHeaders.headers, |
|
289
|
}, |
|
290
|
} |
|
291
|
) |
|
292
|
|
|
293
|
expect(response.status).toEqual(200) |
|
294
|
expect(response.data).toEqual({ |
|
295
|
customer: expect.objectContaining({ |
|
296
|
id: customer.id, |
|
297
|
first_name: "John2", |
|
298
|
last_name: "Doe2", |
|
299
|
email: "tony@start.com", |
|
300
|
}), |
|
301
|
}) |
|
302
|
}) |
|
303
|
}) |
|
304
|
}, |
|
305
|
}) |