Skip to content
Prev Previous commit
Next Next commit
feat TTA-83: frontend admin validation
  • Loading branch information
Rodrigo Lins committed Jul 27, 2022
commit d0911f435f598100aee718a16d618f883dfbb6ba
33,216 changes: 33,009 additions & 207 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@angular/platform-browser": "10.2.2",
"@angular/platform-browser-dynamic": "10.2.2",
"@angular/router": "10.2.2",
"@auth0/angular-jwt": "^5.0.2",
"@azure/app-configuration": "1.1.0",
"@azure/identity": "1.1.0",
"@ng-select/ng-select": "7.2.0",
Expand Down
33 changes: 19 additions & 14 deletions src/app/guards/login-guard/login.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { LoginGuard } from './login.guard';
import { LoginService } from '../../modules/login/services/login.service';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { SocialAuthService } from 'angularx-social-login';
import { of } from 'rxjs';


describe('LoginGuard', () => {
Expand All @@ -15,13 +16,13 @@ describe('LoginGuard', () => {
let azureAdB2CService: AzureAdB2CService;
const azureAdB2CServiceStub = {
isLogin() {
return true;
return of(true);
}
};
let loginService: LoginService;
const loginServiceStub = {
isLogin() {
return true;
return of(true);
}
};
const socialAuthServiceStub = jasmine.createSpyObj('SocialAuthService', ['']);
Expand All @@ -45,37 +46,41 @@ describe('LoginGuard', () => {

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();
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(true));
loginGuard.canActivate().subscribe(isLogin => {
expect(isLogin).toEqual(true);
});
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();
spyOn(loginService, 'isLogin').and.returnValue(of(true));
loginGuard.canActivate().subscribe(isLogin => {
expect(isLogin).toEqual(true);
});
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 on Production', inject([Router], (router: Router) => {
loginGuard.isProduction = true;
spyOn(azureAdB2CService, 'isLogin').and.returnValue(false);
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(false));
spyOn(router, 'navigate').and.stub();
const canActivate = loginGuard.canActivate();
loginGuard.canActivate().subscribe(isLogin => {
expect(isLogin).toEqual(false);
});
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
expect(canActivate).toEqual(false);
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(loginService, 'isLogin').and.returnValue(of(false));
spyOn(router, 'navigate').and.stub();
const canActivate = loginGuard.canActivate();
loginGuard.canActivate().subscribe(isLogin => {
expect(isLogin).toEqual(false);
});
expect(loginService.isLogin).toHaveBeenCalled();
expect(canActivate).toEqual(false);
expect(router.navigate).toHaveBeenCalledWith(['login']);
}));

Expand Down
36 changes: 22 additions & 14 deletions src/app/guards/login-guard/login.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Router, CanActivate } from '@angular/router';
import { AzureAdB2CService } from '../../modules/login/services/azure.ad.b2c.service';
import { LoginService } from '../../modules/login/services/login.service';
import { environment } from 'src/environments/environment';
import { of } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable({
providedIn: 'root',
Expand All @@ -17,21 +19,27 @@ export class LoginGuard implements CanActivate {

canActivate() {
if (this.isProduction) {
if (this.azureAdB2CService.isLogin()) {
this.azureAdB2CService.setCookies();
return true;
} else {
this.router.navigate(['login']);
return false;
}
return this.azureAdB2CService.isLogin().pipe(
map(isLogin => {
if (!isLogin) {
this.router.navigate(['login']);
return false;
}
this.azureAdB2CService.setCookies();
return true;
})
);
} else {
if (this.loginService.isLogin()) {
this.loginService.setCookies();
return true;
} else {
this.router.navigate(['login']);
return false;
}
return this.loginService.isLogin().pipe(
map(isLogin => {
if (!isLogin) {
this.router.navigate(['login']);
return false;
}
this.loginService.setCookies();
return true;
})
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('ActivityManagement Selectors', () => {
description: 'Some description'
},
];

const activitiesOrdered = [
{
id: '002',
Expand Down
49 changes: 28 additions & 21 deletions src/app/modules/login/login.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('LoginComponent', () => {

const azureAdB2CServiceStub = {
isLogin() {
return true;
return of(true);
},
signIn() {
return of();
Expand All @@ -29,7 +29,7 @@ describe('LoginComponent', () => {

const loginServiceStub = {
isLogin() {
return true;
return of(true);
},
signIn() {
return of();
Expand Down Expand Up @@ -86,47 +86,54 @@ describe('LoginComponent', () => {
});

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, 'isLogin').and.returnValue(of(false));
spyOn(azureAdB2CService, 'setCookies').and.returnValue();
spyOn(azureAdB2CService, 'signIn').and.returnValue(of(() => {}));
spyOn(featureToggleCookiesService, 'setCookies').and.returnValue(featureToggleCookiesService.setCookies());

component.login();

expect(azureAdB2CService.signIn).toHaveBeenCalled();
expect(azureAdB2CService.setCookies).toHaveBeenCalled();
expect(featureToggleCookiesService.setCookies).toHaveBeenCalled();
component.login().toPromise()
.then(result => {
expect(azureAdB2CService.signIn).toHaveBeenCalled();
expect(azureAdB2CService.setCookies).toHaveBeenCalled();
expect(featureToggleCookiesService.setCookies).toHaveBeenCalled();
});
}));

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, 'isLogin').and.returnValue(of(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();
component.loginWithGoogle().toPromise()
.then(result => {
expect(loginService.signIn).toHaveBeenCalled();
expect(loginService.setCookies).toHaveBeenCalled();
expect(featureToggleCookiesService.setCookies).toHaveBeenCalled();
});

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(azureAdB2CService, 'isLogin').and.returnValue(of(true));
spyOn(router, 'navigate').and.stub();
component.login();
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
expect(router.navigate).toHaveBeenCalledWith(['']);
component.login().toPromise()
.then(result => {
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(loginService, 'isLogin').and.returnValue(of(true));
spyOn(router, 'navigate').and.stub();
component.loginWithGoogle();
expect(loginService.isLogin).toHaveBeenCalled();
expect(router.navigate).toHaveBeenCalledWith(['']);
component.loginWithGoogle().toPromise()
.then(result => {
expect(loginService.isLogin).toHaveBeenCalled();
expect(router.navigate).toHaveBeenCalledWith(['']);
});
}));
});
45 changes: 29 additions & 16 deletions src/app/modules/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { FeatureToggleCookiesService } from '../shared/feature-toggles/feature-t
import { SocialAuthService, SocialUser } from 'angularx-social-login';
import { environment } from 'src/environments/environment';
import { LoginService } from './services/login.service';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
Expand All @@ -30,29 +32,40 @@ export class LoginComponent implements OnInit {
this.loginService.setLocalStorage('idToken', user.idToken);
this.loginService.getUser(user.idToken).subscribe((response) => {
this.loginService.setCookies();
this.loginService.setLocalStorage('user2', JSON.stringify(response));
const tokenObject = JSON.stringify(response);
const tokenJson = JSON.parse(tokenObject);
this.loginService.setLocalStorage('user', tokenJson.token);
this.router.navigate(['']);
});
}
});
}

login(): void {
if (this.azureAdB2CService.isLogin()) {
this.router.navigate(['']);
} else {
this.azureAdB2CService.signIn().subscribe(() => {
this.featureToggleCookiesService.setCookies();
this.azureAdB2CService.setCookies();
this.router.navigate(['']);
});
}
login() {
return this.azureAdB2CService.isLogin().pipe(
map(isLogin => {
if (isLogin) {
this.router.navigate(['']);
} else {
this.azureAdB2CService.signIn().subscribe(() => {
this.featureToggleCookiesService.setCookies();
this.azureAdB2CService.setCookies();
this.router.navigate(['']);
});
}
})
);
}

loginWithGoogle() {
if (this.loginService.isLogin()) {
this.router.navigate(['']);
} else {
this.loginService.signIn();
}
return this.loginService.isLogin().pipe(
map(isLogin => {
if (isLogin) {
this.router.navigate(['']);
} else {
this.loginService.signIn();
}
})
);
}
}
Loading