aboutsummaryrefslogtreecommitdiffstats
path: root/core/src/views/Setup.cy.ts
blob: f252801c4d889f8709c147c6e8f1795421e18134 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
 * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
import type { SetupConfig, SetupLinks } from '../install'
import SetupView from './Setup.vue'

import '../../css/guest.css'

const defaultConfig = Object.freeze({
	adminlogin: '',
	adminpass: '',
	dbuser: '',
	dbpass: '',
	dbname: '',
	dbtablespace: '',
	dbhost: '',
	dbtype: '',
	databases: {
		sqlite: 'SQLite',
		mysql: 'MySQL/MariaDB',
		pgsql: 'PostgreSQL',
	},
	directory: '',
	hasAutoconfig: false,
	htaccessWorking: true,
	serverRoot: '/var/www/html',
	errors: [],
}) as SetupConfig

const links = {
	adminInstall: 'https://docs.nextcloud.com/server/32/go.php?to=admin-install',
	adminSourceInstall: 'https://docs.nextcloud.com/server/32/go.php?to=admin-source_install',
	adminDBConfiguration: 'https://docs.nextcloud.com/server/32/go.php?to=admin-db-configuration',
} as SetupLinks

describe('Default setup page', () => {
	beforeEach(() => {
		cy.mockInitialState('core', 'links', links)
	})

	afterEach(() => cy.unmockInitialState())

	it('Renders default config', () => {
		cy.mockInitialState('core', 'config', defaultConfig)
		cy.mount(SetupView)

		cy.get('[data-cy-setup-form]').scrollIntoView()
		cy.get('[data-cy-setup-form]').should('be.visible')

		// Single note is the footer help
		cy.get('[data-cy-setup-form-note]')
			.should('have.length', 1)
			.should('be.visible')
		cy.get('[data-cy-setup-form-note]').should('contain', 'See the documentation')

		// DB radio selectors
		cy.get('[data-cy-setup-form-field^="dbtype"]')
			.should('exist')
			.find('input')
			.should('be.checked')

		cy.get('[data-cy-setup-form-field="dbtype-mysql"]').should('exist')
		cy.get('[data-cy-setup-form-field="dbtype-pgsql"]').should('exist')
		cy.get('[data-cy-setup-form-field="dbtype-oci"]').should('not.exist')

		// Sqlite warning
		cy.get('[data-cy-setup-form-db-note="sqlite"]')
			.should('be.visible')

		// admin login, password, data directory and 3 DB radio selectors
		cy.get('[data-cy-setup-form-field]')
			.should('be.visible')
			.should('have.length', 6)
	})

	it('Renders single DB sqlite', () => {
		const config = {
			...defaultConfig,
			databases: {
				sqlite: 'SQLite',
			},
		}
		cy.mockInitialState('core', 'config', config)
		cy.mount(SetupView)

		// No DB radio selectors if only sqlite
		cy.get('[data-cy-setup-form-field^="dbtype"]')
			.should('not.exist')

		// Two warnings: sqlite and single db support
		cy.get('[data-cy-setup-form-db-note="sqlite"]')
			.should('be.visible')
		cy.get('[data-cy-setup-form-db-note="single-db"]')
			.should('be.visible')

		// Admin login, password and data directory
		cy.get('[data-cy-setup-form-field]')
			.should('be.visible')
			.should('have.length', 3)
	})

	it('Renders single DB mysql', () => {
		const config = {
			...defaultConfig,
			databases: {
				mysql: 'MySQL/MariaDB',
			},
		}
		cy.mockInitialState('core', 'config', config)
		cy.mount(SetupView)

		// No DB radio selectors if only mysql
		cy.get('[data-cy-setup-form-field^="dbtype"]')
			.should('not.exist')

		// Single db support warning
		cy.get('[data-cy-setup-form-db-note="single-db"]')
			.should('be.visible')
			.invoke('html')
			.should('contains', links.adminSourceInstall)

		// No SQLite warning
		cy.get('[data-cy-setup-form-db-note="sqlite"]')
			.should('not.exist')

		// Admin login, password, data directory, db user,
		// db password, db name and db host
		cy.get('[data-cy-setup-form-field]')
			.should('be.visible')
			.should('have.length', 7)
	})

	it('Changes fields from sqlite to mysql then oci', () => {
		const config = {
			...defaultConfig,
			databases: {
				sqlite: 'SQLite',
				mysql: 'MySQL/MariaDB',
				pgsql: 'PostgreSQL',
				oci: 'Oracle',
			},
		}
		cy.mockInitialState('core', 'config', config)
		cy.mount(SetupView)

		// SQLite selected
		cy.get('[data-cy-setup-form-field="dbtype-sqlite"]')
			.should('be.visible')
			.find('input')
			.should('be.checked')

		// Admin login, password, data directory and 4 DB radio selectors
		cy.get('[data-cy-setup-form-field]')
			.should('be.visible')
			.should('have.length', 7)

		// Change to MySQL
		cy.get('[data-cy-setup-form-field="dbtype-mysql"]').click()
		cy.get('[data-cy-setup-form-field="dbtype-mysql"] input').should('be.checked')

		// Admin login, password, data directory, db user, db password,
		// db name, db host and 4 DB radio selectors
		cy.get('[data-cy-setup-form-field]')
			.should('be.visible')
			.should('have.length', 11)

		// Change to Oracle
		cy.get('[data-cy-setup-form-field="dbtype-oci"]').click()
		cy.get('[data-cy-setup-form-field="dbtype-oci"] input').should('be.checked')

		// Admin login, password, data directory, db user, db password,
		// db name, db table space, db host and 4 DB radio selectors
		cy.get('[data-cy-setup-form-field]')
			.should('be.visible')
			.should('have.length', 12)
		cy.get('[data-cy-setup-form-field="dbtablespace"]')
			.should('be.visible')
	})
})

