docbrown/spec/requests/admin/badge_achievements_spec.rb
Julianna Tetreault 24a3b50d4a
Add the Ability to Remove Badge Achievements From Users (#9896) [deploy]
* Moves badge_achievement-related code from /admin/bagdes to /admin/badge_achievements
  - Adds an Admin::BadgeAchievements::Controller
  - Moves #award and #award_badges to Admin::BadgeAchievements::Controller
  - Removes #award and #award_badges from Admin::Badges::Controller
  - Moves award.html.erb from /admin/badges to /admin/badge_achievements
  - Removes badge_achievement routes from /admin/badges
  - Adds a redirect for /admin/badges/badge_achievements to routes.rb
  - Cleans up and refactors code in controllers, views, and routes

* Add more actions to Admin::BadgeAchievements::Controller and matching routes
  - Adds an #index and #destory action to the BadgeAchievements::Controller
  - Adds destroy to the badge_achievements routes
  - Adjusts redirects for badge_achievements in routes.rb
  - Removes dead code from the index view for Badges
  - Refactors the existing code in the index view for BadgeAchievements

* Adds an /admin/badge_achievements_spec and cleans up the /admin/badges_spec
  - Removes the badge_achievement-related tests from badges_spec.rb
  - Adds an additional test around deleting badge_achievements

* Add pagination to badge_achievements index view

* Adds badge_achievements to Admin menu items and add comment to routes.rb
  -Add badge_achievements to the Admin nav bar
  -Add a comment regarding redirects for badge_achievements

* Resolve JS console stacktrace notices and change wording on deletion buttons

* Add a Back to Badge Achievements button to the badge award form

* Adjust styling to fix failing /admin/badges_spec.rb

* Uses SQL paging to optimize scalibility and adds search functionality to index.html.erb
  - Use SQL paging in Admin::BadgeAchievements::Controller in #index and #award
  - Add a search by user ID to Badge Achievements index view
  - Add a limit of 15 badges to be shown on the Badge Achievement index view
  - Add a User ID column to the Badge Achievement index view

* Add Award Badge button back to Badge Achievement index.html.erb

* Change @badge to be more explicit and reword warning around badge deletion

* Adds award badge button back, rewords success message, and adjusts SQL paging
2020-08-25 11:14:47 -06:00

92 lines
3.6 KiB
Ruby

require "rails_helper"
require "requests/shared_examples/internal_policy_dependant_request"
RSpec.describe "/admin/badges", type: :request do
let(:admin) { create(:user, :super_admin) }
let!(:badge) { create(:badge, title: "Not 'Hello, world!'") }
let(:params) do
{
badge: {
title: "Hello, world!",
slug: "greeting-badge",
description: "Awarded to welcoming users",
badge_image: Rack::Test::UploadedFile.new("spec/support/fixtures/images/image1.jpeg", "image/jpeg")
}
}
end
it_behaves_like "an InternalPolicy dependant request", Badge do
let(:request) { get "/admin/badges" }
end
describe "POST /admin/badge_achievements/award_badges" do
let(:user) { create(:user) }
let(:user2) { create(:user) }
let(:usernames_string) { "#{user.username}, #{user2.username}" }
let(:usernames_array) { [user.username, user2.username] }
before do
sign_in admin
allow(BadgeAchievements::BadgeAwardWorker).to receive(:perform_async)
end
it "awards badges" do
allow(BadgeAchievements::BadgeAwardWorker).to receive(:perform_async)
post admin_badge_achievements_award_badges_path, params: {
badge: badge.slug,
usernames: usernames_string,
message_markdown: "Hinder me? Thou fool. No living man may hinder me!"
}
expect(BadgeAchievements::BadgeAwardWorker).to have_received(:perform_async).with(
usernames_array, badge.slug, "Hinder me? Thou fool. No living man may hinder me!"
)
expect(request.flash[:success]).to include("Badges are being rewarded. The task will finish shortly.")
end
it "awards badges with default a message" do
allow(BadgeAchievements::BadgeAwardWorker).to receive(:perform_async)
post admin_badge_achievements_award_badges_path, params: {
badge: badge.slug,
usernames: usernames_string,
message_markdown: ""
}
expect(BadgeAchievements::BadgeAwardWorker).to have_received(:perform_async).with(usernames_array, badge.slug,
"Congrats!")
expect(request.flash[:success]).to include("Badges are being rewarded. The task will finish shortly.")
end
it "does not award a badge and raises an error if a badge is not specified" do
post admin_badge_achievements_award_badges_path, params: {
usernames: usernames_string,
message_markdown: ""
}
expect(BadgeAchievements::BadgeAwardWorker).not_to have_received(:perform_async).with(usernames_array,
badge.slug, "")
end
it "does not award a badge if the username provided is not lowercase" do
post admin_badge_achievements_award_badges_path, params: {
badge: badge.slug,
usernames: user.username.upcase,
message_markdown: ""
}
expect(BadgeAchievements::BadgeAwardWorker).not_to have_received(:perform_async).with(user.username.upcase,
badge.slug, "")
end
end
describe "DELETE /admin/badge_achievements/:id" do
let!(:badge_achievement) { create(:badge_achievement) }
before do
sign_in admin
end
it "deletes the badge_achievement" do
expect do
delete "/admin/badge_achievements/#{badge_achievement.id}"
end.to change { BadgeAchievement.all.count }.by(-1)
expect(response.body).to redirect_to "/admin/badge_achievements"
end
end
end