Skip to content

Commit

Permalink
Tests for tag locking (publiclab#9772)
Browse files Browse the repository at this point in the history
* added test for adding locked tag

* admins can add locked tag test

* test for admin can add locked tag

* added test for moderator

* made changes suggested in PR publiclab#9709
  • Loading branch information
Manasa2850 authored and billymoroney1 committed Dec 28, 2021
1 parent a7b442d commit 1b1702b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
6 changes: 3 additions & 3 deletions app/controllers/tag_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def delete
node_tag = NodeTag.where(nid: params[:nid], tid: params[:tid]).first
node = Node.where(nid: params[:nid]).first
# only admins, mods, and tag authors can delete other peoples' tags
if node_tag.uid == current_user.uid || logged_in_as(['admin', 'moderator']) || (node.uid == current_user.uid && node_tag.name != "locked")
if (node_tag.uid == current_user.uid && !node.has_tag('locked')) || logged_in_as(['admin', 'moderator']) || (node.uid == current_user.uid && !node.has_tag('locked'))

tag = Tag.joins(:node_tag)
.select('term_data.name')
Expand All @@ -385,8 +385,8 @@ def delete
end
end
end
elsif node_tag.name == "locked"
flash[:error] = "Only admins can delete the locked tag."
elsif node.has_tag('locked')
flash[:error] = "Only admins can delete tags on locked pages."
redirect_to Node.find_by(nid: params[:nid]).path
else
flash[:error] = I18n.t('tag_controller.must_own_tag_to_delete')
Expand Down
2 changes: 1 addition & 1 deletion app/models/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ def can_tag(tagname, user, errors = false)
errors ? "Only admins may create raw pages." : false
elsif tagname[0..4] == 'rsvp:' && user.username != one_split
errors ? I18n.t('node.only_RSVP_for_yourself') : false
elsif tagname == 'locked' && user.role != 'admin'
elsif tagname == 'locked' && !user.can_moderate?
errors ? I18n.t('node.only_admins_can_lock') : false
elsif tagname == 'blog' && user.role != 'admin' && user.role != 'moderator'
errors ? 'Only moderators or admins can use this tag.' : false
Expand Down
30 changes: 30 additions & 0 deletions test/functional/tag_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,36 @@ def setup
assert_response :success
end

test 'normal users cannot add the locked tag' do
UserSession.create(users(:bob))
post :create,
params: {
name: 'locked',
nid: nodes(:one).nid
}
assert_equal 'Error: only admins can lock pages.', assigns[:output][:errors][0]
end

test 'admin can add the locked tag' do
UserSession.create(users(:admin))
post :create,
params: {
name: 'locked',
nid: nodes(:one).nid
}
assert nodes(:one).has_tag('locked')
end

test 'moderator can add the locked tag' do
UserSession.create(users(:moderator))
post :create,
params: {
name: 'locked',
nid: nodes(:one).nid
}
assert nodes(:one).has_tag('locked')
end

test 'validate unused tag' do
UserSession.create(users(:bob))

Expand Down

0 comments on commit 1b1702b

Please sign in to comment.