Skip to content

Commit bf18a27

Browse files
committed
test(api): Add integration test for new middleware and move api specs to separate folder
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 9b275a1 commit bf18a27

File tree

4 files changed

+120
-3
lines changed

4 files changed

+120
-3
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
*/
2222

23-
import { randUser } from '../utils/index.js'
23+
import { randUser } from '../../utils/index.js'
2424

2525
const user = randUser()
2626
const messages = {

cypress/e2e/api/UsersApi.spec.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* @copyright Copyright (c) 2022 Max <max@nextcloud.com>
3+
*
4+
* @author Max <max@nextcloud.com>
5+
*
6+
* @license AGPL-3.0-or-later
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
import { randUser } from '../../utils/index.js'
24+
25+
const user = randUser()
26+
27+
describe('The user mention API', function() {
28+
29+
before(function() {
30+
cy.createUser(user)
31+
window.OC = {
32+
config: { modRewriteWorking: false },
33+
webroot: '',
34+
}
35+
})
36+
37+
let fileId
38+
let requesttoken
39+
40+
beforeEach(function() {
41+
cy.login(user)
42+
cy.prepareSessionApi().then((token) => {
43+
requesttoken = token
44+
cy.uploadTestFile('test.md')
45+
.then(id => {
46+
fileId = id
47+
})
48+
})
49+
})
50+
51+
it('fetches users with valid session', function() {
52+
cy.createTextSession(fileId).then(connection => {
53+
cy.wrap(connection)
54+
.its('document.id')
55+
.should('equal', fileId)
56+
const requestData = {
57+
method: 'POST',
58+
url: '/apps/text/api/v1/users',
59+
body: {
60+
documentId: connection.document.id,
61+
sessionId: connection.session.id,
62+
sessionToken: connection.session.token,
63+
requesttoken,
64+
},
65+
failOnStatusCode: false,
66+
}
67+
68+
cy.request(requestData).then(({ status }) => {
69+
expect(status).to.eq(200)
70+
})
71+
72+
const invalidRequestData = { ...requestData }
73+
cy.wrap(() => {
74+
invalidRequestData.body = {
75+
...requestData.body,
76+
sessionToken: 'invalid',
77+
}
78+
})
79+
cy.request(invalidRequestData).then(({ status }) => {
80+
expect(status).to.eq(403)
81+
})
82+
83+
cy.wrap(() => {
84+
invalidRequestData.body = {
85+
...requestData.body,
86+
sessionId: 0,
87+
}
88+
})
89+
cy.request(invalidRequestData).then(({ status }) => {
90+
expect(status).to.eq(403)
91+
})
92+
93+
cy.wrap(() => {
94+
invalidRequestData.body = {
95+
...requestData.body,
96+
documentId: 0,
97+
}
98+
})
99+
cy.request(invalidRequestData).then(({ status }) => {
100+
expect(status).to.eq(403)
101+
})
102+
103+
cy.wrap(connection.close())
104+
105+
cy.request(requestData).then(({ status, body }) => {
106+
expect(status).to.eq(403)
107+
})
108+
})
109+
})
110+
})

cypress/support/sessions.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
import SessionApi from '../../src/services/SessionApi.js'
2424
import { emit } from '@nextcloud/event-bus'
2525

26-
Cypress.Commands.add('prepareSessionApi', (fileId) => {
26+
Cypress.Commands.add('prepareSessionApi', () => {
2727
return cy.request('/csrftoken')
28-
.then(({ body }) => emit('csrf-token-update', body))
28+
.then(({ body }) => {
29+
emit('csrf-token-update', body)
30+
return body.token
31+
})
2932
})
3033

3134
Cypress.Commands.add('createTextSession', (fileId, options = {}) => {

src/services/SessionApi.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ export class Connection {
6868
this.#options = options
6969
}
7070

71+
get session() {
72+
return this.#session
73+
}
74+
7175
get document() {
7276
return this.#document
7377
}

0 commit comments

Comments
 (0)