Skip to content

Commit

Permalink
Merge pull request #49 from FrenjaminBanklin/issue/48-lock-in-creator
Browse files Browse the repository at this point in the history
Adjust 'this person can edit this widget' checks on My Widgets page, locks widget instances in creator.
  • Loading branch information
clpetersonucf authored Apr 4, 2019
2 parents 7ae1421 + a89fc0a commit 8e96c09
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"statements": 40,
"branches": 29,
"functions": 45,
"lines": 39
"lines": 40
}
}
},
Expand Down
13 changes: 9 additions & 4 deletions src/js/controllers/ctrl-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,15 @@ app.controller('createCtrl', function(
}
}

const checkUserPublishPerms = () => {
const checkUserPublishPerms = widgetData => {
const deferred = $q.defer()
widgetSrv.canBePublishedByCurrentUser(widget_id).then(canPublish => {
$scope.canPublish = canPublish
deferred.resolve()

if (!widgetData.is_draft && !canPublish)
deferred.reject('Widget type can not be edited by students after publishing.')

deferred.resolve(widgetData)
})
return deferred.promise
}
Expand Down Expand Up @@ -563,10 +567,11 @@ ${msg.toLowerCase()}`,
getQset().then(() => {
if (!$scope.invalid) {
$q(resolve => resolve(inst_id))
.then(widgetSrv.lockWidget)
.then(widgetSrv.getWidget)
.then(checkUserPublishPerms)
.then(embed)
.then(initCreator)
.then(checkUserPublishPerms)
.then(showButtons)
.then(startHeartBeat)
.catch(onInitFail)
Expand All @@ -576,9 +581,9 @@ ${msg.toLowerCase()}`,
// initialize a new creator
$q(resolve => resolve(widget_id))
.then(widgetSrv.getWidgetInfo)
.then(checkUserPublishPerms)
.then(embed)
.then(initCreator)
.then(checkUserPublishPerms)
.then(showButtons)
.then(startHeartBeat)
.catch(onInitFail)
Expand Down
7 changes: 2 additions & 5 deletions src/js/controllers/ctrl-my-widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ app.controller('MyWidgetsController', function(
.all([
userServ.get(),
selectedWidgetSrv.getUserPermissions(),
selectedWidgetSrv.getPublishPermission(),
selectedWidgetSrv.getDateRanges()
])
.then(data => {
Expand All @@ -81,7 +80,6 @@ app.controller('MyWidgetsController', function(

$scope.user = data[0]
$scope.perms = data[1]
$scope.canPublish = data[2]
populateAccess()

$timeout.cancel(loadScoresTimout)
Expand Down Expand Up @@ -137,7 +135,6 @@ app.controller('MyWidgetsController', function(
$scope.selected.shareable = false
$scope.selected.hasScores = false
$scope.perms.collaborators = []
$scope.canPublish = false

// TODO
$scope.perms.error = false
Expand Down Expand Up @@ -263,7 +260,6 @@ app.controller('MyWidgetsController', function(
$scope.perms = {
collaborators: []
}
$scope.canPublish = false
$scope.show = {
collaborationModal: false,
availabilityModal: false,
Expand All @@ -272,7 +268,8 @@ app.controller('MyWidgetsController', function(
exportModal: false,
deleteDialog: false,
embedToggle: false,
editPublishedWarning: false
editPublishedWarning: false,
restrictedPublishWarning: false
}
$scope.SCORE_TAB_GRAPH = 0
$scope.SCORE_TAB_INDIVIDUAL = 1
Expand Down
40 changes: 27 additions & 13 deletions src/js/controllers/ctrl-selectedwidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,28 @@ app.controller('SelectedWidgetController', function(
})
}

const _editWidget = () => {
if ($scope.selected.editable) {
Materia.Coms.Json.send('widget_instance_lock', [$scope.selected.widget.id]).then(success => {
if (success) {
if ($scope.selected.widget.is_draft) {
window.location = $scope.selected.edit
} else {
$scope.show.editPublishedWarning = true
}
const _editWidgetPromise = () => {
return Materia.Coms.Json.send('widget_instance_edit_perms_verify', [
$scope.selected.widget.id
]).then(response => {
if (response.is_locked) {
$scope.alert.msg =
'This widget is currently locked, you will be able to edit this widget when it is no longer being edited by somebody else.'
} else {
if ($scope.selected.widget.is_draft) {
window.location = $scope.selected.edit
} else {
$scope.alert.msg =
'This widget is currently locked, you will be able to edit this widget when it is no longer being edited by somebody else.'
if (response.can_publish) $scope.show.editPublishedWarning = true
else $scope.show.restrictedPublishWarning = true
}
Please.$apply()
})
}
Please.$apply()
})
}

const _editWidget = () => {
if ($scope.selected.editable) {
_editWidgetPromise()
}

return false
Expand Down Expand Up @@ -225,4 +232,11 @@ app.controller('SelectedWidgetController', function(
$scope.enableOlderScores = _enableOlderScores

$scope.alert = Alert

/* develblock:start */
// these method are exposed for testing
$scope.jestTest = {
_editWidgetPromise
}
/* develblock:end */
})
33 changes: 33 additions & 0 deletions src/js/controllers/ctrl-selectedwidget.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ describe('adminWidgetController', () => {
$on: jest.fn()
}

Namespace('Materia.Coms.Json').send = jest.fn()

var controller = $controller('SelectedWidgetController', { $scope })
})

Expand Down Expand Up @@ -67,4 +69,35 @@ describe('adminWidgetController', () => {
$scope.hideModal.bind(this)()
expect(this.$parent.hideModal).toHaveBeenCalled()
})

it('does nothing if a widget is not editable', () => {
$scope.selected = {
editable: false
}

$scope.editWidget()

expect(Materia.Coms.Json.send).not.toHaveBeenCalled()
})

it('sets an alert message if the widget is locked already', () => {
$scope.selected = {
editable: true,
widget: {
id: 1
}
}

Namespace('Materia.Coms.Json').send = jest.fn().mockResolvedValueOnce({
is_locked: true,
can_publish: true
})

return $scope.jestTest._editWidgetPromise().then(() => {
expect(Materia.Coms.Json.send).toHaveBeenCalledWith('widget_instance_edit_perms_verify', [1])
expect($scope.alert.msg).toBe(
'This widget is currently locked, you will be able to edit this widget when it is no longer being edited by somebody else.'
)
})
})
})
10 changes: 0 additions & 10 deletions src/js/services/srv-selectedwidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,6 @@ app.service('selectedWidgetSrv', function($rootScope, $q, OBJECT_TYPES) {
})
}

const getPublishPermission = (widget_id = null) => {
if (widget_id === null) widget_id = _widget.widget.id
const deferred = $q.defer()
Materia.Coms.Json.send('widget_publish_perms_verify', [widget_id]).then(response => {
deferred.resolve(response)
})
return deferred.promise
}

const getPlayLogsForSemester = (term, year) => {
const deferred = $q.defer()

Expand Down Expand Up @@ -227,7 +218,6 @@ app.service('selectedWidgetSrv', function($rootScope, $q, OBJECT_TYPES) {
getSelectedId,
getScoreSummaries,
getUserPermissions,
getPublishPermission,
getPlayLogsForSemester,
getDateRanges,
getSemesterFromTimestamp,
Expand Down
22 changes: 21 additions & 1 deletion src/js/services/srv-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ app.service('widgetSrv', function(selectedWidgetSrv, dateTimeServ, $q, $rootScop
return Materia.Coms.Json.send('widgets_get', [[id]]).then(widgets => widgets[0])
}

const lockWidget = (id = null) => {
const deferred = $q.defer()
Materia.Coms.Json.send('widget_instance_lock', [id]).then(success => {
if (success) {
deferred.resolve(id)
} else {
deferred.reject(
'This widget is currently locked, you will be able to edit this widget when it is no longer being edited by somebody else.'
)
}
})
return deferred.promise
}

const getWidgetsByType = (type = 'featured') => {
return Materia.Coms.Json.send('widgets_get_by_type', [type])
}
Expand Down Expand Up @@ -234,14 +248,20 @@ app.service('widgetSrv', function(selectedWidgetSrv, dateTimeServ, $q, $rootScop
}

const canBePublishedByCurrentUser = widget_id => {
return selectedWidgetSrv.getPublishPermission(widget_id)
const deferred = $q.defer()
Materia.Coms.Json.send('widget_publish_perms_verify', [widget_id]).then(response => {
deferred.resolve(response)
})

return deferred.promise
}

return {
getWidgets,
getWidgetsByType,
getWidget,
getWidgetInfo,
lockWidget,
sortWidgets,
saveWidget,
removeWidget,
Expand Down
46 changes: 46 additions & 0 deletions src/js/services/srv-widget.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ describe('widgetSrv', () => {
expect(_service.getWidgetsByType).toBeDefined()
expect(_service.getWidget).toBeDefined()
expect(_service.getWidgetInfo).toBeDefined()
expect(_service.lockWidget).toBeDefined()
expect(_service.sortWidgets).toBeDefined()
expect(_service.saveWidget).toBeDefined()
expect(_service.removeWidget).toBeDefined()
Expand Down Expand Up @@ -401,4 +402,49 @@ describe('widgetSrv', () => {
$scope.$digest() // processes promise
expect(_selectedWidgetSrv.notifyAccessDenied).toHaveBeenCalled()
})

it('rejects with a message when a widget is already locked', () => {
mockSendPromiseOnce(false)

let promiseSpy = jest.fn()
let promiseCatch = jest.fn()
_service
.lockWidget(1)
.then(promiseSpy)
.catch(promiseCatch)
$scope.$digest() // processes promise

expect(Materia.Coms.Json.send).toHaveBeenCalledWith('widget_instance_lock', [1])
expect(promiseSpy).not.toHaveBeenCalled()
expect(promiseCatch).toHaveBeenCalledWith(
'This widget is currently locked, you will be able to edit this widget when it is no longer being edited by somebody else.'
)
})

it('locks a widget', () => {
mockSendPromiseOnce(true)

let promiseSpy = jest.fn()
let promiseCatch = jest.fn()
_service
.lockWidget(1)
.then(promiseSpy)
.catch(promiseCatch)
$scope.$digest() // processes promise

expect(Materia.Coms.Json.send).toHaveBeenCalledWith('widget_instance_lock', [1])
expect(promiseSpy).toHaveBeenCalledWith(1)
expect(promiseCatch).not.toHaveBeenCalled()
})

it('checks whether a widget can be published by the current user', () => {
mockSendPromiseOnce(true)

let promiseSpy = jest.fn()
_service.canBePublishedByCurrentUser(1).then(promiseSpy)
$scope.$digest() // processes promise

expect(Materia.Coms.Json.send).toHaveBeenCalledWith('widget_publish_perms_verify', [1])
expect(promiseSpy).toHaveBeenCalledWith(true)
})
})

0 comments on commit 8e96c09

Please sign in to comment.