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-155 added unit tests to user-info.service
  • Loading branch information
thegreatyamori authored and Angeluz-07 committed Mar 19, 2021
commit d6b84b200d7969cd9eb311d22c40859e91ff6174
48 changes: 43 additions & 5 deletions src/app/modules/user/services/user-info.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +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({});
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 verify if an user belongs to a certain group in the UI', () => {
const input = 'time-tracker-admin';
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$);

expect(service.verifyGroup(input)).toBeTruthy();
service.isMemberOf(param.groupName).subscribe((value) => {
expect(value).toEqual(param.expectedValue);
});
});
});
});
30 changes: 21 additions & 9 deletions src/app/modules/user/services/user-info.service.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import { Injectable } from '@angular/core';
import { select, Store } from '@ngrx/store';
import { getUserGroupsInfo } from '../store/user.selectors';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { getUserGroups } from '../store/user.selectors';
import { GROUPS } from '../../../../environments/environment';

@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class UserInfoService {
constructor(private store: Store) {}

constructor(private store: Store) { }
groups(): Observable<string[]> {
return this.store.pipe(select(getUserGroups));
}

verifyGroup(groupName: string): boolean {
let isGroupBelongsToUser: boolean;
isMemberOf(groupName: string): Observable<boolean> {
return this.groups().pipe(
map((groups: string[]) => {
return groups.includes(groupName);
})
);
}

const groupsStored = this.store.pipe(select(getUserGroupsInfo)).subscribe((groups) => {
isGroupBelongsToUser = groups.includes(groupName);
});
isAdmin(): Observable<boolean> {
return this.isMemberOf(GROUPS.ADMIN);
}

return isGroupBelongsToUser;
isTester(): Observable<boolean> {
return this.isMemberOf(GROUPS.TESTER);
}
}
4 changes: 4 additions & 0 deletions src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const STACK_EXCHANGE_ACCESS_TOKEN = keys.STACK_EXCHANGE_ACCESS_TOKEN;
export const AZURE_APP_CONFIGURATION_CONNECTION_STRING = keys.AZURE_APP_CONFIGURATION_CONNECTION_STRING;
export const DATE_FORMAT = 'yyyy-MM-dd';
export const DATE_FORMAT_YEAR = 'YYYY-MM-DD';
export const GROUPS = {
ADMIN: 'time-tracker-admin',
TESTER: 'time-tracker-tester',
};

/*
* For easier debugging in development mode, you can import the following file
Expand Down