|
1
|
import { INotificationModuleService } from "@medusajs/framework/types" |
|
2
|
import { Modules, NotificationStatus } from "@medusajs/framework/utils" |
|
3
|
import { moduleIntegrationTestRunner } from "@medusajs/test-utils" |
|
4
|
import { resolve } from "path" |
|
5
|
|
|
6
|
jest.setTimeout(30000) |
|
7
|
|
|
8
|
const successMedusaCloudEmailResponse = { |
|
9
|
ok: true, |
|
10
|
status: 200, |
|
11
|
statusText: "OK", |
|
12
|
json: () => Promise.resolve({ id: "external_id_1" }), |
|
13
|
} |
|
14
|
|
|
15
|
const testNotification = { |
|
16
|
to: "customer@test.com", |
|
17
|
from: "sender@verified.com", |
|
18
|
template: "some-template", |
|
19
|
channel: "email", |
|
20
|
data: { |
|
21
|
link: "https://test.com", |
|
22
|
}, |
|
23
|
provider_data: { |
|
24
|
cc: "cc@test.com", |
|
25
|
}, |
|
26
|
} |
|
27
|
|
|
28
|
moduleIntegrationTestRunner<INotificationModuleService>({ |
|
29
|
moduleName: Modules.NOTIFICATION, |
|
30
|
moduleOptions: { |
|
31
|
cloud: { |
|
32
|
api_key: "test-api-key", |
|
33
|
endpoint: "https://medusacloud.com/emails", |
|
34
|
environment_handle: "test-environment", |
|
35
|
}, |
|
36
|
}, |
|
37
|
testSuite: ({ service }) => |
|
38
|
describe("Medusa Cloud Email provider", () => { |
|
39
|
let fetchMock: jest.SpyInstance |
|
40
|
|
|
41
|
beforeEach(() => { |
|
42
|
fetchMock = jest |
|
43
|
.spyOn(globalThis, "fetch") |
|
44
|
.mockImplementation( |
|
45
|
async () => successMedusaCloudEmailResponse as any |
|
46
|
) |
|
47
|
}) |
|
48
|
|
|
49
|
afterEach(() => { |
|
50
|
fetchMock.mockClear() |
|
51
|
}) |
|
52
|
|
|
53
|
it("should send email notification to Medusa Cloud", async () => { |
|
54
|
const result = await service.createNotifications(testNotification) |
|
55
|
expect(result).toEqual( |
|
56
|
expect.objectContaining({ |
|
57
|
to: "customer@test.com", |
|
58
|
from: "sender@verified.com", |
|
59
|
template: "some-template", |
|
60
|
channel: "email", |
|
61
|
data: { |
|
62
|
link: "https://test.com", |
|
63
|
}, |
|
64
|
provider_data: { |
|
65
|
cc: "cc@test.com", |
|
66
|
}, |
|
67
|
provider_id: "cloud", |
|
68
|
external_id: "external_id_1", |
|
69
|
status: NotificationStatus.SUCCESS, |
|
70
|
}) |
|
71
|
) |
|
72
|
|
|
73
|
const [url, request] = fetchMock.mock.calls[0] |
|
74
|
expect(url).toBe("https://medusacloud.com/emails/send") |
|
75
|
expect(request.method).toBe("POST") |
|
76
|
expect(request.headers["Content-Type"]).toBe("application/json") |
|
77
|
expect(request.headers["Authorization"]).toBe("Basic test-api-key") |
|
78
|
expect(request.headers["x-medusa-environment-handle"]).toBe( |
|
79
|
"test-environment" |
|
80
|
) |
|
81
|
expect(JSON.parse(request.body)).toEqual({ |
|
82
|
to: "customer@test.com", |
|
83
|
from: "sender@verified.com", |
|
84
|
template: "some-template", |
|
85
|
data: { |
|
86
|
link: "https://test.com", |
|
87
|
}, |
|
88
|
provider_data: { |
|
89
|
cc: "cc@test.com", |
|
90
|
}, |
|
91
|
}) |
|
92
|
}) |
|
93
|
|
|
94
|
it("should return an error if the Medusa Cloud Email provider fails", async () => { |
|
95
|
fetchMock.mockImplementation( |
|
96
|
async () => |
|
97
|
({ |
|
98
|
ok: false, |
|
99
|
status: 500, |
|
100
|
statusText: "Internal Server Error", |
|
101
|
json: () => Promise.resolve({ message: "Internal Server Error" }), |
|
102
|
} as any) |
|
103
|
) |
|
104
|
|
|
105
|
await expect( |
|
106
|
service.createNotifications(testNotification) |
|
107
|
).rejects.toThrow() |
|
108
|
}) |
|
109
|
}), |
|
110
|
}) |
|
111
|
|
|
112
|
moduleIntegrationTestRunner<INotificationModuleService>({ |
|
113
|
moduleName: Modules.NOTIFICATION, |
|
114
|
moduleOptions: { |
|
115
|
cloud: { |
|
116
|
api_key: "test-api-key", |
|
117
|
endpoint: "https://medusacloud.com/emails", |
|
118
|
environment_handle: "test-environment", |
|
119
|
}, |
|
120
|
providers: [ |
|
121
|
{ |
|
122
|
resolve: resolve( |
|
123
|
process.cwd() + |
|
124
|
"/integration-tests/__fixtures__/providers/default-provider" |
|
125
|
), |
|
126
|
id: "test-provider", |
|
127
|
options: { |
|
128
|
name: "Test provider", |
|
129
|
channels: ["email"], |
|
130
|
}, |
|
131
|
}, |
|
132
|
], |
|
133
|
}, |
|
134
|
testSuite: ({ service }) => |
|
135
|
describe("Medusa Cloud Email provider - when another email provider is configured", () => { |
|
136
|
let fetchMock: jest.SpyInstance |
|
137
|
|
|
138
|
beforeEach(() => { |
|
139
|
fetchMock = jest |
|
140
|
.spyOn(globalThis, "fetch") |
|
141
|
.mockImplementation( |
|
142
|
async () => successMedusaCloudEmailResponse as any |
|
143
|
) |
|
144
|
}) |
|
145
|
|
|
146
|
afterEach(() => { |
|
147
|
fetchMock.mockClear() |
|
148
|
}) |
|
149
|
|
|
150
|
it("should not enable Medusa Cloud Email provider", async () => { |
|
151
|
const result = await service.createNotifications(testNotification) |
|
152
|
expect(result).toMatchObject({ status: NotificationStatus.SUCCESS }) |
|
153
|
|
|
154
|
expect(fetchMock).not.toHaveBeenCalled() |
|
155
|
}) |
|
156
|
}), |
|
157
|
}) |
|
158
|
|
|
159
|
moduleIntegrationTestRunner<INotificationModuleService>({ |
|
160
|
moduleName: Modules.NOTIFICATION, |
|
161
|
moduleOptions: {}, |
|
162
|
testSuite: ({ service }) => |
|
163
|
describe("Medusa Cloud Email provider - when cloud options are not provided", () => { |
|
164
|
let fetchMock: jest.SpyInstance |
|
165
|
|
|
166
|
beforeEach(() => { |
|
167
|
fetchMock = jest |
|
168
|
.spyOn(globalThis, "fetch") |
|
169
|
.mockImplementation( |
|
170
|
async () => successMedusaCloudEmailResponse as any |
|
171
|
) |
|
172
|
}) |
|
173
|
|
|
174
|
afterEach(() => { |
|
175
|
fetchMock.mockClear() |
|
176
|
}) |
|
177
|
|
|
178
|
it("should not enable Medusa Cloud Email provider", async () => { |
|
179
|
await expect( |
|
180
|
service.createNotifications(testNotification) |
|
181
|
).rejects.toThrow() |
|
182
|
|
|
183
|
expect(fetchMock).not.toHaveBeenCalled() |
|
184
|
}) |
|
185
|
}), |
|
186
|
}) |