1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import { ICustomerModuleService } from "@medusajs/types"
3 import { Modules } from "@medusajs/utils"
4 import {
5 generatePublishableKey,
6 generateStoreHeaders,
7 } from "../../../../helpers/create-admin-user"
8 import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer"
9
10 jest.setTimeout(50000)
11
12 const env = {}
13
14 medusaIntegrationTestRunner({
15 env,
16 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
17 describe("POST /store/customers/:id/addresses/:address_id", () => {
18 let appContainer
19 let customerModuleService: ICustomerModuleService
20 let storeHeaders
21
22 beforeAll(async () => {
23 appContainer = getContainer()
24 customerModuleService = appContainer.resolve(Modules.CUSTOMER)
25 })
26
27 beforeAll(async () => {
28 appContainer = getContainer()
29 const publishableKey = await generatePublishableKey(appContainer)
30 storeHeaders = generateStoreHeaders({ publishableKey })
31
32 await dbUtils.snapshot()
33 })
34
35 it("should update a customer address", async () => {
36 const { customer, jwt } = await createAuthenticatedCustomer(
37 api,
38 storeHeaders
39 )
40
41 const address = await customerModuleService.createCustomerAddresses({
42 customer_id: customer.id,
43 first_name: "John",
44 last_name: "Doe",
45 address_1: "Test street 1",
46 })
47
48 const response = await api.post(
49 `/store/customers/me/addresses/${address.id}`,
50 {
51 first_name: "Jane",
52 },
53 {
54 headers: {
55 authorization: `Bearer ${jwt}`,
56 ...storeHeaders.headers,
57 },
58 }
59 )
60
61 expect(response.status).toEqual(200)
62 expect(response.data.customer.addresses[0]).toEqual(
63 expect.objectContaining({
64 id: address.id,
65 first_name: "Jane",
66 last_name: "Doe",
67 })
68 )
69 })
70
71 it("should fail to update another customer's address", async () => {
72 const { jwt } = await createAuthenticatedCustomer(api, storeHeaders)
73
74 const otherCustomer = await customerModuleService.createCustomers({
75 first_name: "Jane",
76 last_name: "Doe",
77 })
78
79 const address = await customerModuleService.createCustomerAddresses({
80 customer_id: otherCustomer.id,
81 first_name: "John",
82 last_name: "Doe",
83 address_1: "Test street 1",
84 })
85
86 const response = await api
87 .post(
88 `/store/customers/me/addresses/${address.id}`,
89 { first_name: "Jane" },
90 {
91 headers: {
92 authorization: `Bearer ${jwt}`,
93 ...storeHeaders.headers,
94 },
95 }
96 )
97 .catch((e) => e.response)
98
99 expect(response.status).toEqual(404)
100 })
101 })
102 },
103 })