1 import { IUserModuleService } from "@medusajs/framework/types"
2 import { Modules, UserEvents } from "@medusajs/framework/utils"
3 import {
4 MockEventBusService,
5 moduleIntegrationTestRunner,
6 } from "@medusajs/test-utils"
7 import jwt, { JwtPayload } from "jsonwebtoken"
8
9 jest.setTimeout(30000)
10
11 const defaultInviteData = [
12 {
13 id: "1",
14 email: "user_1@test.com",
15 token: "test",
16 },
17 {
18 id: "2",
19 email: "user_2@test.com",
20 token: "test",
21 },
22 ]
23
24 moduleIntegrationTestRunner<IUserModuleService>({
25 moduleName: Modules.USER,
26 moduleOptions: {
27 jwt_secret: "test",
28 },
29 injectedDependencies: {
30 [Modules.EVENT_BUS]: new MockEventBusService(),
31 },
32 testSuite: ({ service }) => {
33 describe("UserModuleService - Invite", () => {
34 beforeEach(async () => {
35 jest.clearAllMocks()
36 })
37
38 describe("listInvites", () => {
39 it("should list invites", async () => {
40 await service.createInvites(defaultInviteData)
41 const invites = await service.listInvites()
42
43 expect(invites).toEqual([
44 expect.objectContaining({
45 id: "1",
46 }),
47 expect.objectContaining({
48 id: "2",
49 }),
50 ])
51 })
52
53 it("should list invites by id", async () => {
54 await service.createInvites(defaultInviteData)
55 const invites = await service.listInvites({
56 id: ["1"],
57 })
58
59 expect(invites).toEqual([
60 expect.objectContaining({
61 id: "1",
62 }),
63 ])
64 })
65 })
66
67 describe("listAndCountInvites", () => {
68 it("should list and count invites", async () => {
69 await service.createInvites(defaultInviteData)
70 const [invites, count] = await service.listAndCountInvites()
71
72 expect(count).toEqual(2)
73 expect(invites).toEqual([
74 expect.objectContaining({
75 id: "1",
76 }),
77 expect.objectContaining({
78 id: "2",
79 }),
80 ])
81 })
82
83 it("should listAndCount invites by id", async () => {
84 await service.createInvites(defaultInviteData)
85 const [invites, count] = await service.listAndCountInvites({
86 id: "1",
87 })
88
89 expect(count).toEqual(1)
90 expect(invites).toEqual([
91 expect.objectContaining({
92 id: "1",
93 }),
94 ])
95 })
96 })
97
98 describe("retrieveInvite", () => {
99 const id = "1"
100
101 it("should return an invite for the given id", async () => {
102 await service.createInvites(defaultInviteData)
103 const invite = await service.retrieveInvite(id)
104
105 expect(invite).toEqual(
106 expect.objectContaining({
107 id,
108 })
109 )
110
111 const tokenContent = jwt.decode(invite.token) as JwtPayload
112 expect(tokenContent.exp).toBeLessThanOrEqual(
113 Date.now() / 1000 + 60 * 60 * 24
114 )
115 })
116
117 it("should throw an error when an invite with the given id does not exist", async () => {
118 const error = await service
119 .retrieveInvite("does-not-exist")
120 .catch((e) => e)
121
122 expect(error.message).toEqual(
123 "Invite with id: does-not-exist was not found"
124 )
125 })
126
127 it("should throw an error when inviteId is not provided", async () => {
128 const error = await service
129 .retrieveInvite(undefined as unknown as string)
130 .catch((e) => e)
131
132 expect(error.message).toEqual("invite - id must be defined")
133 })
134
135 it("should return invite based on config select param", async () => {
136 await service.createInvites(defaultInviteData)
137 const invite = await service.retrieveInvite(id, {
138 select: ["id"],
139 })
140
141 expect(invite).toEqual({
142 id,
143 })
144 })
145 })
146
147 describe("updateInvite", () => {
148 it("should throw an error when an id does not exist", async () => {
149 const error = await service
150 .updateInvites([
151 {
152 id: "does-not-exist",
153 },
154 ])
155 .catch((e) => e)
156
157 expect(error.message).toEqual(
158 'Invite with id "does-not-exist" not found'
159 )
160 })
161
162 it("should emit invite updated events", async () => {
163 await service.createInvites(defaultInviteData)
164
165 jest.clearAllMocks()
166
167 const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
168 await service.updateInvites([
169 {
170 id: "1",
171 accepted: true,
172 },
173 ])
174
175 // 2 events: 1 invite updated, 1 invite token generated
176 const events = eventBusSpy.mock.calls[0][0]
177 expect(events).toHaveLength(2)
178 expect(events).toEqual(
179 expect.arrayContaining([
180 expect.objectContaining({
181 data: { id: "1" },
182 name: UserEvents.INVITE_UPDATED,
183 }),
184 expect.objectContaining({
185 data: { id: "1" },
186 name: UserEvents.INVITE_TOKEN_GENERATED,
187 }),
188 ])
189 )
190 })
191 })
192
193 describe("resendInvite", () => {
194 it("should emit token generated event for invites", async () => {
195 await service.createInvites(defaultInviteData)
196
197 const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
198 eventBusSpy.mockClear()
199
200 await service.refreshInviteTokens(["1"])
201
202 const events = eventBusSpy.mock.calls[0][0]
203 expect(events).toHaveLength(2)
204 expect(events).toEqual(
205 expect.arrayContaining([
206 expect.objectContaining({
207 name: UserEvents.INVITE_UPDATED,
208 data: { id: "1" },
209 }),
210 expect.objectContaining({
211 name: UserEvents.INVITE_TOKEN_GENERATED,
212 data: { id: "1" },
213 }),
214 ])
215 )
216 })
217 })
218 describe("createInvitie", () => {
219 it("should create an invite successfully", async () => {
220 await service.createInvites(defaultInviteData)
221
222 const [invites, count] = await service.listAndCountInvites({
223 id: ["1"],
224 })
225
226 expect(count).toEqual(1)
227 expect(invites[0]).toEqual(
228 expect.objectContaining({
229 id: "1",
230 })
231 )
232 })
233
234 it("should throw if there is an existing user with the invite email", async () => {
235 let error
236 await service.createUsers([
237 {
238 email: "existing@email.com",
239 },
240 ])
241
242 try {
243 await service.createInvites([
244 {
245 email: "existing@email.com",
246 },
247 ])
248 } catch (e) {
249 error = e
250 }
251
252 expect(error.message).toBe(
253 `User account for following email(s) already exist: existing@email.com`
254 )
255 })
256
257 it("should emit invite created events", async () => {
258 const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
259 await service.createInvites(defaultInviteData)
260
261 // 4 events: 2 invites created, 2 invite token generated
262 const events = eventBusSpy.mock.calls[0][0]
263 expect(events).toHaveLength(3)
264
265 expect(events).toEqual(
266 expect.arrayContaining([
267 expect.objectContaining({
268 data: { id: "1" },
269 name: UserEvents.INVITE_CREATED,
270 }),
271 expect.objectContaining({
272 data: { id: "2" },
273 name: UserEvents.INVITE_CREATED,
274 }),
275 expect.objectContaining({
276 data: { id: ["1", "2"] },
277 name: UserEvents.INVITE_TOKEN_GENERATED,
278 }),
279 ])
280 )
281 })
282 })
283 })
284 },
285 })