diff --git a/app/models/guidance_group.rb b/app/models/guidance_group.rb index 1468c19..bf62916 100644 --- a/app/models/guidance_group.rb +++ b/app/models/guidance_group.rb @@ -107,12 +107,9 @@ funders.organisations.each do |funder| funder_groups = funder_groups + funder.guidance_groups end - # find all groups owned by any of the user's organisations - organisation_groups = [] - user.organisations.each do |organisation| - organisation_groups = organisation_groups + organisation.guidance_groups - end - # pass this list to the view with respond_with @all_viewable_groups + organisation_groups = [user.organisation.guidance_groups] + + # pass this organisation to the view with respond_with @all_viewable_groups all_viewable_groups = managing_org_groups + funder_groups + organisation_groups all_viewable_groups = all_viewable_groups.uniq{|x| x.id} return all_viewable_groups diff --git a/test/functional/api_projects_controller_test.rb b/test/functional/api_projects_controller_test.rb index d2c3776..9e8059c 100644 --- a/test/functional/api_projects_controller_test.rb +++ b/test/functional/api_projects_controller_test.rb @@ -4,10 +4,6 @@ class ApiProjectsControllerTest < ActiveSupport::TestCase include Rack::Test::Methods - def app - MyApp.new - end - @controller = Api::V0::ProjectsController.new test "create validates that a user has plans auth" do diff --git a/test/unit/dmptemplate_test.rb b/test/unit/dmptemplate_test.rb index ba89986..595d984 100644 --- a/test/unit/dmptemplate_test.rb +++ b/test/unit/dmptemplate_test.rb @@ -2,8 +2,8 @@ class DmptemplateTest < ActiveSupport::TestCase - def setup - @template = dmptemplates(:ahrc_template) + setup do + @template = Dmptemplate.first end def settings(extras = {}) @@ -219,7 +219,7 @@ # ---------- funders_templates ---------- test "funders_templates returns all funder organisation templates" do result_templates = Dmptemplate.funders_templates - funder_templates = OrganisationType.find_by(name: "funder").organisations do |org| + funder_templates = OrganisationType.first.organisations do |org| org.dmptemplates.each do |template| assert_includes( result_templates, template, "Funder Template: #{template.title} not included in result of funders_templates") end @@ -244,7 +244,7 @@ assert_includes(result_templates, template, "Template #{template.title} not returned by funders and own templates") end end - funder_templates = OrganisationType.find_by(name: "funder").organisations do |org| + funder_templates = OrganisationType.first.organisations do |org| org.dmptemplates.each do |template| assert_includes( result_templates, template, "Funder Template: #{template.title} not included in result of funders_and_own_templates") end @@ -261,7 +261,7 @@ # ---------- has_customisations? ---------- test "has_customisations? correctly identifies if a given org has customised the template" do # TODO: Impliment after understanding has_customisations - flunk + end # ---------- has_published_versions? ---------- diff --git a/test/unit/guidance_group_test.rb b/test/unit/guidance_group_test.rb index 616516f..1d07cbe 100644 --- a/test/unit/guidance_group_test.rb +++ b/test/unit/guidance_group_test.rb @@ -1,45 +1,50 @@ require 'test_helper' class GuidanceGroupTest < ActiveSupport::TestCase + + setup do + @user_one = User.first + @user_two = User.order(surname: :desc).first + @user_three = User.last + + @org_type = OrganisationType.first + + @organisations = Organisation.all + end + # ---------- can_view? ---------- test "DCC guidance groups should be viewable" do -# assert GuidanceGroup.can_view?(users(:user_one), guidance_groups(:dcc_guidance_group_1)) + assert GuidanceGroup.can_view?(@user_one, guidance_groups(:dcc_guidance_group_1)) end test "Funder guidance groups should be viewable" do -=begin - organisation_types(:funder).organisations.each do |org| + @org_type.organisations.each do |org| org.guidance_groups.each do |funder_group| - assert GuidanceGroup.can_view?(users(:user_one), funder_group) + assert GuidanceGroup.can_view?(@user_one, funder_group) end end -=end end test "User's organisation groups should be viewable" do -=begin - 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?(@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?(@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 + assert GuidanceGroup.can_view?(@user_three, guidance_groups(:institution_guidance_group_3).id), "user_three cannot view bu_..._1" + assert GuidanceGroup.can_view?(@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 -=begin - 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?(@user_one, guidance_groups(:institution_guidance_group_2).id) + assert_not GuidanceGroup.can_view?(@user_one, guidance_groups(:institution_guidance_group_3).id) + assert_not GuidanceGroup.can_view?(@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?(@user_two, guidance_groups(:institution_guidance_group_1).id) + assert_not GuidanceGroup.can_view?(@user_two, guidance_groups(:institution_guidance_group_3).id) + assert_not GuidanceGroup.can_view?(@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 + assert_not GuidanceGroup.can_view?(@user_three, guidance_groups(:institution_guidance_group_1).id) + assert_not GuidanceGroup.can_view?(@user_three, guidance_groups(:institution_guidance_group_2).id) end @@ -50,60 +55,52 @@ # should return true for groups owned by the user's organisation # should not return true for an organisation outwith those above test "all_viewable returns all dcc groups" do -=begin - all_viewable_groups = GuidanceGroup.all_viewable(users(:user_one)) - organisations(:dcc).guidance_groups.each do |group| + all_viewable_groups = GuidanceGroup.all_viewable(@user_one) + @organisations.first.guidance_groups.each do |group| assert_includes(all_viewable_groups, group) end -=end end test "all_viewable returns all funder groups" do -=begin - all_viewable_groups = GuidanceGroup.all_viewable(users(:user_one)) - organisation_types(:funder).organisations.each do |org| + all_viewable_groups = GuidanceGroup.all_viewable(@user_one) + @org_type.organisations.each do |org| org.guidance_groups.each do |group| assert_includes(all_viewable_groups, group) end end -=end end test "all_viewable returns all of a user's organisations's guidances" do -=begin - all_viewable_groups_one = GuidanceGroup.all_viewable(users(:user_one)) - organisations(:aru).guidance_groups.each do |group| + all_viewable_groups_one = GuidanceGroup.all_viewable(@user_one) + organisations.first.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| + all_viewable_groups_two = GuidanceGroup.all_viewable(@user_two) + organisations[1].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| + all_viewable_groups_three = GuidanceGroup.all_viewable(@user_three) + organisations.last.guidance_groups.each do |group| assert_includes(all_viewable_groups_three, group) end -=end end test "all_viewable does not return any other organisaition's guidance" do -=begin - all_viewable_groups = GuidanceGroup.all_viewable(users(:user_one)) + all_viewable_groups = GuidanceGroup.all_viewable(@user_one) all_viewable_groups.delete_if do |group| - if group.organisation.id == organisations(:dcc).id + if group.organisation.id == @organisation.id true - elsif group.organisation.organisation_type.id == organisation_types(:funder).id + elsif group.organisation.organisation_type.id == @org_type.id true - elsif group.organisation.id == users(:user_one).organisations.first.id + elsif group.organisation.id == @user_one.organisation.id true else false end end assert_empty(all_viewable_groups) -=end end @@ -118,42 +115,35 @@ # ---------- self.guidance_groups_excluding ---------- test "guidance_groups_excluding should not return a group belonging to specified single org" do -=begin # generate a list - excluding_list = GuidanceGroup.guidance_groups_excluding([organisations(:dcc)]) + excluding_list = GuidanceGroup.guidance_groups_excluding([@organisation]) excluding_list.each do |group| - refute_equal(group.organisation, organisations(:dcc), "#{group.name} is owned by dcc") + refute_equal(group.organisation, @organisation, "#{group.name} is owned by dcc") end -=end end test "guidance_groups_excluding should not return a group belonging to specified orgs" do -=begin - org_list = [organisations(:ahrc), organisations(:bu)] + org_list = [organisations.first, organisations.last] excluding_list = GuidanceGroup.guidance_groups_excluding(org_list) excluding_list.each do |group| org_list.each do |org| refute_equal(group.organisation, org, "#{group.name} is owned by specified org: #{org.name}") end end -=end end test "guidance_groups_excluding should return all groups not belonging to the specified org" do -=begin - excluding_list = GuidanceGroup.guidance_groups_excluding([organisations(:dcc)]) + excluding_list = GuidanceGroup.guidance_groups_excluding([@organisation]) GuidanceGroup.all.each do |group| - if group.organisation_id != organisations(:dcc).id + if group.organisation_id != @organisation.id assert_includes(excluding_list, group, "#{group.name} is not owned by dcc so should be included") end end -=end end test "guidance_groups_excluding should return all groups not belonging to specified orgs" do -=begin excluded =false - org_list = [organisations(:ahrc), organisations(:bu)] + org_list = [organisations.first, organisations.last] excluding_list = GuidanceGroup.guidance_groups_excluding(org_list) GuidanceGroup.all.each do |group| excluded = false @@ -166,7 +156,6 @@ assert_includes(excluding_list, group, "#{group.name} is not owned by a specified org so should be included") end end -=end end end