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) }
|
let(:badge) { Fabricate(:badge, target_posts: true, show_posts: false) }
|
||||||
it 'does not leak private info' do
|
it 'does not leak private info' do
|
||||||
p = create_post
|
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
|
expect(response).to be_success
|
||||||
|
|
||||||
parsed = JSON.parse(response.body)
|
parsed = JSON.parse(response.body)
|
||||||
|
@ -21,7 +21,7 @@ describe UserBadgesController do
|
||||||
|
|
||||||
it "fails when badges are disabled" do
|
it "fails when badges are disabled" do
|
||||||
SiteSetting.enable_badges = false
|
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)
|
expect(response.status).to eq(404)
|
||||||
end
|
end
|
||||||
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) }
|
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
|
it 'requires username or badge_id to be specified' do
|
||||||
expect do
|
get "/user_badges.json"
|
||||||
get :index, format: :json
|
expect(response.status).to eq(400)
|
||||||
end.to raise_error(ActionController::ParameterMissing)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns user_badges for a user' do
|
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)
|
expect(response.status).to eq(200)
|
||||||
parsed = JSON.parse(response.body)
|
parsed = JSON.parse(response.body)
|
||||||
|
@ -44,7 +43,7 @@ describe UserBadgesController do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns user_badges for a badge' do
|
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)
|
expect(response.status).to eq(200)
|
||||||
parsed = JSON.parse(response.body)
|
parsed = JSON.parse(response.body)
|
||||||
|
@ -52,9 +51,9 @@ describe UserBadgesController do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'includes counts when passed the aggregate argument' do
|
it 'includes counts when passed the aggregate argument' do
|
||||||
get :username, params: {
|
get "/user-badges/#{user.username}.json", params: {
|
||||||
username: user.username, grouped: true
|
grouped: true
|
||||||
}, format: :json
|
}
|
||||||
|
|
||||||
expect(response.status).to eq(200)
|
expect(response.status).to eq(200)
|
||||||
parsed = JSON.parse(response.body)
|
parsed = JSON.parse(response.body)
|
||||||
|
@ -64,17 +63,16 @@ describe UserBadgesController do
|
||||||
|
|
||||||
context 'create' do
|
context 'create' do
|
||||||
it 'requires username to be specified' do
|
it 'requires username to be specified' do
|
||||||
expect do
|
post "/user_badges.json", params: { badge_id: badge.id }
|
||||||
post :create, params: { badge_id: badge.id }, format: :json
|
expect(response.status).to eq(400)
|
||||||
end.to raise_error(ActionController::ParameterMissing)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'does not allow regular users to grant badges' do
|
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
|
badge_id: badge.id, username: user.username
|
||||||
}, format: :json
|
}
|
||||||
|
|
||||||
expect(response.status).to eq(403)
|
expect(response.status).to eq(403)
|
||||||
end
|
end
|
||||||
|
@ -83,15 +81,13 @@ describe UserBadgesController do
|
||||||
admin = Fabricate(:admin)
|
admin = Fabricate(:admin)
|
||||||
post_1 = create_post
|
post_1 = create_post
|
||||||
|
|
||||||
log_in_user admin
|
sign_in(admin)
|
||||||
|
|
||||||
StaffActionLogger.any_instance.expects(:log_badge_grant).once
|
post "/user_badges.json", params: {
|
||||||
|
|
||||||
post :create, params: {
|
|
||||||
badge_id: badge.id,
|
badge_id: badge.id,
|
||||||
username: user.username,
|
username: user.username,
|
||||||
reason: Discourse.base_url + post_1.url
|
reason: Discourse.base_url + post_1.url
|
||||||
}, format: :json
|
}
|
||||||
|
|
||||||
expect(response.status).to eq(200)
|
expect(response.status).to eq(200)
|
||||||
|
|
||||||
|
@ -100,43 +96,43 @@ describe UserBadgesController do
|
||||||
expect(user_badge).to be_present
|
expect(user_badge).to be_present
|
||||||
expect(user_badge.granted_by).to eq(admin)
|
expect(user_badge.granted_by).to eq(admin)
|
||||||
expect(user_badge.post_id).to eq(post_1.id)
|
expect(user_badge.post_id).to eq(post_1.id)
|
||||||
|
expect(UserHistory.where(acting_user: admin, target_user: user).count).to eq(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'does not grant badges from regular api calls' do
|
it 'does not grant badges from regular api calls' do
|
||||||
Fabricate(:api_key, user: user)
|
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
|
badge_id: badge.id, username: user.username, api_key: user.api_key.key
|
||||||
}, format: :json
|
}
|
||||||
|
|
||||||
expect(response.status).to eq(403)
|
expect(response.status).to eq(403)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'grants badges from master api calls' do
|
it 'grants badges from master api calls' do
|
||||||
api_key = Fabricate(:api_key)
|
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"
|
badge_id: badge.id, username: user.username, api_key: api_key.key, api_username: "system"
|
||||||
}, format: :json
|
}
|
||||||
|
|
||||||
expect(response.status).to eq(200)
|
expect(response.status).to eq(200)
|
||||||
user_badge = UserBadge.find_by(user: user, badge: badge)
|
user_badge = UserBadge.find_by(user: user, badge: badge)
|
||||||
expect(user_badge).to be_present
|
expect(user_badge).to be_present
|
||||||
expect(user_badge.granted_by).to eq(Discourse.system_user)
|
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
|
end
|
||||||
|
|
||||||
it 'will trigger :user_badge_granted' do
|
it 'will trigger :user_badge_granted' do
|
||||||
log_in :admin
|
sign_in(Fabricate(:admin))
|
||||||
user
|
|
||||||
|
|
||||||
event = DiscourseEvent.track_events do
|
events = DiscourseEvent.track_events do
|
||||||
post :create, params: {
|
post "/user_badges.json", params: {
|
||||||
badge_id: badge.id, username: user.username
|
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
|
||||||
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) }
|
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
|
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)
|
expect(response.status).to eq(403)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'revokes the badge' do
|
it 'revokes the badge' do
|
||||||
log_in :admin
|
admin = Fabricate(:admin)
|
||||||
StaffActionLogger.any_instance.expects(:log_badge_revoke).once
|
sign_in(admin)
|
||||||
delete :destroy, params: { id: user_badge.id }, format: :json
|
delete "/user_badges/#{user_badge.id}.json"
|
||||||
|
|
||||||
expect(response.status).to eq(200)
|
expect(response.status).to eq(200)
|
||||||
expect(UserBadge.find_by(id: user_badge.id)).to eq(nil)
|
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
|
end
|
||||||
|
|
||||||
it 'will trigger :user_badge_removed' do
|
it 'will trigger :user_badge_removed' do
|
||||||
log_in :admin
|
sign_in(Fabricate(:admin))
|
||||||
|
|
||||||
event = DiscourseEvent.track_events do
|
events = DiscourseEvent.track_events do
|
||||||
delete :destroy, params: { id: user_badge.id }, format: :json
|
delete "/user_badges/#{user_badge.id}.json"
|
||||||
end.first
|
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
|
end
|
||||||
end
|
end
|
Loading…
Reference in New Issue