Skip to content

Commit 6716334

Browse files
author
Reihtw
authored
Revert "Tta 83 improve admins validation in frontend (#908)" (#917)
This reverts commit 607fc91.
1 parent 607fc91 commit 6716334

File tree

16 files changed

+324
-33219
lines changed

16 files changed

+324
-33219
lines changed

package-lock.json

Lines changed: 206 additions & 33008 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"@angular/platform-browser": "10.2.2",
2727
"@angular/platform-browser-dynamic": "10.2.2",
2828
"@angular/router": "10.2.2",
29-
"@auth0/angular-jwt": "^5.0.2",
3029
"@azure/app-configuration": "1.1.0",
3130
"@azure/identity": "1.1.0",
3231
"@ng-select/ng-select": "7.2.0",

src/app/app.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import { ActivityEffects } from './modules/activities-management/store/activity-
4848
import { ProjectEffects } from './modules/customer-management/components/projects/components/store/project.effects';
4949
import { TechnologyEffects } from './modules/shared/store/technology.effects';
5050
import { ProjectTypeEffects } from './modules/customer-management/components/projects-type/store/project-type.effects';
51-
import { reducers } from './reducers';
51+
import { reducers, metaReducers } from './reducers';
5252
import { CLIENT_URL, environment } from '../environments/environment';
5353
import { EnvironmentType } from '../environments/enum';
5454
import { CustomerComponent } from './modules/customer-management/pages/customer.component';
@@ -179,6 +179,7 @@ const maskConfig: Partial<IConfig> = {
179179
MatIconModule,
180180
MatListModule,
181181
StoreModule.forRoot(reducers, {
182+
metaReducers,
182183
}),
183184
environment.production === EnvironmentType.TT_DEV
184185
? StoreDevtoolsModule.instrument({

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { LoginGuard } from './login.guard';
77
import { LoginService } from '../../modules/login/services/login.service';
88
import { HttpClientTestingModule } from '@angular/common/http/testing';
99
import { SocialAuthService } from 'angularx-social-login';
10-
import { of } from 'rxjs';
1110

1211

1312
describe('LoginGuard', () => {
@@ -16,13 +15,13 @@ describe('LoginGuard', () => {
1615
let azureAdB2CService: AzureAdB2CService;
1716
const azureAdB2CServiceStub = {
1817
isLogin() {
19-
return of(true);
18+
return true;
2019
}
2120
};
2221
let loginService: LoginService;
2322
const loginServiceStub = {
2423
isLogin() {
25-
return of(true);
24+
return true;
2625
}
2726
};
2827
const socialAuthServiceStub = jasmine.createSpyObj('SocialAuthService', ['']);
@@ -47,41 +46,36 @@ describe('LoginGuard', () => {
4746
it('can activate the route when user is logged-in on Production', () => {
4847
loginGuard.isProduction = true;
4948
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
50-
loginGuard.canActivate().subscribe(canActivate => {
51-
expect(canActivate).toEqual(true);
52-
});
49+
const canActivate = loginGuard.canActivate();
5350
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
51+
expect(canActivate).toEqual(true);
5452
});
5553

56-
5754
it('can activate the route when user is logged-in Locally', () => {
5855
loginGuard.isProduction = false;
59-
spyOn(loginService, 'isLogin').and.returnValue(of(true));
60-
loginGuard.canActivate().subscribe(isLogin => {
61-
expect(isLogin).toEqual(true);
62-
});
56+
spyOn(loginService, 'isLogin').and.returnValue(true);
57+
const canActivate = loginGuard.canActivate();
6358
expect(loginService.isLogin).toHaveBeenCalled();
59+
expect(canActivate).toEqual(true);
6460
});
6561

6662
it('can not active the route and is redirected to login if user is not logged-in on Production', inject([Router], (router: Router) => {
6763
loginGuard.isProduction = true;
6864
spyOn(azureAdB2CService, 'isLogin').and.returnValue(false);
6965
spyOn(router, 'navigate').and.stub();
70-
loginGuard.canActivate().subscribe(canActivate => {
71-
expect(canActivate).toEqual(false);
72-
});
66+
const canActivate = loginGuard.canActivate();
7367
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
68+
expect(canActivate).toEqual(false);
7469
expect(router.navigate).toHaveBeenCalledWith(['login']);
7570
}));
7671

7772
it('can not active the route and is redirected to login if user is not logged-in Locally', inject([Router], (router: Router) => {
7873
loginGuard.isProduction = false;
79-
spyOn(loginService, 'isLogin').and.returnValue(of(false));
74+
spyOn(loginService, 'isLogin').and.returnValue(false);
8075
spyOn(router, 'navigate').and.stub();
81-
loginGuard.canActivate().subscribe(isLogin => {
82-
expect(isLogin).toEqual(false);
83-
});
76+
const canActivate = loginGuard.canActivate();
8477
expect(loginService.isLogin).toHaveBeenCalled();
78+
expect(canActivate).toEqual(false);
8579
expect(router.navigate).toHaveBeenCalledWith(['login']);
8680
}));
8781

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import { Router, CanActivate } from '@angular/router';
33
import { AzureAdB2CService } from '../../modules/login/services/azure.ad.b2c.service';
44
import { LoginService } from '../../modules/login/services/login.service';
55
import { environment } from 'src/environments/environment';
6-
import { map } from 'rxjs/operators';
76
import { EnvironmentType } from 'src/environments/enum';
8-
import { of } from 'rxjs';
97

108

119
@Injectable({
@@ -23,22 +21,19 @@ export class LoginGuard implements CanActivate {
2321
if (this.isProduction) {
2422
if (this.azureAdB2CService.isLogin()) {
2523
this.azureAdB2CService.setCookies();
26-
return of(true);
24+
return true;
2725
} else {
2826
this.router.navigate(['login']);
29-
return of(false);
27+
return false;
3028
}
3129
} else {
32-
return this.loginService.isLogin().pipe(
33-
map(isLogin => {
34-
if (!isLogin) {
35-
this.router.navigate(['login']);
36-
return false;
37-
}
38-
this.loginService.setCookies();
39-
return true;
40-
})
41-
);
30+
if (this.loginService.isLogin()) {
31+
this.loginService.setCookies();
32+
return true;
33+
} else {
34+
this.router.navigate(['login']);
35+
return false;
36+
}
4237
}
4338
}
4439
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('LoginComponent', () => {
1818

1919
const azureAdB2CServiceStub = {
2020
isLogin() {
21-
return of(true);
21+
return true;
2222
},
2323
signIn() {
2424
return of();
@@ -29,7 +29,7 @@ describe('LoginComponent', () => {
2929

3030
const loginServiceStub = {
3131
isLogin() {
32-
return of(true);
32+
return true;
3333
},
3434
signIn() {
3535
return of();
@@ -99,7 +99,7 @@ describe('LoginComponent', () => {
9999
}));
100100

101101
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(of(false));
102+
spyOn(loginService, 'isLogin').and.returnValue(false);
103103
spyOn(loginService, 'setLocalStorage').and.returnValue();
104104
spyOn(loginService, 'getUser').and.returnValue(of(() => {}));
105105
spyOn(loginService, 'setCookies').and.returnValue();
@@ -123,7 +123,7 @@ describe('LoginComponent', () => {
123123
}));
124124

125125
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(of(true));
126+
spyOn(loginService, 'isLogin').and.returnValue(true);
127127
spyOn(router, 'navigate').and.stub();
128128
component.loginWithGoogle();
129129
expect(loginService.isLogin).toHaveBeenCalled();

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { SocialAuthService, SocialUser } from 'angularx-social-login';
77
import { environment } from 'src/environments/environment';
88
import { EnvironmentType } from 'src/environments/enum';
99
import { LoginService } from './services/login.service';
10-
1110
@Component({
1211
selector: 'app-login',
1312
templateUrl: './login.component.html',
@@ -32,9 +31,7 @@ export class LoginComponent implements OnInit {
3231
this.loginService.setLocalStorage('idToken', user.idToken);
3332
this.loginService.getUser(user.idToken).subscribe((response) => {
3433
this.loginService.setCookies();
35-
const tokenObject = JSON.stringify(response);
36-
const tokenJson = JSON.parse(tokenObject);
37-
this.loginService.setLocalStorage('user', tokenJson.token);
34+
this.loginService.setLocalStorage('user2', JSON.stringify(response));
3835
this.router.navigate(['']);
3936
});
4037
}
@@ -52,14 +49,11 @@ export class LoginComponent implements OnInit {
5249
});
5350
}
5451
}
55-
5652
loginWithGoogle() {
57-
this.loginService.isLogin().subscribe(isLogin => {
58-
if (isLogin) {
59-
this.router.navigate(['']);
60-
} else {
61-
this.loginService.signIn();
62-
}
63-
});
53+
if (this.loginService.isLogin()) {
54+
this.router.navigate(['']);
55+
} else {
56+
this.loginService.signIn();
57+
}
6458
}
6559
}

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
22
import { TestBed } from '@angular/core/testing';
3-
import { JwtHelperService } from '@auth0/angular-jwt';
43
import { SocialAuthService } from 'angularx-social-login';
54
import { CookieService } from 'ngx-cookie-service';
65
import { of } from 'rxjs';
@@ -15,10 +14,6 @@ describe('LoginService', () => {
1514
let account;
1615
const socialAuthServiceStub = jasmine.createSpyObj('SocialAuthService', ['signOut', 'signIn']);
1716
const cookieStoreStub = {};
18-
const helper = new JwtHelperService();
19-
const getAccountInfo = () => {
20-
return helper.decodeToken(account);
21-
};
2217

2318
beforeEach(() => {
2419
TestBed.configureTestingModule({
@@ -32,7 +27,12 @@ describe('LoginService', () => {
3227
cookieService = TestBed.inject(CookieService);
3328
httpMock = TestBed.inject(HttpTestingController);
3429
socialAuthService = TestBed.inject(SocialAuthService);
35-
account = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImFiYyIsIm5hbWUiOiJhYmMiLCJlbWFpbCI6ImFiYyIsImdyb3VwcyI6WyJhYmMiXX0.UNxyDT8XzXJhI1F3LySBU7TJlpENPUPHj8my7Obw2ZM';
30+
account = {
31+
id: 'abc',
32+
name: 'abc',
33+
email: 'abc',
34+
groups: ['abc'],
35+
};
3636
let store = {};
3737
const mockLocalStorage = {
3838
getItem: (key: string): string => {
@@ -48,7 +48,7 @@ describe('LoginService', () => {
4848
spyOn(localStorage, 'getItem').and.callFake(mockLocalStorage.getItem);
4949
spyOn(localStorage, 'setItem').and.callFake(mockLocalStorage.setItem);
5050
spyOn(localStorage, 'clear').and.callFake(mockLocalStorage.clear);
51-
localStorage.setItem('user', account);
51+
localStorage.setItem('user2', JSON.stringify(account));
5252
});
5353

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

61-
expect(name).toEqual(getAccountInfo().name);
61+
expect(name).toEqual(account.name);
6262
});
6363

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

67-
expect(userId).toEqual(getAccountInfo().id);
67+
expect(userId).toEqual(account.id);
6868
});
6969

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

73-
expect(userGroup).toEqual(getAccountInfo().groups);
73+
expect(userGroup).toEqual(account.groups);
7474
});
7575

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

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

104-
service.isLogin().subscribe(isLogin => {
105-
expect(isLogin).toEqual(true);
106-
});
103+
const isLogin = service.isLogin();
104+
105+
expect(isLogin).toBeTruthy();
107106
});
108107

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

112-
service.isLogin().subscribe(isLogin => {
113-
expect(isLogin).toEqual(false);
114-
});
111+
const isLogin = service.isLogin();
112+
113+
expect(isLogin).toBeFalsy();
115114
});
116115

117116
it('should login with social angularx-social-login', () => {

0 commit comments

Comments
 (0)