diff --git a/src/app/modules/login/login.component.spec.ts b/src/app/modules/login/login.component.spec.ts index 717bde7a1..9de359249 100644 --- a/src/app/modules/login/login.component.spec.ts +++ b/src/app/modules/login/login.component.spec.ts @@ -8,12 +8,15 @@ import { FeatureToggleCookiesService } from '../shared/feature-toggles/feature-t import { LoginService } from './services/login.service'; import { HttpClientTestingModule } from '@angular/common/http/testing'; import { SocialAuthService } from 'angularx-social-login'; +import { UserService } from '../user/services/user.service'; + describe('LoginComponent', () => { let component: LoginComponent; let fixture: ComponentFixture; let azureAdB2CService: AzureAdB2CService; let loginService: LoginService; + let userService: UserService; let featureToggleCookiesService: FeatureToggleCookiesService; const azureAdB2CServiceStub = { @@ -38,6 +41,16 @@ describe('LoginComponent', () => { } }; + const userTest = { + name: 'user', + email: 'test@test.com', + roles: ['admin'], + groups: ['admin'], + id: 'user_id', + tenant_id: 'tenant_test', + deleted: 'no', + }; + const featureToggleCookiesServiceStub = { setCookies() { return null; @@ -66,6 +79,7 @@ describe('LoginComponent', () => { fixture.detectChanges(); azureAdB2CService = TestBed.inject(AzureAdB2CService); loginService = TestBed.inject(LoginService); + userService = TestBed.inject(UserService); featureToggleCookiesService = TestBed.inject(FeatureToggleCookiesService); }); @@ -90,6 +104,7 @@ describe('LoginComponent', () => { spyOn(azureAdB2CService, 'setCookies').and.returnValue(); spyOn(azureAdB2CService, 'signIn').and.returnValue(of(() => {})); spyOn(azureAdB2CService, 'getUserId').and.returnValue('userId_123'); + spyOn(userService, 'loadUser').withArgs('userId_123').and.returnValue(of(userTest)); spyOn(featureToggleCookiesService, 'setCookies').and.returnValue(featureToggleCookiesService.setCookies()); component.login(); @@ -98,6 +113,7 @@ describe('LoginComponent', () => { expect(azureAdB2CService.setCookies).toHaveBeenCalled(); expect(azureAdB2CService.getUserId).toHaveBeenCalled(); expect(featureToggleCookiesService.setCookies).toHaveBeenCalled(); + expect(userService.loadUser).toHaveBeenCalledWith('userId_123'); })); it('should not sign-up or login with google if is already logged-in into the app on Production', inject([Router], (router: Router) => { diff --git a/src/app/modules/login/login.component.ts b/src/app/modules/login/login.component.ts index 1e26d139a..738a9ec17 100644 --- a/src/app/modules/login/login.component.ts +++ b/src/app/modules/login/login.component.ts @@ -89,14 +89,14 @@ export class LoginComponent implements OnInit { this.azureAdB2CService.signIn().subscribe(() => { this.featureToggleCookiesService.setCookies(); this.azureAdB2CService.setCookies(); - const userId = this.azureAdB2CService.getUserId() + const userId = this.azureAdB2CService.getUserId(); this.userService.loadUser(userId).subscribe((user) => { - const user_groups = { + const userGroups = { groups: user.groups - } - this.loginService.setLocalStorage('user', JSON.stringify(user_groups)); + }; + this.loginService.setLocalStorage('user', JSON.stringify(userGroups)); this.router.navigate(['']); - }) + }); }); } } diff --git a/src/app/modules/login/services/login.service.spec.ts b/src/app/modules/login/services/login.service.spec.ts index 6530662b0..94e246cc6 100644 --- a/src/app/modules/login/services/login.service.spec.ts +++ b/src/app/modules/login/services/login.service.spec.ts @@ -1,4 +1,5 @@ -import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { HttpClient } from '@angular/common/http'; +import { HttpClientTestingModule } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; import { JwtHelperService } from '@auth0/angular-jwt'; import { SocialAuthService } from 'angularx-social-login'; @@ -9,11 +10,11 @@ import { LoginService } from './login.service'; describe('LoginService', () => { let service: LoginService; - let httpMock: HttpTestingController; let cookieService: CookieService; let socialAuthService: SocialAuthService; let account; const socialAuthServiceStub = jasmine.createSpyObj('SocialAuthService', ['signOut', 'signIn']); + const httpClientSpy = jasmine.createSpyObj('HttpClient', ['post', 'get']); const cookieStoreStub = {}; const helper = new JwtHelperService(); const getAccountInfo = () => { @@ -26,11 +27,11 @@ describe('LoginService', () => { providers: [ { providers: CookieService, useValue: cookieStoreStub }, { provide: SocialAuthService, useValue: socialAuthServiceStub }, + { provide: HttpClient, useValue: httpClientSpy } ], }); service = TestBed.inject(LoginService); cookieService = TestBed.inject(CookieService); - httpMock = TestBed.inject(HttpTestingController); socialAuthService = TestBed.inject(SocialAuthService); account = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImFiYyIsIm5hbWUiOiJhYmMiLCJlbWFpbCI6ImFiYyIsImdyb3VwcyI6WyJhYmMiXX0.UNxyDT8XzXJhI1F3LySBU7TJlpENPUPHj8my7Obw2ZM'; let store = {}; @@ -49,6 +50,7 @@ describe('LoginService', () => { spyOn(localStorage, 'setItem').and.callFake(mockLocalStorage.setItem); spyOn(localStorage, 'clear').and.callFake(mockLocalStorage.clear); localStorage.setItem('user', account); + localStorage.setItem('user2', '"test_token_123"'); }); it('should be created', () => { @@ -90,12 +92,16 @@ describe('LoginService', () => { }); it('load a user by sending a token using POST', () => { + const token = 'test_123'; service.baseUrl = '/users'; - service.getUser('token').subscribe(); - - const loadUserRequest = httpMock.expectOne(`${service.baseUrl}/login`); - expect(loadUserRequest.request.method).toBe('POST'); - }); + const mockSuccessDataPost = { + SUCCESS: true, + data: {} + }; + httpClientSpy.post.and.returnValue(of(mockSuccessDataPost)); + service.getUser(token).subscribe(); + expect(httpClientSpy.post).toHaveBeenCalled(); + }); it('should return true when user is Login', () => { spyOn(cookieService, 'check').and.returnValue(true); @@ -122,4 +128,40 @@ describe('LoginService', () => { expect(localStorage.clear).toHaveBeenCalled(); expect(cookieService.deleteAll).toHaveBeenCalled(); }); + + it('should call cookieService when app is isLegacyProd', () => { + service.isLegacyProd = true; + service.localStorageKey = 'user2'; + spyOn(cookieService, 'check').and.returnValue(true); + spyOn(service, 'isValidToken').and.returnValue(of(true)); + service.isLogin().subscribe(isLogin => { + expect(cookieService.check).toHaveBeenCalled(); + }); + }); + + it('should call JSON parse when app is isLegacyProd', () => { + spyOn(JSON, 'parse').and.returnValue('test_user_123'); + service.isLegacyProd = true; + service.localStorageKey = 'user2'; + service.getUserId(); + service.getName(); + service.getUserEmail(); + service.getUserGroup(); + expect(JSON.parse).toHaveBeenCalled(); + }); + + it('should call setLocalStorage when there is a new_token ', () => { + spyOn(cookieService, 'check').and.returnValue(true); + spyOn(service, 'setLocalStorage'); + const token = 'test123'; + service.baseUrl = '/users'; + const mockSuccessDataPost = { + SUCCESS: true, + new_token: 'test_token' + }; + httpClientSpy.post.and.returnValue(of(mockSuccessDataPost)); + service.isValidToken(token).subscribe(); + expect(service.setLocalStorage).toHaveBeenCalled(); + expect(cookieService.check).toHaveBeenCalled(); + }); }); diff --git a/src/app/modules/time-clock/pages/time-clock.component.ts b/src/app/modules/time-clock/pages/time-clock.component.ts index c24f2b6ca..42078d5f3 100644 --- a/src/app/modules/time-clock/pages/time-clock.component.ts +++ b/src/app/modules/time-clock/pages/time-clock.component.ts @@ -43,7 +43,7 @@ export class TimeClockComponent implements OnInit, OnDestroy { }else{ this.loginService.isLogin().subscribe(isLogin => { this.username = isLogin ? this.loginService.getName() : ''; - }) + }); } this.storeSubscription = this.store.pipe(select(getActiveTimeEntry)).subscribe((activeTimeEntry) => { this.activeTimeEntry = activeTimeEntry; diff --git a/src/app/modules/time-clock/services/entry.service.ts b/src/app/modules/time-clock/services/entry.service.ts index 125041202..e8b9a1b63 100644 --- a/src/app/modules/time-clock/services/entry.service.ts +++ b/src/app/modules/time-clock/services/entry.service.ts @@ -46,7 +46,8 @@ export class EntryService { } stopEntryRunning(idEntry: string): Observable { - return (this.urlInProductionLegacy ? this.http.post(`${this.baseUrl}/${idEntry}/stop`, null) : this.http.put(`${this.baseUrl}/stop`, null) ); + return (this.urlInProductionLegacy ? + this.http.post(`${this.baseUrl}/${idEntry}/stop`, null) : this.http.put(`${this.baseUrl}/stop`, null) ); } restartEntry(idEntry: string): Observable { diff --git a/src/app/modules/time-clock/store/entry.reducer.ts b/src/app/modules/time-clock/store/entry.reducer.ts index d3af715f8..023328efb 100644 --- a/src/app/modules/time-clock/store/entry.reducer.ts +++ b/src/app/modules/time-clock/store/entry.reducer.ts @@ -266,7 +266,7 @@ export const entryReducer = (state: EntryState = initialState, action: EntryActi return { ...state, isLoading: true, - resultSumEntriesSelected:{hours:0, minutes:0, seconds:0}, + resultSumEntriesSelected: {hours: 0, minutes: 0, seconds: 0}, reportDataSource: { data: [], isLoading: true diff --git a/src/app/modules/time-clock/store/entry.selectors.spec.ts b/src/app/modules/time-clock/store/entry.selectors.spec.ts index 06c3c02e2..1d5c088c0 100644 --- a/src/app/modules/time-clock/store/entry.selectors.spec.ts +++ b/src/app/modules/time-clock/store/entry.selectors.spec.ts @@ -55,7 +55,7 @@ describe('Entry selectors', () => { }); it('should select resultSumEntriesSelected', () => { - const resultSumEntriesSelected:TotalHours = { hours:0, minutes:0, seconds:0 }; + const resultSumEntriesSelected: TotalHours = { hours: 0, minutes: 0, seconds: 0 }; const entryState = { resultSumEntriesSelected }; expect(selectors.getResultSumEntriesSelected.projector(entryState)).toEqual(resultSumEntriesSelected); diff --git a/src/app/modules/user/services/user-info.service.spec.ts b/src/app/modules/user/services/user-info.service.spec.ts index 79d2ef782..04f17c586 100644 --- a/src/app/modules/user/services/user-info.service.spec.ts +++ b/src/app/modules/user/services/user-info.service.spec.ts @@ -75,4 +75,13 @@ describe('UserInfoService', () => { }); }); + it('should return true if is Admin and isLegacyProduction', () => { + const groupsTT = {groups: ['fake-admin', 'fake-admin-tt']}; + spyOn(mockLoginService, 'getLocalStorage').and.returnValue(JSON.stringify(groupsTT)); + service.isLegacyProduction = true; + service.isMemberOf('fake-admin').subscribe((value) => { + expect(value).toEqual(true); + }); + }); + });