1 import { IFileModuleService } from "@medusajs/framework/types"
2 import { Module, Modules } from "@medusajs/framework/utils"
3 import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
4 import { FileModuleService } from "@services"
5 import { resolve } from "path"
6
7 jest.setTimeout(100000)
8
9 const moduleOptions = {
10 providers: [
11 {
12 resolve: resolve(
13 process.cwd() +
14 "/integration-tests/__fixtures__/providers/default-provider"
15 ),
16 id: "default-provider",
17 },
18 ],
19 }
20
21 moduleIntegrationTestRunner<IFileModuleService>({
22 moduleName: Modules.FILE,
23 moduleOptions: moduleOptions,
24 testSuite: ({ service }) => {
25 describe("File Module Service", () => {
26 it(`should export the appropriate linkable configuration`, () => {
27 const linkable = Module(Modules.FILE, {
28 service: FileModuleService,
29 }).linkable
30
31 expect(Object.keys(linkable)).toEqual(["file"])
32
33 Object.keys(linkable).forEach((key) => {
34 delete linkable[key].toJSON
35 })
36
37 expect(linkable).toEqual({
38 file: {
39 id: {
40 entity: "File",
41 field: "file",
42 linkable: "file_id",
43 primaryKey: "id",
44 serviceName: "file",
45 },
46 },
47 })
48 })
49
50 it("creates and gets a file", async () => {
51 const res = await service.createFiles({
52 filename: "test.jpg",
53 mimeType: "image/jpeg",
54 content: Buffer.from("test"),
55 })
56
57 expect(res).toEqual({
58 id: "test.jpg",
59 url: "test.jpg",
60 })
61
62 // The fake provider returns the file content as the url
63 const downloadUrl = await service.retrieveFile("test.jpg")
64 expect(await new Response(downloadUrl.url).text()).toEqual("test")
65 })
66
67 it("generates a presigned upload URL", async () => {
68 const res = await service.getUploadFileUrls({
69 filename: "test.jpg",
70 mimeType: "image/jpeg",
71 })
72
73 expect(res).toEqual({
74 url: "presigned-url/test.jpg",
75 key: "test.jpg",
76 })
77 })
78
79 it("fails to get a presigned upload URL if a filename isn't provided", async () => {
80 const err = await service
81 .getUploadFileUrls({
82 filename: "",
83 mimeType: "image/jpeg",
84 })
85 .catch((err) => err)
86
87 expect(err.message).toEqual(
88 "File name is required to get a presigned upload URL"
89 )
90 })
91 })
92 },
93 })