merged tests

This commit is contained in:
counterweight 2025-12-24 23:52:52 +01:00
parent 4be45f8f7c
commit 67ffe6a823
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
7 changed files with 212 additions and 599 deletions

View file

@ -40,46 +40,32 @@ test.describe("Exchange Page - Regular User Access", () => {
await loginUser(page, REGULAR_USER.email, REGULAR_USER.password);
});
test("regular user can access exchange page", async ({ page }) => {
await page.goto("/exchange");
test("regular user can access exchange page and all UI elements are visible", async ({
page,
}) => {
// Test navigation
await page.goto("/trades");
await expect(page.getByRole("link", { name: "Exchange" })).toBeVisible();
// Test page access
await page.goto("/exchange");
await expect(page).toHaveURL("/exchange");
await expect(page.getByRole("heading", { name: "Exchange Bitcoin" })).toBeVisible();
});
test("regular user sees Exchange link in navigation", async ({ page }) => {
await page.goto("/trades");
await expect(page.getByRole("link", { name: "Exchange" })).toBeVisible();
});
test("exchange page shows price information", async ({ page }) => {
await page.goto("/exchange");
// Should show market and our price
// Test price information
await expect(page.getByText("Market:")).toBeVisible();
await expect(page.getByText("Our price:")).toBeVisible();
});
test("exchange page shows buy/sell toggle", async ({ page }) => {
await page.goto("/exchange");
// Test buy/sell toggle
await expect(page.getByRole("button", { name: "Buy BTC" })).toBeVisible();
await expect(page.getByRole("button", { name: "Sell BTC" })).toBeVisible();
});
test("exchange page shows payment method selector", async ({ page }) => {
await page.goto("/exchange");
// Test payment method selector
await expect(page.getByText("Payment Method")).toBeVisible();
await expect(page.getByRole("button", { name: /Onchain/ })).toBeVisible();
await expect(page.getByRole("button", { name: /Lightning/ })).toBeVisible();
});
test("exchange page shows amount slider", async ({ page }) => {
await page.goto("/exchange");
// Should show amount section
// Test amount slider
await expect(page.getByText("Amount")).toBeVisible();
await expect(page.locator('input[type="range"]')).toBeVisible();
});
@ -127,7 +113,7 @@ test.describe("Exchange Page - With Availability", () => {
await loginUser(page, REGULAR_USER.email, REGULAR_USER.password);
});
test("shows available slots when availability is set", async ({ page }) => {
test("booking flow - shows slots, confirmation form, and trade details", async ({ page }) => {
await page.goto("/exchange");
// Step 1: Click "Continue to Booking" to proceed to step 2
@ -141,59 +127,31 @@ test.describe("Exchange Page - With Availability", () => {
// Wait for "Available Slots" section to appear
await expect(page.getByRole("heading", { name: /Available Slots for/ })).toBeVisible();
// Wait for loading to finish
await expect(page.getByText("Loading slots...")).not.toBeVisible({ timeout: 10000 });
// Should see some slot buttons
const slotButtons = page.locator("button").filter({ hasText: /^\d{1,2}:\d{2}/ });
await expect(slotButtons.first()).toBeVisible({ timeout: 10000 });
});
test("clicking slot shows confirmation form", async ({ page }) => {
await page.goto("/exchange");
// Step 1: Click "Continue to Booking" to proceed to step 2
await page.getByRole("button", { name: "Continue to Booking" }).click();
// Step 2: Use data-testid for reliable date selection
const tomorrowStr = getTomorrowDateStr();
const dateButton = page.getByTestId(`date-${tomorrowStr}`);
await expect(dateButton).toBeEnabled({ timeout: 15000 });
await dateButton.click();
// Wait for any slot to appear
await expect(page.getByText("Loading slots...")).not.toBeVisible({ timeout: 10000 });
const slotButtons = page.locator("button").filter({ hasText: /^\d{1,2}:\d{2}/ });
await expect(slotButtons.first()).toBeVisible({ timeout: 10000 });
// Click first slot
// Click first slot - should show confirmation form
await slotButtons.first().click();
// Should show confirmation form
await expect(page.getByText("Confirm Trade")).toBeVisible();
await expect(page.getByRole("button", { name: /Confirm/ })).toBeVisible();
});
test("confirmation shows trade details", async ({ page }) => {
// Navigate back to exchange and test second slot selection
await page.goto("/exchange");
// Step 1: Click "Continue to Booking" to proceed to step 2
await page.getByRole("button", { name: "Continue to Booking" }).click();
// Step 2: Use data-testid for reliable date selection
const tomorrowStr = getTomorrowDateStr();
const dateButton = page.getByTestId(`date-${tomorrowStr}`);
await expect(dateButton).toBeEnabled({ timeout: 15000 });
await dateButton.click();
// Wait for slots to load
await page.getByTestId(`date-${tomorrowStr}`).click();
await expect(page.getByText("Loading slots...")).not.toBeVisible({ timeout: 10000 });
const slotButtons = page.locator("button").filter({ hasText: /^\d{1,2}:\d{2}/ });
await expect(slotButtons.first()).toBeVisible({ timeout: 10000 });
const slotButtons2 = page.locator("button").filter({ hasText: /^\d{1,2}:\d{2}/ });
await expect(slotButtons2.first()).toBeVisible({ timeout: 10000 });
// Click second slot
await slotButtons.nth(1).click();
// Click second slot if available, otherwise first
if ((await slotButtons2.count()) > 1) {
await slotButtons2.nth(1).click();
} else {
await slotButtons2.first().click();
}
// Should show confirmation with trade details
await expect(page.getByText("Confirm Trade")).toBeVisible();
@ -240,31 +198,19 @@ test.describe("Exchange Page - With Availability", () => {
});
test.describe("Exchange Page - Access Control", () => {
test("admin cannot access exchange page", async ({ page }) => {
test("admin and unauthenticated users cannot access exchange page", async ({ page }) => {
// Test unauthenticated access
await clearAuth(page);
await loginUser(page, ADMIN_USER.email, ADMIN_USER.password);
await page.goto("/exchange");
// Should be redirected away
await expect(page).not.toHaveURL("/exchange");
});
test("admin does not see Exchange link", async ({ page }) => {
await clearAuth(page);
await loginUser(page, ADMIN_USER.email, ADMIN_USER.password);
await page.goto("/admin/trades");
await expect(page.getByRole("link", { name: "Exchange" })).not.toBeVisible();
});
test("unauthenticated user redirected to login", async ({ page }) => {
await clearAuth(page);
await page.goto("/exchange");
await expect(page).toHaveURL("/login");
// Test admin access
await loginUser(page, ADMIN_USER.email, ADMIN_USER.password);
await page.goto("/admin/trades");
await expect(page.getByRole("link", { name: "Exchange" })).not.toBeVisible();
await page.goto("/exchange");
await expect(page).not.toHaveURL("/exchange");
});
});
@ -274,25 +220,17 @@ test.describe("Trades Page", () => {
await loginUser(page, REGULAR_USER.email, REGULAR_USER.password);
});
test("regular user can access trades page", async ({ page }) => {
test("regular user can access trades page and see empty state", async ({ page }) => {
await page.goto("/trades");
await expect(page).toHaveURL("/trades");
await expect(page.getByRole("heading", { name: "My Trades" })).toBeVisible();
});
test("trades page shows empty state when no trades", async ({ page }) => {
await page.goto("/trades");
// Either shows empty state message or trades list
const content = page.locator("body");
await expect(content).toBeVisible();
});
test("trades page shows Start trading link when empty", async ({ page }) => {
await page.goto("/trades");
// Wait for loading to finish - either "Loading trades..." disappears or we see content
// Wait for loading to finish
await expect(page.getByText("Loading trades...")).not.toBeVisible({ timeout: 5000 });
// Check if it shows empty state with link, or trades exist
@ -337,83 +275,62 @@ test.describe("Admin Trades Page", () => {
});
test.describe("Exchange API", () => {
test("regular user can get price via API", async ({ page, request }) => {
test("API access control - regular user can access exchange APIs, admin cannot", async ({
page,
request,
}) => {
// Test regular user can get price
await clearAuth(page);
await loginUser(page, REGULAR_USER.email, REGULAR_USER.password);
const cookies = await page.context().cookies();
const authCookie = cookies.find((c) => c.name === "auth_token");
let cookies = await page.context().cookies();
let authCookie = cookies.find((c) => c.name === "auth_token");
if (authCookie) {
const response = await request.get(`${API_URL}/api/exchange/price`, {
const priceResponse = await request.get(`${API_URL}/api/exchange/price`, {
headers: {
Cookie: `auth_token=${authCookie.value}`,
},
});
expect(priceResponse.status()).toBe(200);
const priceData = await priceResponse.json();
expect(priceData.config).toBeDefined();
expect(priceData.config.eur_min).toBeDefined();
expect(priceData.config.eur_max).toBeDefined();
expect(response.status()).toBe(200);
const data = await response.json();
expect(data.config).toBeDefined();
expect(data.config.eur_min).toBeDefined();
expect(data.config.eur_max).toBeDefined();
// Test regular user can get trades
const tradesResponse = await request.get(`${API_URL}/api/trades`, {
headers: {
Cookie: `auth_token=${authCookie.value}`,
},
});
expect(tradesResponse.status()).toBe(200);
const tradesData = await tradesResponse.json();
expect(Array.isArray(tradesData)).toBe(true);
}
});
test("admin cannot get price via API", async ({ page, request }) => {
// Test admin cannot get price
await clearAuth(page);
await loginUser(page, ADMIN_USER.email, ADMIN_USER.password);
const cookies = await page.context().cookies();
const authCookie = cookies.find((c) => c.name === "auth_token");
cookies = await page.context().cookies();
authCookie = cookies.find((c) => c.name === "auth_token");
if (authCookie) {
const response = await request.get(`${API_URL}/api/exchange/price`, {
const adminPriceResponse = await request.get(`${API_URL}/api/exchange/price`, {
headers: {
Cookie: `auth_token=${authCookie.value}`,
},
});
expect(adminPriceResponse.status()).toBe(403);
expect(response.status()).toBe(403);
}
});
test("regular user can get trades via API", async ({ page, request }) => {
await clearAuth(page);
await loginUser(page, REGULAR_USER.email, REGULAR_USER.password);
const cookies = await page.context().cookies();
const authCookie = cookies.find((c) => c.name === "auth_token");
if (authCookie) {
const response = await request.get(`${API_URL}/api/trades`, {
// Test admin can get upcoming trades
const adminTradesResponse = await request.get(`${API_URL}/api/admin/trades/upcoming`, {
headers: {
Cookie: `auth_token=${authCookie.value}`,
},
});
expect(response.status()).toBe(200);
const data = await response.json();
expect(Array.isArray(data)).toBe(true);
}
});
test("admin can get upcoming trades via API", async ({ page, request }) => {
await clearAuth(page);
await loginUser(page, ADMIN_USER.email, ADMIN_USER.password);
const cookies = await page.context().cookies();
const authCookie = cookies.find((c) => c.name === "auth_token");
if (authCookie) {
const response = await request.get(`${API_URL}/api/admin/trades/upcoming`, {
headers: {
Cookie: `auth_token=${authCookie.value}`,
},
});
expect(response.status()).toBe(200);
const data = await response.json();
expect(Array.isArray(data)).toBe(true);
expect(adminTradesResponse.status()).toBe(200);
const adminTradesData = await adminTradesResponse.json();
expect(Array.isArray(adminTradesData)).toBe(true);
}
});
});