Skip to content

Commit 8613b59

Browse files
committed
feat(e2e): add data seeding for giftcards and discounts, update user seeding
1 parent 2470f9c commit 8613b59

File tree

2 files changed

+78
-12
lines changed

2 files changed

+78
-12
lines changed

e2e/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ PRODUCTION_POSTGRES_DATABASE=medusa_db
1515

1616
# Backend server API
1717
CLIENT_SERVER=http://localhost:9000
18+
MEDUSA_ADMIN_EMAIL=[email protected]
19+
MEDUSA_ADMIN_PASSWORD=supersecret

e2e/data/seed.ts

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
import axios, { AxiosError } from "axios"
1+
import axios, { AxiosError, AxiosInstance } from "axios"
2+
3+
axios.defaults.baseURL = process.env.CLIENT_SERVER || "http://localhost:9000"
4+
let region = undefined as any
25

36
export async function seedData() {
7+
const axios = getOrInitAxios()
48
return {
59
user: await seedUser(),
610
}
711
}
812

9-
function getUrl(path: string) {
10-
const baseUrl = process.env.CLIENT_SERVER || "http://localhost:9000"
11-
const url = new URL(path, baseUrl)
12-
return url.toString()
13-
}
14-
15-
async function seedUser() {
13+
export async function seedUser(email?: string, password?: string) {
1614
const user = {
1715
first_name: "Test",
1816
last_name: "User",
19-
20-
password: "password",
17+
email: email || "[email protected]",
18+
password: password || "password",
2119
}
2220
try {
23-
await axios.post(getUrl("/store/customers"), user)
21+
await axios.post("/store/customers", user)
2422
return user
2523
} catch (e: unknown) {
26-
e = e as AxiosError
2724
if (e instanceof AxiosError) {
2825
if (e.response && e.response.status) {
2926
const status = e.response.status
@@ -36,3 +33,70 @@ async function seedUser() {
3633
}
3734
}
3835
}
36+
37+
async function loadRegion(axios: AxiosInstance) {
38+
const resp = await axios.get("/admin/regions")
39+
region = resp.data.regions.filter((r: any) => r.currency_code === "usd")[0]
40+
}
41+
42+
async function getOrInitAxios(axios?: AxiosInstance) {
43+
if (!axios) {
44+
axios = await loginAdmin()
45+
}
46+
if (!region) {
47+
await loadRegion(axios)
48+
}
49+
return axios
50+
}
51+
52+
export async function seedGiftcard(axios?: AxiosInstance) {
53+
axios = await getOrInitAxios(axios)
54+
const resp = await axios.post("/admin/gift-cards", {
55+
region_id: region.id,
56+
value: 10000,
57+
})
58+
resp.data.gift_card.amount = resp.data.gift_card.value.toString()
59+
return resp.data.gift_card as {
60+
id: string
61+
code: string
62+
value: number
63+
amount: string
64+
balance: string
65+
}
66+
}
67+
68+
export async function seedDiscount(axios?: AxiosInstance) {
69+
axios = await getOrInitAxios(axios)
70+
const amount = 2000
71+
const resp = await axios.post("/admin/discounts", {
72+
code: "TEST_DISCOUNT_FIXED",
73+
regions: [region.id],
74+
rule: {
75+
type: "fixed",
76+
value: amount,
77+
allocation: "total",
78+
},
79+
})
80+
const discount = resp.data.discount
81+
return {
82+
id: discount.id,
83+
code: discount.code,
84+
rule_id: discount.rule_id,
85+
amount,
86+
}
87+
}
88+
89+
async function loginAdmin() {
90+
const resp = await axios.post("/admin/auth/token", {
91+
email: process.env.MEDUSA_ADMIN_EMAIL || "[email protected]",
92+
password: process.env.MEDUSA_ADMIN_PASSWORD || "supersecret",
93+
})
94+
if (resp.status !== 200) {
95+
throw { error: "must be able to log in user" }
96+
}
97+
return axios.create({
98+
headers: {
99+
Authorization: `Bearer ${resp.data.access_token}`,
100+
},
101+
})
102+
}

0 commit comments

Comments
 (0)