REFACTOR: user badges controller specs to requests (#5912)
This commit is contained in:
parent
79dcd79470
commit
22fcc04d38
|
@ -8,9 +8,9 @@ describe UserBadgesController do
|
|||
let(:badge) { Fabricate(:badge, target_posts: true, show_posts: false) }
|
||||
it 'does not leak private info' do
|
||||
p = create_post
|
||||
UserBadge.create(badge: badge, user: user, post_id: p.id, granted_by_id: -1, granted_at: Time.now)
|
||||
UserBadge.create!(badge: badge, user: user, post_id: p.id, granted_by_id: -1, granted_at: Time.now)
|
||||
|
||||
get :index, params: { badge_id: badge.id }, format: :json
|
||||
get "/user_badges.json", params: { badge_id: badge.id }
|
||||
expect(response).to be_success
|
||||
|
||||
parsed = JSON.parse(response.body)
|
||||
|
@ -21,7 +21,7 @@ describe UserBadgesController do
|
|||
|
||||
it "fails when badges are disabled" do
|
||||
SiteSetting.enable_badges = false
|
||||
get :index, params: { badge_id: badge.id }, format: :json
|
||||
get "/user_badges.json", params: { badge_id: badge.id }
|
||||
expect(response.status).to eq(404)
|
||||
end
|
||||
end
|
||||
|
@ -30,13 +30,12 @@ describe UserBadgesController do
|
|||
let!(:user_badge) { UserBadge.create(badge: badge, user: user, granted_by: Discourse.system_user, granted_at: Time.now) }
|
||||
|
||||
it 'requires username or badge_id to be specified' do
|
||||
expect do
|
||||
get :index, format: :json
|
||||
end.to raise_error(ActionController::ParameterMissing)
|
||||
get "/user_badges.json"
|
||||
expect(response.status).to eq(400)
|
||||
end
|
||||
|
||||
it 'returns user_badges for a user' do
|
||||
get :username, params: { username: user.username }, format: :json
|
||||
get "/user-badges/#{user.username}.json"
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
parsed = JSON.parse(response.body)
|
||||
|
@ -44,7 +43,7 @@ describe UserBadgesController do
|
|||
end
|
||||
|
||||
it 'returns user_badges for a badge' do
|
||||
get :index, params: { badge_id: badge.id }, format: :json
|
||||
get "/user_badges.json", params: { badge_id: badge.id }
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
parsed = JSON.parse(response.body)
|
||||
|
@ -52,9 +51,9 @@ describe UserBadgesController do
|
|||
end
|
||||
|
||||
it 'includes counts when passed the aggregate argument' do
|
||||
get :username, params: {
|
||||
username: user.username, grouped: true
|
||||
}, format: :json
|
||||
get "/user-badges/#{user.username}.json", params: {
|
||||
grouped: true
|
||||
}
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
parsed = JSON.parse(response.body)
|
||||
|
@ -64,17 +63,16 @@ describe UserBadgesController do
|
|||
|
||||
context 'create' do
|
||||
it 'requires username to be specified' do
|
||||
expect do
|
||||
post :create, params: { badge_id: badge.id }, format: :json
|
||||
end.to raise_error(ActionController::ParameterMissing)
|
||||
post "/user_badges.json", params: { badge_id: badge.id }
|
||||
expect(response.status).to eq(400)
|
||||
end
|
||||
|
||||
it 'does not allow regular users to grant badges' do
|
||||
log_in_user Fabricate(:user)
|
||||
sign_in(Fabricate(:user))
|
||||
|
||||
post :create, params: {
|
||||
post "/user_badges.json", params: {
|
||||
badge_id: badge.id, username: user.username
|
||||
}, format: :json
|
||||
}
|
||||
|
||||
expect(response.status).to eq(403)
|
||||
end
|
||||
|
@ -83,15 +81,13 @@ describe UserBadgesController do
|
|||
admin = Fabricate(:admin)
|
||||
post_1 = create_post
|
||||
|
||||
log_in_user admin
|
||||
sign_in(admin)
|
||||
|
||||
StaffActionLogger.any_instance.expects(:log_badge_grant).once
|
||||
|
||||
post :create, params: {
|
||||
post "/user_badges.json", params: {
|
||||
badge_id: badge.id,
|
||||
username: user.username,
|
||||
reason: Discourse.base_url + post_1.url
|
||||
}, format: :json
|
||||
}
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
|
||||
|
@ -100,43 +96,43 @@ describe UserBadgesController do
|
|||
expect(user_badge).to be_present
|
||||
expect(user_badge.granted_by).to eq(admin)
|
||||
expect(user_badge.post_id).to eq(post_1.id)
|
||||
expect(UserHistory.where(acting_user: admin, target_user: user).count).to eq(1)
|
||||
end
|
||||
|
||||
it 'does not grant badges from regular api calls' do
|
||||
Fabricate(:api_key, user: user)
|
||||
|
||||
post :create, params: {
|
||||
post "/user_badges.json", params: {
|
||||
badge_id: badge.id, username: user.username, api_key: user.api_key.key
|
||||
}, format: :json
|
||||
}
|
||||
|
||||
expect(response.status).to eq(403)
|
||||
end
|
||||
|
||||
it 'grants badges from master api calls' do
|
||||
api_key = Fabricate(:api_key)
|
||||
StaffActionLogger.any_instance.expects(:log_badge_grant).never
|
||||
|
||||
post :create, params: {
|
||||
post "/user_badges.json", params: {
|
||||
badge_id: badge.id, username: user.username, api_key: api_key.key, api_username: "system"
|
||||
}, format: :json
|
||||
}
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
user_badge = UserBadge.find_by(user: user, badge: badge)
|
||||
expect(user_badge).to be_present
|
||||
expect(user_badge.granted_by).to eq(Discourse.system_user)
|
||||
expect(UserHistory.where(acting_user: Discourse.system_user, target_user: user).count).to eq(0)
|
||||
end
|
||||
|
||||
it 'will trigger :user_badge_granted' do
|
||||
log_in :admin
|
||||
user
|
||||
sign_in(Fabricate(:admin))
|
||||
|
||||
event = DiscourseEvent.track_events do
|
||||
post :create, params: {
|
||||
events = DiscourseEvent.track_events do
|
||||
post "/user_badges.json", params: {
|
||||
badge_id: badge.id, username: user.username
|
||||
}, format: :json
|
||||
end.first
|
||||
}
|
||||
end.map { |event| event[:event_name] }
|
||||
|
||||
expect(event[:event_name]).to eq(:user_badge_granted)
|
||||
expect(events).to include(:user_badge_granted)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -144,26 +140,28 @@ describe UserBadgesController do
|
|||
let!(:user_badge) { UserBadge.create(badge: badge, user: user, granted_by: Discourse.system_user, granted_at: Time.now) }
|
||||
|
||||
it 'checks that the user is authorized to revoke a badge' do
|
||||
delete :destroy, params: { id: user_badge.id }, format: :json
|
||||
delete "/user_badges/#{user_badge.id}.json"
|
||||
expect(response.status).to eq(403)
|
||||
end
|
||||
|
||||
it 'revokes the badge' do
|
||||
log_in :admin
|
||||
StaffActionLogger.any_instance.expects(:log_badge_revoke).once
|
||||
delete :destroy, params: { id: user_badge.id }, format: :json
|
||||
admin = Fabricate(:admin)
|
||||
sign_in(admin)
|
||||
delete "/user_badges/#{user_badge.id}.json"
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
expect(UserBadge.find_by(id: user_badge.id)).to eq(nil)
|
||||
expect(UserHistory.where(acting_user: admin, target_user: user).count).to eq(1)
|
||||
end
|
||||
|
||||
it 'will trigger :user_badge_removed' do
|
||||
log_in :admin
|
||||
sign_in(Fabricate(:admin))
|
||||
|
||||
event = DiscourseEvent.track_events do
|
||||
delete :destroy, params: { id: user_badge.id }, format: :json
|
||||
end.first
|
||||
events = DiscourseEvent.track_events do
|
||||
delete "/user_badges/#{user_badge.id}.json"
|
||||
end.map { |event| event[:event_name] }
|
||||
|
||||
expect(event[:event_name]).to eq(:user_badge_removed)
|
||||
expect(events).to include(:user_badge_removed)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue