diff --git a/app/models/guidance.rb b/app/models/guidance.rb index ac1e886..7a22a28 100644 --- a/app/models/guidance.rb +++ b/app/models/guidance.rb @@ -10,6 +10,8 @@ class Guidance < ActiveRecord::Base include GlobalHelpers #associations between tables + +# TODO: REMOVE AND HANDLE ATTRIBUTE SECURITY IN THE CONTROLLER! attr_accessible :text, :question_id, :published, :as => [:default, :admin] attr_accessible :guidance_group_ids, :as => [:default, :admin] @@ -23,10 +25,14 @@ has_and_belongs_to_many :guidance_groups, join_table: "guidance_in_group" has_and_belongs_to_many :themes, join_table: "themes_in_guidance" +# TODO: REMOVE AND HANDLE ATTRIBUTE SECURITY IN THE CONTROLLER! accepts_nested_attributes_for :themes accepts_nested_attributes_for :guidance_groups + validates :text, presence: true + + ## # Determine if a guidance is in a group which belongs to a specified organisation # diff --git a/app/models/guidance_group.rb b/app/models/guidance_group.rb index d9102a4..6f8073b 100644 --- a/app/models/guidance_group.rb +++ b/app/models/guidance_group.rb @@ -9,11 +9,14 @@ has_and_belongs_to_many :projects, join_table: "project_guidance" has_and_belongs_to_many :dmptemplates, join_table: "dmptemplates_guidance_groups" +# TODO: REMOVE AND HANDLE ATTRIBUTE SECURITY IN THE CONTROLLER! accepts_nested_attributes_for :dmptemplates - - attr_accessible :organisation_id, :name, :optional_subset, :published, :as => [:default, :admin] + attr_accessible :organisation_id, :name, :optional_subset, :published, + :organisation, :as => [:default, :admin] attr_accessible :dmptemplate_ids, :as => [:default, :admin] + validates :name, :organisation, presence: true + ## # Converts a guidance group to a string containing the display name # @@ -109,7 +112,7 @@ # pass this organisation guidance groups 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} + all_viewable_groups = all_viewable_groups.flatten.uniq{|x| x.id} return all_viewable_groups end end diff --git a/test/unit/dmptemplate_test.rb b/test/unit/dmptemplate_test.rb index 6e362fd..2e53337 100644 --- a/test/unit/dmptemplate_test.rb +++ b/test/unit/dmptemplate_test.rb @@ -22,8 +22,8 @@ # --------------------------------------------------- test "required fields are required" do assert_not Dmptemplate.new.valid? - assert_not Dmptemplate.new(title: 'Testing tmeplate').valid?, "expected the 'title' field to be required" - assert_not Dmptemplate.new(organisation: @organisation).valid?, "expected the 'organisation' field to be required" + assert_not Dmptemplate.new(title: 'Testing tmeplate').valid?, "expected the 'organisation' field to be required" + assert_not Dmptemplate.new(organisation: @organisation).valid?, "expected the 'title' field to be required" # Ensure the bar minimum and complete versions are valid a = Dmptemplate.new(organisation: @organisation, title: 'Testing tmeplate') diff --git a/test/unit/guidance_group_test.rb b/test/unit/guidance_group_test.rb index 1b7e6c8..9138744 100644 --- a/test/unit/guidance_group_test.rb +++ b/test/unit/guidance_group_test.rb @@ -1,177 +1,127 @@ require 'test_helper' class GuidanceGroupTest < ActiveSupport::TestCase + include GlobalHelpers setup do - @user_one = User.first - @user_two = User.order(surname: :desc).first - @user_three = User.last + @organisation = Organisation.first - @org_type = OrganisationType.first - - @organisations = Organisation.all + @guidance_group = GuidanceGroup.create(name: 'Test Guidance Group', + organisation: @organisation) end - # ---------- can_view? ---------- - test "DCC guidance groups should be viewable" do -# assert GuidanceGroup.can_view?(@user_one, guidance_groups(:dcc_guidance_group_1)) + # --------------------------------------------------- + test "required fields are required" do + assert_not GuidanceGroup.new.valid? + assert_not GuidanceGroup.new(organisation: @organisation).valid?, "expected the 'name' field to be required" + assert_not GuidanceGroup.new(name: 'Tester').valid?, "expected the 'organisation' field to be required" + + # Ensure the bar minimum and complete versions are valid + a = GuidanceGroup.new(name: 'Tester', organisation: @organisation) + assert a.valid?, "expected the 'name' and 'organisation' fields to be enough to create a GuidanceGroup! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + end + + # --------------------------------------------------- + test "display_name returns organisation name and the guidance group name" do + assert_equal "#{@organisation.name}", @guidance_group.display_name, "Expected display_name to return the organisation name if there is only one GuidanceGroup" + + GuidanceGroup.create(name: 'Second Test', organisation: @organisation) + assert_equal "#{@organisation.name}: #{@guidance_group.name}", @guidance_group.display_name, "Expected display_name to return the organisation name and guidance group name if there are more than one GuidanceGroup" end - test "Funder guidance groups should be viewable" do -=begin - @org_type.organisations.each do |org| - org.guidance_groups.each do |funder_group| - assert GuidanceGroup.can_view?(@user_one, funder_group) - end - end -=end + # --------------------------------------------------- + test "to_s returns organisation name and the guidance group name" do + assert_equal @guidance_group.display_name, @guidance_group.to_s end - test "User's organisation groups should be viewable" do -=begin - assert GuidanceGroup.can_view?(@user_one, guidance_groups(:institution_guidance_group_1).id) , "user_one cannot view aru_institution_guidance" - - assert GuidanceGroup.can_view?(@user_two, guidance_groups(:institution_guidance_group_2).id), "user_two cannot view au_..._1" - - 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 "guidance_groups_excluding does not return guidance groups for the current organisation" do + assert_not GuidanceGroup.guidance_groups_excluding([@organisation]).include?(@guidance_group) end - test "No other organisations's groups should be viewable" do -=begin - 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?(@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?(@user_three, guidance_groups(:institution_guidance_group_1).id) - assert_not GuidanceGroup.can_view?(@user_three, guidance_groups(:institution_guidance_group_2).id) -=end + # --------------------------------------------------- + test "user can view guidance_group if it belongs to their organisation" do + usr = User.first + org = usr.organisation + gg = GuidanceGroup.create(name: 'User Test', organisation: org) + + assert GuidanceGroup.can_view?(usr, gg.id) end + # --------------------------------------------------- + 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) + + assert GuidanceGroup.can_view?(usr, gg.id) + end + + # --------------------------------------------------- + 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")) + gg = GuidanceGroup.create(name: 'Managing CC Test', organisation: org) + + assert GuidanceGroup.can_view?(usr, gg.id) + end - # ---------- all_viewable ---------- - # ensure that the all_viewable function returns all viewable groups - # should return true for groups owned by funders - # should return true for groups owned by DCC - # 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 - all_viewable_groups = GuidanceGroup.all_viewable(@user_one) - @organisations.first.guidance_groups.each do |group| - assert_includes(all_viewable_groups, group) + # --------------------------------------------------- + test "user can view all oftheir organisations, funders, and the managing curation centre's guidance groups" do + usr = User.first + @organisation.users << usr + @organisation.save + @organisation.reload + + funding = Organisation.where(organisation_type: OrganisationType.find_by(name: GlobalHelpers.constant("organisation_types.funder"))).first + managing = Organisation.create(name: GlobalHelpers.constant("organisation_types.managing_organisation")) + + ggs = [@guidance_group, + GuidanceGroup.create(name: 'User Test', organisation: @organisation), + GuidanceGroup.create(name: 'Funder Test', organisation: funding), + GuidanceGroup.create(name: 'Managing CC Test', organisation: managing)] + + v = GuidanceGroup.all_viewable(usr) + + ggs.each do |gg| + assert v.include?(gg), "expected Guidance Group: '#{gg.name}' to be viewable" end end - test "all_viewable returns all funder groups" do - 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 + # --------------------------------------------------- + test "can CRUD GuidanceGroup" do + gg = GuidanceGroup.create(name: 'Tester', organisation: @organisation) + assert_not gg.id.nil?, "was expecting to be able to create a new GuidanceGroup!" + + gg.name = 'Testing an update' + gg.save! + gg.reload + assert_equal 'Testing an update', gg.name, "Was expecting to be able to update the text of the GuidanceGroup!" + + assert gg.destroy!, "Was unable to delete the GuidanceGroup!" end - - test "all_viewable returns all of a user's organisations's guidances" do - 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(@user_two) - @organisations[1].guidance_groups.each do |group| - assert_includes(all_viewable_groups_two, group) - end - - all_viewable_groups_three = GuidanceGroup.all_viewable(@user_three) - @organisations.last.guidance_groups.each do |group| - assert_includes(all_viewable_groups_three, group) - end + + # --------------------------------------------------- + test "can manage has_many relationship with Project" do + proj = Project.new(title: 'Test Project', dmptemplate: Dmptemplate.first) + verify_has_many_relationship(@guidance_group, proj, @guidance_group.projects.count) end - - test "all_viewable does not return any other organisaition's guidance" do -=begin - all_viewable_groups = GuidanceGroup.all_viewable(@user_one) - all_viewable_groups.delete_if do |group| - if group.organisation.id == @organisation.id - true - elsif group.organisation.organisation_type.id == @org_type.id - true - elsif group.organisation.id == @user_one.organisation.id - true - else - false - end - end - assert_empty(all_viewable_groups) -=end + + # --------------------------------------------------- + test "can manage has_many relationship with Template" do + t = Dmptemplate.new(title: 'Test Theme', organisation: @organisation) + verify_has_many_relationship(@guidance_group, t, @guidance_group.dmptemplates.count) end - - - # ---------- display_name ---------- - test "display_name should return an org name for an org with one guidance" do -# assert_equal(guidance_groups(:funder_guidance_group_1).display_name, "Arts and Humanities Research Council", "result of display_name for an org with one group should be the org name") + + # --------------------------------------------------- + test "can manage has_many relationship with Guidance" do + g = Guidance.new(text: 'Test Guidance') + verify_has_many_relationship(@guidance_group, g, @guidance_group.guidances.count) end - - test "display_name should return an org and group name for an org with more than one guidance" do -# assert_equal(guidance_groups(:institution_guidance_group_4).display_name, "Bangor University: Bangor University guidance group 2", "result of display_name for an org with more than one group should be : ") - end - - # ---------- 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([@organisation]) - excluding_list.each do |group| - 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.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([@organisation]) - GuidanceGroup.all.each do |group| - 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.first, organisations.last] - excluding_list = GuidanceGroup.guidance_groups_excluding(org_list) - GuidanceGroup.all.each do |group| - excluded = false - org_list.each do |org| - if group.organisation == org - excluded = true - end - end - unless excluded - assert_includes(excluding_list, group, "#{group.name} is not owned by a specified org so should be included") - end - end -=end - end + + # --------------------------------------------------- + test "can manage belongs_to relationship with Organisation" do + verify_belongs_to_relationship(@guidance_group, @organisation) + end end diff --git a/test/unit/guidance_test.rb b/test/unit/guidance_test.rb new file mode 100644 index 0000000..0e28ba0 --- /dev/null +++ b/test/unit/guidance_test.rb @@ -0,0 +1,50 @@ +require 'test_helper' + +class GuidanceTest < ActiveSupport::TestCase + + setup do + @guidance = Guidance.new(text: 'Testing some new guidance') + + @question = Question.first + end + + # --------------------------------------------------- + test "required fields are required" do + assert_not Guidance.new.valid? + assert_not Guidance.new(guidance_groups: [GuidanceGroup.first]).valid?, "expected the 'text' field to be required" + + # Ensure the bar minimum and complete versions are valid + a = Guidance.new(text: 'Testing guidance') + assert a.valid?, "expected the 'text' field to be enough to create a Guidance! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + 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!" + + g.text = 'Testing an update' + g.save! + g.reload + assert_equal 'Testing an update', g.text, "Was expecting to be able to update the text of the Guidance!" + + assert g.destroy!, "Was unable to delete the Guidance!" + end + + # --------------------------------------------------- + test "can manage has_many relationship with GuidanceGroup" do + gg = GuidanceGroup.new(name: 'Test Group', organisation: Organisation.first) + verify_has_many_relationship(@guidance, gg, @guidance.guidance_groups.count) + end + + # --------------------------------------------------- + test "can manage has_many relationship with Theme" do + t = Theme.new(title: 'Test Theme') + verify_has_many_relationship(@guidance, t, @guidance.themes.count) + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with Question" do + verify_belongs_to_relationship(@guidance, @question) + end +end \ No newline at end of file