|
1
|
import { IAuthModuleService } from "@medusajs/framework/types" |
|
2
|
import { Module, Modules } from "@medusajs/framework/utils" |
|
3
|
import { AuthModuleService } from "@services" |
|
4
|
import { moduleIntegrationTestRunner, SuiteOptions } from "@medusajs/test-utils" |
|
5
|
import { resolve } from "path" |
|
6
|
|
|
7
|
let moduleOptions = { |
|
8
|
providers: [ |
|
9
|
{ |
|
10
|
resolve: resolve( |
|
11
|
process.cwd() + |
|
12
|
"/integration-tests/__fixtures__/providers/default-provider" |
|
13
|
), |
|
14
|
id: "plaintextpass", |
|
15
|
}, |
|
16
|
], |
|
17
|
} |
|
18
|
|
|
19
|
jest.setTimeout(30000) |
|
20
|
|
|
21
|
moduleIntegrationTestRunner({ |
|
22
|
moduleName: Modules.AUTH, |
|
23
|
moduleOptions, |
|
24
|
testSuite: ({ service }: SuiteOptions<IAuthModuleService>) => |
|
25
|
describe("Auth Module Service", () => { |
|
26
|
beforeEach(async () => { |
|
27
|
await service.createAuthIdentities({ |
|
28
|
provider_identities: [ |
|
29
|
{ |
|
30
|
entity_id: "test@admin.com", |
|
31
|
provider: "plaintextpass", |
|
32
|
provider_metadata: { |
|
33
|
password: "plaintext", |
|
34
|
}, |
|
35
|
}, |
|
36
|
], |
|
37
|
}) |
|
38
|
}) |
|
39
|
|
|
40
|
it(`should export the appropriate linkable configuration`, () => { |
|
41
|
const linkable = Module(Modules.AUTH, { |
|
42
|
service: AuthModuleService, |
|
43
|
}).linkable |
|
44
|
|
|
45
|
expect(Object.keys(linkable)).toEqual([ |
|
46
|
"authIdentity", |
|
47
|
"providerIdentity", |
|
48
|
]) |
|
49
|
|
|
50
|
linkable.authIdentity.toJSON = undefined |
|
51
|
|
|
52
|
expect(linkable.authIdentity).toEqual({ |
|
53
|
id: { |
|
54
|
linkable: "auth_identity_id", |
|
55
|
entity: "AuthIdentity", |
|
56
|
primaryKey: "id", |
|
57
|
serviceName: "auth", |
|
58
|
field: "authIdentity", |
|
59
|
}, |
|
60
|
}) |
|
61
|
|
|
62
|
linkable.providerIdentity.toJSON = undefined |
|
63
|
|
|
64
|
expect(linkable.providerIdentity).toEqual({ |
|
65
|
id: { |
|
66
|
linkable: "provider_identity_id", |
|
67
|
entity: "ProviderIdentity", |
|
68
|
primaryKey: "id", |
|
69
|
serviceName: "auth", |
|
70
|
field: "providerIdentity", |
|
71
|
}, |
|
72
|
}) |
|
73
|
}) |
|
74
|
|
|
75
|
it("it fails if the provider does not exist", async () => { |
|
76
|
const err = await service |
|
77
|
.authenticate("facebook", { |
|
78
|
body: { |
|
79
|
email: "test@admin.com", |
|
80
|
password: "password", |
|
81
|
}, |
|
82
|
}) |
|
83
|
.catch((e) => e) |
|
84
|
|
|
85
|
expect(err).toEqual({ |
|
86
|
success: false, |
|
87
|
error: ` |
|
88
|
Unable to retrieve the auth provider with id: facebook |
|
89
|
Please make sure that the provider is registered in the container and it is configured correctly in your project configuration file.`, |
|
90
|
}) |
|
91
|
}) |
|
92
|
|
|
93
|
it("successfully calls the provider for authentication if correct password", async () => { |
|
94
|
const result = await service.authenticate("plaintextpass", { |
|
95
|
body: { |
|
96
|
email: "test@admin.com", |
|
97
|
password: "plaintext", |
|
98
|
}, |
|
99
|
}) |
|
100
|
|
|
101
|
expect(result).toEqual( |
|
102
|
expect.objectContaining({ |
|
103
|
success: true, |
|
104
|
authIdentity: expect.objectContaining({ |
|
105
|
id: expect.any(String), |
|
106
|
provider_identities: [ |
|
107
|
expect.objectContaining({ entity_id: "test@admin.com" }), |
|
108
|
], |
|
109
|
}), |
|
110
|
}) |
|
111
|
) |
|
112
|
}) |
|
113
|
|
|
114
|
it("should fail if the password is incorrect", async () => { |
|
115
|
const result = await service |
|
116
|
.authenticate("plaintextpass", { |
|
117
|
body: { |
|
118
|
email: "test@admin.com", |
|
119
|
password: "incorrect", |
|
120
|
}, |
|
121
|
}) |
|
122
|
.catch((e) => e) |
|
123
|
|
|
124
|
expect(result).toEqual( |
|
125
|
expect.objectContaining({ |
|
126
|
success: false, |
|
127
|
error: "Invalid email or password", |
|
128
|
}) |
|
129
|
) |
|
130
|
}) |
|
131
|
|
|
132
|
it("successfully create a new entity if nonexistent", async () => { |
|
133
|
const result = await service.authenticate("plaintextpass", { |
|
134
|
body: { |
|
135
|
email: "new@admin.com", |
|
136
|
password: "newpass", |
|
137
|
}, |
|
138
|
}) |
|
139
|
|
|
140
|
const dbAuthIdentity = await service.retrieveAuthIdentity( |
|
141
|
result.authIdentity?.id!, |
|
142
|
{ relations: ["provider_identities"] } |
|
143
|
) |
|
144
|
|
|
145
|
expect(dbAuthIdentity).toEqual( |
|
146
|
expect.objectContaining({ |
|
147
|
id: expect.any(String), |
|
148
|
provider_identities: [ |
|
149
|
expect.objectContaining({ entity_id: "new@admin.com" }), |
|
150
|
], |
|
151
|
}) |
|
152
|
) |
|
153
|
}) |
|
154
|
}), |
|
155
|
}) |