Skip to content

Commit 2085ba7

Browse files
fix: TTA-141 Fix broken tests and test coverage in the UI
1 parent 399ac39 commit 2085ba7

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

src/app/modules/login/login.component.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ import { FeatureToggleCookiesService } from '../shared/feature-toggles/feature-t
88
import { LoginService } from './services/login.service';
99
import { HttpClientTestingModule } from '@angular/common/http/testing';
1010
import { SocialAuthService } from 'angularx-social-login';
11+
import { UserService } from '../user/services/user.service';
12+
1113

1214
describe('LoginComponent', () => {
1315
let component: LoginComponent;
1416
let fixture: ComponentFixture<LoginComponent>;
1517
let azureAdB2CService: AzureAdB2CService;
1618
let loginService: LoginService;
19+
let userService: UserService;
1720
let featureToggleCookiesService: FeatureToggleCookiesService;
1821

1922
const azureAdB2CServiceStub = {
@@ -38,6 +41,16 @@ describe('LoginComponent', () => {
3841
}
3942
};
4043

44+
const userTest = {
45+
name: 'user',
46+
47+
roles: ['admin'],
48+
groups: ['admin'],
49+
id: 'user_id',
50+
tenant_id: 'tenant_test',
51+
deleted: 'no',
52+
}
53+
4154
const featureToggleCookiesServiceStub = {
4255
setCookies() {
4356
return null;
@@ -66,6 +79,7 @@ describe('LoginComponent', () => {
6679
fixture.detectChanges();
6780
azureAdB2CService = TestBed.inject(AzureAdB2CService);
6881
loginService = TestBed.inject(LoginService);
82+
userService = TestBed.inject(UserService);
6983
featureToggleCookiesService = TestBed.inject(FeatureToggleCookiesService);
7084
});
7185

@@ -90,6 +104,7 @@ describe('LoginComponent', () => {
90104
spyOn(azureAdB2CService, 'setCookies').and.returnValue();
91105
spyOn(azureAdB2CService, 'signIn').and.returnValue(of(() => {}));
92106
spyOn(azureAdB2CService, 'getUserId').and.returnValue('userId_123');
107+
spyOn(userService, 'loadUser').withArgs('userId_123').and.returnValue(of(userTest));
93108
spyOn(featureToggleCookiesService, 'setCookies').and.returnValue(featureToggleCookiesService.setCookies());
94109

95110
component.login();
@@ -98,6 +113,7 @@ describe('LoginComponent', () => {
98113
expect(azureAdB2CService.setCookies).toHaveBeenCalled();
99114
expect(azureAdB2CService.getUserId).toHaveBeenCalled();
100115
expect(featureToggleCookiesService.setCookies).toHaveBeenCalled();
116+
expect(userService.loadUser).toHaveBeenCalledWith('userId_123');
101117
}));
102118

103119
it('should sign up or login with google if is not logged-in into the app Locally', inject([Router], (router: Router) => {

src/app/modules/login/services/login.service.spec.ts

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { HttpClient } from '@angular/common/http';
12
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
23
import { TestBed } from '@angular/core/testing';
34
import { JwtHelperService } from '@auth0/angular-jwt';
@@ -14,6 +15,7 @@ describe('LoginService', () => {
1415
let socialAuthService: SocialAuthService;
1516
let account;
1617
const socialAuthServiceStub = jasmine.createSpyObj('SocialAuthService', ['signOut', 'signIn']);
18+
const httpClientSpy = jasmine.createSpyObj('HttpClient', ['post', 'get']);
1719
const cookieStoreStub = {};
1820
const helper = new JwtHelperService();
1921
const getAccountInfo = () => {
@@ -26,6 +28,7 @@ describe('LoginService', () => {
2628
providers: [
2729
{ providers: CookieService, useValue: cookieStoreStub },
2830
{ provide: SocialAuthService, useValue: socialAuthServiceStub },
31+
{ provide: HttpClient, useValue: httpClientSpy }
2932
],
3033
});
3134
service = TestBed.inject(LoginService);
@@ -49,6 +52,7 @@ describe('LoginService', () => {
4952
spyOn(localStorage, 'setItem').and.callFake(mockLocalStorage.setItem);
5053
spyOn(localStorage, 'clear').and.callFake(mockLocalStorage.clear);
5154
localStorage.setItem('user', account);
55+
localStorage.setItem('user2', '"test_token_123"');
5256
});
5357

5458
it('should be created', () => {
@@ -90,12 +94,16 @@ describe('LoginService', () => {
9094
});
9195

9296
it('load a user by sending a token using POST', () => {
97+
const token = 'test_123'
9398
service.baseUrl = '/users';
99+
const mockSuccessDataPost = {
100+
SUCCESS: true,
101+
data: {}
102+
}
103+
httpClientSpy.post.and.returnValue(of(mockSuccessDataPost));
94104
service.getUser('token').subscribe();
95-
96-
const loadUserRequest = httpMock.expectOne(`${service.baseUrl}/login`);
97-
expect(loadUserRequest.request.method).toBe('POST');
98-
});
105+
expect(httpClientSpy.post).toHaveBeenCalled();
106+
});
99107

100108
it('should return true when user is Login', () => {
101109
spyOn(cookieService, 'check').and.returnValue(true);
@@ -128,4 +136,40 @@ describe('LoginService', () => {
128136
expect(localStorage.clear).toHaveBeenCalled();
129137
expect(cookieService.deleteAll).toHaveBeenCalled();
130138
});
139+
140+
it('should call cookieService when app is isLegacyProd', () => {
141+
service.isLegacyProd = true;
142+
service.localStorageKey = 'user2';
143+
spyOn(cookieService, 'check').and.returnValue(true);
144+
spyOn(service, 'isValidToken').and.returnValue(of(true));
145+
service.isLogin().subscribe(isLogin => {
146+
expect(cookieService.check).toHaveBeenCalled();
147+
});
148+
});
149+
150+
it('should call JSON parse when app is isLegacyProd', () => {
151+
spyOn(JSON, 'parse').and.returnValue('test_user_123');
152+
service.isLegacyProd = true;
153+
service.localStorageKey = 'user2';
154+
service.getUserId();
155+
service.getName();
156+
service.getUserEmail();
157+
service.getUserGroup();
158+
expect(JSON.parse).toHaveBeenCalled();
159+
});
160+
161+
it('should call setLocalStorage when there is a new_token ', () => {
162+
spyOn(cookieService, 'check').and.returnValue(true);
163+
spyOn(service, 'setLocalStorage');
164+
const token = 'test123'
165+
service.baseUrl = '/users';
166+
const mockSuccessDataPost = {
167+
SUCCESS: true,
168+
new_token: 'test_token'
169+
}
170+
httpClientSpy.post.and.returnValue(of(mockSuccessDataPost));
171+
service.isValidToken(token).subscribe();
172+
expect(service.setLocalStorage).toHaveBeenCalled();
173+
expect(cookieService.check).toHaveBeenCalled();
174+
});
131175
});

src/app/modules/user/services/user-info.service.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,13 @@ describe('UserInfoService', () => {
7575
});
7676
});
7777

78+
it('should return true if is Admin and isLegacyProduction', () => {
79+
const groupsTT = {groups: ['fake-admin', 'fake-admin-tt']};
80+
spyOn(mockLoginService, 'getLocalStorage').and.returnValue(JSON.stringify(groupsTT));
81+
service.isLegacyProduction = true;
82+
service.isMemberOf('fake-admin').subscribe((value) => {
83+
expect(value).toEqual(true);
84+
});
85+
});
86+
7887
});

0 commit comments

Comments
 (0)