Skip to content

Commit

Permalink
599 add fosterer role (#641)
Browse files Browse the repository at this point in the history
* Add FOSTERER_PERMISSIONS

* Move the role assignment callback from the AdopterFosterAccount factory to the User factory, and make default user type for AdopterFosterAccount factory :adopter

* Add fosterer factory

* Add fosterer context to policy tests

* Add staff and staff_admin contexts to Organizations::AdoptablePetPolicyTest

* Refactor staff and staff_admin factories to move role association logic from account factory to user factory

* Add dashboard permission to FOSTERER_PERMISSIONS
  • Loading branch information
mononoken authored Apr 15, 2024
1 parent 6cd0bee commit 02fe736
Show file tree
Hide file tree
Showing 21 changed files with 361 additions and 22 deletions.
7 changes: 7 additions & 0 deletions app/models/concerns/authorizable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
manage_adopter_foster_profiles
].freeze

FOSTERER_PERMISSIONS = %i[
view_adopter_foster_dashboard
create_adopter_foster_profiles
manage_adopter_foster_profiles
].freeze

STAFF_PERMISSIONS = (
ADOPTER_PERMISSIONS.excluding(
%i[
Expand Down Expand Up @@ -39,6 +45,7 @@

PERMISSIONS = {
adopter: ADOPTER_PERMISSIONS,
fosterer: FOSTERER_PERMISSIONS,
staff: STAFF_PERMISSIONS,
admin: ADMIN_PERMISSIONS
}.freeze
Expand Down
6 changes: 1 addition & 5 deletions test/factories/adopter_foster_accounts.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
FactoryBot.define do
factory :adopter_foster_account do
user { association :user }
user { association :adopter }

trait :with_profile do
adopter_foster_profile do
association :adopter_foster_profile, adopter_foster_account: instance
end
end

after(:build) do |_account, context|
context.user.add_role(:adopter, context.user.organization)
end
end
end
12 changes: 1 addition & 11 deletions test/factories/staff_accounts.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
FactoryBot.define do
factory :staff_account do
user { association :user }
user { association :staff }
deactivated_at { nil }

trait :deactivated do
deactivated_at { DateTime.now }
end

trait :admin do
after(:build) do |_account, context|
context.user.add_role(:admin, context.organization)
end
end

after(:build) do |_account, context|
context.user.add_role(:staff, context.organization)
end
end
end
31 changes: 30 additions & 1 deletion test/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@
association :adopter_foster_account, :with_profile, user: instance
end
end

after(:build) do |user, _context|
user.add_role(:adopter, user.organization)
end
end

factory :fosterer do
adopter_foster_account do
association :adopter_foster_account, user: instance
end

trait :with_profile do
adopter_foster_account do
association :adopter_foster_account, :with_profile, user: instance
end
end

after(:build) do |user, _context|
user.add_role(:fosterer, user.organization)
end
end

factory :staff do
Expand All @@ -29,11 +49,20 @@
association :staff_account, :deactivated, user: instance
end
end

after(:build) do |user, _context|
user.add_role(:staff, user.organization)
end
end

factory :staff_admin do
staff_account do
association :staff_account, :admin, user: instance
association :staff_account, user: instance
end

after(:build) do |user, _context|
user.add_role(:staff, user.organization)
user.add_role(:admin, user.organization)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/integration/adoptable_pet_show_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AdoptablePetShowTest < ActionDispatch::IntegrationTest

context "staff" do
setup do
sign_in create(:staff_account).user
sign_in create(:staff)
end

should "see an available pet" do
Expand Down
3 changes: 1 addition & 2 deletions test/integration/adoption_application_reviews_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class AdoptionApplicationReviewsTest < ActionDispatch::IntegrationTest

context "active staff" do
setup do
@staff_account = create(:staff_account)
sign_in @staff_account.user
sign_in create(:staff)
end

should "see all applications" do
Expand Down
30 changes: 30 additions & 0 deletions test/policies/adopter_application_policy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ class AdopterApplicationPolicyTest < ActiveSupport::TestCase
assert_equal @action.call, true
end
end

context "when user is fosterer with profile" do
setup do
@user = create(:fosterer, :with_profile)
end

should "return false" do
assert_equal @action.call, false
end
end
end

context "#create?" do
Expand Down Expand Up @@ -176,6 +186,16 @@ class AdopterApplicationPolicyTest < ActiveSupport::TestCase
end
end
end

context "when user is fosterer with profile" do
setup do
@user = create(:fosterer, :with_profile)
end

should "return false" do
assert_equal @action.call, false
end
end
end

context "existing record action" do
Expand Down Expand Up @@ -212,6 +232,16 @@ class AdopterApplicationPolicyTest < ActiveSupport::TestCase
end
end

context "when user is fosterer" do
setup do
@user = create(:fosterer)
end

should "return false" do
assert_equal @action.call, false
end
end

context "when user is active staff" do
setup do
@user = create(:staff)
Expand Down
10 changes: 10 additions & 0 deletions test/policies/attachment_policy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ class ActiveStorage::AttachmentPolicyTest < ActiveSupport::TestCase
end
end

context "when user is fosterer" do
setup do
@user = create(:fosterer)
end

should "return false" do
assert_equal @action.call, false
end
end

context "when user is active staff" do
setup do
@user = create(:staff)
Expand Down
60 changes: 58 additions & 2 deletions test/policies/organizations/adoptable_pet_policy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ class Organizations::AdoptablePetPolicyTest < ActiveSupport::TestCase
end
end

context "when user is fosterer" do
setup do
@user = create(:fosterer)
end

should "return false" do
assert_equal @action.call, false
end
end

context "when user is staff" do
setup do
@user = create(:staff)
Expand Down Expand Up @@ -82,8 +92,54 @@ class Organizations::AdoptablePetPolicyTest < ActiveSupport::TestCase
@user = create(:adopter, :with_profile)
end

should "return false" do
assert_equal @action.call, false
context "when user is nil" do
setup do
@user = nil
end

should "return false" do
assert_equal @action.call, false
end
end

context "when user is adopter" do
setup do
@user = create(:adopter)
end

should "return false" do
assert_equal @action.call, false
end
end

context "when user is fosterer" do
setup do
@user = create(:fosterer)
end

should "return false" do
assert_equal @action.call, false
end
end

context "when user is staff" do
setup do
@user = create(:staff)
end

should "return true" do
assert_equal @action.call, true
end
end

context "when user is admin" do
setup do
@user = create(:staff_admin)
end

should "return true" do
assert_equal @action.call, true
end
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions test/policies/organizations/adopter_application_policy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ class Organizations::AdopterApplicationPolicyTest < ActiveSupport::TestCase
end
end

context "when user is fosterer" do
setup do
@user = create(:fosterer)
end

should "return false" do
assert_equal @action.call, false
end
end

context "when user is activated staff" do
setup do
@user = create(:staff)
Expand Down Expand Up @@ -136,6 +146,16 @@ class Organizations::AdopterApplicationPolicyTest < ActiveSupport::TestCase
end
end

context "when user is fosterer" do
setup do
@user = create(:fosterer)
end

should "return false" do
assert_equal @action.call, false
end
end

context "when user is activated staff" do
setup do
@user = create(:staff)
Expand Down
52 changes: 52 additions & 0 deletions test/policies/organizations/adopter_foster_profile_policy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ class Organizations::AdopterFosterProfilePolicyTest < ActiveSupport::TestCase
end
end

context "when user is fosterer without profile" do
setup do
@user = create(:fosterer)
end

should "return true" do
assert_equal @action.call, true
end
end

context "when user is fosterer with profile" do
setup do
@user = create(:fosterer, :with_profile)
end

should "return false" do
assert_equal @action.call, false
end
end

context "when user is staff" do
setup do
@user = create(:staff)
Expand Down Expand Up @@ -124,6 +144,38 @@ class Organizations::AdopterFosterProfilePolicyTest < ActiveSupport::TestCase
end
end

context "when user is fosterer without profile" do
setup do
@user = create(:fosterer)
end

should "return false" do
assert_equal @action.call, false
end
end

context "when user is fosterer with profile" do
setup do
@user = create(:fosterer, :with_profile)
end

context "when profile does not belong to user" do
should "return false" do
assert_equal @action.call, false
end
end

context "when profile belongs to user" do
setup do
@user = @profile.adopter_foster_account.user
end

should "return true" do
assert_equal @action.call, true
end
end
end

context "when user is staff" do
setup do
@user = create(:staff)
Expand Down
10 changes: 10 additions & 0 deletions test/policies/organizations/dashboard_policy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ class Organizations::DashboardPolicyTest < ActiveSupport::TestCase
end
end

context "when user is fosterer" do
setup do
@user = create(:fosterer)
end

should "return false" do
assert_equal @action.call, false
end
end

context "when user is deactivated staff" do
setup do
@user = create(:staff, :deactivated)
Expand Down
Loading

0 comments on commit 02fe736

Please sign in to comment.