describe('Setup page with errors and warning', () => {
	beforeEach(() => {
		cy.mockInitialState('core', 'links', links)
	})

	afterEach(() => cy.unmockInitialState())

	it('Renders error from backend', () => {
		const config = {
			...defaultConfig,
			errors: [
				{
					error: 'Error message',
					hint: 'Error hint',
				},
			],
		}
		cy.mockInitialState('core', 'config', config)
		cy.mount(SetupView)

		// Error message and hint
		cy.get('[data-cy-setup-form-note="error"]')
			.should('be.visible')
			.should('have.length', 1)
			.should('contain', 'Error message')
			.should('contain', 'Error hint')
	})

	it('Renders errors from backend', () => {
		const config = {
			...defaultConfig,
			errors: [
				'Error message 1',
				{
					error: 'Error message',
					hint: 'Error hint',
				},
			],
		}
		cy.mockInitialState('core', 'config', config)
		cy.mount(SetupView)

		// Error message and hint
		cy.get('[data-cy-setup-form-note="error"]')
			.should('be.visible')
			.should('have.length', 2)
		cy.get('[data-cy-setup-form-note="error"]').eq(0)
			.should('contain', 'Error message 1')
		cy.get('[data-cy-setup-form-note="error"]').eq(1)
			.should('contain', 'Error message')
			.should('contain', 'Error hint')
	})

	it('Renders all the submitted fields on error', () => {
		const config = {
			...defaultConfig,
			adminlogin: 'admin',
			adminpass: 'password',
			dbname: 'nextcloud',
			dbtype: 'mysql',
			dbuser: 'nextcloud',
			dbpass: 'password',
			dbhost: 'localhost',
			directory: '/var/www/html/nextcloud',
		} as SetupConfig
		cy.mockInitialState('core', 'config', config)
		cy.mount(SetupView)

		cy.get('input[data-cy-setup-form-field="adminlogin"]')
			.should('have.value', 'admin')
		cy.get('input[data-cy-setup-form-field="adminpass"]')
			.should('have.value', 'password')
		cy.get('[data-cy-setup-form-field="dbtype-mysql"] input')
			.should('be.checked')
		cy.get('input[data-cy-setup-form-field="dbname"]')
			.should('have.value', 'nextcloud')
		cy.get('input[data-cy-setup-form-field="dbuser"]')
			.should('have.value', 'nextcloud')
		cy.get('input[data-cy-setup-form-field="dbpass"]')
			.should('have.value', 'password')
		cy.get('input[data-cy-setup-form-field="dbhost"]')
			.should('have.value', 'localhost')
		cy.get('input[data-cy-setup-form-field="directory"]')
			.should('have.value', '/var/www/html/nextcloud')
	})

	it('Renders the htaccess warning', () => {
		const config = {
			...defaultConfig,
			htaccessWorking: false,
		}
		cy.mockInitialState('core', 'config', config)
		cy.mount(SetupView)

		cy.get('[data-cy-setup-form-note="htaccess"]')
			.should('be.visible')
			.should('contain', 'Security warning')
			.invoke('html')
			.should('contains', links.adminInstall)
	})
})

