diff --git a/ui/src/app/account/login/login.component.spec.ts b/ui/src/app/account/login/login.component.spec.ts index 10eca249d..fc6644fcf 100644 --- a/ui/src/app/account/login/login.component.spec.ts +++ b/ui/src/app/account/login/login.component.spec.ts @@ -1,23 +1,144 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; - -import { LoginComponent } from './login.component'; +import { + ComponentFixture, + TestBed, + tick, + fakeAsync, +} from '@angular/core/testing' +import { LoginComponent } from './login.component' +import { ReactiveFormsModule } from '@angular/forms' +import { RouterTestingModule } from '@angular/router/testing' +import { LoginService } from '../service/login.service' +import { StateStorageService } from '../service/state-storage.service' +import { AccountService } from '../service/account.service' +import { of, throwError } from 'rxjs' describe('LoginComponent', () => { - let component: LoginComponent; - let fixture: ComponentFixture; + let component: LoginComponent + let fixture: ComponentFixture + let loginService: jasmine.SpyObj + let stateStorageService: jasmine.SpyObj + let accountService: jasmine.SpyObj - beforeEach(async () => { - await TestBed.configureTestingModule({ - declarations: [ LoginComponent ] - }) - .compileComponents(); + beforeEach(() => { + const loginServiceSpy = jasmine.createSpyObj('LoginService', [ + 'login', + 'logout', + ]) + const stateStorageServiceSpy = jasmine.createSpyObj('StateStorageService', [ + 'getUrl', + 'storeUrl', + ]) + const accountServiceSpy = jasmine.createSpyObj('AccountService', [ + 'getAccountData', + ]) - fixture = TestBed.createComponent(LoginComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); + TestBed.configureTestingModule({ + declarations: [LoginComponent], + imports: [ReactiveFormsModule, RouterTestingModule], + providers: [ + { provide: LoginService, useValue: loginServiceSpy }, + { provide: StateStorageService, useValue: stateStorageServiceSpy }, + { provide: AccountService, useValue: accountServiceSpy }, + ], + }).compileComponents() + + fixture = TestBed.createComponent(LoginComponent) + component = fixture.componentInstance + loginService = TestBed.inject(LoginService) as jasmine.SpyObj + stateStorageService = TestBed.inject( + StateStorageService + ) as jasmine.SpyObj + accountService = TestBed.inject( + AccountService + ) as jasmine.SpyObj + }) it('should create', () => { - expect(component).toBeTruthy(); - }); -}); + expect(component).toBeTruthy() + }) + + it('should call loginService.login and handle successful login', fakeAsync(() => { + const mockLoginResult = { mfaRequired: false } + loginService.login.and.returnValue(of(mockLoginResult)) + accountService.getAccountData.and.returnValue( + of({ + activated: true, + authorities: ['test', 'test'], + email: 'email@email.com', + firstName: 'name', + langKey: 'en', + lastName: 'surname', + imageUrl: 'url', + salesforceId: 'sfid', + loggedAs: false, + loginAs: 'sfid', + mainContact: false, + mfaEnabled: false, + }) + ) + + component.loginForm.patchValue({ + username: 'testuser', + password: 'testpassword', + mfaCode: '', + }) + + component.login() + + tick() // Wait for Observable to emit + + expect(component.showMfa).toBe(false) + expect(component.authenticationError).toBe(false) + })) + + it('should handle MFA required', fakeAsync(() => { + const mockLoginResult = { mfaRequired: true } + loginService.login.and.returnValue(of(mockLoginResult)) + + component.loginForm.patchValue({ + username: 'testuser', + password: 'testpassword', + mfaCode: '', + }) + + component.login() + + tick() // Wait for Observable to emit + + expect(component.showMfa).toBe(true) + })) + + it('should handle MFA code error', fakeAsync(() => { + const mockLoginResult = { mfaRequired: true } + loginService.login.and.returnValue(of(mockLoginResult)) + + component.loginForm.patchValue({ + username: 'testuser', + password: 'testpassword', + mfaCode: 'invalidCode', + }) + + component.login() + + tick() // Wait for Observable to emit + + expect(component.showMfa).toBe(true) + expect(component.mfaError).toBe(true) + })) + + it('should handle login error', fakeAsync(() => { + loginService.login.and.returnValue(throwError('Error')) + + component.loginForm.patchValue({ + username: 'testuser', + password: 'testpassword', + mfaCode: '', + }) + + component.login() + + tick() // Wait for Observable to emit + + expect(component.authenticationError).toBe(true) + })) +}) diff --git a/ui/src/app/app.component.spec.ts b/ui/src/app/app.component.spec.ts index 2c21743bf..2f0ba86ee 100644 --- a/ui/src/app/app.component.spec.ts +++ b/ui/src/app/app.component.spec.ts @@ -28,10 +28,5 @@ describe('AppComponent', () => { expect(app.title).toEqual('gateway'); }); - it('should render title', () => { - const fixture = TestBed.createComponent(AppComponent); - fixture.detectChanges(); - const compiled = fixture.nativeElement as HTMLElement; - expect(compiled.querySelector('.content span')?.textContent).toContain('gateway app is running!'); - }); + });