-
-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from QAComet/qacomet/discount-tests
Test suite around discount functionality
- Loading branch information
Showing
10 changed files
with
1,465 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,5 @@ PRODUCTION_POSTGRES_DATABASE=medusa_db | |
|
||
# Backend server API | ||
CLIENT_SERVER=http://localhost:9000 | ||
MEDUSA_ADMIN_EMAIL=[email protected] | ||
MEDUSA_ADMIN_PASSWORD=supersecret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,26 @@ | ||
import axios, { AxiosError } from "axios" | ||
import axios, { AxiosError, AxiosInstance } from "axios" | ||
|
||
axios.defaults.baseURL = process.env.CLIENT_SERVER || "http://localhost:9000" | ||
let region = undefined as any | ||
|
||
export async function seedData() { | ||
const axios = getOrInitAxios() | ||
return { | ||
user: await seedUser(), | ||
} | ||
} | ||
|
||
function getUrl(path: string) { | ||
const baseUrl = process.env.CLIENT_SERVER || "http://localhost:9000" | ||
const url = new URL(path, baseUrl) | ||
return url.toString() | ||
} | ||
|
||
async function seedUser() { | ||
export async function seedUser(email?: string, password?: string) { | ||
const user = { | ||
first_name: "Test", | ||
last_name: "User", | ||
email: "[email protected]", | ||
password: "password", | ||
email: email || "[email protected]", | ||
password: password || "password", | ||
} | ||
try { | ||
await axios.post(getUrl("/store/customers"), user) | ||
await axios.post("/store/customers", user) | ||
return user | ||
} catch (e: unknown) { | ||
e = e as AxiosError | ||
if (e instanceof AxiosError) { | ||
if (e.response && e.response.status) { | ||
const status = e.response.status | ||
|
@@ -36,3 +33,70 @@ async function seedUser() { | |
} | ||
} | ||
} | ||
|
||
async function loadRegion(axios: AxiosInstance) { | ||
const resp = await axios.get("/admin/regions") | ||
region = resp.data.regions.filter((r: any) => r.currency_code === "usd")[0] | ||
} | ||
|
||
async function getOrInitAxios(axios?: AxiosInstance) { | ||
if (!axios) { | ||
axios = await loginAdmin() | ||
} | ||
if (!region) { | ||
await loadRegion(axios) | ||
} | ||
return axios | ||
} | ||
|
||
export async function seedGiftcard(axios?: AxiosInstance) { | ||
axios = await getOrInitAxios(axios) | ||
const resp = await axios.post("/admin/gift-cards", { | ||
region_id: region.id, | ||
value: 10000, | ||
}) | ||
resp.data.gift_card.amount = resp.data.gift_card.value.toString() | ||
return resp.data.gift_card as { | ||
id: string | ||
code: string | ||
value: number | ||
amount: string | ||
balance: string | ||
} | ||
} | ||
|
||
export async function seedDiscount(axios?: AxiosInstance) { | ||
axios = await getOrInitAxios(axios) | ||
const amount = 2000 | ||
const resp = await axios.post("/admin/discounts", { | ||
code: "TEST_DISCOUNT_FIXED", | ||
regions: [region.id], | ||
rule: { | ||
type: "fixed", | ||
value: amount, | ||
allocation: "total", | ||
}, | ||
}) | ||
const discount = resp.data.discount | ||
return { | ||
id: discount.id, | ||
code: discount.code, | ||
rule_id: discount.rule_id, | ||
amount, | ||
} | ||
} | ||
|
||
async function loginAdmin() { | ||
const resp = await axios.post("/admin/auth/token", { | ||
email: process.env.MEDUSA_ADMIN_EMAIL || "[email protected]", | ||
password: process.env.MEDUSA_ADMIN_PASSWORD || "supersecret", | ||
}) | ||
if (resp.status !== 200) { | ||
throw { error: "must be able to log in user" } | ||
} | ||
return axios.create({ | ||
headers: { | ||
Authorization: `Bearer ${resp.data.access_token}`, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.