Skip to content
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

fix(Peer Group): Non admins can not create new peer groupings #1434

Merged
merged 4 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MatDialogRef } from '@angular/material/dialog';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { of } from 'rxjs';
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
import { of, throwError } from 'rxjs';
import { PeerGrouping } from '../../../../../app/domain/peerGrouping';
import { ReferenceComponent } from '../../../../../app/domain/referenceComponent';
import { StudentTeacherCommonServicesModule } from '../../../../../app/student-teacher-common-services.module';
Expand All @@ -15,6 +15,7 @@ const REFERENCE_COMPONENT_COMPONENT_ID1 = 'component1';
let component: CreateNewPeerGroupingDialogComponent;
let createNewPeerGroupingSpy: jasmine.Spy, dialogCloseSpy: jasmine.Spy;
let fixture: ComponentFixture<CreateNewPeerGroupingDialogComponent>;
let snackBar: MatSnackBar;

describe('CreateNewPeerGroupingDialogComponent', () => {
beforeEach(async () => {
Expand All @@ -30,6 +31,7 @@ describe('CreateNewPeerGroupingDialogComponent', () => {
beforeEach(() => {
fixture = TestBed.createComponent(CreateNewPeerGroupingDialogComponent);
component = fixture.componentInstance;
snackBar = TestBed.inject(MatSnackBar);
fixture.detectChanges();
});
create();
Expand All @@ -41,6 +43,7 @@ function create() {
create_DifferentIdeasMaximizeLogic_ShouldCreatePeerGroup();
create_DifferentScoresAnyLogic_ShouldCreatePeerGroup();
create_DifferentScoresMaximizeLogic_ShouldCreatePeerGroup();
create_ErrorOccurs_ShowsError();
}

function create_RandomLogic_ShouldCreatePeerGroup() {
Expand Down Expand Up @@ -98,3 +101,41 @@ function expectLogicCreateNewPeerGrouping(logicType: string, mode: string) {
expect(createNewPeerGroupingSpy).toHaveBeenCalledWith(newPeerGrouping);
expect(dialogCloseSpy).toHaveBeenCalled();
}

function create_ErrorOccurs_ShowsError(): void {
describe('create new peer grouping returns error', () => {
create_GenericErrorOccurs_ShowsError();
create_NotAuthorizedErrorOccurs_ShowsError();
});
}

function create_GenericErrorOccurs_ShowsError(): void {
describe('returns generic error', () => {
it('shows generic error message in snackbar', async () => {
returnErrorExpectErrorMessage('genericError', 'An error occurred. Please try again.');
});
});
}

function create_NotAuthorizedErrorOccurs_ShowsError(): void {
describe('returns not authorized error', () => {
it('shows not authorized error in snackbar', async () => {
returnErrorExpectErrorMessage('notAuthorized', 'You are not allowed to perform this action.');
});
});
}

function returnErrorExpectErrorMessage(messageCode: string, errorMessage: string): void {
const snackBarSpy = spyOn(snackBar, 'open');
createNewPeerGroupingSpy.and.returnValue(
throwError(() => {
return {
error: {
messageCode: messageCode
}
};
})
);
component.create();
expect(snackBarSpy).toHaveBeenCalledWith(errorMessage);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,24 @@ export class CreateNewPeerGroupingDialogComponent extends AuthorPeerGroupingDial
create(): Subscription {
this.peerGrouping.tag = this.peerGroupingAuthoringService.getUniqueTag();
this.updatePeerGroupingLogic();
return this.peerGroupingAuthoringService.createNewPeerGrouping(this.peerGrouping).subscribe(
() => {
return this.peerGroupingAuthoringService.createNewPeerGrouping(this.peerGrouping).subscribe({
next: () => {
this.dialogRef.close();
},
() => {
this.snackBar.open($localize`Please try again (Error: duplicate tag).`);
error: ({ error }) => {
this.handleError(error);
}
);
});
}

private handleError(error: any): void {
switch (error.messageCode) {
case 'genericError':
this.snackBar.open($localize`An error occurred. Please try again.`);
break;
case 'notAuthorized':
this.snackBar.open($localize`You are not allowed to perform this action.`);
break;
}
}
}
14 changes: 9 additions & 5 deletions src/messages.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2574,6 +2574,10 @@ Click &quot;Cancel&quot; to keep the invalid JSON open so you can fix it.</sourc
<context context-type="sourcefile">src/app/teacher/account/edit-profile/edit-profile.component.ts</context>
<context context-type="linenumber">149</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/assets/wise5/authoringTool/peer-grouping/create-new-peer-grouping-dialog/create-new-peer-grouping-dialog.component.ts</context>
<context context-type="linenumber">45</context>
</context-group>
</trans-unit>
<trans-unit id="3979fc2476b643d89e8a9f38d1bbc3014c4a4891" datatype="html">
<source>Password Changed</source>
Expand Down Expand Up @@ -11860,11 +11864,11 @@ Click &quot;Cancel&quot; to keep the invalid JSON open so you can fix it.</sourc
<context context-type="linenumber">63</context>
</context-group>
</trans-unit>
<trans-unit id="1612472698133518580" datatype="html">
<source>Please try again (Error: duplicate tag).</source>
<trans-unit id="8313195963105123811" datatype="html">
<source>You are not allowed to perform this action.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/assets/wise5/authoringTool/peer-grouping/create-new-peer-grouping-dialog/create-new-peer-grouping-dialog.component.ts</context>
<context context-type="linenumber">37</context>
<context context-type="linenumber">48</context>
</context-group>
</trans-unit>
<trans-unit id="f49dc696db7f816c672e23d20e563dd132a8e714" datatype="html">
Expand Down Expand Up @@ -12190,14 +12194,14 @@ Click &quot;Cancel&quot; to keep the invalid JSON open so you can fix it.</sourc
<source>Are you sure you want to delete the selected item?</source>
<context-group purpose="location">
<context context-type="sourcefile">src/assets/wise5/authoringTool/project-authoring/project-authoring.component.ts</context>
<context context-type="linenumber">95</context>
<context context-type="linenumber">105</context>
</context-group>
</trans-unit>
<trans-unit id="1189930234736223663" datatype="html">
<source>Are you sure you want to delete the <x id="PH" equiv-text="selectedNodeIds.length"/> selected items?</source>
<context-group purpose="location">
<context context-type="sourcefile">src/assets/wise5/authoringTool/project-authoring/project-authoring.component.ts</context>
<context context-type="linenumber">96</context>
<context context-type="linenumber">106</context>
</context-group>
</trans-unit>
<trans-unit id="e8fb2ceb6f8d4c3e90a6a688e9024461e67f44f0" datatype="html">
Expand Down
Loading