diff --git a/app/models/guidance_group.rb b/app/models/guidance_group.rb index 21da8cc..84c66b8 100644 --- a/app/models/guidance_group.rb +++ b/app/models/guidance_group.rb @@ -43,7 +43,7 @@ guidance_group = GuidanceGroup.find_by(id: id) viewable = false # groups are viewable if they are owned by any of the user's organisations - user.organisations do |organisation| + user.organisations.each do |organisation| if guidance_group.organisation.id == organisation.id viewable = true end diff --git a/test/unit/guidance_group_test.rb b/test/unit/guidance_group_test.rb new file mode 100644 index 0000000..c7274e5 --- /dev/null +++ b/test/unit/guidance_group_test.rb @@ -0,0 +1,89 @@ +require 'test_helper' + +class GuidanceGroupTest < ActiveSupport::TestCase + test "DCC guidance groups should be viewable" do + assert GuidanceGroup.can_view(users(:user_one), guidance_groups(:dcc_guidance_group_1)) + end + + # FIXME + test "Funder guidance groups should be viewable" do + organisation_types(:funder).organisations.each do |funder_group| + assert GuidanceGroup.can_view(users(:user_one), funder_group) + end + end + + # FIXME + test "User's organisation groups should be viewable" do + assert GuidanceGroup.can_view(users(:user_one), guidance_groups(:institution_guidance_group_1).id) , "user_one cannot view aru_institution_guidance" + + assert GuidanceGroup.can_view(users(:user_two), guidance_groups(:institution_guidance_group_2).id), "user_two cannot view au_..._1" + + assert GuidanceGroup.can_view(users(:user_three), guidance_groups(:institution_guidance_group_3).id), "user_three cannot view bu_..._1" + assert GuidanceGroup.can_view(users(:user_three), guidance_groups(:institution_guidance_group_4).id), "user_three cannot view bu_..._2" + end + + test "No other organisations's groups should be viewable" do + assert_not GuidanceGroup.can_view(users(:user_one), guidance_groups(:institution_guidance_group_2).id) + assert_not GuidanceGroup.can_view(users(:user_one), guidance_groups(:institution_guidance_group_3).id) + assert_not GuidanceGroup.can_view(users(:user_one), guidance_groups(:institution_guidance_group_4).id) + + assert_not GuidanceGroup.can_view(users(:user_two), guidance_groups(:institution_guidance_group_1).id) + assert_not GuidanceGroup.can_view(users(:user_two), guidance_groups(:institution_guidance_group_3).id) + assert_not GuidanceGroup.can_view(users(:user_two), guidance_groups(:institution_guidance_group_4).id) + + assert_not GuidanceGroup.can_view(users(:user_three), guidance_groups(:institution_guidance_group_1).id) + assert_not GuidanceGroup.can_view(users(:user_three), guidance_groups(:institution_guidance_group_2).id) + end + + # would be better to instead start with dcc and find all attached guidances? + # I think so so I will impliment here and back-impliment to guidances + # TODO: impliment in guidances + test "all_viewable returns all dcc groups" do + all_viewable_groups = GuidanceGroup.all_viewable(users(:user_one)) + organisations(:dcc).guidance_groups.each do |group| + assert_includes(all_viewable_groups, group) + end + end + + test "all_viewable returns all funder groups" do + all_viewable_groups = GuidanceGroup.all_viewable(users(:user_one)) + organisation_types(:funder).organisations.each do |org| + org.guidance_groups.each do |group| + assert_includes(all_viewable_groups, group) + end + end + end + + test "all_viewable returns all of a user's organisations's guidances" do + all_viewable_groups_one = GuidanceGroup.all_viewable(users(:user_one)) + organisations(:aru).guidance_groups.each do |group| + assert_includes(all_viewable_groups_one, group) + end + + all_viewable_groups_two = GuidanceGroup.all_viewable(users(:user_two)) + organisations(:au).guidance_groups.each do |group| + assert_includes(all_viewable_groups_two, group) + end + + all_viewable_groups_three = GuidanceGroup.all_viewable(users(:user_three)) + organisations(:bu).guidance_groups.each do |group| + assert_includes(all_viewable_groups_three, group) + end + end + + # FIXME + test "all_viewable does not return any other organisaition's guidance" do + all_viewable_groups = GuidanceGroup.all_viewable(users(:user_one)) + all_viewable_groups.delete_if do |group| + if group.organisation.id == organisations(:dcc).identify + true + elsif group.organisation.organisation_type.id == organisation_types(:funder).identify + true + elsif group.organisation.id == users(:user_one).organisations.first.identify + true + else + false + end + end + end +end