Skip to content

Commit

Permalink
test(api): Add integration test for new middleware and move api specs…
Browse files Browse the repository at this point in the history
… to separate folder

Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Jun 8, 2023
1 parent 9b275a1 commit bf18a27
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 3 deletions.
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
110 changes: 110 additions & 0 deletions cypress/e2e/api/UsersApi.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* @copyright Copyright (c) 2022 Max <max@nextcloud.com>
*
* @author Max <max@nextcloud.com>
*
* @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

0 comments on commit bf18a27

Please sign in to comment.