|
| 1 | +import { test, expect } from "@playwright/test"; |
| 2 | +import { ApiClient } from "../utils/apiClient"; |
| 3 | + |
| 4 | +test.describe("Auth API (DummyJSON)", () => { |
| 5 | + test("GET /auth/me returns user profile when authorized (token)", async ({ request, baseURL }) => { |
| 6 | + const api = new ApiClient(request, baseURL!); |
| 7 | + |
| 8 | + // 1) Login to get token |
| 9 | + const loginRes = await api.post("/auth/login", { |
| 10 | + username: "emilys", |
| 11 | + password: "emilyspass", |
| 12 | + expiresInMins: 30, |
| 13 | + }); |
| 14 | + |
| 15 | + expect(loginRes.status()).toBe(200); |
| 16 | + await api.assertJson(loginRes); |
| 17 | + |
| 18 | + const loginBody = await loginRes.json(); |
| 19 | + expect(loginBody).toHaveProperty("accessToken"); |
| 20 | + const token = loginBody.accessToken as string; |
| 21 | + expect(token.length).toBeGreaterThan(10); |
| 22 | + |
| 23 | + // 2) Call protected endpoint with Bearer token |
| 24 | + const meRes = await api.get("/auth/me", { |
| 25 | + Authorization: `Bearer ${token}`, |
| 26 | + }); |
| 27 | + |
| 28 | + expect(meRes.status()).toBe(200); |
| 29 | + await api.assertJson(meRes); |
| 30 | + |
| 31 | + const meBody = await meRes.json(); |
| 32 | + |
| 33 | + // 3) Assertions that matter for QA (shape + consistency) |
| 34 | + expect(meBody).toHaveProperty("id"); |
| 35 | + expect(meBody).toHaveProperty("username"); |
| 36 | + expect(typeof meBody.id).toBe("number"); |
| 37 | + expect(typeof meBody.username).toBe("string"); |
| 38 | + |
| 39 | + // Optional: confirm same username (if API returns it in login payload) |
| 40 | + // DummyJSON обычно возвращает username в логине — если есть, проверим |
| 41 | + if (loginBody.username) { |
| 42 | + expect(meBody.username).toBe(loginBody.username); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + test("GET /auth/me fails without token (negative)", async ({ request, baseURL }) => { |
| 47 | + const api = new ApiClient(request, baseURL!); |
| 48 | + |
| 49 | + const res = await api.get("/auth/me"); |
| 50 | + expect(res.ok()).toBeFalsy(); |
| 51 | + // обычно 401, но пусть будет устойчиво: |
| 52 | + expect([400, 401, 403]).toContain(res.status()); |
| 53 | + }); |
| 54 | +}); |
0 commit comments