Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test: TT-453 fix test and add login test
  • Loading branch information
ararcos committed Dec 23, 2021
commit 2d108a3ed1a96bb4e6ef9a3f21cef713e007da95
2 changes: 1 addition & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ const maskConfig: Partial<IConfig> = {
{
id: GoogleLoginProvider.PROVIDER_ID,
provider: new GoogleLoginProvider(
'711486856840-f8u9pdmkk44nmkb0c9lbsjvolp2hulur.apps.googleusercontent.com'
'565556796659-hscrj9e6m2krc41cfng898793ocfnb8j.apps.googleusercontent.com'
)
}
]
Expand Down
23 changes: 20 additions & 3 deletions src/app/guards/login-guard/login.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('LoginGuard', () => {
return true;
}
};
const socialAuthServiceStub = jasmine.createSpyObj('SocialAuthService', ['authState']);
const socialAuthServiceStub = jasmine.createSpyObj('SocialAuthService', ['']);
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ RouterTestingModule, HttpClientTestingModule ],
Expand All @@ -43,16 +43,23 @@ describe('LoginGuard', () => {
expect(loginGuard).toBeTruthy();
});

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

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

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

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

});
12 changes: 2 additions & 10 deletions src/app/modules/login/login.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@
<h3>Please log in</h3>
</div>

<div class="login-controls" *ngIf="!isDevelopment">
<button (click)="login()" class="btn btn-primary">login</button>
</div>

<div class="container" style="max-width: 550px" *ngIf="isDevelopment">
<div >
<div class="login-controls">
<button type="button" (click)="loginWithGoogle()" class="btn btn-primary">Login with Google</button>
</div>
</div>
<div class="login-controls">
<button (click)="isProduction ? login() : loginWithGoogle() " class="btn btn-primary">login</button>
</div>

</div>
33 changes: 28 additions & 5 deletions src/app/modules/login/login.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('LoginComponent', () => {
}
};

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

beforeEach(() => {
socialAuthServiceStub.authState = of(null);
socialAuthServiceStub.authState = of('some value');
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
component.isDevelopment = false;
fixture.detectChanges();
azureAdB2CService = TestBed.inject(AzureAdB2CService);
loginService = TestBed.inject(LoginService);
Expand All @@ -86,7 +85,7 @@ describe('LoginComponent', () => {
expect(component).toBeTruthy();
});

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

it('should not sign-up or login with google if is already logged-in into the app', inject([Router], (router: Router) => {
it('should sign up or login with google if is not logged-in into the app Locally', inject([Router], (router: Router) => {
spyOn(loginService, 'isLogin').and.returnValue(false);
spyOn(loginService, 'setLocalStorage').and.returnValue();
spyOn(loginService, 'getUser').and.returnValue(of(() => {}));
spyOn(loginService, 'setCookies').and.returnValue();
spyOn(loginService, 'signIn').and.returnValue();
spyOn(featureToggleCookiesService, 'setCookies').and.returnValue(featureToggleCookiesService.setCookies());

component.ngOnInit();
component.loginWithGoogle();

expect(loginService.signIn).toHaveBeenCalled();
expect(loginService.setCookies).toHaveBeenCalled();
expect(featureToggleCookiesService.setCookies).toHaveBeenCalled();
}));

it('should not sign-up or login with google if is already logged-in into the app on Production', inject([Router], (router: Router) => {
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
spyOn(router, 'navigate').and.stub();
component.login();
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
expect(router.navigate).toHaveBeenCalledWith(['']);
}));

it('should not sign-up or login with google if is already logged-in into the app Locally', inject([Router], (router: Router) => {
spyOn(loginService, 'isLogin').and.returnValue(true);
spyOn(router, 'navigate').and.stub();
component.loginWithGoogle();
expect(loginService.isLogin).toHaveBeenCalled();
expect(router.navigate).toHaveBeenCalledWith(['']);
}));
});
22 changes: 10 additions & 12 deletions src/app/modules/login/login.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { AzureAdB2CService } from './services/azure.ad.b2c.service';
import { Router } from '@angular/router';
import { FeatureToggleCookiesService } from '../shared/feature-toggles/feature-toggle-cookies/feature-toggle-cookies.service';
Expand All @@ -11,9 +11,9 @@ import { LoginService } from './services/login.service';
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss'],
})
export class LoginComponent {
export class LoginComponent implements OnInit {
socialUser: SocialUser;
isDevelopment = true;
isProduction = environment.production;

constructor(
private azureAdB2CService: AzureAdB2CService,
Expand All @@ -23,8 +23,7 @@ export class LoginComponent {
private loginService?: LoginService
) {}

OnInit() {
this.isDevelopment = !environment.production;
ngOnInit() {
this.socialAuthService.authState.subscribe((user) => {
if (user != null) {
this.featureToggleCookiesService.setCookies();
Expand All @@ -49,12 +48,11 @@ export class LoginComponent {
});
}
}

loginWithGoogle(): void {
this.loginService.signIn();
}

logOut(): void {
this.loginService.logout();
loginWithGoogle() {
if (this.loginService.isLogin()) {
this.router.navigate(['']);
} else {
this.loginService.signIn();
}
}
}
128 changes: 128 additions & 0 deletions src/app/modules/login/services/login.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { SocialAuthService } from 'angularx-social-login';
import { CookieService } from 'ngx-cookie-service';
import { of } from 'rxjs';

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 cookieStoreStub = {};

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
{ providers: CookieService, useValue: cookieStoreStub },
{ provide: SocialAuthService, useValue: socialAuthServiceStub },
],
});
service = TestBed.inject(LoginService);
cookieService = TestBed.inject(CookieService);
httpMock = TestBed.inject(HttpTestingController);
socialAuthService = TestBed.inject(SocialAuthService);
account = {
id: 'abc',
name: 'abc',
email: 'abc',
groups: ['abc'],
};
let store = {};
const mockLocalStorage = {
getItem: (key: string): string => {
return key in store ? store[key] : null;
},
setItem: (key: string, value: string) => {
store[key] = `${value}`;
},
clear: () => {
store = {};
},
};
spyOn(localStorage, 'getItem').and.callFake(mockLocalStorage.getItem);
spyOn(localStorage, 'setItem').and.callFake(mockLocalStorage.setItem);
spyOn(localStorage, 'clear').and.callFake(mockLocalStorage.clear);
localStorage.setItem('user2', JSON.stringify(account));
});

it('should be created', () => {
expect(service).toBeTruthy();
});

it('should get name from localStorage', () => {
const name = service.getName();

expect(name).toEqual(account.name);
});

it('should get userId from localStorage', () => {
const userId = service.getUserId();

expect(userId).toEqual(account.id);
});

it('should get UserGroup from localStorage', () => {
const userGroup = service.getUserGroup();

expect(userGroup).toEqual(account.groups);
});

it('should get BearerToken from localStorage', () => {
localStorage.setItem('idToken', 'token');

const bearerToken = service.getBearerToken();

expect(bearerToken).toEqual('token');
});

it('should set key and value in localStorage', () => {
service.setLocalStorage('key', 'value');

const value = localStorage.getItem('key');

expect(value).toEqual('value');
});

it('load a user by sending a token using POST', () => {
service.baseUrl = '/users';
service.getUser('token').subscribe();

const loadUserRequest = httpMock.expectOne(`${service.baseUrl}/login`);
expect(loadUserRequest.request.method).toBe('POST');
});

it('should return true when user is Login', () => {
spyOn(cookieService, 'check').and.returnValue(true);

const isLogin = service.isLogin();

expect(isLogin).toBeTruthy();
});

it('should return false when user is not Login', () => {
spyOn(cookieService, 'check').and.returnValue(false);

const isLogin = service.isLogin();

expect(isLogin).toBeFalsy();
});

it('should login with social angularx-social-login', () => {
service.signIn();
expect(socialAuthService.signIn).toHaveBeenCalled();
});

it('should logout with social angularx-social-login', () => {
service.logout();
const cookies = cookieService.getAll();
expect(socialAuthService.signOut).toHaveBeenCalled();
expect(localStorage.length).toEqual(0);
expect(cookies).toEqual({});
});
});
8 changes: 4 additions & 4 deletions src/app/modules/login/services/login.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from 'src/environments/environment';
import { Injectable } from '@angular/core';
import { GoogleLoginProvider, SocialAuthService } from 'angularx-social-login';
import { CookieService } from 'ngx-cookie-service';
import { SocialAuthService, GoogleLoginProvider } from 'angularx-social-login';
import { UserEnum } from 'src/environments/enum';
import { environment } from 'src/environments/environment';

