1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import {
3 IApiKeyModuleService,
4 ISalesChannelModuleService,
5 } from "@medusajs/types"
6 import { Modules, remoteQueryObjectFromString } from "@medusajs/utils"
7
8 jest.setTimeout(50000)
9
10 const env = {}
11
12 medusaIntegrationTestRunner({
13 env,
14 testSuite: ({ dbConnection, getContainer, api }) => {
15 describe("Publishable keys and sales channel link", () => {
16 let appContainer
17 let apiKeyModule: IApiKeyModuleService
18 let scModuleService: ISalesChannelModuleService
19 let remoteQuery
20 let remoteLink
21
22 beforeAll(async () => {
23 appContainer = getContainer()
24 apiKeyModule = appContainer.resolve(Modules.API_KEY)
25 scModuleService = appContainer.resolve(Modules.SALES_CHANNEL)
26 remoteQuery = appContainer.resolve("remoteQuery")
27 remoteLink = appContainer.resolve("remoteLink")
28 })
29
30 it("should query api key and sales channels link with remote query", async () => {
31 const salesChannel = await scModuleService.createSalesChannels({
32 name: "Webshop",
33 })
34
35 const apiKeys = await apiKeyModule.createApiKeys([
36 {
37 title: "Api key",
38 type: "publishable",
39 created_by: "test",
40 },
41 {
42 title: "Api key 2",
43 type: "publishable",
44 created_by: "test",
45 },
46 ])
47
48 await remoteLink.create([
49 {
50 [Modules.API_KEY]: {
51 publishable_key_id: apiKeys[0].id,
52 },
53 [Modules.SALES_CHANNEL]: {
54 sales_channel_id: salesChannel.id,
55 },
56 },
57 {
58 [Modules.API_KEY]: {
59 publishable_key_id: apiKeys[1].id,
60 },
61 [Modules.SALES_CHANNEL]: {
62 sales_channel_id: salesChannel.id,
63 },
64 },
65 ])
66
67 const queryObject = remoteQueryObjectFromString({
68 entryPoint: "api_key",
69 variables: {
70 filters: { token: apiKeys[0].token },
71 },
72 fields: ["id", "sales_channels.id"],
73 })
74 const keyLinks = await remoteQuery(queryObject)
75
76 expect(keyLinks).toHaveLength(1)
77 expect(keyLinks).toEqual(
78 expect.arrayContaining([
79 expect.objectContaining({
80 id: apiKeys[0].id,
81 sales_channels: expect.arrayContaining([
82 expect.objectContaining({ id: salesChannel.id }),
83 ]),
84 }),
85 ])
86 )
87 })
88 })
89 },
90 })