|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +import { Users } from './fixtures/userStates'; |
| 5 | +import { Utils, Registration } from './page-objects'; |
| 6 | +import { test, expect } from './utils/test'; |
| 7 | + |
| 8 | +const IFRAME_URL = 'http://iframe.rocket.chat'; |
| 9 | +const API_URL = 'http://auth.rocket.chat/api/login'; |
| 10 | + |
| 11 | +test.describe('iframe-authentication', () => { |
| 12 | + let poRegistration: Registration; |
| 13 | + let poUtils: Utils; |
| 14 | + |
| 15 | + test.beforeAll(async ({ api }) => { |
| 16 | + await api.post('/settings/Accounts_iframe_enabled', { value: true }); |
| 17 | + await api.post('/settings/Accounts_iframe_url', { value: IFRAME_URL }); |
| 18 | + await api.post('/settings/Accounts_Iframe_api_url', { value: API_URL }); |
| 19 | + await api.post('/settings/Accounts_Iframe_api_method', { value: 'POST' }); |
| 20 | + }); |
| 21 | + |
| 22 | + test.afterAll(async ({ api }) => { |
| 23 | + await api.post('/settings/Accounts_iframe_enabled', { value: false }); |
| 24 | + await api.post('/settings/Accounts_iframe_url', { value: '' }); |
| 25 | + await api.post('/settings/Accounts_Iframe_api_url', { value: '' }); |
| 26 | + await api.post('/settings/Accounts_Iframe_api_method', { value: '' }); |
| 27 | + }); |
| 28 | + |
| 29 | + test.beforeEach(async ({ page }) => { |
| 30 | + poRegistration = new Registration(page); |
| 31 | + poUtils = new Utils(page); |
| 32 | + |
| 33 | + await page.route(API_URL, async (route) => { |
| 34 | + await route.fulfill({ |
| 35 | + status: 200, |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + const htmlContent = fs |
| 40 | + .readFileSync(path.resolve(__dirname, 'fixtures/files/iframe-login.html'), 'utf-8') |
| 41 | + .replace('REPLACE_WITH_TOKEN', Users.user1.data.loginToken); |
| 42 | + |
| 43 | + await page.route(IFRAME_URL, async (route) => { |
| 44 | + await route.fulfill({ |
| 45 | + status: 200, |
| 46 | + contentType: 'text/html', |
| 47 | + body: htmlContent, |
| 48 | + }); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + test('should render iframe instead of login page', async ({ page }) => { |
| 53 | + await page.goto('/home'); |
| 54 | + |
| 55 | + await expect(poRegistration.loginIframeForm).toBeVisible(); |
| 56 | + }); |
| 57 | + |
| 58 | + test('should render iframe login page if API returns error', async ({ page }) => { |
| 59 | + await page.route(API_URL, async (route) => { |
| 60 | + await route.fulfill({ |
| 61 | + status: 500, |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + await page.goto('/home'); |
| 66 | + |
| 67 | + await expect(poRegistration.loginIframeForm).toBeVisible(); |
| 68 | + }); |
| 69 | + |
| 70 | + test('should login with token when API returns valid token', async ({ page }) => { |
| 71 | + await page.route(API_URL, async (route) => { |
| 72 | + await route.fulfill({ |
| 73 | + status: 200, |
| 74 | + contentType: 'application/json', |
| 75 | + body: JSON.stringify({ loginToken: Users.user1.data.loginToken }), |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + await page.goto('/home'); |
| 80 | + await expect(poUtils.mainContent).toBeVisible(); |
| 81 | + }); |
| 82 | + |
| 83 | + test('should show login page when API returns invalid token', async ({ page }) => { |
| 84 | + await page.route(API_URL, async (route) => { |
| 85 | + await route.fulfill({ |
| 86 | + status: 200, |
| 87 | + contentType: 'application/json', |
| 88 | + body: JSON.stringify({ loginToken: 'invalid-token' }), |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + await page.goto('/home'); |
| 93 | + await expect(poRegistration.loginIframeForm).toBeVisible(); |
| 94 | + }); |
| 95 | + |
| 96 | + test('should login through iframe', async ({ page }) => { |
| 97 | + await page.goto('/home'); |
| 98 | + |
| 99 | + await expect(poRegistration.loginIframeForm).toBeVisible(); |
| 100 | + |
| 101 | + await poRegistration.loginIframeSubmitButton.click(); |
| 102 | + |
| 103 | + await expect(poUtils.mainContent).toBeVisible(); |
| 104 | + }); |
| 105 | + |
| 106 | + test('should return error to iframe when login fails', async ({ page }) => { |
| 107 | + const htmlContent = fs.readFileSync(path.resolve(__dirname, 'fixtures/files/iframe-login.html'), 'utf-8'); |
| 108 | + |
| 109 | + await page.route(IFRAME_URL, async (route) => { |
| 110 | + await route.fulfill({ |
| 111 | + status: 200, |
| 112 | + contentType: 'text/html', |
| 113 | + body: htmlContent, |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + await page.goto('/home'); |
| 118 | + |
| 119 | + await expect(poRegistration.loginIframeForm).toBeVisible(); |
| 120 | + |
| 121 | + await poRegistration.loginIframeSubmitButton.click(); |
| 122 | + |
| 123 | + await expect(poRegistration.loginIframeError).toBeVisible(); |
| 124 | + }); |
| 125 | + |
| 126 | + test.describe('incomplete settings', () => { |
| 127 | + test.beforeAll(async ({ api }) => { |
| 128 | + await api.post('/settings/Accounts_Iframe_api_url', { value: '' }); |
| 129 | + }); |
| 130 | + |
| 131 | + test.afterAll(async ({ api }) => { |
| 132 | + await api.post('/settings/Accounts_Iframe_api_url', { value: API_URL }); |
| 133 | + }); |
| 134 | + |
| 135 | + test('should render default login page, if settings are incomplete', async ({ page }) => { |
| 136 | + const htmlContent = fs.readFileSync(path.resolve(__dirname, 'fixtures/files/iframe-login.html'), 'utf-8'); |
| 137 | + |
| 138 | + await page.route(IFRAME_URL, async (route) => { |
| 139 | + await route.fulfill({ |
| 140 | + status: 200, |
| 141 | + contentType: 'text/html', |
| 142 | + body: htmlContent, |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + await page.goto('/home'); |
| 147 | + await expect(poRegistration.btnLogin).toBeVisible(); |
| 148 | + await expect(poRegistration.loginIframeForm).not.toBeVisible(); |
| 149 | + }); |
| 150 | + }); |
| 151 | +}); |
0 commit comments