Skip to content

Commit abeddb3

Browse files
committed
refactor: Modularize setup-public-share.ts
This update enhances the testing framework for public sharing by improving the setup and management of share contexts. It refactors the code to make the `createShare` function reusable externally, ensuring better testability and flexibility. Additionally, the update optimizes share permission adjustments and improves state management, making public share setups more efficient and maintainable. Signed-off-by: nfebe <[email protected]>
1 parent c10b8e2 commit abeddb3

File tree

1 file changed

+77
-64
lines changed

1 file changed

+77
-64
lines changed

cypress/e2e/files_sharing/public-share/setup-public-share.ts

Lines changed: 77 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,110 +5,123 @@
55
import type { User } from '@nextcloud/cypress'
66
import { openSharingPanel } from '../FilesSharingUtils.ts'
77

8-
let user: User
9-
let url: string
8+
export interface ShareContext {
9+
user: User
10+
url?: string
11+
}
12+
13+
const defaultShareContext: ShareContext = {
14+
user: {} as User,
15+
url: undefined,
16+
}
1017

1118
/**
12-
* URL of the share
19+
* Retrieves the URL of the share.
20+
* Throws an error if the share context is not initialized properly.
21+
*
22+
* @param context The current share context (defaults to `defaultShareContext` if not provided).
23+
* @return The share URL.
24+
* @throws Error if the share context has no URL.
1325
*/
14-
export function getShareUrl() {
15-
if (url === undefined) {
16-
throw new Error('You need to setup the share first!')
26+
export function getShareUrl(context: ShareContext = defaultShareContext): string {
27+
if (!context.url) {
28+
throw new Error('Share context is not properly initialized with a URL.')
1729
}
18-
return url
30+
return context.url
1931
}
2032

2133
/**
2234
* Setup the available data
35+
* @param context The current share context
2336
* @param shareName The name of the shared folder
2437
*/
25-
function setupData(shareName: string) {
26-
cy.mkdir(user, `/${shareName}`)
27-
cy.mkdir(user, `/${shareName}/subfolder`)
28-
cy.uploadContent(user, new Blob(['<content>foo</content>']), 'text/plain', `/${shareName}/foo.txt`)
29-
cy.uploadContent(user, new Blob(['<content>bar</content>']), 'text/plain', `/${shareName}/subfolder/bar.txt`)
38+
export function setupData(context: ShareContext, shareName: string): void {
39+
cy.mkdir(context.user, `/${shareName}`)
40+
cy.mkdir(context.user, `/${shareName}/subfolder`)
41+
cy.uploadContent(context.user, new Blob(['<content>foo</content>']), 'text/plain', `/${shareName}/foo.txt`)
42+
cy.uploadContent(context.user, new Blob(['<content>bar</content>']), 'text/plain', `/${shareName}/subfolder/bar.txt`)
3043
}
3144

3245
/**
3346
* Create a public link share
47+
* @param context The current share context
3448
* @param shareName The name of the shared folder
3549
*/
36-
function createShare(shareName: string) {
37-
cy.login(user)
38-
// open the files app
39-
cy.visit('/apps/files')
40-
// open the sidebar
41-
openSharingPanel(shareName)
42-
// create the share
50+
export function createShare(context: ShareContext, shareName: string) {
51+
cy.login(context.user)
52+
cy.visit('/apps/files') // Open the files app
53+
openSharingPanel(shareName) // Open the sharing sidebar
54+
4355
cy.intercept('POST', '**/ocs/v2.php/apps/files_sharing/api/v1/shares').as('createShare')
44-
cy.findByRole('button', { name: 'Create a new share link' })
45-
.click()
56+
cy.findByRole('button', { name: 'Create a new share link' }).click()
4657

47-
// extract the link
58+
// Extract the share link
4859
return cy.wait('@createShare')
4960
.should(({ response }) => {
50-
const { ocs } = response!.body
51-
url = ocs?.data.url
52-
expect(url).to.match(/^http:\/\//)
61+
expect(response?.statusCode).to.eq(200)
62+
const url = response?.body?.ocs?.data?.url
63+
expect(url).to.match(/^https?:\/\//)
64+
context.url = url
5365
})
54-
.then(() => cy.wrap(url))
66+
.then(() => cy.wrap(context.url))
5567
}
5668

5769
/**
5870
* Adjust share permissions to be editable
5971
*/
60-
function adjustSharePermission() {
61-
// Update the share to be a file drop
72+
function adjustSharePermission(): void {
6273
cy.findByRole('list', { name: 'Link shares' })
6374
.findAllByRole('listitem')
6475
.first()
6576
.findByRole('button', { name: /Actions/i })
6677
.click()
67-
cy.findByRole('menuitem', { name: /Customize link/i })
68-
.should('be.visible')
69-
.click()
78+
cy.findByRole('menuitem', { name: /Customize link/i }).click()
79+
80+
cy.get('[data-cy-files-sharing-share-permissions-bundle]').should('be.visible')
81+
cy.get('[data-cy-files-sharing-share-permissions-bundle="upload-edit"]').click()
7082

71-
// Enable upload-edit
72-
cy.get('[data-cy-files-sharing-share-permissions-bundle]')
73-
.should('be.visible')
74-
cy.get('[data-cy-files-sharing-share-permissions-bundle="upload-edit"]')
75-
.click()
76-
// save changes
7783
cy.intercept('PUT', '**/ocs/v2.php/apps/files_sharing/api/v1/shares/*').as('updateShare')
78-
cy.findByRole('button', { name: 'Update share' })
79-
.click()
80-
cy.wait('@updateShare')
84+
cy.findByRole('button', { name: 'Update share' }).click()
85+
cy.wait('@updateShare').its('response.statusCode').should('eq', 200)
8186
}
8287

8388
/**
8489
* Setup a public share and backup the state.
8590
* If the setup was already done in another run, the state will be restored.
8691
*
92+
* @param shareName The name of the shared folder
8793
* @return The URL of the share
8894
*/
89-
export function setupPublicShare(): Cypress.Chainable<string> {
90-
const shareName = 'shared'
95+
export function setupPublicShare(shareName = 'shared'): Cypress.Chainable<string> {
9196

92-
return cy.task('getVariable', { key: 'public-share-data' })
93-
.then((data) => {
94-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
95-
const { dataSnapshot, shareUrl } = data as any || {}
96-
if (dataSnapshot) {
97-
cy.restoreState(dataSnapshot)
98-
url = shareUrl
99-
return cy.wrap(shareUrl as string)
100-
} else {
101-
const shareData: Record<string, unknown> = {}
102-
return cy.createRandomUser()
103-
.then(($user) => { user = $user })
104-
.then(() => setupData(shareName))
105-
.then(() => createShare(shareName))
106-
.then((value) => { shareData.shareUrl = value })
107-
.then(() => adjustSharePermission())
108-
.then(() => cy.saveState().then((value) => { shareData.dataSnapshot = value }))
109-
.then(() => cy.task('setVariable', { key: 'public-share-data', value: shareData }))
110-
.then(() => cy.log(`Public share setup, URL: ${shareData.shareUrl}`))
111-
.then(() => cy.wrap(url))
112-
}
113-
})
97+
return cy.task('getVariable', { key: 'public-share-data' }).then((data) => {
98+
// Leave dataSnapshot part unchanged
99+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
100+
const { dataSnapshot, shareUrl } = data as any || {}
101+
if (dataSnapshot) {
102+
cy.restoreState(dataSnapshot)
103+
defaultShareContext.url = shareUrl
104+
return cy.wrap(shareUrl as string)
105+
} else {
106+
const shareData: Record<string, unknown> = {}
107+
return cy.createRandomUser()
108+
.then((user) => {
109+
defaultShareContext.user = user
110+
})
111+
.then(() => setupData(defaultShareContext, shareName))
112+
.then(() => createShare(defaultShareContext, shareName))
113+
.then((url) => {
114+
shareData.shareUrl = url
115+
})
116+
.then(() => adjustSharePermission())
117+
.then(() =>
118+
cy.saveState().then((snapshot) => {
119+
shareData.dataSnapshot = snapshot
120+
}),
121+
)
122+
.then(() => cy.task('setVariable', { key: 'public-share-data', value: shareData }))
123+
.then(() => cy.log(`Public share setup, URL: ${shareData.shareUrl}`))
124+
.then(() => cy.wrap(defaultShareContext.url))
125+
}
126+
})
114127
}

0 commit comments

Comments
 (0)