|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { |
| 5 | + AuthEndpointPaths, |
| 6 | + AuthEndpointSchema, |
| 7 | + AuthEndpointAliases, |
| 8 | + EndpointMapping, |
| 9 | + getAuthEndpointUrl, |
| 10 | +} from './auth-endpoints.zod'; |
| 11 | + |
| 12 | +describe('AuthEndpointPaths', () => { |
| 13 | + it('should define email/password authentication endpoints', () => { |
| 14 | + expect(AuthEndpointPaths.signInEmail).toBe('/sign-in/email'); |
| 15 | + expect(AuthEndpointPaths.signUpEmail).toBe('/sign-up/email'); |
| 16 | + expect(AuthEndpointPaths.signOut).toBe('/sign-out'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should define session management endpoints', () => { |
| 20 | + expect(AuthEndpointPaths.getSession).toBe('/get-session'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should define password management endpoints', () => { |
| 24 | + expect(AuthEndpointPaths.forgetPassword).toBe('/forget-password'); |
| 25 | + expect(AuthEndpointPaths.resetPassword).toBe('/reset-password'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should define email verification endpoints', () => { |
| 29 | + expect(AuthEndpointPaths.sendVerificationEmail).toBe('/send-verification-email'); |
| 30 | + expect(AuthEndpointPaths.verifyEmail).toBe('/verify-email'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should define 2FA endpoints', () => { |
| 34 | + expect(AuthEndpointPaths.twoFactorEnable).toBe('/two-factor/enable'); |
| 35 | + expect(AuthEndpointPaths.twoFactorVerify).toBe('/two-factor/verify'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should define passkey endpoints', () => { |
| 39 | + expect(AuthEndpointPaths.passkeyRegister).toBe('/passkey/register'); |
| 40 | + expect(AuthEndpointPaths.passkeyAuthenticate).toBe('/passkey/authenticate'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should define magic link endpoints', () => { |
| 44 | + expect(AuthEndpointPaths.magicLinkSend).toBe('/magic-link/send'); |
| 45 | + expect(AuthEndpointPaths.magicLinkVerify).toBe('/magic-link/verify'); |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +describe('AuthEndpointSchema', () => { |
| 50 | + it('should validate signInEmail endpoint', () => { |
| 51 | + const endpoint = AuthEndpointSchema.shape.signInEmail.parse({ |
| 52 | + method: 'POST', |
| 53 | + path: '/sign-in/email', |
| 54 | + description: 'Sign in with email and password', |
| 55 | + }); |
| 56 | + |
| 57 | + expect(endpoint.method).toBe('POST'); |
| 58 | + expect(endpoint.path).toBe('/sign-in/email'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should validate signUpEmail endpoint', () => { |
| 62 | + const endpoint = AuthEndpointSchema.shape.signUpEmail.parse({ |
| 63 | + method: 'POST', |
| 64 | + path: '/sign-up/email', |
| 65 | + description: 'Register new user with email and password', |
| 66 | + }); |
| 67 | + |
| 68 | + expect(endpoint.method).toBe('POST'); |
| 69 | + expect(endpoint.path).toBe('/sign-up/email'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should validate getSession endpoint', () => { |
| 73 | + const endpoint = AuthEndpointSchema.shape.getSession.parse({ |
| 74 | + method: 'GET', |
| 75 | + path: '/get-session', |
| 76 | + description: 'Get current user session', |
| 77 | + }); |
| 78 | + |
| 79 | + expect(endpoint.method).toBe('GET'); |
| 80 | + expect(endpoint.path).toBe('/get-session'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should reject invalid HTTP method', () => { |
| 84 | + expect(() => |
| 85 | + AuthEndpointSchema.shape.signInEmail.parse({ |
| 86 | + method: 'GET', // Should be POST |
| 87 | + path: '/sign-in/email', |
| 88 | + description: 'Sign in with email and password', |
| 89 | + }) |
| 90 | + ).toThrow(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should reject invalid path', () => { |
| 94 | + expect(() => |
| 95 | + AuthEndpointSchema.shape.signInEmail.parse({ |
| 96 | + method: 'POST', |
| 97 | + path: '/wrong-path', // Should be /sign-in/email |
| 98 | + description: 'Sign in with email and password', |
| 99 | + }) |
| 100 | + ).toThrow(); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe('AuthEndpointAliases', () => { |
| 105 | + it('should map common names to canonical endpoints', () => { |
| 106 | + expect(AuthEndpointAliases.login).toBe('/sign-in/email'); |
| 107 | + expect(AuthEndpointAliases.register).toBe('/sign-up/email'); |
| 108 | + expect(AuthEndpointAliases.logout).toBe('/sign-out'); |
| 109 | + expect(AuthEndpointAliases.me).toBe('/get-session'); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +describe('EndpointMapping', () => { |
| 114 | + it('should map legacy paths to canonical paths', () => { |
| 115 | + expect(EndpointMapping['/login']).toBe('/sign-in/email'); |
| 116 | + expect(EndpointMapping['/register']).toBe('/sign-up/email'); |
| 117 | + expect(EndpointMapping['/logout']).toBe('/sign-out'); |
| 118 | + expect(EndpointMapping['/me']).toBe('/get-session'); |
| 119 | + expect(EndpointMapping['/refresh']).toBe('/get-session'); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +describe('getAuthEndpointUrl', () => { |
| 124 | + it('should construct full endpoint URLs', () => { |
| 125 | + const basePath = '/api/v1/auth'; |
| 126 | + |
| 127 | + expect(getAuthEndpointUrl(basePath, 'signInEmail')).toBe('/api/v1/auth/sign-in/email'); |
| 128 | + expect(getAuthEndpointUrl(basePath, 'signUpEmail')).toBe('/api/v1/auth/sign-up/email'); |
| 129 | + expect(getAuthEndpointUrl(basePath, 'getSession')).toBe('/api/v1/auth/get-session'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should handle trailing slash in basePath', () => { |
| 133 | + const basePath = '/api/v1/auth/'; |
| 134 | + |
| 135 | + expect(getAuthEndpointUrl(basePath, 'signInEmail')).toBe('/api/v1/auth/sign-in/email'); |
| 136 | + expect(getAuthEndpointUrl(basePath, 'getSession')).toBe('/api/v1/auth/get-session'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should work with different base paths', () => { |
| 140 | + expect(getAuthEndpointUrl('/custom/auth', 'signInEmail')).toBe('/custom/auth/sign-in/email'); |
| 141 | + expect(getAuthEndpointUrl('http://localhost:3000/api/auth', 'signUpEmail')).toBe( |
| 142 | + 'http://localhost:3000/api/auth/sign-up/email' |
| 143 | + ); |
| 144 | + }); |
| 145 | +}); |
0 commit comments