|
| 1 | +import request from 'supertest'; |
| 2 | +import { FastifyInstance } from 'fastify'; |
| 3 | +import { getTestContext, updateTestContext } from './testContext'; // Assuming updateTestContext might be needed if we were to store new cookies, though not strictly for this test's core logic. |
| 4 | + |
| 5 | +describe('Global Enable Login E2E Tests', () => { |
| 6 | + let server: FastifyInstance; |
| 7 | + let port: number; |
| 8 | + let adminCookie: string | undefined; |
| 9 | + |
| 10 | + // User credentials (matching those in 2-user-registration.e2e.test.ts and 3-email-login.e2e.test.ts) |
| 11 | + const adminUserCredentials = { |
| 12 | + email: 'admin@example.com', |
| 13 | + password: 'SecurePassword123!', |
| 14 | + }; |
| 15 | + const regularUserCredentials = { |
| 16 | + email: 'user@example.com', |
| 17 | + password: 'SecurePassword456!', |
| 18 | + }; |
| 19 | + |
| 20 | + beforeAll(async () => { |
| 21 | + const context = getTestContext(); |
| 22 | + server = context.server!; |
| 23 | + port = context.port; |
| 24 | + |
| 25 | + // Always perform a fresh login for the admin user for this test suite |
| 26 | + const loginResponse = await request(server.server) |
| 27 | + .post('/api/auth/email/login') |
| 28 | + .send(adminUserCredentials); |
| 29 | + |
| 30 | + if (loginResponse.status === 200 && loginResponse.headers['set-cookie']) { |
| 31 | + adminCookie = loginResponse.headers['set-cookie'][0]; |
| 32 | + // Optionally update test context if this cookie should be available to subsequent, separate test files, |
| 33 | + // but for this suite, the local adminCookie is sufficient. |
| 34 | + // updateTestContext({ firstUserLoginCookie: adminCookie }); |
| 35 | + } else { |
| 36 | + console.error('Failed to log in admin user in beforeAll for 7-global-enable-login tests:', loginResponse.body); |
| 37 | + throw new Error('Admin user login failed in beforeAll, cannot proceed with tests for 7-global-enable-login.'); |
| 38 | + } |
| 39 | + |
| 40 | + if (!adminCookie) { |
| 41 | + throw new Error('Admin cookie could not be obtained in beforeAll for 7-global-enable-login.'); |
| 42 | + } |
| 43 | + |
| 44 | + // Initial state: ensure login is enabled before starting tests in this suite |
| 45 | + // This also serves as a check that setEnableLogin works with true using the fresh cookie |
| 46 | + await setEnableLogin(true, adminCookie); |
| 47 | + }); |
| 48 | + |
| 49 | + afterAll(async () => { |
| 50 | + // Cleanup: Ensure global.enable_login is set back to true after all tests in this suite |
| 51 | + if (adminCookie) { |
| 52 | + try { |
| 53 | + await setEnableLogin(true, adminCookie); |
| 54 | + console.log('Successfully reset global.enable_login to true in afterAll.'); |
| 55 | + } catch (error) { |
| 56 | + console.error('Failed to reset global.enable_login in afterAll:', error); |
| 57 | + } |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + // Helper function to set the global.enable_login setting |
| 62 | + async function setEnableLogin(enable: boolean, cookie: string) { |
| 63 | + const response = await request(server.server) |
| 64 | + .put('/api/settings/global.enable_login') |
| 65 | + .set('Cookie', cookie) |
| 66 | + .send({ value: enable.toString(), group_id: 'global' }); // value must be a string |
| 67 | + |
| 68 | + expect(response.status).toBe(200); // Assuming 200 OK for successful setting update |
| 69 | + expect(response.body).toHaveProperty('success', true); |
| 70 | + expect(response.body.data).toHaveProperty('key', 'global.enable_login'); |
| 71 | + expect(response.body.data).toHaveProperty('value', enable.toString()); |
| 72 | + return response; |
| 73 | + } |
| 74 | + |
| 75 | + it('should allow login when global.enable_login is true', async () => { |
| 76 | + // Ensure setting is true (might be redundant due to beforeAll/previous test, but good for test isolation) |
| 77 | + await setEnableLogin(true, adminCookie!); |
| 78 | + |
| 79 | + const loginResponse = await request(server.server) |
| 80 | + .post('/api/auth/email/login') |
| 81 | + .send(regularUserCredentials); |
| 82 | + |
| 83 | + expect(loginResponse.status).toBe(200); |
| 84 | + expect(loginResponse.body).toHaveProperty('success', true); |
| 85 | + expect(loginResponse.body.user.email).toBe(regularUserCredentials.email); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should prevent login when global.enable_login is set to false', async () => { |
| 89 | + // Set global.enable_login to false |
| 90 | + await setEnableLogin(false, adminCookie!); |
| 91 | + |
| 92 | + const loginResponse = await request(server.server) |
| 93 | + .post('/api/auth/email/login') |
| 94 | + .send(regularUserCredentials); |
| 95 | + |
| 96 | + expect(loginResponse.status).toBe(403); |
| 97 | + expect(loginResponse.body).toHaveProperty('success', false); |
| 98 | + expect(loginResponse.body.error).toBe('Login is currently disabled by administrator.'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should allow login again when global.enable_login is set back to true', async () => { |
| 102 | + // Set global.enable_login back to true |
| 103 | + await setEnableLogin(true, adminCookie!); |
| 104 | + |
| 105 | + const loginResponse = await request(server.server) |
| 106 | + .post('/api/auth/email/login') |
| 107 | + .send(regularUserCredentials); |
| 108 | + |
| 109 | + expect(loginResponse.status).toBe(200); |
| 110 | + expect(loginResponse.body).toHaveProperty('success', true); |
| 111 | + expect(loginResponse.body.user.email).toBe(regularUserCredentials.email); |
| 112 | + }); |
| 113 | +}); |
0 commit comments