Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: TTA-141 Fix broken tests and test coverage in the UI
  • Loading branch information
jimmyjaramillo committed Aug 30, 2022
commit 321cae2da9ddfacde75add75d556ee0ada3fee66
16 changes: 16 additions & 0 deletions src/app/modules/login/login.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoginComponent>;
let azureAdB2CService: AzureAdB2CService;
let loginService: LoginService;
let userService: UserService;
let featureToggleCookiesService: FeatureToggleCookiesService;

const azureAdB2CServiceStub = {
Expand All @@ -38,6 +41,16 @@ describe('LoginComponent', () => {
}
};

const userTest = {
name: 'user',
email: '[email protected]',
roles: ['admin'],
groups: ['admin'],
id: 'user_id',
tenant_id: 'tenant_test',
deleted: 'no',
};

const featureToggleCookiesServiceStub = {
setCookies() {
return null;
Expand Down Expand Up @@ -66,6 +79,7 @@ describe('LoginComponent', () => {
fixture.detectChanges();
azureAdB2CService = TestBed.inject(AzureAdB2CService);
loginService = TestBed.inject(LoginService);
userService = TestBed.inject(UserService);
featureToggleCookiesService = TestBed.inject(FeatureToggleCookiesService);
});

Expand All @@ -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();
Expand All @@ -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) => {
Expand Down
10 changes: 5 additions & 5 deletions src/app/modules/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(['']);
})
});
});
}
}
Expand Down
58 changes: 50 additions & 8 deletions src/app/modules/login/services/login.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 = () => {
Expand All @@ -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 = {};
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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);
Expand All @@ -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();
});
});
2 changes: 1 addition & 1 deletion src/app/modules/time-clock/pages/time-clock.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/app/modules/time-clock/services/entry.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export class EntryService {
}

stopEntryRunning(idEntry: string): Observable<any> {
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<Entry> {
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/time-clock/store/entry.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/time-clock/store/entry.selectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions src/app/modules/user/services/user-info.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

});