diff --git a/test/unit/guidance_group_test.rb b/test/unit/guidance_group_test.rb index 9138744..8363157 100644 --- a/test/unit/guidance_group_test.rb +++ b/test/unit/guidance_group_test.rb @@ -51,8 +51,8 @@ # --------------------------------------------------- test "user can view guidance_group if it belongs to a funder" do usr = User.first - orgs = Organisation.where(organisation_type: OrganisationType.find_by(name: GlobalHelpers.constant("organisation_types.funder"))) - gg = GuidanceGroup.create(name: 'Funder Test', organisation: orgs.first) + org = Organisation.find_by(organisation_type: OrganisationType.find_by(name: GlobalHelpers.constant("organisation_types.funder"))) + gg = GuidanceGroup.create(name: 'Funder Test', organisation: org) assert GuidanceGroup.can_view?(usr, gg.id) end @@ -60,7 +60,7 @@ # --------------------------------------------------- test "user can view guidance_group if it belongs to the managing curation centre" do usr = User.first - org = Organisation.create(name: GlobalHelpers.constant("organisation_types.managing_organisation")) + org = Organisation.find_by(name: GlobalHelpers.constant("organisation_types.managing_organisation")) gg = GuidanceGroup.create(name: 'Managing CC Test', organisation: org) assert GuidanceGroup.can_view?(usr, gg.id) diff --git a/test/unit/guidance_test.rb b/test/unit/guidance_test.rb index 0e28ba0..2afd194 100644 --- a/test/unit/guidance_test.rb +++ b/test/unit/guidance_test.rb @@ -19,6 +19,69 @@ end # --------------------------------------------------- + test "correctly identifies guidance as belonging to the organisation" do + assert @guidance.in_group_belonging_to?(@user.organisation.id), "expected the guidance to belong to the organisation" + + @guidance.guidance_groups = [] + @guidance.save! + + assert_not @guidance.in_group_belonging_to?(@user.organisation), "expected the guidance to NOT belong to the organisation" + end + + # --------------------------------------------------- + test "retrieves guidance by organisation" do + org = Organisation.create(name: 'Tester 123') + assert Guidance.by_organisation(org.id).empty?, "expected the newly created organisation to have no guidance" + + assert_not Guidance.by_organisation(@user.organisation.id).empty?, "expected the organisation to have guidance" + end + + # --------------------------------------------------- + test "correctly identifies whether the user can view the guidance" do + g = Guidance.create!(text: 'Unviewable guidance') + + assert_not Guidance.can_view?(@user, g), "expected guidance that is not attached to a GuidanceGroup to be unviewable" + + managing = Organisation.find_by( name: GlobalHelpers.constant("organisation_types.managing_organisation")) + funder = Organisation.find_by(organisation_type: OrganisationType.find_by( name: GlobalHelpers.constant("organisation_types.funder"))) + + assert Guidance.can_view?(@user, @guidance.id), "expected the user to be able to view guidance belonging to their organisation" + + @guidance_group.organisation = managing + @guidance_group.save! + assert Guidance.can_view?(@user, @guidance.id), "expected the user to be able to view guidance belonging to the managing organisation" + + @guidance_group.organisation = funder + @guidance_group.save! + assert Guidance.can_view?(@user, @guidance.id), "expected the user to be able to view guidance belonging to a funder" + end + + # --------------------------------------------------- + test "make sure a user can view all appropriate guidance" do + viewable = Guidance.all_viewable(@user) + + assert viewable.include?(@guidance), "expected the user to be able to view guidance belonging to their organisation" + + managing = Organisation.find_by( name: GlobalHelpers.constant("organisation_types.managing_organisation")) + funder = Organisation.find_by(organisation_type: OrganisationType.find_by( name: GlobalHelpers.constant("organisation_types.funder"))) + + GuidanceGroup.create(name: 'managing guidance group test', organisation: managing) + GuidanceGroup.create(name: 'funder guidance group test', organisation: funder) + + managing.guidance_groups.each do |gg| + gg.guidances.each do |g| + assert viewable.include?(g), "expected the user to be able to view all managing organisation guidance" + end + end + + funder.guidance_groups.each do |gg| + gg.guidances.each do |g| + assert viewable.include?(g), "expected the user to be able to view all funder guidance" + end + end + end + + # --------------------------------------------------- test "can CRUD Guidance" do g = Guidance.create(text: 'Testing guidance') assert_not g.id.nil?, "was expecting to be able to create a new Guidance!"