aboutsummaryrefslogtreecommitdiffstats
path: root/cypress/e2e/files_external/files-user-credentials.cy.ts
blob: 1911c5477c3c3654747ecca9902d844cfcb8851d (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
/**
 * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

import { User } from '@nextcloud/cypress'
import { AuthBackend, createStorageWithConfig, StorageBackend } from './StorageUtils'
import { getActionEntryForFile, getRowForFile, navigateToFolder, triggerInlineActionForFile } from '../files/FilesUtils'

import { ACTION_CREDENTIALS_EXTERNAL_STORAGE } from '../../../apps/files_external/src/actions/enterCredentialsAction'
import { handlePasswordConfirmation } from '../settings/usersUtils'

describe('Files user credentials', { testIsolation: true }, () => {
	let user1: User
	let user2: User
	let storageUser: User

	beforeEach(() => {
	})

	before(() => {
		cy.runOccCommand('app:enable files_external')

		// Create some users
		cy.createRandomUser().then((user) => { user1 = user })
		cy.createRandomUser().then((user) => { user2 = user })

		// This user will hold the webdav storage
		cy.createRandomUser().then((user) => {
			storageUser = user
			cy.uploadFile(user, 'image.jpg')
		})
	})

	after(() => {
		// Cleanup global storages
		cy.runOccCommand('files_external:list --output=json').then(({ stdout }) => {
			const list = JSON.parse(stdout)
			list.forEach((storage) => cy.runOccCommand(`files_external:delete --yes ${storage.mount_id}`), { failOnNonZeroExit: false })
		})

		cy.runOccCommand('app:disable files_external')
	})

	it('Create a user storage with user credentials', () => {
		const url = Cypress.config('baseUrl') + '/remote.php/dav/files/' + storageUser.userId
		createStorageWithConfig(storageUser.userId, StorageBackend.DAV, AuthBackend.UserProvided, { host: url.replace('index.php/', ''), secure: 'false' })

		cy.login(user1)
		cy.visit('/apps/files/extstoragemounts')
		getRowForFile(storageUser.userId).should('be.visible')

		cy.intercept('PUT', '**/apps/files_external/userglobalstorages/*').as('setCredentials')

		triggerInlineActionForFile(storageUser.userId, ACTION_CREDENTIALS_EXTERNAL_STORAGE)

		// See credentials dialog
		cy.findByRole('dialog', { name: 'Storage credentials' }).as('storageDialog')
		cy.get('@storageDialog').should('be.visible')
		cy.get('@storageDialog').findByRole('textbox', { name: 'Login' }).type(storageUser.userId)
		cy.get('@storageDialog').get('input[type="password"]').type(storageUser.password)
		cy.get('@storageDialog').get('button').contains('Confirm').click()
		cy.get('@storageDialog').should('not.exist')

		// Storage dialog now closed, the user auth dialog should be visible
		cy.findByRole('dialog', { name: 'Confirm your password' }).as('authDialog')
		cy.get('@authDialog').should('be.visible')
		handlePasswordConfirmation(user1.password)

		// Wait for the credentials to be set
		cy.wait('@setCredentials')

		// Auth dialog should be closed and the set credentials button should be gone
		cy.get('@authDialog').should('not.exist', { timeout: 2000 })
		getActionEntryForFile(storageUser.userId, ACTION_CREDENTIALS_EXTERNAL_STORAGE).should('not.exist')

		// Finally, the storage should be accessible
		cy.visit('/apps/files')
		navigateToFolder(storageUser.userId)
		getRowForFile('image.jpg').should('be.visible')
	})

	it('Create a user storage with GLOBAL user credentials', () => {
		const url = Cypress.config('baseUrl') + '/remote.php/dav/files/' + storageUser.userId
		createStorageWithConfig('storage1', StorageBackend.DAV, AuthBackend.UserGlobalAuth, { host: url.replace('index.php/', ''), secure: 'false' })

		cy.login(user2)
		cy.visit('/apps/files/extstoragemounts')
		getRowForFile('storage1').should('be.visible')

		cy.intercept('PUT', '**/apps/files_external/userglobalstorages/*').as('setCredentials')

		triggerInlineActionForFile('storage1', ACTION_CREDENTIALS_EXTERNAL_STORAGE)

		// See credentials dialog
		cy.findByRole('dialog', { name: 'Storage credentials' }).as('storageDialog')
		cy.get('@storageDialog').should('be.visible')
		cy.get('@storageDialog').findByRole('textbox', { name: 'Login' }).type(storageUser.userId)
		cy.get('@storageDialog').get('input[type="password"]').type(storageUser.password)
		cy.get('@storageDialog').get('button').contains('Confirm').click()
		cy.get('@storageDialog').should('not.exist')

		// Storage dialog now closed, the user auth dialog should be visible
		cy.findByRole('dialog', { name: 'Confirm your password' }).as('authDialog')
		cy.get('@authDialog').should('be.visible')
		handlePasswordConfirmation(user2.password)

		// Wait for the credentials to be set
		cy.wait('@setCredentials')

		// Auth dialog should be closed and the set credentials button should be gone
		cy.get('@authDialog').should('not.exist', { timeout: 2000 })
		getActionEntryForFile('storage1', ACTION_CREDENTIALS_EXTERNAL_STORAGE).should('not.exist')

		// Finally, the storage should be accessible
		cy.visit('/apps/files')
		navigateToFolder('storage1')
		getRowForFile('image.jpg').should('be.visible')
	})

	it('Create another user storage while reusing GLOBAL user credentials', () => {
		const url = Cypress.config('baseUrl') + '/remote.php/dav/files/' + storageUser.userId
		createStorageWithConfig('storage2', StorageBackend.DAV, AuthBackend.UserGlobalAuth, { host: url.replace('index.php/', ''), secure: 'false' })

		cy.login(user2)
		cy.visit('/apps/files/extstoragemounts')
		getRowForFile('storage2').should('be.visible')

		// Since we already have set the credentials, the action should not be present
		getActionEntryForFile('storage1', ACTION_CREDENTIALS_EXTERNAL_STORAGE).should('not.exist')
		getActionEntryForFile('storage2', ACTION_CREDENTIALS_EXTERNAL_STORAGE).should('not.exist')

		// Finally, the storage should be accessible
		cy.visit('/apps/files')
		navigateToFolder('storage2')
		getRowForFile('image.jpg').should('be.visible')
	})
})