|
| 1 | +/** |
| 2 | + * Fixture personalizada para tests del módulo Show |
| 3 | + * Configura X-API-TOKEN (mayúsculas) solo para endpoints de Show |
| 4 | + */ |
| 5 | + |
| 6 | +const baseTest = require("@playwright/test"); |
| 7 | +require("dotenv").config(); |
| 8 | +const { faker } = require("@faker-js/faker"); |
| 9 | + |
| 10 | +const createShow = async (authRequest, attrs = {}) => { |
| 11 | + const payload = { |
| 12 | + title: `[QA-AUTO] Show ${faker.string.alphanumeric(6)} ${Date.now()}`, |
| 13 | + type: "tvshow", |
| 14 | + account: process.env.ACCOUNT_ID || "test-account", |
| 15 | + ...attrs, |
| 16 | + }; |
| 17 | + |
| 18 | + const res = await authRequest.post("/api/show", { form: payload }); |
| 19 | + if (!res.ok()) { |
| 20 | + const txt = await res.text().catch(() => ""); |
| 21 | + throw new Error(`createShow failed: ${res.status()} ${txt}`); |
| 22 | + } |
| 23 | + |
| 24 | + const body = await res.json(); |
| 25 | + // API returns: { data: show } | { status, data } | show at root | array |
| 26 | + const raw = body?.data ?? body; |
| 27 | + return Array.isArray(raw) ? raw[0] : raw; |
| 28 | +}; |
| 29 | + |
| 30 | +const deleteShow = async (authRequest, showId) => { |
| 31 | + try { |
| 32 | + const res = await authRequest.delete(`/api/show/${showId}`); |
| 33 | + return res.ok(); |
| 34 | + } catch (err) { |
| 35 | + console.error(`Error deleting show ${showId}:`, err.message); |
| 36 | + return false; |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +exports.test = baseTest.test.extend({ |
| 41 | + // -- authRequest con X-API-TOKEN (solo para Show) -- |
| 42 | + authRequest: async ({ playwright }, use) => { |
| 43 | + const context = await playwright.request.newContext({ |
| 44 | + baseURL: process.env.BASE_URL, |
| 45 | + extraHTTPHeaders: { |
| 46 | + "X-API-Token": process.env.API_TOKEN, |
| 47 | + }, |
| 48 | + }); |
| 49 | + |
| 50 | + await use(context); |
| 51 | + await context.dispose(); |
| 52 | + }, |
| 53 | + |
| 54 | + // -- Información de contexto -- |
| 55 | + accountId: async ({}, use) => { |
| 56 | + const accountId = process.env.ACCOUNT_ID || "test-account-id"; |
| 57 | + await use(accountId); |
| 58 | + }, |
| 59 | + |
| 60 | + tempShow: async ({ authRequest }, use) => { |
| 61 | + const show = await createShow(authRequest, { |
| 62 | + type: "tvshow", |
| 63 | + is_published: "true", |
| 64 | + }); |
| 65 | + |
| 66 | + await use(show); |
| 67 | + |
| 68 | + // Cleanup |
| 69 | + await deleteShow(authRequest, show._id); |
| 70 | + }, |
| 71 | + |
| 72 | + // -- Show para ser eliminado en el test -- |
| 73 | + tempShowForDelete: async ({ authRequest }, use) => { |
| 74 | + const show = await createShow(authRequest, { |
| 75 | + type: "radioshow", |
| 76 | + }); |
| 77 | + |
| 78 | + await use(show); |
| 79 | + |
| 80 | + // Intenta limpiar en caso de que el test no lo elimine completamente |
| 81 | + try { |
| 82 | + await deleteShow(authRequest, show._id); |
| 83 | + } catch (err) { |
| 84 | + // Ignore |
| 85 | + } |
| 86 | + }, |
| 87 | + |
| 88 | + // -- Usuario restringido para pruebas de control de acceso -- |
| 89 | + restrictedUser: async ({}, use) => { |
| 90 | + const restrictedUserId = |
| 91 | + process.env.RESTRICTED_USER_ID || "restricted-user-123"; |
| 92 | + await use(restrictedUserId); |
| 93 | + }, |
| 94 | + |
| 95 | + // -- Múltiples shows para pruebas de Search -- |
| 96 | + tempShowsBatch: async ({ authRequest }, use) => { |
| 97 | + const shows = []; |
| 98 | + const types = ["tvshow", "radioshow", "podcast"]; |
| 99 | + for (let i = 0; i < 3; i++) { |
| 100 | + const show = await createShow(authRequest, { |
| 101 | + title: `[QA-AUTO] Batch Show ${i} ${Date.now()}`, |
| 102 | + type: types[i % types.length], |
| 103 | + is_published: i === 0 ? "true" : "false", |
| 104 | + }); |
| 105 | + shows.push(show); |
| 106 | + } |
| 107 | + |
| 108 | + await use(shows); |
| 109 | + |
| 110 | + // Cleanup |
| 111 | + for (const show of shows) { |
| 112 | + await deleteShow(authRequest, show._id); |
| 113 | + } |
| 114 | + }, |
| 115 | +}); |
| 116 | + |
| 117 | +exports.expect = baseTest.expect; |
| 118 | + |
| 119 | +module.exports = exports; |
0 commit comments