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
Revert "Tta 83 improve admins validation in frontend (#908)"
This reverts commit 607fc91.
  • Loading branch information
Reihtw authored Aug 12, 2022
commit 31e111254289beeb32882003e178cec03664221a
33,214 changes: 206 additions & 33,008 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"@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
3 changes: 2 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import { ActivityEffects } from './modules/activities-management/store/activity-
import { ProjectEffects } from './modules/customer-management/components/projects/components/store/project.effects';
import { TechnologyEffects } from './modules/shared/store/technology.effects';
import { ProjectTypeEffects } from './modules/customer-management/components/projects-type/store/project-type.effects';
import { reducers } from './reducers';
import { reducers, metaReducers } from './reducers';
import { CLIENT_URL, environment } from '../environments/environment';
import { EnvironmentType } from '../environments/enum';
import { CustomerComponent } from './modules/customer-management/pages/customer.component';
Expand Down Expand Up @@ -179,6 +179,7 @@ const maskConfig: Partial<IConfig> = {
MatIconModule,
MatListModule,
StoreModule.forRoot(reducers, {
metaReducers,
}),
environment.production === EnvironmentType.TT_DEV
? StoreDevtoolsModule.instrument({
Expand Down
30 changes: 12 additions & 18 deletions src/app/guards/login-guard/login.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ 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 @@ -16,13 +15,13 @@ describe('LoginGuard', () => {
let azureAdB2CService: AzureAdB2CService;
const azureAdB2CServiceStub = {
isLogin() {
return of(true);
return true;
}
};
let loginService: LoginService;
const loginServiceStub = {
isLogin() {
return of(true);
return true;
}
};
const socialAuthServiceStub = jasmine.createSpyObj('SocialAuthService', ['']);
Expand All @@ -47,41 +46,36 @@ describe('LoginGuard', () => {
it('can activate the route when user is logged-in on Production', () => {
loginGuard.isProduction = true;
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
loginGuard.canActivate().subscribe(canActivate => {
expect(canActivate).toEqual(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(of(true));
loginGuard.canActivate().subscribe(isLogin => {
expect(isLogin).toEqual(true);
});
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 on Production', inject([Router], (router: Router) => {
loginGuard.isProduction = true;
spyOn(azureAdB2CService, 'isLogin').and.returnValue(false);
spyOn(router, 'navigate').and.stub();
loginGuard.canActivate().subscribe(canActivate => {
expect(canActivate).toEqual(false);
});
const canActivate = loginGuard.canActivate();
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(of(false));
spyOn(loginService, 'isLogin').and.returnValue(false);
spyOn(router, 'navigate').and.stub();
loginGuard.canActivate().subscribe(isLogin => {
expect(isLogin).toEqual(false);
});
const canActivate = loginGuard.canActivate();
expect(loginService.isLogin).toHaveBeenCalled();
expect(canActivate).toEqual(false);
expect(router.navigate).toHaveBeenCalledWith(['login']);
}));

Expand Down
23 changes: 9 additions & 14 deletions src/app/guards/login-guard/login.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ 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 { map } from 'rxjs/operators';
import { EnvironmentType } from 'src/environments/enum';
import { of } from 'rxjs';


@Injectable({
Expand All @@ -23,22 +21,19 @@ export class LoginGuard implements CanActivate {
if (this.isProduction) {
if (this.azureAdB2CService.isLogin()) {
this.azureAdB2CService.setCookies();
return of(true);
return true;
} else {
this.router.navigate(['login']);
return of(false);
return false;
}
} else {
return this.loginService.isLogin().pipe(
map(isLogin => {
if (!isLogin) {
this.router.navigate(['login']);
return false;
}
this.loginService.setCookies();
return true;
})
);
if (this.loginService.isLogin()) {
this.loginService.setCookies();
return true;
} else {
this.router.navigate(['login']);
return false;
}
}
}
}
8 changes: 4 additions & 4 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 of(true);
return true;
},
signIn() {
return of();
Expand All @@ -29,7 +29,7 @@ describe('LoginComponent', () => {

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

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(of(false));
spyOn(loginService, 'isLogin').and.returnValue(false);
spyOn(loginService, 'setLocalStorage').and.returnValue();
spyOn(loginService, 'getUser').and.returnValue(of(() => {}));
spyOn(loginService, 'setCookies').and.returnValue();
Expand All @@ -123,7 +123,7 @@ describe('LoginComponent', () => {
}));

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(of(true));
spyOn(loginService, 'isLogin').and.returnValue(true);
spyOn(router, 'navigate').and.stub();
component.loginWithGoogle();
expect(loginService.isLogin).toHaveBeenCalled();
Expand Down
18 changes: 6 additions & 12 deletions src/app/modules/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { SocialAuthService, SocialUser } from 'angularx-social-login';
import { environment } from 'src/environments/environment';
import { EnvironmentType } from 'src/environments/enum';
import { LoginService } from './services/login.service';

@Component({
selector: 'app-login',
templateUrl: './login.component.html',
Expand All @@ -32,9 +31,7 @@ export class LoginComponent implements OnInit {
this.loginService.setLocalStorage('idToken', user.idToken);
this.loginService.getUser(user.idToken).subscribe((response) => {
this.loginService.setCookies();
const tokenObject = JSON.stringify(response);
const tokenJson = JSON.parse(tokenObject);
this.loginService.setLocalStorage('user', tokenJson.token);
this.loginService.setLocalStorage('user2', JSON.stringify(response));
this.router.navigate(['']);
});
}
Expand All @@ -52,14 +49,11 @@ export class LoginComponent implements OnInit {
});
}
}

loginWithGoogle() {
this.loginService.isLogin().subscribe(isLogin => {
if (isLogin) {
this.router.navigate(['']);
} else {
this.loginService.signIn();
}
});
if (this.loginService.isLogin()) {
this.router.navigate(['']);
} else {
this.loginService.signIn();
}
}
}
35 changes: 17 additions & 18 deletions src/app/modules/login/services/login.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { JwtHelperService } from '@auth0/angular-jwt';
import { SocialAuthService } from 'angularx-social-login';
import { CookieService } from 'ngx-cookie-service';
import { of } from 'rxjs';
Expand All @@ -15,10 +14,6 @@ describe('LoginService', () => {
let account;
const socialAuthServiceStub = jasmine.createSpyObj('SocialAuthService', ['signOut', 'signIn']);
const cookieStoreStub = {};
const helper = new JwtHelperService();
const getAccountInfo = () => {
return helper.decodeToken(account);
};

beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -32,7 +27,12 @@ describe('LoginService', () => {
cookieService = TestBed.inject(CookieService);
httpMock = TestBed.inject(HttpTestingController);
socialAuthService = TestBed.inject(SocialAuthService);
account = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImFiYyIsIm5hbWUiOiJhYmMiLCJlbWFpbCI6ImFiYyIsImdyb3VwcyI6WyJhYmMiXX0.UNxyDT8XzXJhI1F3LySBU7TJlpENPUPHj8my7Obw2ZM';
account = {
id: 'abc',
name: 'abc',
email: 'abc',
groups: ['abc'],
};
let store = {};
const mockLocalStorage = {
getItem: (key: string): string => {
Expand All @@ -48,7 +48,7 @@ describe('LoginService', () => {
spyOn(localStorage, 'getItem').and.callFake(mockLocalStorage.getItem);
spyOn(localStorage, 'setItem').and.callFake(mockLocalStorage.setItem);
spyOn(localStorage, 'clear').and.callFake(mockLocalStorage.clear);
localStorage.setItem('user', account);
localStorage.setItem('user2', JSON.stringify(account));
});

it('should be created', () => {
Expand All @@ -58,19 +58,19 @@ describe('LoginService', () => {
it('should get name from localStorage', () => {
const name = service.getName();

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

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

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

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

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

it('should get BearerToken from localStorage', () => {
Expand Down Expand Up @@ -99,19 +99,18 @@ describe('LoginService', () => {

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

service.isLogin().subscribe(isLogin => {
expect(isLogin).toEqual(true);
});
const isLogin = service.isLogin();

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

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

service.isLogin().subscribe(isLogin => {
expect(isLogin).toEqual(false);
});
const isLogin = service.isLogin();

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

it('should login with social angularx-social-login', () => {
Expand Down
Loading