1 import { IAuthModuleService } from "@medusajs/framework/types"
2 import { Modules } from "@medusajs/framework/utils"
3 import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
4 import { createAuthIdentities } from "../../__fixtures__/auth-identity"
5
6 jest.setTimeout(30000)
7
8 moduleIntegrationTestRunner<IAuthModuleService>({
9 moduleName: Modules.AUTH,
10 testSuite: ({ service }) => {
11 describe("AuthModuleService - AuthIdentity", () => {
12 beforeEach(async () => {
13 await createAuthIdentities(service)
14 })
15
16 describe("listAuthIdentities", () => {
17 it("should list authIdentities", async () => {
18 const authIdentities = await service.listAuthIdentities(
19 {},
20 { relations: ["provider_identities"] }
21 )
22
23 expect(authIdentities).toEqual([
24 expect.objectContaining({
25 provider_identities: [
26 expect.objectContaining({ provider: "store" }),
27 ],
28 }),
29 expect.objectContaining({
30 provider_identities: [
31 expect.objectContaining({ provider: "manual" }),
32 ],
33 }),
34 expect.objectContaining({
35 provider_identities: [
36 expect.objectContaining({ provider: "manual" }),
37 ],
38 }),
39 ])
40 })
41
42 it("should list authIdentities by id", async () => {
43 const authIdentities = await service.listAuthIdentities({
44 id: ["test-id"],
45 })
46
47 expect(authIdentities).toEqual([
48 expect.objectContaining({
49 id: "test-id",
50 }),
51 ])
52 })
53
54 it("should list authIdentities by provider", async () => {
55 const authIdentities = await service.listAuthIdentities({
56 provider_identities: {
57 provider: "manual",
58 },
59 })
60
61 expect(authIdentities).toEqual([
62 expect.objectContaining({
63 id: "test-id",
64 }),
65 expect.objectContaining({
66 id: "test-id-1",
67 }),
68 ])
69 })
70
71 it("should list authIdentities by meta data", async () => {
72 const authIdentities = await service.listAuthIdentities({
73 app_metadata: {
74 user_id: "user-1",
75 },
76 })
77
78 expect(authIdentities).toEqual([
79 expect.objectContaining({
80 id: "test-id",
81 }),
82 ])
83 })
84 })
85
86 describe("listAndCountAuthIdentities", () => {
87 it("should list and count authIdentities", async () => {
88 const [authIdentities, count] =
89 await service.listAndCountAuthIdentities(
90 {},
91 { relations: ["provider_identities"] }
92 )
93
94 expect(count).toEqual(3)
95 expect(authIdentities).toEqual([
96 expect.objectContaining({
97 provider_identities: [
98 expect.objectContaining({ provider: "store" }),
99 ],
100 }),
101 expect.objectContaining({
102 provider_identities: [
103 expect.objectContaining({ provider: "manual" }),
104 ],
105 }),
106 expect.objectContaining({
107 provider_identities: [
108 expect.objectContaining({ provider: "manual" }),
109 ],
110 }),
111 ])
112 })
113
114 it("should listAndCount authIdentities by provider_id", async () => {
115 const [authIdentities, count] =
116 await service.listAndCountAuthIdentities({
117 provider_identities: { provider: "manual" },
118 })
119
120 expect(count).toEqual(2)
121 expect(authIdentities).toEqual([
122 expect.objectContaining({
123 id: "test-id",
124 }),
125 expect.objectContaining({
126 id: "test-id-1",
127 }),
128 ])
129 })
130 })
131
132 describe("retrieveAuthIdentity", () => {
133 const id = "test-id"
134
135 it("should return an authIdentity for the given id", async () => {
136 const authIdentity = await service.retrieveAuthIdentity(id)
137
138 expect(authIdentity).toEqual(
139 expect.objectContaining({
140 id,
141 })
142 )
143 })
144
145 it("should throw an error when an authIdentity with the given id does not exist", async () => {
146 let error
147
148 try {
149 await service.retrieveAuthIdentity("does-not-exist")
150 } catch (e) {
151 error = e
152 }
153
154 expect(error.message).toEqual(
155 "AuthIdentity with id: does-not-exist was not found"
156 )
157 })
158
159 it("should not return an authIdentity with password hash", async () => {
160 const authIdentity = await service.retrieveAuthIdentity("test-id-1")
161
162 expect(authIdentity).toEqual(
163 expect.objectContaining({
164 id: "test-id-1",
165 })
166 )
167 expect(
168 authIdentity.provider_identities?.[0].provider_metadata?.[
169 "password_hash"
170 ]
171 ).toEqual(undefined)
172 })
173
174 it("should throw an error when a authIdentityId is not provided", async () => {
175 let error
176
177 try {
178 await service.retrieveAuthIdentity(undefined as unknown as string)
179 } catch (e) {
180 error = e
181 }
182
183 expect(error.message).toEqual("authIdentity - id must be defined")
184 })
185
186 it("should return authIdentity based on config select param", async () => {
187 const authIdentity = await service.retrieveAuthIdentity(id, {
188 select: ["id"],
189 })
190
191 expect(authIdentity).toEqual({
192 id,
193 })
194 })
195 })
196
197 describe("deleteAuthIdentity", () => {
198 const id = "test-id"
199
200 it("should delete the authIdentities given an id successfully", async () => {
201 await service.deleteAuthIdentities([id])
202
203 const authIdentities = await service.listAuthIdentities({
204 id: [id],
205 })
206
207 expect(authIdentities).toHaveLength(0)
208 })
209 })
210
211 describe("updateAuthIdentity", () => {
212 const id = "test-id"
213
214 it("should throw an error when a id does not exist", async () => {
215 let error
216
217 try {
218 await service.updateAuthIdentities([
219 {
220 id: "does-not-exist",
221 },
222 ])
223 } catch (e) {
224 error = e
225 }
226
227 expect(error.message).toEqual(
228 'AuthIdentity with id "does-not-exist" not found'
229 )
230 })
231
232 it("should update authIdentity", async () => {
233 await service.updateAuthIdentities([
234 {
235 id,
236 app_metadata: { email: "test@email.com" },
237 },
238 ])
239
240 const [authIdentity] = await service.listAuthIdentities({ id: [id] })
241 expect(authIdentity).toEqual(
242 expect.objectContaining({
243 app_metadata: expect.objectContaining({
244 email: "test@email.com",
245 }),
246 })
247 )
248 })
249 })
250
251 describe("createAuthIdentity", () => {
252 it("should create a authIdentity successfully", async () => {
253 await service.createAuthIdentities([
254 {
255 id: "test",
256 provider_identities: [
257 {
258 provider: "manual",
259 entity_id: "test",
260 },
261 ],
262 },
263 ])
264
265 const [authIdentity, count] =
266 await service.listAndCountAuthIdentities({
267 id: ["test"],
268 })
269
270 expect(count).toEqual(1)
271 expect(authIdentity[0]).toEqual(
272 expect.objectContaining({
273 id: "test",
274 })
275 )
276 })
277 })
278
279 describe("createProviderIdentity", () => {
280 it("should create a providerIdentity successfully", async () => {
281 let authIdentity = await service.retrieveAuthIdentity("test-id", {
282 relations: ["provider_identities"],
283 })
284
285 expect(authIdentity).toEqual(
286 expect.not.objectContaining({
287 provider_identities: [
288 expect.objectContaining({ provider: "manual" }),
289 expect.objectContaining({ provider: "github" }),
290 ],
291 })
292 )
293
294 await service.createProviderIdentities({
295 id: "test",
296 entity_id: "christian@medusajs.com",
297 provider: "github",
298 auth_identity_id: authIdentity.id,
299 })
300
301 authIdentity = await service.retrieveAuthIdentity("test-id", {
302 relations: ["provider_identities"],
303 })
304
305 expect(authIdentity).toEqual(
306 expect.objectContaining({
307 provider_identities: [
308 expect.objectContaining({ provider: "manual" }),
309 expect.objectContaining({ provider: "github" }),
310 ],
311 })
312 )
313 })
314
315 it("should list authIdentities by newly created provider", async () => {
316 await service.createProviderIdentities([
317 {
318 id: "test",
319 entity_id: "christian@medusajs.com",
320 provider: "github",
321 auth_identity_id: "test-id",
322 },
323 ])
324
325 const authIdentities = await service.listAuthIdentities({
326 provider_identities: {
327 provider: "github",
328 },
329 })
330
331 expect(authIdentities).toEqual([
332 expect.objectContaining({
333 id: "test-id",
334 }),
335 ])
336 })
337 })
338
339 describe("deleteProviderIdentity", () => {
340 const entity_id = "provider-test-id"
341
342 it("should delete the providerIdentities given an id successfully", async () => {
343 let providerIdentities = await service.listProviderIdentities({
344 entity_id,
345 })
346
347 expect(providerIdentities).toHaveLength(1)
348 const providerIdentityId = providerIdentities[0].id
349
350 await service.deleteProviderIdentities([providerIdentityId])
351
352 providerIdentities = await service.listProviderIdentities({
353 entity_id,
354 })
355
356 expect(providerIdentities).toHaveLength(0)
357 })
358 })
359
360 describe("updateProviderIdentity", () => {
361 const entity_id = "provider-test-id"
362
363 it("should throw an error when a id does not exist", async () => {
364 let error
365
366 try {
367 await service.updateProviderIdentities([
368 {
369 id: "does-not-exist",
370 },
371 ])
372 } catch (e) {
373 error = e
374 }
375
376 expect(error.message).toEqual(
377 'ProviderIdentity with id "does-not-exist" not found'
378 )
379 })
380
381 it("should update providerIdentity", async () => {
382 let [providerIdentity] = await service.listProviderIdentities({
383 entity_id,
384 })
385 await service.updateProviderIdentities([
386 {
387 id: providerIdentity.id,
388 provider_metadata: { email: "test@email.com" },
389 },
390 ])
391
392 const providerIdentities = await service.listProviderIdentities({
393 id: [providerIdentity.id],
394 })
395
396 expect(providerIdentities[0]).toEqual(
397 expect.objectContaining({
398 provider_metadata: expect.objectContaining({
399 email: "test@email.com",
400 }),
401 })
402 )
403 })
404 })
405 })
406 },
407 })