|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | + |
| 3 | +test.describe('Authentication and Protected Flow', () => { |
| 4 | + test('should login with test wallet and access protected API', async ({ page }) => { |
| 5 | + // 0. Fulfill the "open browser" requirement |
| 6 | + await page.goto('/'); |
| 7 | + |
| 8 | + // 1. Prepare env data for mock backend |
| 9 | + const testWalletAddress = process.env.TEST_WALLET_ADDRESS || 'GDEMOXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; |
| 10 | + const testSignature = process.env.TEST_SIGNATURE || 'mock-signature'; |
| 11 | + |
| 12 | + // 2. Perform mock login via the browser context |
| 13 | + const loginResponse = await page.request.post('/api/auth/login', { |
| 14 | + data: { |
| 15 | + address: testWalletAddress, |
| 16 | + signature: testSignature |
| 17 | + } |
| 18 | + }); |
| 19 | + |
| 20 | + // Assert successful login |
| 21 | + expect(loginResponse.status()).toBe(200); |
| 22 | + const loginData = await loginResponse.json(); |
| 23 | + expect(loginData).toHaveProperty('success', true); |
| 24 | + expect(loginData).toHaveProperty('token'); |
| 25 | + |
| 26 | + // 3. Access the protected flow with the session cookie automatically attached to the page's request context |
| 27 | + const splitResponse = await page.request.get('/api/split'); |
| 28 | + |
| 29 | + // Assert successful access to protected route |
| 30 | + expect(splitResponse.status()).toBe(200); |
| 31 | + const splitData = await splitResponse.json(); |
| 32 | + expect(splitData).toHaveProperty('allocations'); |
| 33 | + expect(splitData.allocations).toMatchObject({ |
| 34 | + dailySpending: expect.any(Number), |
| 35 | + savings: expect.any(Number), |
| 36 | + bills: expect.any(Number), |
| 37 | + insurance: expect.any(Number), |
| 38 | + }); |
| 39 | + }); |
| 40 | +}); |
0 commit comments