|
5 | 5 | import type { User } from '@nextcloud/cypress'
|
6 | 6 | import { openSharingPanel } from '../FilesSharingUtils.ts'
|
7 | 7 |
|
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 | +} |
10 | 17 |
|
11 | 18 | /**
|
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. |
13 | 25 | */
|
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.') |
17 | 29 | }
|
18 |
| - return url |
| 30 | + return context.url |
19 | 31 | }
|
20 | 32 |
|
21 | 33 | /**
|
22 | 34 | * Setup the available data
|
| 35 | + * @param context The current share context |
23 | 36 | * @param shareName The name of the shared folder
|
24 | 37 | */
|
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`) |
30 | 43 | }
|
31 | 44 |
|
32 | 45 | /**
|
33 | 46 | * Create a public link share
|
| 47 | + * @param context The current share context |
34 | 48 | * @param shareName The name of the shared folder
|
35 | 49 | */
|
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 | + |
43 | 55 | 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() |
46 | 57 |
|
47 |
| - // extract the link |
| 58 | + // Extract the share link |
48 | 59 | return cy.wait('@createShare')
|
49 | 60 | .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 |
53 | 65 | })
|
54 |
| - .then(() => cy.wrap(url)) |
| 66 | + .then(() => cy.wrap(context.url)) |
55 | 67 | }
|
56 | 68 |
|
57 | 69 | /**
|
58 | 70 | * Adjust share permissions to be editable
|
59 | 71 | */
|
60 |
| -function adjustSharePermission() { |
61 |
| - // Update the share to be a file drop |
| 72 | +function adjustSharePermission(): void { |
62 | 73 | cy.findByRole('list', { name: 'Link shares' })
|
63 | 74 | .findAllByRole('listitem')
|
64 | 75 | .first()
|
65 | 76 | .findByRole('button', { name: /Actions/i })
|
66 | 77 | .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() |
70 | 82 |
|
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 |
77 | 83 | 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) |
81 | 86 | }
|
82 | 87 |
|
83 | 88 | /**
|
84 | 89 | * Setup a public share and backup the state.
|
85 | 90 | * If the setup was already done in another run, the state will be restored.
|
86 | 91 | *
|
| 92 | + * @param shareName The name of the shared folder |
87 | 93 | * @return The URL of the share
|
88 | 94 | */
|
89 |
| -export function setupPublicShare(): Cypress.Chainable<string> { |
90 |
| - const shareName = 'shared' |
| 95 | +export function setupPublicShare(shareName = 'shared'): Cypress.Chainable<string> { |
91 | 96 |
|
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 | + }) |
114 | 127 | }
|
0 commit comments