Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add missing tests for request token #672

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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()
})
})
Loading