describe('Setup page with autoconfig', () => {
	beforeEach(() => {
		cy.mockInitialState('core', 'links', links)
	})

	afterEach(() => cy.unmockInitialState())

	it('Renders autoconfig', () => {
		const config = {
			...defaultConfig,
			hasAutoconfig: true,
			dbname: 'nextcloud',
			dbtype: 'mysql',
			dbuser: 'nextcloud',
			dbpass: 'password',
			dbhost: 'localhost',
			directory: '/var/www/html/nextcloud',
		} as SetupConfig
		cy.mockInitialState('core', 'config', config)
		cy.mount(SetupView)

		// Autoconfig info note
		cy.get('[data-cy-setup-form-note="autoconfig"]')
			.should('be.visible')
			.should('contain', 'Autoconfig file detected')

		// Database and storage section is hidden as already set in autoconfig
		cy.get('[data-cy-setup-form-advanced-config]').should('be.visible')
			.invoke('attr', 'open')
			.should('equal', undefined)

		// Oracle tablespace is hidden
		cy.get('[data-cy-setup-form-field="dbtablespace"]')
			.should('not.exist')
	})
})

describe('Submit a full form sends the data', () => {
	beforeEach(() => {
		cy.mockInitialState('core', 'links', links)
	})

	afterEach(() => cy.unmockInitialState())

	it('Submits a full form', () => {
		const config = {
			...defaultConfig,
			adminlogin: 'admin',
			adminpass: 'password',
			dbname: 'nextcloud',
			dbtype: 'mysql',
			dbuser: 'nextcloud',
			dbpass: 'password',
			dbhost: 'localhost',
			dbtablespace: 'tablespace',
			directory: '/var/www/html/nextcloud',
		} as SetupConfig

		cy.intercept('POST', '**', {
			delay: 2000,
		}).as('setup')

		cy.mockInitialState('core', 'config', config)
		cy.mount(SetupView)

		// Not chaining breaks the test as the POST prevents the element from being retrieved twice
		// eslint-disable-next-line cypress/unsafe-to-chain-command
		cy.get('[data-cy-setup-form-submit]')
			.click()
			.invoke('attr', 'disabled')
			.should('equal', 'disabled', { timeout: 500 })

		cy.wait('@setup')
			.its('request.body')
			.should('deep.equal', new URLSearchParams({
				adminlogin: 'admin',
				adminpass: 'password',
				directory: '/var/www/html/nextcloud',
				dbtype: 'mysql',
				dbuser: 'nextcloud',
				dbpass: 'password',
				dbname: 'nextcloud',
				dbhost: 'localhost',
			}).toString())
	})
})