Skip to content

Commit

Permalink
Merge pull request #1089 from CruGlobal/1112780-designation-editor-si…
Browse files Browse the repository at this point in the history
…gn-in

[HS-1112780] Force sign-in after 422 response in designation editor
  • Loading branch information
canac authored Mar 7, 2024
2 parents baa8293 + 5d15986 commit 27e3f00
Showing 2 changed files with 70 additions and 6 deletions.
18 changes: 16 additions & 2 deletions src/app/designationEditor/designationEditor.component.js
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ import sessionEnforcerService, {
EnforcerCallbacks,
EnforcerModes
} from 'common/services/session/sessionEnforcer.service'
import sessionModalService from 'common/services/session/sessionModal.service'
import sessionService, { Roles } from 'common/services/session/session.service'
import designationEditorService from 'common/services/api/designationEditor.service'

@@ -37,12 +38,13 @@ const componentName = 'designationEditor'

class DesignationEditorController {
/* @ngInject */
constructor ($scope, $log, $q, $uibModal, $location, $window, $timeout, envService, sessionService, sessionEnforcerService, designationEditorService) {
constructor ($scope, $log, $q, $uibModal, $location, $window, $timeout, envService, sessionService, sessionEnforcerService, sessionModalService, designationEditorService) {
this.$scope = $scope
this.$log = $log
this.$timeout = $timeout
this.sessionService = sessionService
this.sessionEnforcerService = sessionEnforcerService
this.sessionModalService = sessionModalService
this.designationEditorService = designationEditorService

this.imgDomain = envService.read('imgDomain')
@@ -93,17 +95,28 @@ class DesignationEditorController {
// get designation photos
this.designationEditorService.getPhotos(this.designationNumber)
]).then(responses => {
this.contentLoaded = true
this.loadingOverlay = false
this.designationContent = responses[0].data
this.designationPhotos = responses[1].data
this.carouselImages = this.extractCarouselUrls()
this.updateCarousel()
}, error => {
if (error.status === 422 && !this.retried) {
return this.sessionModalService.open('sign-in', {
backdrop: 'static',
keyboard: false
}).result.then(() => {
this.retried = true
return this.getDesignationContent()
})
}

this.contentLoaded = false
this.loadingOverlay = false
this.loadingContentError = true
this.$log.error('Error loading designation content or photos.', error)
}).finally(() => {
this.retried = false
})
}

@@ -391,6 +404,7 @@ export default angular
commonModule.name,
sessionService.name,
sessionEnforcerService.name,
sessionModalService.name,
designationEditorService.name,
titleModalController.name,
pageOptionsModalController.name,
58 changes: 54 additions & 4 deletions src/app/designationEditor/designationEditor.spec.js
Original file line number Diff line number Diff line change
@@ -156,11 +156,11 @@ describe('Designation Editor', function () {
})

describe('getDesignationContent()', () => {
it('to skip if no designation number', () => {
it('should skip if no designation number', () => {
expect($ctrl.getDesignationContent()).toEqual(undefined)
})

it('to getDesignationContent', () => {
it('should load designation content and photos', (done) => {
$ctrl.designationNumber = designationSecurityResponse.designationNumber

$httpBackend.expectGET(designationConstants.designationEndpoint + '?designationNumber=' + designationSecurityResponse.designationNumber)
@@ -170,9 +170,59 @@ describe('Designation Editor', function () {

$ctrl.getDesignationContent().then(() => {
expect($ctrl.designationContent).toBeDefined()
expect($ctrl.contentLoaded).toEqual(true)
expect($ctrl.contentLoaded).toEqual(false)
expect($ctrl.loadingContentError).toEqual(false)
})
done()
}).catch(done)
$httpBackend.flush()
})

it('should sign in and try again if the response is 422', (done) => {
$ctrl.designationNumber = designationSecurityResponse.designationNumber

$httpBackend.expectGET(designationConstants.designationEndpoint + '?designationNumber=' + designationSecurityResponse.designationNumber)
.respond(422, null)
$httpBackend.expectGET(designationConstants.designationImagesEndpoint + '?designationNumber=' + designationSecurityResponse.designationNumber)
.respond(422, null)

const signInDeferred = $q.defer()
jest.spyOn($ctrl.sessionModalService, 'open').mockReturnValue({ result: signInDeferred.promise })

$ctrl.getDesignationContent().then(() => {
expect($ctrl.sessionModalService.open).toHaveBeenCalled()
expect($ctrl.designationContent).toBeDefined()
expect($ctrl.contentLoaded).toEqual(false)
expect($ctrl.loadingContentError).toEqual(false)
done()
}).catch(done)
$httpBackend.flush()

$httpBackend.expectGET(designationConstants.designationEndpoint + '?designationNumber=' + designationSecurityResponse.designationNumber)
.respond(200, designationSecurityResponse)
$httpBackend.expectGET(designationConstants.designationImagesEndpoint + '?designationNumber=' + designationSecurityResponse.designationNumber)
.respond(200, [])
signInDeferred.resolve()
$httpBackend.flush()
})

it('should only retry once if the response is repeatedly 422', (done) => {
$ctrl.designationNumber = designationSecurityResponse.designationNumber

$httpBackend.whenGET(designationConstants.designationEndpoint + '?designationNumber=' + designationSecurityResponse.designationNumber)
.respond(422, null)
$httpBackend.whenGET(designationConstants.designationImagesEndpoint + '?designationNumber=' + designationSecurityResponse.designationNumber)
.respond(422, null)

jest.spyOn($ctrl.sessionModalService, 'open').mockReturnValue({ result: $q.resolve() })

$ctrl.getDesignationContent().then(() => {
expect($ctrl.sessionModalService.open).toHaveBeenCalledTimes(1)
expect($ctrl.designationContent).toBeUndefined()
expect($ctrl.contentLoaded).toEqual(false)
expect($ctrl.loadingContentError).toEqual(true)
done()
}).catch(done)
$httpBackend.flush()
$httpBackend.flush()
})

0 comments on commit 27e3f00

Please sign in to comment.