docbrown/spec/services/user_role_service_spec.rb
Andy Zhao 22c9714919 V2 of Reports System (#441)
* Fix weird breakpoint top-nav issue

* Fix error handling when submitting blank note

* Change status closed to invalid

* Add ability to sort between status

* WIP

* Add working views for new report page

* Add mailer WIP

* Add MVP of new reports system

* Move collapse to the right & add show page link

* Fix buttons for index page

* Change collapse to collapse/expand

* Remove email functionality from new report

* Update copy of report abuse responses

* WIP of showing emails in reports

* Fix notes for user_role_service.rb

* Remove unused scripts from internal.html.erb

* Update tests for reports dashboard

* Add missing migration file

* Add finishing touches to reports dashboard

* Remove deprecated methods

* Update view with better timestamps

* Update and write new tests

* Add .codeclimate.yml

* Remove commented out editor thing

* Undo commented out code for recaptcha method

* Undo comment out code for development

* Use new comment path instead of old comment path
2018-09-04 17:08:41 -04:00

98 lines
3.7 KiB
Ruby

require "rails_helper"
RSpec.describe UserRoleService do
let(:user) { create(:user) }
let(:admin) { create(:user, :super_admin) }
let(:banned_user) { create(:user, :banned) }
describe "#check_for_roles" do
it "unbans a user if they were previously banned" do
described_class.new(banned_user, admin.id).check_for_roles(banned: "0")
expect(banned_user.has_role?(:banned)).to eq(false)
end
it "bans a user when there is a reason included" do
described_class.new(user, admin.id).
check_for_roles(banned: "1", reason_for_ban: "some reason")
expect(user.has_role?(:banned)).to eq(true)
end
it "gives an error if there was no reason for ban included" do
described_class.new(user, admin.id).check_for_roles(banned: "1")
expect(user.errors.messages[:reason_for_ban].first).
to eq("can't be blank if banned is checked")
end
it "gives an error if there was a reason for ban but banned was not checked" do
described_class.new(user, admin.id).
check_for_roles(banned: "0", reason_for_ban: "some reason")
expect(user.errors.messages[:banned].first).
to eq("was not checked but had the reason filled out")
end
it "warns a user when there is a reason included" do
described_class.new(user, admin.id).
check_for_roles(warned: "1", reason_for_warning: "some_reason")
expect(user.has_role?(:warned)).to eq(true)
end
it "gives an error if there was no reason for warning included" do
described_class.new(user, admin.id).check_for_roles(warned: "1")
expect(user.errors.messages[:reason_for_warning].first).
to eq("can't be blank if warned is checked")
end
it "gives an error if there was a reason for warning but warned was not checked" do
described_class.new(user, admin.id).
check_for_roles(warned: "0", reason_for_warning: "some reason")
expect(user.errors.messages[:warned].first).
to eq("was not checked but had the reason filled out")
end
end
describe "#new_roles?" do
it "adds the trusted role to a user with valid params" do
described_class.new(user, admin.id).send(:new_roles?, trusted: "1")
expect(user.has_role?(:trusted)).to eq(true)
end
it "adds the analytics role to a user with valid params" do
described_class.new(user, admin.id).send(:new_roles?, analytics: "1")
expect(user.has_role?(:analytics_beta_tester)).to eq(true)
end
it "adds the scholar role to a user with valid params" do
described_class.new(user, admin.id).send(:new_roles?, scholar: "1")
expect(user.has_role?(:workshop_pass)).to eq(true)
end
it "adds workshop_expiration date with valid params" do
expiration_date = Time.now + 1.year
described_class.new(user, admin.id).
send(:new_roles?, scholar: "1", workshop_expiration: expiration_date)
expect(user.workshop_expiration).to eq(expiration_date)
end
it "doesn't add a workshop_expiration date if scholar is not checked" do
expiration_date = Time.now + 1.year
described_class.new(user, admin.id).
send(:new_roles?, scholar: "0", workshop_expiration: expiration_date)
expect(user.workshop_expiration).to eq(nil)
end
end
describe "#create_or_update_note" do
note_params = {
reason: "banned",
content: "some reason",
noteable_type: "User",
}
it "updates a user's previous note" do
user.notes.create(note_params.merge(noteable_id: user.id, author_id: user.id))
described_class.new(user, admin.id).
send(:create_or_update_note, "banned", "a more specific reason")
expect(user.notes.count).to eq(1)
end
end
end