Skip to content

Commit

Permalink
Merge pull request #672 from nextcloud-libraries/test/request-token
Browse files Browse the repository at this point in the history
test: Add missing tests for request token
  • Loading branch information
susnux authored Aug 2, 2024
2 parents 9a04ee4 + 72c7072 commit a10290f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
5 changes: 2 additions & 3 deletions lib/requesttoken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ const observers: CsrfTokenObserver[] = []
export function getRequestToken(): string | null {
if (token === undefined) {
// Only on first load, try to get token from document
const tokenElement = document?.getElementsByTagName('head')[0]
token = tokenElement ? tokenElement.getAttribute('data-requesttoken') : null
token = document.head.dataset.requesttoken ?? null
}
return token
}
Expand All @@ -42,7 +41,7 @@ subscribe('csrf-token-update', (e: unknown) => {
try {
observer(token!)
} catch (e) {
console.error('error updating CSRF token observer', e)
console.error('Error updating CSRF token observer', e)
}
})
})
40 changes: 32 additions & 8 deletions test/request-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,36 @@
import { emit } from '@nextcloud/event-bus'
import { beforeEach, describe, expect, test, vi } from 'vitest'

import { getRequestToken, onRequestTokenUpdate } from '../lib/index'

describe('request token', () => {
beforeEach(() => {
emit('csrf-token-update', {
token: undefined,
})
vi.resetModules()
vi.resetAllMocks()
delete document.head.dataset.requesttoken
})

test('updates token via event', () => {
test('return null if no token found', async () => {
const { getRequestToken } = await import('../lib')
expect(getRequestToken()).toBe(null)
})

test('find correct value', () => {
test('read initial token', async () => {
document.head.dataset.requesttoken = 'random-token'
const { getRequestToken } = await import('../lib')
expect(getRequestToken()).toBe('random-token')
})

test('can update token by event', async () => {
const { getRequestToken } = await import('../lib')

emit('csrf-token-update', {
token: 'token123',
})

expect(getRequestToken()).toBe('token123')
})

test('request token observer is called', () => {
test('request token observer is called', async () => {
const { onRequestTokenUpdate } = await import('../lib')
const observer = vi.fn(() => { })

onRequestTokenUpdate(observer)
Expand All @@ -36,4 +44,20 @@ describe('request token', () => {

expect(observer.mock.calls.length).toBe(1)
})

test('handle exception in observer', async () => {
const spy = vi.spyOn(window.console, 'error')
const { onRequestTokenUpdate } = await import('../lib')
const observer = vi.fn(() => { throw new Error('!Error!') })
// silence the console
spy.mockImplementationOnce(() => {})

onRequestTokenUpdate(observer)
emit('csrf-token-update', {
token: 'token123',
})

expect(observer.mock.calls.length).toBe(1)
expect(spy).toHaveBeenCalledOnce()
})
})

0 comments on commit a10290f

Please sign in to comment.