@Injectable({
providedIn: 'root',
providedIn: 'root'
})
export class LoginService {
baseUrl: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,22 @@ describe('DetailsFieldsComponent', () => {

it('should emit ngOnChange without data', () => {
component.entryToEdit = null;
const formValue = {
project_id: '',
project_name: '',
activity_id: '',
uri: '',
start_date: formatDate(new Date(), DATE_FORMAT, 'en'),
end_date: formatDate(new Date(), DATE_FORMAT, 'en'),
start_hour: '00:00',
end_hour: '00:00',
description: '',
technology: '',
};
component.ngOnChanges();

expect(component.shouldRestartEntry).toBeFalse();
expect(component.entryForm.value).toEqual(initialData);
expect(component.entryForm.value).toEqual(formValue);
component.activities$.subscribe((item) => {
expect(item.length).not.toBe(null);
expect(item.length).toBe(3);
Expand Down
23 changes: 22 additions & 1 deletion src/app/modules/shared/components/user/user.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ describe('UserComponent', () => {
fixture = TestBed.createComponent(UserComponent);
component = fixture.componentInstance;
fixture.detectChanges();
azureAdB2CService = TestBed.inject(AzureAdB2CService);
loginService = TestBed.inject(LoginService);
azureAdB2CService = TestBed.inject(AzureAdB2CService);
});

it('component should be created', () => {
Expand Down Expand Up @@ -80,4 +80,25 @@ describe('UserComponent', () => {
expect(azureAdB2CService.getUserEmail).toHaveBeenCalledTimes(0);
expect(azureAdB2CService.setTenantId).not.toHaveBeenCalled();
});
it('onInit checks if isLogin and gets the name and set tenantIn in the storage Locally', () => {
component.isProduction = false;
spyOn(loginService, 'isLogin').and.returnValue(true);
spyOn(loginService, 'getName').and.returnValue('Name');
spyOn(loginService, 'getUserEmail').and.returnValue('Email');
component.ngOnInit();
expect(loginService.isLogin).toHaveBeenCalled();
expect(loginService.getName).toHaveBeenCalled();
expect(loginService.getUserEmail).toHaveBeenCalled();
});

it('onInit does not get the name if isLogin false Locally', () => {
component.isProduction = false;
spyOn(loginService, 'isLogin').and.returnValue(false);
spyOn(loginService, 'getName').and.returnValue('Name');
spyOn(loginService, 'getUserEmail').and.returnValue('Email');
component.ngOnInit();
expect(loginService.isLogin).toHaveBeenCalled();
expect(loginService.getName).toHaveBeenCalledTimes(0);
expect(loginService.getUserEmail).toHaveBeenCalledTimes(0);
});
});