-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UserService: test 'createUserModel' method (#381)
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
Showing
2 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,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); | ||
} |