Files
vikunja/frontend/src/helpers/parseScopesFromQuery.test.ts
kolaente ff01f8e859 feat(api-tokens): support title and scopes query parameters (#2143)
This allows external integrations to link directly to the API token creation page with pre-selected title and permission scopes. URLs can now use `?title=Name&scopes=group:perm,group:perm` format to pre-populate the form.

Example URL:
```
/user/settings/api-tokens?title=My%20Integration&scopes=tasks:create,tasks:delete,projects:read_all
```
2026-01-24 18:08:23 +00:00

81 lines
2.0 KiB
TypeScript

import {describe, it, expect} from 'vitest'
import {parseScopesFromQuery} from './parseScopesFromQuery'
describe('parseScopesFromQuery', () => {
it('returns empty object for null input', () => {
expect(parseScopesFromQuery(null)).toEqual({})
})
it('returns empty object for undefined input', () => {
expect(parseScopesFromQuery(undefined)).toEqual({})
})
it('returns empty object for empty string', () => {
expect(parseScopesFromQuery('')).toEqual({})
})
it('parses a single scope', () => {
expect(parseScopesFromQuery('tasks:read')).toEqual({
tasks: ['read'],
})
})
it('parses multiple scopes in the same group', () => {
expect(parseScopesFromQuery('tasks:read,tasks:write')).toEqual({
tasks: ['read', 'write'],
})
})
it('parses scopes across different groups', () => {
expect(parseScopesFromQuery('tasks:read,projects:write,labels:read')).toEqual({
tasks: ['read'],
projects: ['write'],
labels: ['read'],
})
})
it('handles whitespace around commas', () => {
expect(parseScopesFromQuery('tasks:read , projects:write')).toEqual({
tasks: ['read'],
projects: ['write'],
})
})
it('handles whitespace around colons', () => {
expect(parseScopesFromQuery('tasks : read')).toEqual({
tasks: ['read'],
})
})
it('ignores malformed entries without colons', () => {
expect(parseScopesFromQuery('tasks:read,invalidentry,projects:write')).toEqual({
tasks: ['read'],
projects: ['write'],
})
})
it('ignores entries with empty group', () => {
expect(parseScopesFromQuery(':read,tasks:write')).toEqual({
tasks: ['write'],
})
})
it('ignores entries with empty permission', () => {
expect(parseScopesFromQuery('tasks:,projects:write')).toEqual({
projects: ['write'],
})
})
it('handles trailing commas', () => {
expect(parseScopesFromQuery('tasks:read,')).toEqual({
tasks: ['read'],
})
})
it('handles leading commas', () => {
expect(parseScopesFromQuery(',tasks:read')).toEqual({
tasks: ['read'],
})
})
})