Skip to content

Commit ac29f3a

Browse files
committed
test: add test for register
1 parent 8ea4f89 commit ac29f3a

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

test/unit/api.register.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { describe, it, expect, beforeEach } from 'vitest'
2+
import { setup, $fetch, createPage } from '@nuxt/test-utils'
3+
4+
describe('API: Registration', async () => {
5+
await setup({
6+
rootDir: './playground'
7+
})
8+
9+
beforeEach(async () => {
10+
// Clean up any existing test users
11+
try {
12+
await $fetch('/api/nuxt-users', {
13+
method: 'DELETE',
14+
query: { email: 'test@example.com' }
15+
})
16+
} catch {
17+
// Ignore errors if user doesn't exist
18+
}
19+
})
20+
21+
it('should register a new user successfully', async () => {
22+
const response = await $fetch('/api/nuxt-users/register', {
23+
method: 'POST',
24+
body: {
25+
email: 'test@example.com',
26+
name: 'Test User',
27+
password: 'testpass123'
28+
}
29+
})
30+
31+
expect(response.user).toBeDefined()
32+
expect(response.user.email).toBe('test@example.com')
33+
expect(response.user.name).toBe('Test User')
34+
expect(response.message).toContain('Please check your email')
35+
})
36+
37+
it('should reject registration with invalid email', async () => {
38+
await expect($fetch('/api/nuxt-users/register', {
39+
method: 'POST',
40+
body: {
41+
email: 'invalid-email',
42+
name: 'Test User',
43+
password: 'testpass123'
44+
}
45+
})).rejects.toThrow()
46+
})
47+
48+
it('should reject registration with missing fields', async () => {
49+
await expect($fetch('/api/nuxt-users/register', {
50+
method: 'POST',
51+
body: {
52+
email: 'test@example.com'
53+
// Missing name and password
54+
}
55+
})).rejects.toThrow()
56+
})
57+
58+
it('should handle email confirmation', async () => {
59+
// This test would require mocking the token generation
60+
// For now, just test that the endpoint exists
61+
await expect($fetch('/api/nuxt-users/confirm-email', {
62+
query: {
63+
token: 'invalid-token',
64+
email: 'test@example.com'
65+
}
66+
})).rejects.toThrow('Invalid or expired')
67+
})
68+
})

test/unit/auto-whitelist.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { describe, it, expect } from 'vitest'
2+
3+
// Mock a minimal module options structure for testing the whitelist logic
4+
interface TestModuleOptions {
5+
auth: {
6+
whitelist: string[]
7+
}
8+
}
9+
10+
// Extract the whitelist logic for testing
11+
const getWhitelistWithAutoConfirmEmail = (options: TestModuleOptions) => {
12+
const combinedWhitelist = [...options.auth.whitelist]
13+
// Auto-whitelist /confirm-email if /register is whitelisted
14+
if (combinedWhitelist.includes('/register') && !combinedWhitelist.includes('/confirm-email')) {
15+
combinedWhitelist.push('/confirm-email')
16+
}
17+
return combinedWhitelist
18+
}
19+
20+
describe('Auto-whitelist functionality', () => {
21+
it('should auto-add /confirm-email when /register is whitelisted', () => {
22+
const options: TestModuleOptions = {
23+
auth: {
24+
whitelist: ['/noauth', '/register']
25+
}
26+
}
27+
28+
const result = getWhitelistWithAutoConfirmEmail(options)
29+
30+
expect(result).toContain('/register')
31+
expect(result).toContain('/confirm-email')
32+
expect(result).toContain('/noauth')
33+
})
34+
35+
it('should not duplicate /confirm-email if already present', () => {
36+
const options: TestModuleOptions = {
37+
auth: {
38+
whitelist: ['/noauth', '/register', '/confirm-email']
39+
}
40+
}
41+
42+
const result = getWhitelistWithAutoConfirmEmail(options)
43+
44+
expect(result.filter(route => route === '/confirm-email')).toHaveLength(1)
45+
})
46+
47+
it('should not add /confirm-email if /register is not whitelisted', () => {
48+
const options: TestModuleOptions = {
49+
auth: {
50+
whitelist: ['/noauth', '/login']
51+
}
52+
}
53+
54+
const result = getWhitelistWithAutoConfirmEmail(options)
55+
56+
expect(result).not.toContain('/confirm-email')
57+
expect(result).toContain('/noauth')
58+
expect(result).toContain('/login')
59+
})
60+
61+
it('should handle empty whitelist', () => {
62+
const options: TestModuleOptions = {
63+
auth: {
64+
whitelist: []
65+
}
66+
}
67+
68+
const result = getWhitelistWithAutoConfirmEmail(options)
69+
70+
expect(result).not.toContain('/confirm-email')
71+
expect(result).toHaveLength(0)
72+
})
73+
})

0 commit comments

Comments
 (0)