railyard/apps/ritgram/web/tests/authService.test.ts
caelum0x f80b04ae0f feat(ritgram): move GitGram app into railyard as Ritgram, next to Rithub
Moved later/gitgram-ai/gitgram/ to apps/ritgram/ (fresh history; full history stays in caelum0x/gitgram).
AITimeMachine stays separate in later/gitgram-ai per the Rithub master plan.
Logs, pid files and env files are ignored; GPL-3.0 mobile/proximity noted in THIRD_PARTY_NOTICES.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-13 04:18:46 +03:00

102 lines
2.9 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import MockAdapter from 'axios-mock-adapter'
import api from '@/services/api'
import { authService } from '@/services/authService'
const mock = new MockAdapter(api)
const mockUser = {
id: '1',
username: 'testuser',
email: 'test@example.com',
}
describe('authService', () => {
beforeEach(() => {
mock.reset()
localStorage.clear()
})
describe('login', () => {
it('should login and store user in localStorage', async () => {
mock.onPost('/v1/auth/login').reply(200, {
user: mockUser,
token: 'access-token',
})
const result = await authService.login('test@example.com', 'password')
expect(result.user).toEqual(mockUser)
expect(localStorage.setItem).toHaveBeenCalledWith('user', JSON.stringify(mockUser))
})
it('should return 2FA challenge when required', async () => {
mock.onPost('/v1/auth/login').reply(200, {
requires2FA: true,
tempToken: 'temp-2fa',
})
const result = await authService.login('test@example.com', 'password')
expect(result.requires2FA).toBe(true)
expect(result.tempToken).toBe('temp-2fa')
})
it('should throw on invalid credentials', async () => {
mock.onPost('/v1/auth/login').reply(401, { message: 'Invalid credentials' })
await expect(authService.login('bad@email.com', 'wrong')).rejects.toThrow()
})
})
describe('register', () => {
it('should register and store user in localStorage', async () => {
mock.onPost('/v1/auth/register').reply(201, {
user: mockUser,
token: 'access-token',
})
const result = await authService.register({
username: 'testuser',
email: 'test@example.com',
password: 'password123',
})
expect(result.user).toEqual(mockUser)
expect(localStorage.setItem).toHaveBeenCalledWith('user', JSON.stringify(mockUser))
})
})
describe('logout', () => {
it('should call logout endpoint and clear user from localStorage', async () => {
mock.onPost('/v1/auth/logout').reply(200, { message: 'Logged out' })
await authService.logout()
expect(localStorage.removeItem).toHaveBeenCalledWith('user')
})
})
describe('getMe', () => {
it('should return current user', async () => {
mock.onGet('/v1/auth/me').reply(200, { user: mockUser, account: null })
const result = await authService.getMe()
expect(result.user).toEqual(mockUser)
})
})
describe('forgotPassword', () => {
it('should send forgot password request', async () => {
mock.onPost('/v1/auth/forgot-password').reply(200, {
message: 'If an account exists with this email, a password reset link has been sent',
})
const result = await authService.forgotPassword('test@example.com')
expect(result.message).toContain('password reset link')
})
})
})