|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import FormData from "form-data" |
|
3
|
import fs from "fs/promises" |
|
4
|
import os from "os" |
|
5
|
import path from "path" |
|
6
|
import { |
|
7
|
adminHeaders, |
|
8
|
createAdminUser, |
|
9
|
} from "../../../../helpers/create-admin-user" |
|
10
|
|
|
11
|
jest.setTimeout(30000) |
|
12
|
|
|
13
|
const getUploadReq = (files: { name: string; content: string }[]) => { |
|
14
|
const form = new FormData() |
|
15
|
files.forEach((file) => { |
|
16
|
form.append("files", Buffer.from(file.content), file.name) |
|
17
|
}) |
|
18
|
|
|
19
|
return { |
|
20
|
form, |
|
21
|
meta: { |
|
22
|
headers: { |
|
23
|
...adminHeaders.headers, |
|
24
|
...form.getHeaders(), |
|
25
|
}, |
|
26
|
}, |
|
27
|
} |
|
28
|
} |
|
29
|
|
|
30
|
medusaIntegrationTestRunner({ |
|
31
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
32
|
afterAll(async () => { |
|
33
|
await fs.rm(path.join(os.tmpdir(), "uploads"), { recursive: true }) |
|
34
|
}) |
|
35
|
|
|
36
|
beforeAll(async () => { |
|
37
|
await createAdminUser(dbConnection, adminHeaders, getContainer()) |
|
38
|
|
|
39
|
await dbUtils.snapshot() |
|
40
|
}) |
|
41
|
|
|
42
|
describe("POST /admin/uploads", () => { |
|
43
|
it("uploads a single file successfully", async () => { |
|
44
|
const { form, meta } = getUploadReq([ |
|
45
|
{ name: "first.jpeg", content: "first content" }, |
|
46
|
{ name: "second.jpeg", content: "second content" }, |
|
47
|
]) |
|
48
|
const response = await api.post("/admin/uploads", form, meta) |
|
49
|
|
|
50
|
expect(response.status).toEqual(200) |
|
51
|
expect(response.data.files).toHaveLength(2) |
|
52
|
expect(response.data.files).toEqual( |
|
53
|
expect.arrayContaining([ |
|
54
|
expect.objectContaining({ |
|
55
|
id: expect.any(String), |
|
56
|
url: expect.any(String), |
|
57
|
}), |
|
58
|
]) |
|
59
|
) |
|
60
|
}) |
|
61
|
}) |
|
62
|
|
|
63
|
describe("GET /admin/uploads/:id", () => { |
|
64
|
let fileKey = "" |
|
65
|
beforeEach(async () => { |
|
66
|
const { form, meta } = getUploadReq([ |
|
67
|
{ name: "test.jpeg", content: "test content" }, |
|
68
|
]) |
|
69
|
|
|
70
|
fileKey = (await api.post("/admin/uploads", form, meta)).data.files[0] |
|
71
|
.id |
|
72
|
}) |
|
73
|
|
|
74
|
it("gets a URL to the requested file successfully", async () => { |
|
75
|
const response = await api.get( |
|
76
|
`/admin/uploads/${fileKey}`, |
|
77
|
adminHeaders |
|
78
|
) |
|
79
|
|
|
80
|
expect(response.status).toEqual(200) |
|
81
|
expect(response.data.file.url).toEqual( |
|
82
|
expect.stringContaining(`/static/${fileKey}`) |
|
83
|
) |
|
84
|
}) |
|
85
|
}) |
|
86
|
|
|
87
|
describe("DELETE /admin/uploads/:id", () => { |
|
88
|
let fileKey = "" |
|
89
|
beforeEach(async () => { |
|
90
|
const { form, meta } = getUploadReq([ |
|
91
|
{ name: "test.jpeg", content: "test content" }, |
|
92
|
]) |
|
93
|
|
|
94
|
fileKey = (await api.post("/admin/uploads", form, meta)).data.files[0] |
|
95
|
.id |
|
96
|
}) |
|
97
|
|
|
98
|
it("deletes the specified file successfully", async () => { |
|
99
|
const response = await api.delete( |
|
100
|
`/admin/uploads/${fileKey}`, |
|
101
|
adminHeaders |
|
102
|
) |
|
103
|
|
|
104
|
expect(response.status).toEqual(200) |
|
105
|
expect(response.data).toEqual({ |
|
106
|
id: fileKey, |
|
107
|
object: "file", |
|
108
|
deleted: true, |
|
109
|
}) |
|
110
|
|
|
111
|
const { response: err } = await api |
|
112
|
.get(`/admin/uploads/${fileKey}`, adminHeaders) |
|
113
|
.catch((e) => e) |
|
114
|
|
|
115
|
expect(err.status).toEqual(404) |
|
116
|
expect(err.data.message).toEqual(`File with key ${fileKey} not found`) |
|
117
|
}) |
|
118
|
}) |
|
119
|
}, |
|
120
|
}) |