Skip to content

Commit

Permalink
UserService: test 'createUserModel' method (#381)
Browse files Browse the repository at this point in the history
The createUserModel method in UserService accepts a
json representation of data.csv, and a GitHub id, and
then returns a User object corresponding to that user.

Let's test whether the User objects are correctly created.
  • Loading branch information
anubh-v authored Apr 16, 2020
1 parent daa5ff6 commit 89a5c8b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
42 changes: 40 additions & 2 deletions tests/constants/data.constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DataFile } from '../../src/app/core/models/data-file.model';
import { Team } from '../../src/app/core/models/team.model';
import { User, UserRole } from '../../src/app/core/models/user.model';
import { UserRole } from '../../src/app/core/models/user.model';

export const csvString = `
role,name,team
Expand Down Expand Up @@ -53,7 +53,45 @@ export const jsonData = {
q: { 'CS2103T-W12-4': 'true' }
},
'admins-allocation': { damithc: {}, geshuming: {} }
};
}

// These are objects representing some users and teams in jsonData
const TEAM_3 = {
id: 'CS2103T-W12-3',
teamMembers: [{loginId: 'junwei96', role: UserRole.Student},
{loginId: '003-samuel', role: UserRole.Student} ,
{loginId: 'damithc', role: UserRole.Student},
{loginId: 'ptvrajsk', role: UserRole.Student}]
}

const TEAM_4 = {
id: 'CS2103T-W12-4',
teamMembers: [{loginId: 'ronaklakhotia', role: UserRole.Student}]
}

export const USER_JUNWEI = {
loginId: 'junwei96',
role: UserRole.Student,
team: TEAM_3
}

export const USER_Q = {
loginId: 'q',
role: UserRole.Tutor,
allocatedTeams: [TEAM_4]
}

export const USER_SHUMING = {
loginId: 'geshuming',
role: UserRole.Admin,
allocatedTeams: []
}

export const USER_WITH_TWO_ROLES = {
loginId: 'damithc',
role: UserRole.Admin,
allocatedTeams: []
}

export const dataFileTeamStructure: DataFile = {
teamStructure: new Map<string, Team>([
Expand Down
55 changes: 55 additions & 0 deletions tests/services/user.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { jsonData, USER_JUNWEI, USER_Q, USER_SHUMING, USER_WITH_TWO_ROLES } from '../constants/data.constants'
import { UserService } from '../../src/app/core/services/user.service'
import { User, UserRole } from '../../src/app/core/models/user.model';
import { of } from 'rxjs';

let dataService: any;

describe('UserService', () => {
describe('.createUserModel(loginId)', () => {
beforeAll(() => {
dataService = jasmine.createSpyObj("DataService", ['getDataFile']);
dataService.getDataFile.and.returnValue(of(jsonData));
});

it('creates a Student user correctly', async () => {
await createAndVerifyUser(USER_JUNWEI.loginId, USER_JUNWEI);
});

it('creates a Tutor user correctly when loginId is very short', async () => {
await createAndVerifyUser(USER_Q.loginId, USER_Q);
});

it('creates an Admin user correctly', async () => {
await createAndVerifyUser(USER_SHUMING.loginId, USER_SHUMING);
});

it('treats the loginId in a case insensitive manner', async () => {
await createAndVerifyUser('JUNWEi96', USER_JUNWEI);
});

it('assigns highest possible role to a user who has multiple roles in data.csv', async () => {
await createAndVerifyUser(USER_WITH_TWO_ROLES.loginId, USER_WITH_TWO_ROLES);
});

it('throws an error if the user is unauthorized', (done) => {
const userService = new UserService(null, dataService);
userService.createUserModel('RandomUser').subscribe(
user => {
fail('This test case should have failed.');
done();
},
error => {
expect(error).toBe('Unauthorized user.');
done();
}
);
});
});
});

async function createAndVerifyUser(loginId: string, expectedUser: User) {
const userService = new UserService(null, dataService);
const actualUser = await userService.createUserModel(loginId).toPromise();
expect(actualUser).toEqual(expectedUser);
}

0 comments on commit 89a5c8b

Please sign in to comment.