Skip to content

Commit

Permalink
test(cy): rely on @nextcloud/axios for requests
Browse files Browse the repository at this point in the history
This greatly simplifies the handling of csrf-tokens and auth.

Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Apr 26, 2024
1 parent a8c437f commit e1baf67
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 326 deletions.
5 changes: 0 additions & 5 deletions cypress/e2e/api/SessionApi.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ describe('The session Api', function() {

before(function() {
cy.createUser(user)
cy.prepareWindowForSessionApi()
})

beforeEach(function() {
cy.login(user)
cy.prepareSessionApi()
})

describe('open the session', function() {
Expand Down Expand Up @@ -223,7 +221,6 @@ describe('The session Api', function() {
})
.then(() => cy.clearCookies())
.then(() => {
cy.prepareSessionApi()
return cy.createTextSession(undefined, { filePath: '', shareToken })
.then(con => {
connection = con
Expand All @@ -247,7 +244,6 @@ describe('The session Api', function() {
.should('be.at.least', 1)
cy.save(connection, { version: 1, autosaveContent: '# Heading 1', manualSave: true })
cy.login(user)
cy.prepareSessionApi()
cy.downloadFile('saves.md')
.its('data')
.should('eql', '# Heading 1')
Expand Down Expand Up @@ -290,7 +286,6 @@ describe('The session Api', function() {
cy.log(token)
shareToken = token
cy.clearCookies()
cy.prepareSessionApi()
cy.createTextSession(undefined, { filePath: '', shareToken })
.then(con => {
connection = con
Expand Down
2 changes: 0 additions & 2 deletions cypress/e2e/api/SyncServiceProvider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ describe('Sync service provider', function() {

before(function() {
cy.createUser(user)
cy.prepareWindowForSessionApi()
})

beforeEach(function() {
cy.login(user)
cy.prepareSessionApi()
cy.uploadTestFile('test.md')
.then(id => {
fileId = id
Expand Down
2 changes: 0 additions & 2 deletions cypress/e2e/api/UsersApi.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ describe('The user mention API', function() {

before(function() {
cy.createUser(user)
cy.prepareWindowForSessionApi()
})

beforeEach(function() {
cy.login(user)
cy.uploadTestFile('test.md').as('fileId')
.then(cy.createTextSession).as('connection')
cy.getRequestToken()
})

afterEach(function() {
Expand Down
55 changes: 6 additions & 49 deletions cypress/e2e/directediting.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,6 @@ import { initUserAndFiles, randUser } from '../utils/index.js'

const user = randUser()

const createDirectEditingLink = (user, file) => {
cy.login(user)
return cy.request({
method: 'POST',
url: `${Cypress.env('baseUrl')}/ocs/v2.php/apps/files/api/v1/directEditing/open?format=json`,
form: true,
body: {
path: file,
},
auth: { user: user.userId, pass: user.password },
headers: {
'OCS-ApiRequest': 'true',
'Content-Type': 'application/x-www-form-urlencoded',
},
}).then(response => {
cy.log(response)
const token = response.body?.ocs?.data?.url
cy.log(`Created direct editing token for ${user.userId}`, token)
return cy.wrap(token)
})
}

const createDirectEditingLinkForNewFile = (user, file) => {
cy.login(user)
return cy.request({
method: 'POST',
url: `${Cypress.env('baseUrl')}/ocs/v2.php/apps/files/api/v1/directEditing/create?format=json`,
form: true,
body: {
path: file,
editorId: 'text',
creatorId: 'textdocument',
},
auth: { user: user.userId, pass: user.password },
headers: {
'OCS-ApiRequest': 'true',
'Content-Type': 'application/x-www-form-urlencoded',
},
}).then(response => {
cy.log(response)
const token = response.body?.ocs?.data?.url
cy.log(`Created direct editing token for ${user.userId}`, token)
return cy.wrap(token)
})
}

describe('direct editing', function() {

const visitHooks = {
Expand All @@ -66,7 +20,8 @@ describe('direct editing', function() {
cy.intercept({ method: 'POST', url: '**/session/*/push' }).as('push')
cy.intercept({ method: 'POST', url: '**/session/*/sync' }).as('sync')

createDirectEditingLink(user, 'empty.md')
cy.login(user)
cy.createDirectEditingLink('empty.txt')
.then((token) => {
cy.logout()
cy.visit(token, visitHooks)
Expand Down Expand Up @@ -94,7 +49,8 @@ describe('direct editing', function() {
cy.intercept({ method: 'POST', url: '**/session/*/push' }).as('push')
cy.intercept({ method: 'POST', url: '**/session/*/sync' }).as('sync')

createDirectEditingLinkForNewFile(user, 'newfile.md')
cy.login(user)
cy.createDirectEditingLinkForNewFile('newfile.md')
.then((token) => {
cy.logout()
cy.visit(token, visitHooks)
Expand Down Expand Up @@ -123,7 +79,8 @@ describe('direct editing', function() {
cy.intercept({ method: 'POST', url: '**/session/*/push' }).as('push')
cy.intercept({ method: 'POST', url: '**/session/*/sync' }).as('sync')

createDirectEditingLink(user, 'empty.txt')
cy.login(user)
cy.createDirectEditingLink('empty.txt')
.then((token) => {
cy.logout()
cy.visit(token, visitHooks)
Expand Down
2 changes: 1 addition & 1 deletion cypress/e2e/propfind.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('Text PROPFIND extension ', function() {
cy.uploadFile('test.md', 'text/markdown', '/Readme.md')
cy.propfindFolder('/')
.should('have.property', richWorkspace, '## Hello world\n')
cy.removeFile('/Readme.md')
cy.deleteFile('/Readme.md')
cy.propfindFolder('/')
.should('have.property', richWorkspace, '')
})
Expand Down
Loading

0 comments on commit e1baf67

Please sign in to comment.