Add test to mark profile as spam (#20800)
* add test to mark profile as spam * rubocop * move test
This commit is contained in:
parent
ed1b4045d3
commit
f6161780d9
2 changed files with 73 additions and 3 deletions
|
|
@ -267,11 +267,11 @@ class UsersController < ApplicationController
|
|||
end
|
||||
|
||||
def toggle_spam
|
||||
@target_user = User.find_by(id: params[:id])
|
||||
render json, status: :not_found unless @target_user
|
||||
|
||||
authorize @current_user
|
||||
|
||||
@target_user = User.find_by(id: params[:id])
|
||||
error_not_found and return unless @target_user
|
||||
|
||||
begin
|
||||
case request.method_symbol
|
||||
when :put
|
||||
|
|
|
|||
70
spec/requests/admin_user_marks_user_as_spam_spec.rb
Normal file
70
spec/requests/admin_user_marks_user_as_spam_spec.rb
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe "Spam Toggling for User", type: :request do
|
||||
let!(:user) { create(:user) }
|
||||
let!(:admin) { create(:user, :admin) }
|
||||
let!(:non_admin) { create(:user) }
|
||||
let!(:spam_user) { create(:user, :spam) }
|
||||
|
||||
describe "PUT /users/:id/spam" do
|
||||
context "when user exists" do
|
||||
before do
|
||||
sign_in admin
|
||||
put spam_user_path(user)
|
||||
end
|
||||
|
||||
it "marks user as spam" do
|
||||
expect(response).to have_http_status(:no_content)
|
||||
expect(user.reload).to be_spam
|
||||
end
|
||||
end
|
||||
|
||||
context "when user does not exist" do
|
||||
before { sign_in admin }
|
||||
|
||||
it "returns a not found status" do
|
||||
put spam_user_path(id: -1)
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
end
|
||||
|
||||
context "when unauthorized" do
|
||||
before { sign_in non_admin }
|
||||
|
||||
it "prevents non-admins from marking a user as spam" do
|
||||
expect { put spam_user_path(user) }.to raise_error(Pundit::NotAuthorizedError)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "DELETE /users/:id/spam" do
|
||||
context "when user exists and is marked as spam" do
|
||||
before do
|
||||
sign_in admin
|
||||
delete spam_user_path(spam_user)
|
||||
end
|
||||
|
||||
it "removes user from spam" do
|
||||
expect(response).to have_http_status(:no_content)
|
||||
expect(spam_user.reload).not_to be_spam
|
||||
end
|
||||
end
|
||||
|
||||
context "when user does not exist" do
|
||||
before { sign_in admin }
|
||||
|
||||
it "returns a not found status" do
|
||||
delete spam_user_path(id: -1) # Changed to a likely non-existent ID
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
end
|
||||
|
||||
context "when unauthorized" do
|
||||
before { sign_in non_admin }
|
||||
|
||||
it "prevents non-admins from removing a user from spam" do
|
||||
expect { delete spam_user_path(user) }.to raise_error(Pundit::NotAuthorizedError)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue