-
Notifications
You must be signed in to change notification settings - Fork 1
TT 155 consume user info #650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5950f27
feat: consume user info
Angeluz-07 88ae09d
feat: TT-155 register userEfecct in AppModule
cf2c16a
feat: TT-155 create user module and model
Angeluz-07 a970234
test: TT-155 Added unit tests for user ngrx flow
f4359a7
feat: TT-155 create a service to verifyGroup in user
2e67284
test: TT-155 modify test input data
d6b84b2
test: TT-155 added unit tests to user-info.service
thegreatyamori 29e1b27
test: TT-155 deleted two extra lines in sidebar
thegreatyamori 872952e
fix: TT-155 resolve comments & fix user.selectors.spec
thegreatyamori File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ testem.log | |
.keys.json | ||
keys.ts | ||
src/environments/keys.ts | ||
debug.log | ||
|
||
# System Files | ||
.DS_Store | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface User { | ||
name: string; | ||
email: string; | ||
roles?: string[]; | ||
groups?: string[]; | ||
id: string; | ||
tenant_id?: string; | ||
deleted?: string; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { MockStore, provideMockStore } from '@ngrx/store/testing'; | ||
import { of } from 'rxjs'; | ||
import { getUserGroups } from '../store/user.selectors'; | ||
import { UserInfoService } from './user-info.service'; | ||
|
||
describe('UserInfoService', () => { | ||
let service: UserInfoService; | ||
let store: MockStore; | ||
let mockGetUserGroupsSelector: any; | ||
const initialState = { | ||
name: 'Unknown Name', | ||
email: '[email protected]', | ||
roles: [], | ||
groups: ['fake-admin', 'fake-tester'], | ||
id: 'dummy_id_load', | ||
tenant_id: 'dummy_tenant_id_load', | ||
deleted: '', | ||
}; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [provideMockStore({ initialState })], | ||
}); | ||
service = TestBed.inject(UserInfoService); | ||
store = TestBed.inject(MockStore); | ||
mockGetUserGroupsSelector = store.overrideSelector(getUserGroups, initialState.groups); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it('should call groups selector', () => { | ||
const expectedGroups = ['fake-admin', 'fake-tester']; | ||
|
||
service.groups().subscribe((value) => { | ||
expect(value).toEqual(expectedGroups); | ||
}); | ||
}); | ||
|
||
const params = [ | ||
{ groupName: 'fake-admin', expectedValue: true, groups: ['fake-admin', 'fake-tester'] }, | ||
{ groupName: 'fake-owner', expectedValue: false, groups: ['fake-admin', 'fake-tester'] }, | ||
]; | ||
|
||
params.map((param) => { | ||
it(`given group ${param.groupName} and groups [${param.groups.toString()}], isMemberOf() should return ${ | ||
param.expectedValue | ||
}`, () => { | ||
const groups$ = of(param.groups); | ||
|
||
spyOn(service, 'groups').and.returnValue(groups$); | ||
|
||
service.isMemberOf(param.groupName).subscribe((value) => { | ||
expect(value).toEqual(param.expectedValue); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { select, Store } from '@ngrx/store'; | ||
import { Observable } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
import { getUserGroups } from '../store/user.selectors'; | ||
import { GROUPS } from '../../../../environments/environment'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class UserInfoService { | ||
constructor(private store: Store) {} | ||
|
||
groups(): Observable<string[]> { | ||
return this.store.pipe(select(getUserGroups)); | ||
} | ||
|
||
isMemberOf(groupName: string): Observable<boolean> { | ||
return this.groups().pipe( | ||
map((groups: string[]) => { | ||
return groups.includes(groupName); | ||
}) | ||
); | ||
} | ||
|
||
isAdmin(): Observable<boolean> { | ||
return this.isMemberOf(GROUPS.ADMIN); | ||
} | ||
|
||
isTester(): Observable<boolean> { | ||
return this.isMemberOf(GROUPS.TESTER); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { Observable } from 'rxjs'; | ||
import { environment } from 'src/environments/environment'; | ||
import { User } from '../models/user'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class UserService { | ||
|
||
constructor(private http: HttpClient) { | ||
} | ||
|
||
baseUrl = `${environment.timeTrackerApiUrl}/users`; | ||
|
||
loadUser(userId: string): Observable<User> { | ||
return this.http.get<User>(`${this.baseUrl}/${userId}`); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { LoadUserFail, LoadUserSuccess, UserActionTypes } from './user.actions'; | ||
|
||
import { User } from '../models/user'; | ||
|
||
describe('Actions for User', () => { | ||
it('LoadUserSuccess type is UserActionTypes.LOAD_USER_SUCCESS', () => { | ||
const user: User = { | ||
name: 'Unknown Name', | ||
email: '[email protected]', | ||
roles: [], | ||
groups: [], | ||
id: 'dummy_id_load', | ||
tenant_id: 'dummy_tenant_id_load', | ||
deleted: '' | ||
}; | ||
|
||
const loadUserSuccess = new LoadUserSuccess(user); | ||
|
||
expect(loadUserSuccess.type).toEqual(UserActionTypes.LOAD_USER_SUCCESS); | ||
}); | ||
|
||
it('LoadUserFail type is UserActionTypes.LOAD_USER_FAIL', () => { | ||
const loadUserFail = new LoadUserFail('error'); | ||
|
||
expect(loadUserFail.type).toEqual(UserActionTypes.LOAD_USER_FAIL); | ||
}); | ||
|
||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Action } from '@ngrx/store'; | ||
import { User } from '../models/user'; | ||
|
||
export enum UserActionTypes { | ||
LOAD_USER = '[User] LOAD_USER', | ||
LOAD_USER_SUCCESS = '[User] LOAD_USER_SUCCESS', | ||
LOAD_USER_FAIL = '[User] LOAD_USER_FAIL', | ||
} | ||
|
||
export class LoadUser implements Action { | ||
public readonly type = UserActionTypes.LOAD_USER; | ||
constructor(readonly userId: string) {} | ||
} | ||
|
||
export class LoadUserSuccess implements Action { | ||
public readonly type = UserActionTypes.LOAD_USER_SUCCESS; | ||
|
||
constructor(readonly payload: User) {} | ||
} | ||
|
||
export class LoadUserFail implements Action { | ||
public readonly type = UserActionTypes.LOAD_USER_FAIL; | ||
|
||
constructor(public error: string) {} | ||
} | ||
|
||
export type UserActions = LoadUser | LoadUserSuccess | LoadUserFail; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Observable, of, throwError } from 'rxjs'; | ||
import { Action } from '@ngrx/store'; | ||
import { User } from '../models/user'; | ||
import { UserEffects } from './user.effects'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { UserService } from '../services/user.service'; | ||
import { provideMockActions } from '@ngrx/effects/testing'; | ||
import { HttpClientTestingModule } from '@angular/common/http/testing'; | ||
import { LoadUser, UserActionTypes } from './user.actions'; | ||
|
||
describe('UserEffects', () => { | ||
let actions$: Observable<Action>; | ||
let effects: UserEffects; | ||
let service: UserService; | ||
const userInfo: User = { | ||
name: 'Unknown Name', | ||
email: '[email protected]', | ||
roles: [], | ||
groups: [], | ||
id: 'dummy_tenant_id_load', | ||
tenant_id: null, | ||
deleted: null | ||
}; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [UserEffects, provideMockActions(() => actions$)], | ||
imports: [HttpClientTestingModule], | ||
}); | ||
|
||
effects = TestBed.inject(UserEffects); | ||
service = TestBed.inject(UserService); | ||
}); | ||
|
||
it('should be created', async () => { | ||
expect(effects).toBeTruthy(); | ||
}); | ||
|
||
it('action type is LOAD_USER_SUCCESS when service is executed successfully', async () => { | ||
const userId = 'dummy_id_load'; | ||
const serviceSpy = spyOn(service, 'loadUser'); | ||
|
||
actions$ = of(new LoadUser(userId)); | ||
serviceSpy.and.returnValue(of(userInfo)); | ||
|
||
effects.loadUserInfo$.subscribe((action) => { | ||
expect(action.type).toEqual(UserActionTypes.LOAD_USER_SUCCESS); | ||
}); | ||
}); | ||
|
||
it('action type is LOAD_USER_FAIL when service fail in execution', async () => { | ||
const userId = 'dummy_id_load'; | ||
const serviceSpy = spyOn(service, 'loadUser'); | ||
|
||
actions$ = of(new LoadUser(userId)); | ||
serviceSpy.and.returnValue(throwError({ error: { message: 'fail!' } })); | ||
|
||
effects.loadUserInfo$.subscribe((action) => { | ||
expect(action.type).toEqual(UserActionTypes.LOAD_USER_FAIL); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Actions, ofType, Effect } from '@ngrx/effects'; | ||
import { Action } from '@ngrx/store'; | ||
import { Observable, of } from 'rxjs'; | ||
import { catchError, map, mergeMap } from 'rxjs/operators'; | ||
import { UserService } from '../services/user.service'; | ||
import * as actions from './user.actions'; | ||
|
||
@Injectable() | ||
export class UserEffects { | ||
constructor(private actions$: Actions, private userService: UserService) {} | ||
|
||
@Effect() | ||
loadUserInfo$: Observable<Action> = this.actions$.pipe( | ||
ofType(actions.UserActionTypes.LOAD_USER), | ||
map((action: actions.LoadUser) => action.userId), | ||
mergeMap((userId) => | ||
this.userService.loadUser(userId).pipe( | ||
map((response) => new actions.LoadUserSuccess(response)), | ||
catchError((error) => of(new actions.LoadUserFail(error))) | ||
) | ||
) | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { userReducer } from './user.reducer'; | ||
import { LoadUser, LoadUserFail, LoadUserSuccess } from './user.actions'; | ||
import { User } from '../models/user'; | ||
|
||
describe('userReducer', () => { | ||
const initialState = { | ||
name: '', | ||
email: '', | ||
roles: [], | ||
groups: [], | ||
}; | ||
|
||
it('on LoadUser, state equal to initialState', () => { | ||
const userId = 'dummy_id_load'; | ||
const action = new LoadUser(userId); | ||
const state = userReducer(initialState, action); | ||
|
||
expect(state).toEqual(initialState); | ||
}); | ||
|
||
it('on LoadUserSuccess, userFound is saved in store', () => { | ||
const userFound: User = { | ||
name: 'Unknown Name', | ||
email: '[email protected]', | ||
roles: [], | ||
groups: [], | ||
id: 'dummy_id_load', | ||
tenant_id: null, | ||
deleted: null | ||
}; | ||
|
||
const action = new LoadUserSuccess(userFound); | ||
const state = userReducer(initialState, action); | ||
|
||
expect(state).toEqual(userFound); | ||
}); | ||
|
||
it('on LoadUserFail, state equal to initialState', () => { | ||
const action = new LoadUserFail('error'); | ||
const state = userReducer(initialState, action); | ||
|
||
expect(state).toEqual(initialState); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.