Skip to content

Commit 2d108a3

Browse files
committed
test: TT-453 fix test and add login test
1 parent 2899c6a commit 2d108a3

File tree

9 files changed

+229
-37
lines changed

9 files changed

+229
-37
lines changed

src/app/app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ const maskConfig: Partial<IConfig> = {
205205
{
206206
id: GoogleLoginProvider.PROVIDER_ID,
207207
provider: new GoogleLoginProvider(
208-
'711486856840-f8u9pdmkk44nmkb0c9lbsjvolp2hulur.apps.googleusercontent.com'
208+
'565556796659-hscrj9e6m2krc41cfng898793ocfnb8j.apps.googleusercontent.com'
209209
)
210210
}
211211
]

src/app/guards/login-guard/login.guard.spec.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('LoginGuard', () => {
2424
return true;
2525
}
2626
};
27-
const socialAuthServiceStub = jasmine.createSpyObj('SocialAuthService', ['authState']);
27+
const socialAuthServiceStub = jasmine.createSpyObj('SocialAuthService', ['']);
2828
beforeEach(() => {
2929
TestBed.configureTestingModule({
3030
imports: [ RouterTestingModule, HttpClientTestingModule ],
@@ -43,16 +43,23 @@ describe('LoginGuard', () => {
4343
expect(loginGuard).toBeTruthy();
4444
});
4545

46-
it('can activate the route when user is logged-in', () => {
46+
it('can activate the route when user is logged-in on Production', () => {
4747
loginGuard.isProduction = true;
4848
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
4949
const canActivate = loginGuard.canActivate();
5050
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
5151
expect(canActivate).toEqual(true);
5252
});
5353

54+
it('can activate the route when user is logged-in Locally', () => {
55+
loginGuard.isProduction = false;
56+
spyOn(loginService, 'isLogin').and.returnValue(true);
57+
const canActivate = loginGuard.canActivate();
58+
expect(loginService.isLogin).toHaveBeenCalled();
59+
expect(canActivate).toEqual(true);
60+
});
5461

55-
it('can not active the route and is redirected to login if user is not logged-in', inject([Router], (router: Router) => {
62+
it('can not active the route and is redirected to login if user is not logged-in on Production', inject([Router], (router: Router) => {
5663
loginGuard.isProduction = true;
5764
spyOn(azureAdB2CService, 'isLogin').and.returnValue(false);
5865
spyOn(router, 'navigate').and.stub();
@@ -62,4 +69,14 @@ describe('LoginGuard', () => {
6269
expect(router.navigate).toHaveBeenCalledWith(['login']);
6370
}));
6471

72+
it('can not active the route and is redirected to login if user is not logged-in Locally', inject([Router], (router: Router) => {
73+
loginGuard.isProduction = false;
74+
spyOn(loginService, 'isLogin').and.returnValue(false);
75+
spyOn(router, 'navigate').and.stub();
76+
const canActivate = loginGuard.canActivate();
77+
expect(loginService.isLogin).toHaveBeenCalled();
78+
expect(canActivate).toEqual(false);
79+
expect(router.navigate).toHaveBeenCalledWith(['login']);
80+
}));
81+
6582
});

src/app/modules/login/login.component.html

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,8 @@
77
<h3>Please log in</h3>
88
</div>
99

10-
<div class="login-controls" *ngIf="!isDevelopment">
11-
<button (click)="login()" class="btn btn-primary">login</button>
12-
</div>
13-
14-
<div class="container" style="max-width: 550px" *ngIf="isDevelopment">
15-
<div >
16-
<div class="login-controls">
17-
<button type="button" (click)="loginWithGoogle()" class="btn btn-primary">Login with Google</button>
18-
</div>
19-
</div>
10+
<div class="login-controls">
11+
<button (click)="isProduction ? login() : loginWithGoogle() " class="btn btn-primary">login</button>
2012
</div>
2113

2214
</div>

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('LoginComponent', () => {
4444
}
4545
};
4646

47-
const socialAuthServiceStub = jasmine.createSpyObj('SocialAuthService', ['authState']);
47+
const socialAuthServiceStub = jasmine.createSpyObj('SocialAuthService', ['signIn', 'authState']);
4848
beforeEach(waitForAsync(() => {
4949
TestBed.configureTestingModule({
5050
imports: [ RouterTestingModule, HttpClientTestingModule],
@@ -60,10 +60,9 @@ describe('LoginComponent', () => {
6060
}));
6161

6262
beforeEach(() => {
63-
socialAuthServiceStub.authState = of(null);
63+
socialAuthServiceStub.authState = of('some value');
6464
fixture = TestBed.createComponent(LoginComponent);
6565
component = fixture.componentInstance;
66-
component.isDevelopment = false;
6766
fixture.detectChanges();
6867
azureAdB2CService = TestBed.inject(AzureAdB2CService);
6968
loginService = TestBed.inject(LoginService);
@@ -86,7 +85,7 @@ describe('LoginComponent', () => {
8685
expect(component).toBeTruthy();
8786
});
8887

89-
it('should sign up or login with google if is not logged-in into the app', inject([Router], (router: Router) => {
88+
it('should sign up or login with google if is not logged-in into the app on Production', inject([Router], (router: Router) => {
9089
spyOn(azureAdB2CService, 'isLogin').and.returnValue(false);
9190
spyOn(azureAdB2CService, 'setCookies').and.returnValue();
9291
spyOn(azureAdB2CService, 'signIn').and.returnValue(of(() => {}));
@@ -99,11 +98,35 @@ describe('LoginComponent', () => {
9998
expect(featureToggleCookiesService.setCookies).toHaveBeenCalled();
10099
}));
101100

102-
it('should not sign-up or login with google if is already logged-in into the app', inject([Router], (router: Router) => {
101+
it('should sign up or login with google if is not logged-in into the app Locally', inject([Router], (router: Router) => {
102+
spyOn(loginService, 'isLogin').and.returnValue(false);
103+
spyOn(loginService, 'setLocalStorage').and.returnValue();
104+
spyOn(loginService, 'getUser').and.returnValue(of(() => {}));
105+
spyOn(loginService, 'setCookies').and.returnValue();
106+
spyOn(loginService, 'signIn').and.returnValue();
107+
spyOn(featureToggleCookiesService, 'setCookies').and.returnValue(featureToggleCookiesService.setCookies());
108+
109+
component.ngOnInit();
110+
component.loginWithGoogle();
111+
112+
expect(loginService.signIn).toHaveBeenCalled();
113+
expect(loginService.setCookies).toHaveBeenCalled();
114+
expect(featureToggleCookiesService.setCookies).toHaveBeenCalled();
115+
}));
116+
117+
it('should not sign-up or login with google if is already logged-in into the app on Production', inject([Router], (router: Router) => {
103118
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
104119
spyOn(router, 'navigate').and.stub();
105120
component.login();
106121
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
107122
expect(router.navigate).toHaveBeenCalledWith(['']);
108123
}));
124+
125+
it('should not sign-up or login with google if is already logged-in into the app Locally', inject([Router], (router: Router) => {
126+
spyOn(loginService, 'isLogin').and.returnValue(true);
127+
spyOn(router, 'navigate').and.stub();
128+
component.loginWithGoogle();
129+
expect(loginService.isLogin).toHaveBeenCalled();
130+
expect(router.navigate).toHaveBeenCalledWith(['']);
131+
}));
109132
});

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component } from '@angular/core';
1+
import { Component, OnInit } from '@angular/core';
22
import { AzureAdB2CService } from './services/azure.ad.b2c.service';
33
import { Router } from '@angular/router';
44
import { FeatureToggleCookiesService } from '../shared/feature-toggles/feature-toggle-cookies/feature-toggle-cookies.service';
@@ -11,9 +11,9 @@ import { LoginService } from './services/login.service';
1111
templateUrl: './login.component.html',
1212
styleUrls: ['./login.component.scss'],
1313
})
14-
export class LoginComponent {
14+
export class LoginComponent implements OnInit {
1515
socialUser: SocialUser;
16-
isDevelopment = true;
16+
isProduction = environment.production;
1717

1818
constructor(
1919
private azureAdB2CService: AzureAdB2CService,
@@ -23,8 +23,7 @@ export class LoginComponent {
2323
private loginService?: LoginService
2424
) {}
2525

26-
OnInit() {
27-
this.isDevelopment = !environment.production;
26+
ngOnInit() {
2827
this.socialAuthService.authState.subscribe((user) => {
2928
if (user != null) {
3029
this.featureToggleCookiesService.setCookies();
@@ -49,12 +48,11 @@ export class LoginComponent {
4948
});
5049
}
5150
}
52-
53-
loginWithGoogle(): void {
54-
this.loginService.signIn();
55-
}
56-
57-
logOut(): void {
58-
this.loginService.logout();
51+
loginWithGoogle() {
52+
if (this.loginService.isLogin()) {
53+
this.router.navigate(['']);
54+
} else {
55+
this.loginService.signIn();
56+
}
5957
}
6058
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2+
import { TestBed } from '@angular/core/testing';
3+
import { SocialAuthService } from 'angularx-social-login';
4+
import { CookieService } from 'ngx-cookie-service';
5+
import { of } from 'rxjs';
6+
7+
import { LoginService } from './login.service';
8+
9+
describe('LoginService', () => {
10+
let service: LoginService;
11+
let httpMock: HttpTestingController;
12+
let cookieService: CookieService;
13+
let socialAuthService: SocialAuthService;
14+
let account;
15+
const socialAuthServiceStub = jasmine.createSpyObj('SocialAuthService', ['signOut', 'signIn']);
16+
const cookieStoreStub = {};
17+
18+
beforeEach(() => {
19+
TestBed.configureTestingModule({
20+
imports: [HttpClientTestingModule],
21+
providers: [
22+
{ providers: CookieService, useValue: cookieStoreStub },
23+
{ provide: SocialAuthService, useValue: socialAuthServiceStub },
24+
],
25+
});
26+
service = TestBed.inject(LoginService);
27+
cookieService = TestBed.inject(CookieService);
28+
httpMock = TestBed.inject(HttpTestingController);
29+
socialAuthService = TestBed.inject(SocialAuthService);
30+
account = {
31+
id: 'abc',
32+
name: 'abc',
33+
email: 'abc',
34+
groups: ['abc'],
35+
};
36+
let store = {};
37+
const mockLocalStorage = {
38+
getItem: (key: string): string => {
39+
return key in store ? store[key] : null;
40+
},
41+
setItem: (key: string, value: string) => {
42+
store[key] = `${value}`;
43+
},
44+
clear: () => {
45+
store = {};
46+
},
47+
};
48+
spyOn(localStorage, 'getItem').and.callFake(mockLocalStorage.getItem);
49+
spyOn(localStorage, 'setItem').and.callFake(mockLocalStorage.setItem);
50+
spyOn(localStorage, 'clear').and.callFake(mockLocalStorage.clear);
51+
localStorage.setItem('user2', JSON.stringify(account));
52+
});
53+
54+
it('should be created', () => {
55+
expect(service).toBeTruthy();
56+
});
57+
58+
it('should get name from localStorage', () => {
59+
const name = service.getName();
60+
61+
expect(name).toEqual(account.name);
62+
});
63+
64+
it('should get userId from localStorage', () => {
65+
const userId = service.getUserId();
66+
67+
expect(userId).toEqual(account.id);
68+
});
69+
70+
it('should get UserGroup from localStorage', () => {
71+
const userGroup = service.getUserGroup();
72+
73+
expect(userGroup).toEqual(account.groups);
74+
});
75+
76+
it('should get BearerToken from localStorage', () => {
77+
localStorage.setItem('idToken', 'token');
78+
79+
const bearerToken = service.getBearerToken();
80+
81+
expect(bearerToken).toEqual('token');
82+
});
83+
84+
it('should set key and value in localStorage', () => {
85+
service.setLocalStorage('key', 'value');
86+
87+
const value = localStorage.getItem('key');
88+
89+
expect(value).toEqual('value');
90+
});
91+
92+
it('load a user by sending a token using POST', () => {
93+
service.baseUrl = '/users';
94+
service.getUser('token').subscribe();
95+
96+
const loadUserRequest = httpMock.expectOne(`${service.baseUrl}/login`);
97+
expect(loadUserRequest.request.method).toBe('POST');
98+
});
99+
100+
it('should return true when user is Login', () => {
101+
spyOn(cookieService, 'check').and.returnValue(true);
102+
103+
const isLogin = service.isLogin();
104+
105+
expect(isLogin).toBeTruthy();
106+
});
107+
108+
it('should return false when user is not Login', () => {
109+
spyOn(cookieService, 'check').and.returnValue(false);
110+
111+
const isLogin = service.isLogin();
112+
113+
expect(isLogin).toBeFalsy();
114+
});
115+
116+
it('should login with social angularx-social-login', () => {
117+
service.signIn();
118+
expect(socialAuthService.signIn).toHaveBeenCalled();
119+
});
120+
121+
it('should logout with social angularx-social-login', () => {
122+
service.logout();
123+
const cookies = cookieService.getAll();
124+
expect(socialAuthService.signOut).toHaveBeenCalled();
125+
expect(localStorage.length).toEqual(0);
126+
expect(cookies).toEqual({});
127+
});
128+
});

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Injectable } from '@angular/core';
21
import { HttpClient } from '@angular/common/http';
3-
import { environment } from 'src/environments/environment';
2+
import { Injectable } from '@angular/core';
3+
import { GoogleLoginProvider, SocialAuthService } from 'angularx-social-login';
44
import { CookieService } from 'ngx-cookie-service';
5-
import { SocialAuthService, GoogleLoginProvider } from 'angularx-social-login';
65
import { UserEnum } from 'src/environments/enum';
6+
import { environment } from 'src/environments/environment';
77

88
@Injectable({
9-
providedIn: 'root',
9+
providedIn: 'root'
1010
})
1111
export class LoginService {
1212
baseUrl: string;

src/app/modules/shared/components/details-fields/details-fields.component.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,22 @@ describe('DetailsFieldsComponent', () => {
227227

228228
it('should emit ngOnChange without data', () => {
229229
component.entryToEdit = null;
230+
const formValue = {
231+
project_id: '',
232+
project_name: '',
233+
activity_id: '',
234+
uri: '',
235+
start_date: formatDate(new Date(), DATE_FORMAT, 'en'),
236+
end_date: formatDate(new Date(), DATE_FORMAT, 'en'),
237+
start_hour: '00:00',
238+
end_hour: '00:00',
239+
description: '',
240+
technology: '',
241+
};
230242
component.ngOnChanges();
243+
231244
expect(component.shouldRestartEntry).toBeFalse();
232-
expect(component.entryForm.value).toEqual(initialData);
245+
expect(component.entryForm.value).toEqual(formValue);
233246
component.activities$.subscribe((item) => {
234247
expect(item.length).not.toBe(null);
235248
expect(item.length).toBe(3);

src/app/modules/shared/components/user/user.component.spec.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ describe('UserComponent', () => {
4747
fixture = TestBed.createComponent(UserComponent);
4848
component = fixture.componentInstance;
4949
fixture.detectChanges();
50-
azureAdB2CService = TestBed.inject(AzureAdB2CService);
5150
loginService = TestBed.inject(LoginService);
51+
azureAdB2CService = TestBed.inject(AzureAdB2CService);
5252
});
5353

5454
it('component should be created', () => {
@@ -80,4 +80,25 @@ describe('UserComponent', () => {
8080
expect(azureAdB2CService.getUserEmail).toHaveBeenCalledTimes(0);
8181
expect(azureAdB2CService.setTenantId).not.toHaveBeenCalled();
8282
});
83+
it('onInit checks if isLogin and gets the name and set tenantIn in the storage Locally', () => {
84+
component.isProduction = false;
85+
spyOn(loginService, 'isLogin').and.returnValue(true);
86+
spyOn(loginService, 'getName').and.returnValue('Name');
87+
spyOn(loginService, 'getUserEmail').and.returnValue('Email');
88+
component.ngOnInit();
89+
expect(loginService.isLogin).toHaveBeenCalled();
90+
expect(loginService.getName).toHaveBeenCalled();
91+
expect(loginService.getUserEmail).toHaveBeenCalled();
92+
});
93+
94+
it('onInit does not get the name if isLogin false Locally', () => {
95+
component.isProduction = false;
96+
spyOn(loginService, 'isLogin').and.returnValue(false);
97+
spyOn(loginService, 'getName').and.returnValue('Name');
98+
spyOn(loginService, 'getUserEmail').and.returnValue('Email');
99+
component.ngOnInit();
100+
expect(loginService.isLogin).toHaveBeenCalled();
101+
expect(loginService.getName).toHaveBeenCalledTimes(0);
102+
expect(loginService.getUserEmail).toHaveBeenCalledTimes(0);
103+
});
83104
});

0 commit comments

Comments
 (0)