Skip to content

Commit

Permalink
N21-1269 fix roster logic
Browse files Browse the repository at this point in the history
  • Loading branch information
arnegns committed Sep 19, 2023
1 parent 72024ef commit 01433fc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,10 @@ describe('FeathersRosterService', () => {
describe('when pseudonym is given', () => {
const setup = () => {
const school: SchoolDO = schoolDOFactory.buildWithId();
const externalTool: ExternalTool = externalToolFactory.buildWithId();
const clientId = 'testClientId';
const externalTool: ExternalTool = externalToolFactory.withOauth2Config({ clientId }).buildWithId();
const externalToolId: string = externalTool.id as string;

const otherExternalTool: ExternalTool = externalToolFactory.buildWithId();
const schoolExternalTool: SchoolExternalTool = schoolExternalToolFactory.buildWithId({
toolId: externalTool.id,
Expand Down Expand Up @@ -239,6 +241,7 @@ describe('FeathersRosterService', () => {
});

pseudonymService.findPseudonymByPseudonym.mockResolvedValue(pseudonym);
externalToolService.findExternalToolByOAuth2ConfigClientId.mockResolvedValue(externalTool);
courseService.findAllByUserId.mockResolvedValue(courses);
contextExternalToolService.findAllByContext.mockResolvedValueOnce([
contextExternalTool,
Expand All @@ -254,6 +257,7 @@ describe('FeathersRosterService', () => {
return {
pseudonym,
externalToolId,
clientId,
user,
courses,
schoolExternalTool,
Expand All @@ -263,25 +267,25 @@ describe('FeathersRosterService', () => {
};

it('should call the pseudonym service to find the pseudonym', async () => {
const { pseudonym, externalToolId } = setup();
const { pseudonym, clientId } = setup();

await service.getUserGroups(pseudonym.pseudonym, externalToolId);
await service.getUserGroups(pseudonym.pseudonym, clientId);

expect(pseudonymService.findPseudonymByPseudonym).toHaveBeenCalledWith(pseudonym.pseudonym);
});

it('should call the course service to find the courses for the userId of the pseudonym', async () => {
const { pseudonym, externalToolId } = setup();
const { pseudonym, clientId } = setup();

await service.getUserGroups(pseudonym.pseudonym, externalToolId);
await service.getUserGroups(pseudonym.pseudonym, clientId);

expect(courseService.findAllByUserId).toHaveBeenCalledWith(pseudonym.userId);
});

it('should call the context external tool service to find the external tools for each course', async () => {
const { pseudonym, courses, externalToolId } = setup();
const { pseudonym, courses, clientId } = setup();

await service.getUserGroups(pseudonym.pseudonym, externalToolId);
await service.getUserGroups(pseudonym.pseudonym, clientId);

expect(contextExternalToolService.findAllByContext.mock.calls).toEqual([
[new ContextRef({ id: courses[0].id, type: ToolContextType.COURSE })],
Expand All @@ -291,9 +295,9 @@ describe('FeathersRosterService', () => {
});

it('should call school external tool service to find the school external tool for each context external tool', async () => {
const { pseudonym, externalToolId, schoolExternalTool, otherSchoolExternalTool } = setup();
const { pseudonym, clientId, schoolExternalTool, otherSchoolExternalTool } = setup();

await service.getUserGroups(pseudonym.pseudonym, externalToolId);
await service.getUserGroups(pseudonym.pseudonym, clientId);

expect(schoolExternalToolService.getSchoolExternalToolById.mock.calls).toEqual([
[schoolExternalTool.id],
Expand All @@ -302,17 +306,17 @@ describe('FeathersRosterService', () => {
});

it('should call external tool service to find the external tool for each school external tool', async () => {
const { pseudonym, externalToolId, otherExternalTool } = setup();
const { pseudonym, clientId, otherExternalTool, externalToolId } = setup();

await service.getUserGroups(pseudonym.pseudonym, externalToolId);
await service.getUserGroups(pseudonym.pseudonym, clientId);

expect(externalToolService.findExternalToolById.mock.calls).toEqual([[externalToolId], [otherExternalTool.id]]);
});

it('should return a group for each course where the tool of the users pseudonym is used', async () => {
const { pseudonym, externalToolId, courses } = setup();
const { pseudonym, clientId, courses } = setup();

const result = await service.getUserGroups(pseudonym.pseudonym, externalToolId);
const result = await service.getUserGroups(pseudonym.pseudonym, clientId);

expect(result).toEqual({
data: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ export class FeathersRosterService {
return userMetadata;
}

async getUserGroups(pseudonym: string, externalToolId: string): Promise<UserGroups> {
async getUserGroups(pseudonym: string, oauth2ClientId: string): Promise<UserGroups> {
const loadedPseudonym: Pseudonym = await this.findPseudonymByPseudonym(pseudonym);
const externalTool: ExternalTool = await this.validateAndGetExternalTool(oauth2ClientId);

let courses: Course[] = await this.getCoursesFromUsersPseudonym(loadedPseudonym);
courses = await this.filterCoursesByToolAvailability(courses, externalToolId);
courses = await this.filterCoursesByToolAvailability(courses, externalTool.id as string);

const userGroups: UserGroups = {
data: {
Expand Down
2 changes: 1 addition & 1 deletion src/services/roster/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ module.exports = function roster() {
if (Configuration.get('FEATURE_CTL_TOOLS_TAB_ENABLED')) {
const userGroups = await app
.service('nest-feathers-roster-service')
.getUserGroups(params.pseudonym, params.originToolId);
.getUserGroups(params.pseudonym, params.tokenInfo.client_id);
return userGroups;
}

Expand Down
2 changes: 1 addition & 1 deletion test/services/roster/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {

chai.use(chaiHttp);

describe.only('roster service', function oauth() {
describe('roster service', function oauth() {
let app;
let metadataService;
let userGroupsService;
Expand Down

0 comments on commit 01433fc

Please sign in to comment.