221 lines
5.6 KiB
TypeScript
221 lines
5.6 KiB
TypeScript
import { describe, expect, it } from 'bun:test'
|
|
import { loginBody, getInfoResponse, loginResponseSchema, getInfoSchema } from '~/modules/auth/schema'
|
|
|
|
describe('Auth Schema', () => {
|
|
describe('loginBody', () => {
|
|
it('should validate correct login body', () => {
|
|
const validBody = {
|
|
email: 'test@example.com',
|
|
password: 'password123',
|
|
}
|
|
|
|
expect(() => loginBody.parse(validBody)).not.toThrow()
|
|
})
|
|
|
|
it('should reject invalid email format', () => {
|
|
const invalidBody = {
|
|
email: 'invalid-email',
|
|
password: 'password123',
|
|
}
|
|
|
|
expect(() => loginBody.parse(invalidBody)).toThrow()
|
|
})
|
|
|
|
it('should reject short password', () => {
|
|
const invalidBody = {
|
|
email: 'test@example.com',
|
|
password: 'short',
|
|
}
|
|
|
|
expect(() => loginBody.parse(invalidBody)).toThrow()
|
|
})
|
|
|
|
it('should reject missing email', () => {
|
|
const invalidBody = {
|
|
password: 'password123',
|
|
}
|
|
|
|
expect(() => loginBody.parse(invalidBody)).toThrow()
|
|
})
|
|
|
|
it('should reject missing password', () => {
|
|
const invalidBody = {
|
|
email: 'test@example.com',
|
|
}
|
|
|
|
expect(() => loginBody.parse(invalidBody)).toThrow()
|
|
})
|
|
})
|
|
|
|
describe('getInfoResponse', () => {
|
|
it('should validate admin user info', () => {
|
|
const adminUser = {
|
|
id: 123,
|
|
name: 'Admin User',
|
|
email: 'admin@example.com',
|
|
phone: null,
|
|
image: null,
|
|
googleId: null,
|
|
role: 'admin',
|
|
status: true,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
}
|
|
|
|
expect(() => getInfoResponse.parse(adminUser)).not.toThrow()
|
|
})
|
|
|
|
it('should validate volunteer user info', () => {
|
|
const volunteerUser = {
|
|
id: 456,
|
|
name: 'Volunteer User',
|
|
email: 'volunteer@example.com',
|
|
phone: '+1234567890',
|
|
image: null,
|
|
googleId: null,
|
|
role: 'volunteer',
|
|
status: true,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
}
|
|
|
|
expect(() => getInfoResponse.parse(volunteerUser)).not.toThrow()
|
|
})
|
|
|
|
it('should reject invalid role', () => {
|
|
const invalidUser = {
|
|
id: 'user-123',
|
|
name: 'Test User',
|
|
email: 'test@example.com',
|
|
role: 'invalid-role',
|
|
image: null,
|
|
googleId: null,
|
|
}
|
|
|
|
expect(() => getInfoResponse.parse(invalidUser)).toThrow()
|
|
})
|
|
|
|
it('should reject volunteer without volunteer data', () => {
|
|
const invalidVolunteer = {
|
|
role: 'volunteer',
|
|
}
|
|
|
|
expect(() => getInfoResponse.parse(invalidVolunteer)).toThrow()
|
|
})
|
|
})
|
|
|
|
describe('loginResponseSchema', () => {
|
|
it('should have correct 200 response structure', () => {
|
|
const response200 = loginResponseSchema[200]
|
|
expect(response200).toBeDefined()
|
|
|
|
const validResponse = {
|
|
message: 'Logged in.',
|
|
data: {
|
|
accessToken: 'some-jwt-token',
|
|
},
|
|
}
|
|
|
|
expect(() => response200.parse(validResponse)).not.toThrow()
|
|
})
|
|
|
|
it('should have correct 401 response structure', () => {
|
|
const response401 = loginResponseSchema[401]
|
|
expect(response401).toBeDefined()
|
|
|
|
const validError = {
|
|
message: 'Invalid username or password.',
|
|
}
|
|
|
|
expect(() => response401.parse(validError)).not.toThrow()
|
|
})
|
|
|
|
it('should reject 200 response with wrong message', () => {
|
|
const response200 = loginResponseSchema[200]
|
|
const invalidResponse = {
|
|
message: 'Wrong message.',
|
|
data: {
|
|
accessToken: 'some-jwt-token',
|
|
},
|
|
}
|
|
|
|
expect(() => response200.parse(invalidResponse)).toThrow()
|
|
})
|
|
})
|
|
|
|
describe('getInfoSchema', () => {
|
|
it('should have correct 200 response structure for admin', () => {
|
|
const response200 = getInfoSchema[200]
|
|
expect(response200).toBeDefined()
|
|
|
|
const validResponse = {
|
|
message: 'Get user info.',
|
|
data: {
|
|
id: 123,
|
|
name: 'Admin User',
|
|
email: 'admin@example.com',
|
|
phone: null,
|
|
image: null,
|
|
googleId: null,
|
|
role: 'admin',
|
|
status: true,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
},
|
|
}
|
|
|
|
expect(() => response200.parse(validResponse)).not.toThrow()
|
|
})
|
|
|
|
it('should have correct 200 response structure for volunteer', () => {
|
|
const response200 = getInfoSchema[200]
|
|
|
|
const validResponse = {
|
|
message: 'Get user info.',
|
|
data: {
|
|
id: 789,
|
|
name: 'Volunteer User',
|
|
email: 'volunteer@example.com',
|
|
phone: '+1234567890',
|
|
image: null,
|
|
googleId: null,
|
|
role: 'volunteer',
|
|
status: true,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
},
|
|
}
|
|
|
|
expect(() => response200.parse(validResponse)).not.toThrow()
|
|
})
|
|
|
|
it('should have correct 401 response structure', () => {
|
|
const response401 = getInfoSchema[401]
|
|
expect(response401).toBeDefined()
|
|
|
|
const validError = {
|
|
message: 'Unauthenticated.',
|
|
}
|
|
|
|
expect(() => response401.parse(validError)).not.toThrow()
|
|
})
|
|
|
|
it('should reject 200 response with wrong message', () => {
|
|
const response200 = getInfoSchema[200]
|
|
const invalidResponse = {
|
|
message: 'Wrong message.',
|
|
data: {
|
|
id: 'user-123',
|
|
name: 'Test User',
|
|
email: 'test@example.com',
|
|
role: 'admin',
|
|
image: null,
|
|
googleId: null,
|
|
},
|
|
}
|
|
|
|
expect(() => response200.parse(invalidResponse)).toThrow()
|
|
})
|
|
})
|
|
})
|