diff --git a/app/models/org.rb b/app/models/org.rb index 9fc5bd7..bf3113e 100644 --- a/app/models/org.rb +++ b/app/models/org.rb @@ -57,7 +57,11 @@ # What do they do? do they do it efficiently, and do we need them? -# TODO: Should these be hardcoded? +# TODO: Should these be hardcoded? Also, an Org can currently be multiple org_types at one time. +# For example you can do: funder = true; project = true; school = true +# Calling type in the above scenario returns "Funder" which is a bit misleading +# Is FlagShihTzu's Bit flag the appropriate structure here or should we use an enum? +# Tests are setup currently to work with this issue. ## # returns the name of the type of the organisation as a string # defaults to none if no org type present diff --git a/app/models/user.rb b/app/models/user.rb index 645cbbe..2a0ac33 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -201,14 +201,6 @@ perms.include? Perm.find_by(name: constant("roles.change_org_details")) end - ## - # checks if the user can grant the api to organisations - # - # @return [Boolean] true if the user can grant api permissions to organisations - def can_grant_api_to_orgs? - perms.include? Perm.find_by(name: constant('roles.grant_api_to_orgs')) - end - ## # checks if the user can grant the api to organisations diff --git a/test/unit/org_test.rb b/test/unit/org_test.rb index 347cc7e..60b0f6f 100644 --- a/test/unit/org_test.rb +++ b/test/unit/org_test.rb @@ -35,16 +35,29 @@ test "type returns the correct value" do @org.institution = true assert_equal "Institution", @org.type + + @org.institution = false @org.funder = true assert_equal "Funder", @org.type + + @org.funder = false @org.organisation = true assert_equal "Organisation", @org.type + + @org.organisation = false @org.research_institute = true assert_equal "Research Institute", @org.type + + @org.research_institute = false @org.project = true assert_equal "Project", @org.type + + @org.project = false @org.school = true assert_equal "School", @org.type + + @org.school = false + assert_equal "None", @org.type end # --------------------------------------------------- diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 4ab6bc5..9fd0e79 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -175,6 +175,56 @@ end # --------------------------------------------------- + test "Returns the appropriate identifier for the specified scheme" do + 3.times do |i| + scheme = IdentifierScheme.create!({name: "test-#{i}", active: true}) + + @user.user_identifiers << UserIdentifier.new(identifier_scheme: scheme, identifier: i.to_s) + end + @user.save! + + 3.times do |i| + scheme = IdentifierScheme.find_by(name: "test-#{i}") + + assert_equal i.to_s, @user.identifier_for(scheme), "expected the identifier for #{scheme.name} to be '#{i.to_s}'" + end + end + + # --------------------------------------------------- + test "can_super_admin is properly set" do + perms = Perm.where('name IN (?)', ['add_organisations', 'change_org_affiliation', 'grant_api_to_orgs') + user = User.create!(email: 'tester@example.edu', password: 'password', perms: perms) + + assert user.can_super_admin?, "expected the user to be able to super_admin if they can add orgs, change a user's org and grant api access to an org" + + perms.each do |p| + last = p + user.perms << last unless last.nil? + user.perms.delete(p) + user.save! + + assert_not user.can_super_admin?, "expected the removal of the #{p.name} perm to prevent the user from being a super_admin" + end + end + + # --------------------------------------------------- + test "can_org_admin is properly set" do + perms = Perm.where('name IN (?)', ['grant_permissions', 'modify_templates', 'modify_guidance', 'change_org_details') + user = User.create!(email: 'tester@example.edu', password: 'password', perms: perms) + + assert user.can_org_admin?, "expected the user to be able to org_admin if they can grant perms, modify templates, modify guidance and change org details" + + perms.each do |p| + last = p + user.perms << last unless last.nil? + user.perms.delete(p) + user.save! + + assert_not user.can_org_admin?, "expected the removal of the #{p.name} perm to prevent the user from being a org_admin" + end + end + + # --------------------------------------------------- test "Can only change the org if permissions allow" do user = User.first org = user.org