Beautiful Open-Source Form Builder
import { describe, it, expect, vi } from 'vitest'
/**
* Tests for template edit permission logic in templates/[slug].vue
* Ensures the edit button is shown only to template owners, admins, and template editors
*/
describe('Template Slug Page - canEditTemplate', () => {
describe('canEditTemplate computed property', () => {
it('should allow admin users to edit any template', () => {
const user = { id: 1, admin: true, template_editor: false }
const template = { id: 1, creator_id: 999 }
const canEdit = user && (user.admin || user.template_editor || template.creator_id === user.id)
expect(canEdit).toBe(true)
})
it('should allow template_editor users to edit any template', () => {
const user = { id: 1, admin: false, template_editor: true }
const template = { id: 1, creator_id: 999 }
const canEdit = user && (user.admin || user.template_editor || template.creator_id === user.id)
expect(canEdit).toBe(true)
})
it('should allow template creator to edit their own template', () => {
const user = { id: 1, admin: false, template_editor: false }
const template = { id: 1, creator_id: 1 }
const canEdit = user && (user.admin || user.template_editor || template.creator_id === user.id)
expect(canEdit).toBe(true)
})
it('should not allow non-creator, non-admin users to edit templates', () => {
const user = { id: 1, admin: false, template_editor: false }
const template = { id: 1, creator_id: 999 }
const canEdit = user && (user.admin || user.template_editor || template.creator_id === user.id)
expect(canEdit).toBe(false)
})
it('should not allow unauthenticated users to edit templates', () => {
const user = null
const template = { id: 1, creator_id: 999 }
const canEdit = user && (user.admin || user.template_editor || template.creator_id === user.id)
expect(canEdit).toBe(null)
})
it('should require creator_id match when user has no special roles', () => {
const user = { id: 2, admin: false, template_editor: false }
const template = { id: 1, creator_id: 2 }
const canEdit = user && (user.admin || user.template_editor || template.creator_id === user.id)
expect(canEdit).toBe(true)
})
it('should reject when creator_id does not match and no special roles', () => {
const user = { id: 2, admin: false, template_editor: false }
const template = { id: 1, creator_id: 3 }
const canEdit = user && (user.admin || user.template_editor || template.creator_id === user.id)
expect(canEdit).toBe(false)
})
})
})