1 import { asValue } from "@medusajs/deps/awilix"
2 import { createMedusaContainer } from "@medusajs/utils"
3 import loadProviders from "../providers"
4 import AuthMfaProviderService from "../../services/mfa-provider"
5 import AuthVerificationProviderService from "../../services/verification-provider"
6
7 class MfaProviderWithoutIdentifier {
8 readonly method = "missing_identifier"
9 }
10
11 class VerificationProviderWithoutIdentifier {
12 readonly identifier = "missing_identifier"
13 }
14
15 describe("providers loader - MFA", () => {
16 let container
17 const logger = {
18 error: jest.fn(),
19 }
20
21 beforeEach(() => {
22 container = createMedusaContainer()
23 container.register({
24 logger: asValue(logger),
25 })
26 })
27
28 afterEach(() => {
29 jest.clearAllMocks()
30 })
31
32 it("rejects MFA providers without a static identifier", async () => {
33 const err = await loadProviders({
34 container,
35 options: {
36 mfa: {
37 providers: [
38 {
39 resolve: {
40 services: [MfaProviderWithoutIdentifier],
41 },
42 id: "missing_identifier",
43 },
44 ],
45 },
46 },
47 } as any).catch((e) => e)
48
49 expect(err).toBeTruthy()
50 expect(err.message).toBe(
51 "Trying to register an MFA provider without a provider identifier."
52 )
53 })
54
55 it("returns a helpful error when an MFA method id is unknown", async () => {
56 await loadProviders({
57 container,
58 options: {},
59 } as any)
60
61 const service = new AuthMfaProviderService((container as any).cradle)
62
63 await expect(
64 service.start("unknown", {
65 auth_identity_id: "auth-id",
66 provider: "unknown",
67 })
68 ).rejects.toThrow("Unable to retrieve the MFA method with id: unknown")
69
70 expect(logger.error).toHaveBeenCalledWith(
71 expect.stringContaining("AwilixResolutionError"),
72 expect.any(Error)
73 )
74 })
75 })
76
77 describe("providers loader - verification", () => {
78 let container
79 const logger = {
80 error: jest.fn(),
81 }
82
83 beforeEach(() => {
84 container = createMedusaContainer()
85 container.register({
86 logger: asValue(logger),
87 })
88 })
89
90 afterEach(() => {
91 jest.clearAllMocks()
92 })
93
94 it("rejects verification providers without a static identifier", async () => {
95 const err = await loadProviders({
96 container,
97 options: {
98 verification: {
99 providers: [
100 {
101 resolve: {
102 services: [VerificationProviderWithoutIdentifier],
103 },
104 id: "missing_identifier",
105 },
106 ],
107 },
108 },
109 } as any).catch((e) => e)
110
111 expect(err).toBeTruthy()
112 expect(err.message).toBe(
113 "Trying to register a verification provider without a provider identifier."
114 )
115 })
116
117 it("returns a helpful error when a verification provider id is unknown", async () => {
118 await loadProviders({
119 container,
120 options: {},
121 } as any)
122
123 const service = new AuthVerificationProviderService(
124 (container as any).cradle
125 )
126
127 await expect(
128 service.request("unknown", {
129 auth_identity_id: "auth-id",
130 entity_id: "verify@test.com",
131 entity_type: "email",
132 code_provider: "unknown",
133 })
134 ).rejects.toThrow(
135 "Unable to retrieve the verification provider with id: unknown"
136 )
137
138 expect(logger.error).toHaveBeenCalledWith(
139 expect.stringContaining("AwilixResolutionError"),
140 expect.any(Error)
141 )
142 })
143 })