diff options
author | Roeland Jago Douma <rullzer@users.noreply.github.com> | 2020-02-11 08:35:07 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-11 08:35:07 +0100 |
commit | db5ffc6d94703f49df8f5ba13670ad7544cf0bc0 (patch) | |
tree | db79a07de4dace365b515a4b5f7b07df04173be8 /core/src | |
parent | 29ec5e6c96ee110eb2752132c5978aedbf83e4a8 (diff) | |
parent | c47a0a01534d81f923d2d1f69da764f1c6a3754e (diff) | |
download | nextcloud-server-db5ffc6d94703f49df8f5ba13670ad7544cf0bc0.tar.gz nextcloud-server-db5ffc6d94703f49df8f5ba13670ad7544cf0bc0.zip |
Merge pull request #19389 from nextcloud/test/js-api
Test request token logic a bit more
Diffstat (limited to 'core/src')
-rw-r--r-- | core/src/OC/requesttoken.js | 33 | ||||
-rw-r--r-- | core/src/OCP/initialstate.js | 29 | ||||
-rw-r--r-- | core/src/session-heartbeat.js | 4 | ||||
-rw-r--r-- | core/src/tests/OC/requesttoken.spec.js | 27 |
4 files changed, 52 insertions, 41 deletions
diff --git a/core/src/OC/requesttoken.js b/core/src/OC/requesttoken.js index b25fdafd5b4..6c011879b8b 100644 --- a/core/src/OC/requesttoken.js +++ b/core/src/OC/requesttoken.js @@ -21,20 +21,35 @@ import { emit } from '@nextcloud/event-bus' -let token = document.getElementsByTagName('head')[0].getAttribute('data-requesttoken') +/** + * @private + * @param {Document} global the document to read the initial value from + * @param {Function} emit the function to invoke for every new token + * @returns {Object} + */ +export const manageToken = (global, emit) => { + let token = global.getElementsByTagName('head')[0].getAttribute('data-requesttoken') + + return { + getToken: () => token, + setToken: newToken => { + token = newToken + + emit('csrf-token-update', { + token, + }) + }, + } +} + +const manageFromDocument = manageToken(document, emit) /** * @returns {string} */ -export const getToken = () => token +export const getToken = manageFromDocument.getToken /** * @param {String} newToken new token */ -export const setToken = newToken => { - token = newToken - - emit('csrf-token-update', { - token, - }) -} +export const setToken = manageFromDocument.setToken diff --git a/core/src/OCP/initialstate.js b/core/src/OCP/initialstate.js deleted file mode 100644 index 57a1162bedb..00000000000 --- a/core/src/OCP/initialstate.js +++ /dev/null @@ -1,29 +0,0 @@ -/* - * @copyright Copyright (c) 2019 Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * 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/>. - * - */ - -/** - * @namespace OCP.InitialState - */ - -import { loadState as load } from '@nextcloud/initial-state' - -export const loadState = load diff --git a/core/src/session-heartbeat.js b/core/src/session-heartbeat.js index 88c519e1dde..a941720d853 100644 --- a/core/src/session-heartbeat.js +++ b/core/src/session-heartbeat.js @@ -103,14 +103,14 @@ export const initSessionHeartBeat = () => { // Let apps know we're online and requests will have the new token emit('networkOnline', { - success: true + success: true, }) } catch (e) { console.error('could not update session token after resuming network', e) // Let apps know we're online but requests might have an outdated token emit('networkOnline', { - success: false + success: false, }) } }) diff --git a/core/src/tests/OC/requesttoken.spec.js b/core/src/tests/OC/requesttoken.spec.js index 401d3766af4..d19a4b8e9c8 100644 --- a/core/src/tests/OC/requesttoken.spec.js +++ b/core/src/tests/OC/requesttoken.spec.js @@ -19,12 +19,37 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +import {JSDOM} from 'jsdom' import {subscribe, unsubscribe} from '@nextcloud/event-bus' -import {setToken} from '../../OC/requesttoken' +import {manageToken, setToken} from '../../OC/requesttoken' describe('request token', () => { + let dom + let emit + let manager + const token = 'abc123' + + beforeEach(() => { + dom = new JSDOM() + emit = sinon.spy() + const head = dom.window.document.getElementsByTagName('head')[0] + head.setAttribute('data-requesttoken', token) + + manager = manageToken(dom.window.document, emit) + }) + + it('reads the token from the document', () => { + expect(manager.getToken()).to.equal('abc123') + }) + + it('remembers the updated token', () => { + manager.setToken('bca321') + + expect(manager.getToken()).to.equal('bca321') + }) + describe('@nextcloud/auth integration', () => { let listener |