This commit is contained in:
2025-12-12 21:39:48 +01:00
parent ddbd99b747
commit cae81b1088
49 changed files with 777 additions and 269 deletions

View File

@@ -3,7 +3,7 @@ import { CheckAuthenticationUseCase } from '../../../../packages/automation/appl
import { AuthenticationState } from '@gridpilot/automation/domain/value-objects/AuthenticationState';
import { BrowserAuthenticationState } from '@gridpilot/automation/domain/value-objects/BrowserAuthenticationState';
import { Result } from '../../../../packages/shared/result/Result';
import type { IAuthenticationService } from '../../../../packages/automation/application/ports/IAuthenticationService';
import type { AuthenticationServicePort } from '../../../../packages/automation/application/ports/AuthenticationServicePort';
interface ISessionValidator {
validateSession(): Promise<Result<boolean>>;
@@ -44,7 +44,7 @@ describe('CheckAuthenticationUseCase', () => {
describe('File-based validation only', () => {
it('should return AUTHENTICATED when cookies are valid', async () => {
const useCase = new CheckAuthenticationUseCase(
mockAuthService as unknown as IAuthenticationService
mockAuthService as unknown as AuthenticationServicePort
);
mockAuthService.checkSession.mockResolvedValue(
@@ -63,7 +63,7 @@ describe('CheckAuthenticationUseCase', () => {
it('should return EXPIRED when cookies are expired', async () => {
const useCase = new CheckAuthenticationUseCase(
mockAuthService as unknown as IAuthenticationService
mockAuthService as unknown as AuthenticationServicePort
);
mockAuthService.checkSession.mockResolvedValue(
@@ -81,7 +81,7 @@ describe('CheckAuthenticationUseCase', () => {
it('should return UNKNOWN when no session exists', async () => {
const useCase = new CheckAuthenticationUseCase(
mockAuthService as unknown as IAuthenticationService
mockAuthService as unknown as AuthenticationServicePort
);
mockAuthService.checkSession.mockResolvedValue(
@@ -101,7 +101,7 @@ describe('CheckAuthenticationUseCase', () => {
describe('Server-side validation enabled', () => {
it('should confirm AUTHENTICATED when file and server both validate', async () => {
const useCase = new CheckAuthenticationUseCase(
mockAuthService as unknown as IAuthenticationService,
mockAuthService as unknown as AuthenticationServicePort,
mockSessionValidator as unknown as ISessionValidator
);
@@ -124,7 +124,7 @@ describe('CheckAuthenticationUseCase', () => {
it('should return EXPIRED when file says valid but server rejects', async () => {
const useCase = new CheckAuthenticationUseCase(
mockAuthService as unknown as IAuthenticationService,
mockAuthService as unknown as AuthenticationServicePort,
mockSessionValidator as unknown as ISessionValidator
);
@@ -146,7 +146,7 @@ describe('CheckAuthenticationUseCase', () => {
it('should work without ISessionValidator injected (optional dependency)', async () => {
const useCase = new CheckAuthenticationUseCase(
mockAuthService as unknown as IAuthenticationService
mockAuthService as unknown as AuthenticationServicePort
);
mockAuthService.checkSession.mockResolvedValue(
@@ -166,7 +166,7 @@ describe('CheckAuthenticationUseCase', () => {
describe('Error handling', () => {
it('should not block file-based result if server validation fails', async () => {
const useCase = new CheckAuthenticationUseCase(
mockAuthService as unknown as IAuthenticationService,
mockAuthService as unknown as AuthenticationServicePort,
mockSessionValidator as unknown as ISessionValidator
);
@@ -188,7 +188,7 @@ describe('CheckAuthenticationUseCase', () => {
it('should handle authentication service errors gracefully', async () => {
const useCase = new CheckAuthenticationUseCase(
mockAuthService as unknown as IAuthenticationService
mockAuthService as unknown as AuthenticationServicePort
);
mockAuthService.checkSession.mockResolvedValue(
@@ -203,7 +203,7 @@ describe('CheckAuthenticationUseCase', () => {
it('should handle session expiry check errors gracefully', async () => {
const useCase = new CheckAuthenticationUseCase(
mockAuthService as unknown as IAuthenticationService
mockAuthService as unknown as AuthenticationServicePort
);
mockAuthService.checkSession.mockResolvedValue(
@@ -224,7 +224,7 @@ describe('CheckAuthenticationUseCase', () => {
describe('Page content verification', () => {
it('should call verifyPageAuthentication when verifyPageContent is true', async () => {
const useCase = new CheckAuthenticationUseCase(
mockAuthService as unknown as IAuthenticationService
mockAuthService as unknown as AuthenticationServicePort
);
mockAuthService.checkSession.mockResolvedValue(
@@ -244,7 +244,7 @@ describe('CheckAuthenticationUseCase', () => {
it('should return EXPIRED when cookies valid but page shows login UI', async () => {
const useCase = new CheckAuthenticationUseCase(
mockAuthService as unknown as IAuthenticationService
mockAuthService as unknown as AuthenticationServicePort
);
mockAuthService.checkSession.mockResolvedValue(
@@ -265,7 +265,7 @@ describe('CheckAuthenticationUseCase', () => {
it('should return AUTHENTICATED when both cookies AND page authenticated', async () => {
const useCase = new CheckAuthenticationUseCase(
mockAuthService as unknown as IAuthenticationService
mockAuthService as unknown as AuthenticationServicePort
);
mockAuthService.checkSession.mockResolvedValue(
@@ -286,7 +286,7 @@ describe('CheckAuthenticationUseCase', () => {
it('should default verifyPageContent to false (backward compatible)', async () => {
const useCase = new CheckAuthenticationUseCase(
mockAuthService as unknown as IAuthenticationService
mockAuthService as unknown as AuthenticationServicePort
);
mockAuthService.checkSession.mockResolvedValue(
@@ -304,7 +304,7 @@ describe('CheckAuthenticationUseCase', () => {
it('should handle verifyPageAuthentication errors gracefully', async () => {
const useCase = new CheckAuthenticationUseCase(
mockAuthService as unknown as IAuthenticationService
mockAuthService as unknown as AuthenticationServicePort
);
mockAuthService.checkSession.mockResolvedValue(
@@ -328,7 +328,7 @@ describe('CheckAuthenticationUseCase', () => {
describe('BDD Scenarios', () => {
it('Given valid session cookies, When checking auth, Then return AUTHENTICATED', async () => {
const useCase = new CheckAuthenticationUseCase(
mockAuthService as unknown as IAuthenticationService
mockAuthService as unknown as AuthenticationServicePort
);
mockAuthService.checkSession.mockResolvedValue(
@@ -345,7 +345,7 @@ describe('CheckAuthenticationUseCase', () => {
it('Given expired session cookies, When checking auth, Then return EXPIRED', async () => {
const useCase = new CheckAuthenticationUseCase(
mockAuthService as unknown as IAuthenticationService
mockAuthService as unknown as AuthenticationServicePort
);
mockAuthService.checkSession.mockResolvedValue(
@@ -362,7 +362,7 @@ describe('CheckAuthenticationUseCase', () => {
it('Given no session file, When checking auth, Then return UNKNOWN', async () => {
const useCase = new CheckAuthenticationUseCase(
mockAuthService as unknown as IAuthenticationService
mockAuthService as unknown as AuthenticationServicePort
);
mockAuthService.checkSession.mockResolvedValue(
@@ -379,7 +379,7 @@ describe('CheckAuthenticationUseCase', () => {
it('Given valid cookies but page shows login, When verifying page content, Then return EXPIRED', async () => {
const useCase = new CheckAuthenticationUseCase(
mockAuthService as unknown as IAuthenticationService
mockAuthService as unknown as AuthenticationServicePort
);
mockAuthService.checkSession.mockResolvedValue(