Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test(api): Add integration test for new middleware and move api specs…
… to separate folder

Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Jun 12, 2023
commit 9473eff478490de21e127ac9939e1efabc06acc3
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
*/

import { randUser } from '../utils/index.js'
import { randUser } from '../../utils/index.js'

const user = randUser()
const messages = {
Expand Down
112 changes: 112 additions & 0 deletions cypress/e2e/api/UsersApi.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* @copyright Copyright (c) 2022 Max <[email protected]>
* @copyright Copyright (c) 2023 Julius Härtl <[email protected]>
*
* @author Max <[email protected]>
* @author Julius Härtl <[email protected]>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

import { randUser } from '../../utils/index.js'

const user = randUser()

describe('The user mention API', function() {

before(function() {
cy.createUser(user)
window.OC = {
config: { modRewriteWorking: false },
webroot: '',
}
})

let fileId
let requesttoken

beforeEach(function() {
cy.login(user)
cy.prepareSessionApi().then((token) => {
requesttoken = token
cy.uploadTestFile('test.md')
.then(id => {
fileId = id
})
})
})

it('fetches users with valid session', function() {
cy.createTextSession(fileId).then(connection => {
cy.wrap(connection)
.its('document.id')
.should('equal', fileId)
const requestData = {
method: 'POST',
url: '/apps/text/api/v1/users',
body: {
documentId: connection.document.id,
sessionId: connection.session.id,
sessionToken: connection.session.token,
requesttoken,
},
failOnStatusCode: false,
}

cy.request(requestData).then(({ status }) => {
expect(status).to.eq(200)
})

const invalidRequestData = { ...requestData }
cy.wrap(() => {
invalidRequestData.body = {
...requestData.body,
sessionToken: 'invalid',
}
})
cy.request(invalidRequestData).then(({ status }) => {
expect(status).to.eq(403)
})

cy.wrap(() => {
invalidRequestData.body = {
...requestData.body,
sessionId: 0,
}
})
cy.request(invalidRequestData).then(({ status }) => {
expect(status).to.eq(403)
})

cy.wrap(() => {
invalidRequestData.body = {
...requestData.body,
documentId: 0,
}
})
cy.request(invalidRequestData).then(({ status }) => {
expect(status).to.eq(403)
})

cy.wrap(connection.close())

cy.request(requestData).then(({ status, body }) => {
expect(status).to.eq(403)
})
})
})
})
7 changes: 5 additions & 2 deletions cypress/support/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
import SessionApi from '../../src/services/SessionApi.js'
import { emit } from '@nextcloud/event-bus'

Cypress.Commands.add('prepareSessionApi', (fileId) => {
Cypress.Commands.add('prepareSessionApi', () => {
return cy.request('/csrftoken')
.then(({ body }) => emit('csrf-token-update', body))
.then(({ body }) => {
emit('csrf-token-update', body)
return body.token
})
})

Cypress.Commands.add('createTextSession', (fileId, options = {}) => {
Expand Down
4 changes: 4 additions & 0 deletions src/services/SessionApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export class Connection {
this.#options = options
}

get session() {
return this.#session
}

get document() {
return this.#document
}
Expand Down