Skip to content

Commit

Permalink
Addressing issue with first timers adding tags. (#9829)
Browse files Browse the repository at this point in the history
* 🐛 minor bug fix, addressing issue with first timers adding tags.

* ♻️ minor refactor, using function call to compare user role.

* ♻️🔥 remove logged_in_as function call in conditional statement.

* fixing failing tests by blocking first time posters from adding tags

Co-authored-by: 17sushmita <[email protected]>
  • Loading branch information
lonwabo-mnyaiza and 17sushmita authored Jun 23, 2021
1 parent 3a6c594 commit ba7b7bd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
2 changes: 2 additions & 0 deletions app/models/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,8 @@ def can_tag(tagname, user, errors = false)
errors ? I18n.t('node.page_does_not_exist') : false
elsif socials[one_split&.to_sym].present?
errors ? "This tag is used for associating a #{socials[one_split.to_sym]} account. <a href='https://publiclab.org/wiki/oauth'>Click here to read more </a>" : false
elsif user.first_time_poster && !(user.username == self.author.username || self.coauthors&.exists?(username: user.username) || (['admin', 'moderator'].include? user.role))
errors ? 'Adding tags to other people’s posts is not available to you until your own first post has been approved by site moderators' : false
else
true
end
Expand Down
21 changes: 9 additions & 12 deletions test/functional/tag_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,24 @@ def setup

# create accepts comma-delimited list of tags
test 'add one or two tags' do
UserSession.create(users(:bob))
UserSession.create(users(:jeff))

post :create, params: { name: 'mytag', nid: nodes(:one).nid, uid: users(:bob).id }
post :create, params: { name: 'mytag', nid: nodes(:one).nid }

assert_equal 'mytag', assigns[:tags].last.name
assert_redirected_to(nodes(:one).path)

post :create,
params: {
name: 'mysecondtag,mythirdtag',
nid: nodes(:one).nid,
uid: users(:bob).id
nid: nodes(:one).nid
}

assert_equal 'mysecondtag', assigns[:tags][assigns[:tags].length - 2].name
assert_equal 'mythirdtag', assigns[:tags].last.name
assert_redirected_to(nodes(:one).path)

post :create, params: { name: 'myfourthtag,myfifthtag', nid: nodes(:one).nid, uid: users(:bob).id }, xhr: true
post :create, params: { name: 'myfourthtag,myfifthtag', nid: nodes(:one).nid }, xhr: true

assert_response :success
assert_equal [['myfourthtag', Tag.find_by_name('myfourthtag').tid, nodes(:one).nid.to_s], ['myfifthtag', Tag.find_by_name('myfifthtag').tid, nodes(:one).nid.to_s]], JSON.parse(response.body)['saved']
Expand Down Expand Up @@ -91,7 +90,7 @@ def setup
end

test "won't add invalid tags" do
UserSession.create(users(:bob))
UserSession.create(users(:jeff))

post :create,
params: {
Expand Down Expand Up @@ -131,13 +130,12 @@ def setup

# create returns JSON list of errors in response[:errors]
test 'add duplicate tag' do
UserSession.create(users(:bob))
UserSession.create(users(:jeff))

post :create,
params: {
name: 'mytag',
nid: nodes(:one).nid,
uid: users(:bob)
}

assert_redirected_to(nodes(:one).path)
Expand All @@ -148,7 +146,6 @@ def setup
params: {
name: 'mytag',
nid: nodes(:one).nid,
uid: users(:bob)
}

assert_redirected_to(nodes(:one).path)
Expand Down Expand Up @@ -478,9 +475,9 @@ def setup

@controller = old_controller

UserSession.create(users(:bob))
post :create, params: { name: 'mytag', nid: nodes(:one).nid, uid: users(:bob) }
post :create, params: { name: 'mytag', nid: nodes(:one).nid, uid: users(:bob) }
UserSession.create(users(:jeff))
post :create, params: { name: 'mytag', nid: nodes(:one).nid }
post :create, params: { name: 'mytag', nid: nodes(:one).nid }
assert_equal I18n.t('tag_controller.tag_already_exists'), assigns[:output][:errors][0]
end
end
Expand Down

0 comments on commit ba7b7bd

Please sign in to comment.