|
| 1 | +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. |
| 2 | +// See LICENSE.txt for license information. |
| 3 | + |
| 4 | +const baseUrl = (process.env.MM_TEST_SERVER_URL ?? '').replace(/\/$/, ''); |
| 5 | +const username = process.env.MM_TEST_USER_NAME; |
| 6 | +const password = process.env.MM_TEST_PASSWORD; |
| 7 | + |
| 8 | +if (!baseUrl || !username || !password) { |
| 9 | + console.error('MM_TEST_SERVER_URL, MM_TEST_USER_NAME, and MM_TEST_PASSWORD are required'); |
| 10 | + process.exit(1); |
| 11 | +} |
| 12 | + |
| 13 | +async function apiLogin() { |
| 14 | + const response = await fetch(`${baseUrl}/api/v4/users/login`, { |
| 15 | + method: 'POST', |
| 16 | + headers: {'Content-Type': 'application/json'}, |
| 17 | + body: JSON.stringify({login_id: username, password}), |
| 18 | + }); |
| 19 | + if (!response.ok) { |
| 20 | + throw new Error(`Login failed: ${response.status} ${await response.text()}`); |
| 21 | + } |
| 22 | + |
| 23 | + const token = response.headers.get('Token') ?? response.headers.get('token'); |
| 24 | + if (token) { |
| 25 | + return token; |
| 26 | + } |
| 27 | + |
| 28 | + const body = await response.json(); |
| 29 | + if (body.token) { |
| 30 | + return body.token; |
| 31 | + } |
| 32 | + |
| 33 | + throw new Error('Login did not return a session token'); |
| 34 | +} |
| 35 | + |
| 36 | +async function apiRequest(token, path, init = {}) { |
| 37 | + const response = await fetch(`${baseUrl}${path}`, { |
| 38 | + ...init, |
| 39 | + headers: { |
| 40 | + Authorization: `Bearer ${token}`, |
| 41 | + 'Content-Type': 'application/json', |
| 42 | + ...init.headers, |
| 43 | + }, |
| 44 | + }); |
| 45 | + if (!response.ok) { |
| 46 | + throw new Error(`${init.method ?? 'GET'} ${path} failed: ${response.status} ${await response.text()}`); |
| 47 | + } |
| 48 | + return response.json(); |
| 49 | +} |
| 50 | + |
| 51 | +async function main() { |
| 52 | + const token = await apiLogin(); |
| 53 | + const config = await apiRequest(token, '/api/v4/config'); |
| 54 | + |
| 55 | + if (config.FileSettings?.EnablePublicLink === true) { |
| 56 | + console.log('Public links already enabled on the E2E server'); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + const siteURL = config.ServiceSettings?.SiteURL ?? baseUrl; |
| 61 | + await apiRequest(token, '/api/v4/config', { |
| 62 | + method: 'PUT', |
| 63 | + body: JSON.stringify({ |
| 64 | + ...config, |
| 65 | + ServiceSettings: {...config.ServiceSettings, SiteURL: siteURL}, |
| 66 | + FileSettings: {...config.FileSettings, EnablePublicLink: true}, |
| 67 | + }), |
| 68 | + }); |
| 69 | + |
| 70 | + const updated = await apiRequest(token, '/api/v4/config'); |
| 71 | + if (updated.FileSettings?.EnablePublicLink !== true) { |
| 72 | + throw new Error('Failed to enable public links on the E2E server'); |
| 73 | + } |
| 74 | + |
| 75 | + console.log('Enabled public links on the E2E server'); |
| 76 | +} |
| 77 | + |
| 78 | +main().catch((error) => { |
| 79 | + console.error(error); |
| 80 | + process.exit(1); |
| 81 | +}); |
0 commit comments