import { DyNTS_AuthService } from './auth.service'; import { Request } from 'express'; import { DyFM_Error } from '@futdevpro/fsm-dynamo'; import { DyNTS_global_settings } from '../../_collections/global-settings.const'; class TestAuthService extends DyNTS_AuthService { static getInstance(): TestAuthService { return TestAuthService.getSingletonInstance(); } authenticate_token = async (req: Request, res: any): Promise => { // Mock implementation }; authenticate_tokenSelf = async (req: Request, res: any): Promise => { // Mock implementation }; authenticate_tokenPerm_accUsageData = async (req: Request, res: any): Promise => { // Mock implementation }; getIssuerFromRequest(req: Request): string { return 'test-issuer'; } getUsernameFromRequest(req: Request): string { return 'test-user'; } } describe('| DyNTS_AuthService', () => { let service: TestAuthService; beforeEach(() => { service = TestAuthService.getInstance(); }); it('| should be a singleton instance', () => { const instance1 = TestAuthService.getInstance(); const instance2 = TestAuthService.getInstance(); expect(instance1).toBe(instance2); expect(instance1).toBeInstanceOf(TestAuthService); }); it('| should have default service name', () => { expect(service.serviceName).toBe('AuthService'); }); it('| should have default error user message', () => { expect(service.defaultErrorUserMsg).toBeDefined(); expect(service.defaultErrorUserMsg).toContain('Auth Error'); }); describe('| getTokenFromRequest', () => { it('| should extract token from authorization header', () => { const req = { headers: { authorization: 'Bearer test-token-123', }, } as any as Request; const result = service.getTokenFromRequest(req); expect(result).toBe('test-token-123'); }); it('| should throw error when authorization header is missing', () => { const req = { headers: {}, } as any as Request; expect(() => { service.getTokenFromRequest(req); }).toThrow(); }); it('| should throw error when authorization header is null', () => { const req = { headers: { authorization: null, }, } as any as Request; expect(() => { service.getTokenFromRequest(req); }).toThrow(); }); it('| should throw error when token is missing from header', () => { const req = { headers: { authorization: 'Bearer ', }, } as any as Request; expect(() => { service.getTokenFromRequest(req); }).toThrow(); }); it('| should throw error when authorization header does not contain Bearer', () => { const req = { headers: { authorization: 'InvalidFormat token', }, } as any as Request; const result = service.getTokenFromRequest(req); expect(result).toBe('token'); }); it('| should handle token with spaces correctly', () => { const req = { headers: { authorization: 'Bearer token-with-spaces', }, } as any as Request; const result = service.getTokenFromRequest(req); expect(result).toBe('token-with-spaces'); }); it('| should include error code in thrown error', () => { const req = { headers: {}, } as any as Request; try { service.getTokenFromRequest(req); fail('Should have thrown an error'); } catch (error) { expect(error).toBeInstanceOf(DyFM_Error); expect((error as DyFM_Error)._errorCode).toContain('DyNTS-AS0-GT0'); } }); it('| should include confidential content in error when header missing', () => { const req = { headers: { 'x-custom-header': 'value', }, } as any as Request; try { service.getTokenFromRequest(req); fail('Should have thrown an error'); } catch (error) { expect(error).toBeInstanceOf(DyFM_Error); expect((error as DyFM_Error).confidentialContent).toBeDefined(); } }); }); });