diff --git a/app/controllers/guidances_controller.rb b/app/controllers/guidances_controller.rb index 81ea8c9..75c0808 100644 --- a/app/controllers/guidances_controller.rb +++ b/app/controllers/guidances_controller.rb @@ -20,7 +20,11 @@ def admin_new @guidance = Guidance.new authorize @guidance - @templates = Template.funders_and_own_templates(current_user.org_id) + + #@templates = Template.funders_and_own_templates(current_user.org_id) + # Replacing weird accessor on Template + @templates = (Org.funders.collect{|o| o.templates } + current_user.org.templates).flatten + @phases = nil @templates.includes(:phases).each do |template| if @phases.nil? then diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index b23c6ca..1cb7c24 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -49,6 +49,22 @@ end end + def new + if user_signed_in? then + @plan = Plan.new + authorize @plan + @funders = Org.funder.all + + respond_to do |format| + format.html # new.html.erb + end + else + respond_to do |format| + format.html { redirect_to edit_user_registration_path } + end + end + end + # GET /projects/1/edit # Should this be removed? diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index da81ea8..cded887 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -15,24 +15,22 @@ def roadmap end - # GET /projects/publicly_available + # GET /plans/publicly_available # ----------------------------------------------------------- def public_plans - @projects = Project.publicly_visible.order(title: :asc) + @plans = Plan.where(visibility: :publicly_visible).order(title: :asc) end - # GET /projects/[:project_slug]/public_export + # GET /plans/[:plan_slug]/public_export # ------------------------------------------------------------- def public_export - @project = Project.find(params[:id]) + @plan = Plan.find(params[:id]) # Force PDF response request.format = :pdf # if the project is designated as public - if @project.visibility == :publicly_visible - @plan = @project.plans.first - + if @plan.visibility == :publicly_visible if !@plan.nil? @exported_plan = ExportedPlan.new.tap do |ep| ep.plan = @plan diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index 5601a7b..37c6800 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -19,7 +19,7 @@ end @templates_own = current_templates.values #funders templates - @templates_funders = Template.funders_templates + @templates_funders = Org.funders.collect{|o| o.templates } #Template.funders_templates end diff --git a/app/models/plan.rb b/app/models/plan.rb index 2490966..0a7de28 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -15,6 +15,11 @@ accepts_nested_attributes_for :template has_many :exported_plans + has_many :roles +# COMMENTED OUT THE DIRECT CONNECTION HERE TO Users to prevent assignment of users without an access_level specified (currently defaults to creator) +# has_many :users, through: :roles + + ## # Possibly needed for active_admin # -relies on protected_attributes gem as syntax depricated in rails 4.2 @@ -28,8 +33,9 @@ # public is a Ruby keyword so using publicly enum visibility: [:organisationally_visible, :publicly_visible, :is_test, :privately_visible] - #TODO: work out why this messes up plan creation - #validates :template, :title, :users, presence: true + #TODO: work out why this messes up plan creation : + # briley: Removed reliance on :users, its really on :roles (shouldn't have a plan without at least a creator right?) It should be ok like this though now + validates :template, :title, presence: true ## # Constants @@ -46,29 +52,6 @@ end alias_method :super_settings, :settings - - - def new - if user_signed_in? then - @plan = Plan.new - authorize @plan - @funders = Org.funder.all - - respond_to do |format| - format.html # new.html.erb - end - else - respond_to do |format| - format.html { redirect_to edit_user_registration_path } - end - end - end - - - - - - ## # Proxy through to the template settings (or defaults if this plan doesn't have diff --git a/app/models/role.rb b/app/models/role.rb index 282802d..7bbe072 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -1,6 +1,8 @@ class Role < ActiveRecord::Base include FlagShihTzu + before_validation :check_access_level + ## # Associations belongs_to :user @@ -14,36 +16,8 @@ 3 => :editor, column: 'access' - - - # EVALUATE CLASS AND INSTANCE METHODS BELOW - # - # What do they do? do they do it efficiently, and do we need them? - # These functions are from the old project_groups model - - - - ## - # returns the user's email unless it is nil - # - # @return [Boolean, String] false if no email exists, the email otherwise - def email - unless user.nil? - return user.email - end - end - - ## - # define a new user for the project group by email - # - # @param new_email [String] the email of the new user for the project group - # @return [User] the new user - def email=(new_email) - unless User.find_by(email: email).nil? then - user = User.find_by(email: email) - end - self.save! - end + validates :user, :plan, :access, presence: true + validates :access, numericality: {greater_than: 0} ## # return the access level for the current project group @@ -60,7 +34,6 @@ else return 1 end - self.save! end ## @@ -81,6 +54,11 @@ else self.editor = false end - self.save! + self.creator = true unless self.administrator? || self.editor? + end + + # Ensures that the access attribute is set + def check_access_level + self.access_level = self.access_level end end diff --git a/app/models/section.rb b/app/models/section.rb index 60fd094..4b484ab 100644 --- a/app/models/section.rb +++ b/app/models/section.rb @@ -14,6 +14,8 @@ :questions_attributes, :organisation, :phase, :modifiable, :as => [:default, :admin] + validates :phase, :title, :number, presence: true + ## # return the title of the section # diff --git a/app/models/suggested_answer.rb b/app/models/suggested_answer.rb index 5b94c8d..f3aaaf8 100644 --- a/app/models/suggested_answer.rb +++ b/app/models/suggested_answer.rb @@ -12,6 +12,8 @@ :org, :question, :as => [:default, :admin] + validates :question, :org, :text, presence: true + # EVALUATE CLASS AND INSTANCE METHODS BELOW # # What do they do? do they do it efficiently, and do we need them? diff --git a/app/models/template.rb b/app/models/template.rb index 63b48de..cc8912c 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -9,6 +9,9 @@ has_many :sections, through: :phases has_many :questions, through: :sections + has_many :customizations, class_name: 'Template', foreign_key: 'dmptemplate_id' + belongs_to :dmptemplate, class_name: 'Template' + ## # Possibly needed for active_admin # -relies on protected_attributes gem as syntax depricated in rails 4.2 @@ -21,7 +24,7 @@ s.key :export, defaults: Settings::Template::DEFAULT_SETTINGS end - + validates :org, :title, :version, presence: true # EVALUATE CLASS AND INSTANCE METHODS BELOW # @@ -35,6 +38,7 @@ # # @param ot [String] name of an organisation type e.g. founder # @return [Array] list of published dmptemplates +=begin def self.templates_org_type(ot) # DISCUSS - This function other than the check for the template being published # is a superclass for the below funders_templates @@ -79,7 +83,7 @@ new_templates = self.where("org_id = ?", org_id) return new_templates end - + ## # returns an array with all funders and of the given organisations's # institutional templates @@ -105,7 +109,7 @@ return templates_list end - + ## # Returns the string name of the organisation type of the organisation who # owns this dmptemplate @@ -115,7 +119,8 @@ org_type = org.organisation_type return org_type end - +=end + ## # Verify if a template has customisation by given organisation # @@ -142,6 +147,7 @@ # end end +=begin ## # verify if there are any publish version for the template # @@ -152,8 +158,8 @@ end return false end - - +=end + # OLD CODE STARTS HERE end diff --git a/app/models/theme.rb b/app/models/theme.rb index 11cc32e..315dfe8 100644 --- a/app/models/theme.rb +++ b/app/models/theme.rb @@ -13,6 +13,8 @@ attr_accessible :description, :title, :locale , :as => [:default, :admin] + validates :title, presence: true + # EVALUATE CLASS AND INSTANCE METHODS BELOW # # What do they do? do they do it efficiently, and do we need them? diff --git a/app/models/token_permission_type.rb b/app/models/token_permission_type.rb index a7362d9..71f8186 100644 --- a/app/models/token_permission_type.rb +++ b/app/models/token_permission_type.rb @@ -2,7 +2,8 @@ ## # Associations #has_and_belongs_to_many :org_token_permissions, join_table: "org_token_permissions" - has_and_belongs_to_many :organisations, join_table: 'org_token_permissions', unique: true +# has_and_belongs_to_many :organisations, join_table: 'org_token_permissions', unique: true + has_and_belongs_to_many :orgs, join_table: 'org_token_permissions', unique: true ## # Possibly needed for active_admin diff --git a/app/views/shared/_register_form.html.erb b/app/views/shared/_register_form.html.erb index cb61bfb..dce2ad3 100644 --- a/app/views/shared/_register_form.html.erb +++ b/app/views/shared/_register_form.html.erb @@ -25,7 +25,7 @@ <%= f.hidden_field "user_identifiers[#{scheme}]", value: resource.user_identifiers.first.identifier%> <% end %>
  • - <%= collection_select(:user, :organisation_id, Org.where("parent_id IS NULL").order("sort_name ASC, name ASC"), :id, :name, {include_blank: constant("organisation_types.organisation")}, { :class => 'typeahead org_sign_up' }) %> + <%= collection_select(:user, :org_id, Org.where("parent_id IS NULL").order("sort_name ASC, name ASC"), :id, :name, {include_blank: constant("organisation_types.organisation")}, { :class => 'typeahead org_sign_up' }) %>
  • <% other_organisations = Array.new %> diff --git a/app/views/static_pages/public_plans.html.erb b/app/views/static_pages/public_plans.html.erb index 16cdb15..03c2159 100644 --- a/app/views/static_pages/public_plans.html.erb +++ b/app/views/static_pages/public_plans.html.erb @@ -1,10 +1,10 @@ -<%- model_class = Project -%> +<%- model_class = Plan -%>

    <%= raw t("public_plans_page.title") %>

    -<% if @projects.count > 0 %> +<% if @plans.count > 0 %>

    <%= raw t("public_plans_page.body_text_html", {app_name: Rails.configuration.branding[:application][:name]}) %>

    @@ -19,14 +19,14 @@ - <% @projects.each do |project| %> + <% @plans.each do |plan| %> <% ['non_link_name', 'template', 'organisation', 'owner'].each do |column| %> - <%= project_list_column_body(column, project) %> + <%= project_list_column_body(column, plan) %> <% end %> - <%= link_to t("helpers.project.tab_export"), "#{public_export_path(project)}", :class => "dmp_table_link" %> + <%= link_to t("helpers.project.tab_export"), "#{public_export_path(plan)}", :class => "dmp_table_link" %> <% end %> diff --git a/app/views/templates/_show_question.html.erb b/app/views/templates/_show_question.html.erb index 122c44f..666512a 100644 --- a/app/views/templates/_show_question.html.erb +++ b/app/views/templates/_show_question.html.erb @@ -62,7 +62,7 @@ - <% if question.section.phase.template.org_type != constant("organisation_types.funder") %> + <% if question.section.phase.template.org.org_type != constant("organisation_types.funder") %> <% suggested_answer = question.get_suggested_answer(current_user.org_id) %> <% if !suggested_answer.nil? && suggested_answer.text != "" %> diff --git a/app/views/templates/admin_index.html.erb b/app/views/templates/admin_index.html.erb index 4051b7d..99dde0d 100644 --- a/app/views/templates/admin_index.html.erb +++ b/app/views/templates/admin_index.html.erb @@ -114,7 +114,7 @@ <%if current_user.can_org_admin? then%> - <% if org_template.org_type == constant("organisation_types.funder") then %> + <% if org_template.org.org_type == constant("organisation_types.funder") then %> <%if org_template.has_customisations?(current_user.org_id, org_template) then%> <% b_label = t("org_admin.templates.edit_customisation")%> <%else%> diff --git a/app/views/templates/admin_template.html.erb b/app/views/templates/admin_template.html.erb index fec8cc3..0e6c3fc 100644 --- a/app/views/templates/admin_template.html.erb +++ b/app/views/templates/admin_template.html.erb @@ -19,7 +19,7 @@
    - <% if @template.org_type != constant("organisation_types.funder") || current_user.org_type == constant("organisation_types.funder") then %> + <% if @template.org.org_type != constant("organisation_types.funder") || current_user.org_type == constant("organisation_types.funder") then %> diff --git a/db/seeds.rb b/db/seeds.rb index 216bfda..69f9e6b 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -197,7 +197,7 @@ password_confirmation: "password123", org: Org.find_by(abbreviation: 'GA'), language: Language.find_by(abbreviation: I18n.locale), - perms: Perm.where.not(name: ['admin', 'add_organisations', 'change_org_affiliation']), + perms: Perm.where.not(name: ['admin', 'add_organisations', 'change_org_affiliation', 'grant_api_to_orgs']), accept_terms: true, confirmed_at: Time.zone.now}, {email: "org_admin@example.com", @@ -207,7 +207,7 @@ password_confirmation: "password123", org: Org.find_by(abbreviation: 'UOS'), language: Language.find_by(abbreviation: I18n.locale), - perms: Perm.where.not(name: ['admin', 'add_organisations', 'change_org_affiliation']), + perms: Perm.where.not(name: ['admin', 'add_organisations', 'change_org_affiliation', 'grant_api_to_orgs']), accept_terms: true, confirmed_at: Time.zone.now}, {email: "org_user@example.com", diff --git a/test/functional/application_controller_test.rb b/test/functional/application_controller_test.rb index df2ad7d..705724f 100644 --- a/test/functional/application_controller_test.rb +++ b/test/functional/application_controller_test.rb @@ -4,7 +4,7 @@ include Devise::Test::IntegrationHelpers setup do - @user = users(:cc_super) + @user = User.first stub_blog_calls end @@ -14,21 +14,21 @@ # ---------------------------------------------------------------- test "make sure unauthorized users are redirected to the root path" do - proj = Project.first - get project_path(I18n.locale, proj) + plan = Plan.first + get project_path(I18n.locale, plan) assert_redirected_to "#{root_path}?locale=#{I18n.locale}" end # ---------------------------------------------------------------- test "we can change the locale by changing the URL" do - proj = Project.first + plan = Plan.first if I18n.available_locales.count > 1 # Verify that passing a locale in the URL will set the locale other = I18n.available_locales.last - get project_path(other, proj) + get project_path(other, plan) assert_redirected_to "#{root_path}?locale=#{I18n.locale}", "Expected the changed locale to appear in the query string" assert_equal other, I18n.locale, "Expected the locale to have been set when passing it in URL" end @@ -49,18 +49,18 @@ end # ---------------------------------------------------------------- - test "a user's organisation language specification is used if no locale is passed in the URL and the user has no language setting" do + test "a user's org language specification is used if no locale is passed in the URL and the user has no language setting" do if I18n.available_locales.count > 1 @user.language = nil - @user.organisation[:language_id] = Language.find_by(abbreviation: I18n.available_locales.last).id + @user.org[:language_id] = Language.find_by(abbreviation: I18n.available_locales.last).id @user.save! sign_in @user get root_path - org_lang = Language.find(@user.organisation[:language_id]).abbreviation - assert_equal org_lang.to_s, I18n.locale.to_s, "Expected the locale to have been set to the organisation's chosen language" - assert "#{projects_path}".starts_with?("/#{org_lang}/"), "Expected the system to use the organisation's language specification" + org_lang = Language.find(@user.org[:language_id]).abbreviation + assert_equal org_lang.to_s, I18n.locale.to_s, "Expected the locale to have been set to the org's chosen language" + assert "#{projects_path}".starts_with?("/#{org_lang}/"), "Expected the system to use the org's language specification" end end diff --git a/test/functional/registrations_controller_test.rb b/test/functional/registrations_controller_test.rb index d610c83..1e9136b 100644 --- a/test/functional/registrations_controller_test.rb +++ b/test/functional/registrations_controller_test.rb @@ -4,7 +4,7 @@ include Devise::Test::IntegrationHelpers setup do - @user = users(:cc_super) + @user = User.first end # ------------------------------------------------------------- @@ -57,7 +57,7 @@ cntr = 1 # Test the bare minimum requirements and then all options [form, form.merge({email: "foo.bar#{cntr}@test.org", - organisation_id: Organisation.first.id})].each do |params| + organisation_id: Org.first.id})].each do |params| post user_registration_path, {user: params} assert_response :redirect diff --git a/test/functional/static_pages_controller_test.rb b/test/functional/static_pages_controller_test.rb index 11efee1..f2cd2ff 100644 --- a/test/functional/static_pages_controller_test.rb +++ b/test/functional/static_pages_controller_test.rb @@ -3,10 +3,8 @@ include Devise::Test::IntegrationHelpers setup do - @public_project = Project.create!({title: 'Public Test Project', - dmptemplate: Dmptemplate.first, - organisation: Organisation.first, - visibility: :publicly_visible}) + @public_plan = Plan.create!({title: 'Public Test Project', template: Template.first, + org: Org.first, visibility: :publicly_visible}) end # ---------------------------------------------------------- @@ -14,12 +12,12 @@ get public_plans_path(locale: I18n.locale) assert_response :success - assert_not_nil assigns(:projects) + assert_not_nil assigns(:plans) all_public = true - assigns(:projects).each do |project| - all_public = false unless project.publicly_visible? + assigns(:plans).each do |plan| + all_public = false unless plan.publicly_visible? end assert all_public, "expected all of the plans to have public visibility!" @@ -42,21 +40,17 @@ # ---------------------------------------------------------- test "should NOT export a non-public plan to unauthorized users" do # Set the is_public flag to false and try to access it when not logged in - @public_project.visibility = :privately_visible - @public_project.save! + @public_plan.visibility = :privately_visible + @public_plan.save! - get public_export_path(locale: I18n.locale, id: @public_project) + get public_export_path(locale: I18n.locale, id: @public_plan) assert_redirected_to "#{public_plans_path}", "expected to be redirected to the home page!" assert_equal I18n.t('helpers.settings.plans.errors.no_access_account'), flash[:notice], "Expected an unauthorized message when trying to export a plan (via the public_export route) when the plan is not actually public" - # Set the is_public flag to false and assign ownership to a different user and then try to access it as a non-owner - @public_project.assign_creator(User.last) - @public_project.save! - sign_in User.first - get public_export_path(locale: I18n.locale, id: @public_project) + get public_export_path(locale: I18n.locale, id: @public_plan) assert_redirected_to "#{public_plans_path}", "expected to be redirected to the home page!" assert_equal I18n.t('helpers.settings.plans.errors.no_access_account'), flash[:notice], "Expected an unauthorized message when trying to export a plan (via the public_export route) when the plan is not actually public" diff --git a/test/functional/users/omniauth_callbacks_controller_test.rb b/test/functional/users/omniauth_callbacks_controller_test.rb index 0ea505a..a163631 100644 --- a/test/functional/users/omniauth_callbacks_controller_test.rb +++ b/test/functional/users/omniauth_callbacks_controller_test.rb @@ -3,7 +3,7 @@ setup do @schemes = IdentifierScheme.all - @user = users(:cc_super) + @user = User.first @callback_uris = {} diff --git a/test/test_helper.rb b/test/test_helper.rb index 9b8050d..c5698d7 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -79,7 +79,7 @@ principal_investigator: 'me', principal_investigator_identifier: 'me-1234', description: "this is my plan's informative description", identifier: '1234567890', data_contact: 'me@example.com', visibility: 0, - users: [User.last]) + roles: [Role.new(user: User.last, creator: true)]) assert @plan.valid?, "unable to create new Plan: #{@plan.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" @plan.save! diff --git a/test/unit/option_warning_test.rb b/test/unit/option_warning_test.rb deleted file mode 100644 index e682774..0000000 --- a/test/unit/option_warning_test.rb +++ /dev/null @@ -1,49 +0,0 @@ -require 'test_helper' - -class OptionWarningTest < ActiveSupport::TestCase - include GlobalHelpers - - setup do - @option = Option.first - @organisation = Organisation.first - - @option_warning = OptionWarning.create(organisation: @organisation, option: @option, text: 'Testing') - end - - # --------------------------------------------------- - test "required fields are required" do - assert_not OptionWarning.new.valid? - assert_not OptionWarning.new(option: @option, organisation: @organisation).valid?, "expected the 'text' field to be required" - - # Ensure the bare minimum and complete versions are valid - assert OptionWarning.new(text: 'Test', organisation: @organisation, option: @option).valid?, "expected the 'text', 'option' and 'organisation' fields to be enough to create an OptionWarning!" - end - - # --------------------------------------------------- - test "to_s returns the text of the option warning" do - assert_equal @option_warning.text, @option_warning.to_s, "expected the to_s method to return the text field" - end - - # --------------------------------------------------- - test "can CRUD Guidance" do - obj = OptionWarning.create(organisation: @organisation, option: @option, text: 'Testing option warnings') - assert_not obj.id.nil?, "was expecting to be able to create a new OptionWarning!" - - obj.text = 'Testing an update' - obj.save! - obj.reload - assert_equal 'Testing an update', obj.text, "Was expecting to be able to update the text of the OptionWarning!" - - assert obj.destroy!, "Was unable to delete the OptionWarning!" - end - - # --------------------------------------------------- - test "can manage belongs_to relationship with Organisation" do - verify_belongs_to_relationship(@option_warning, @organisation) - end - - # --------------------------------------------------- - test "can manage belongs_to relationship with Option" do - verify_belongs_to_relationship(@option_warning, @option) - end -end \ No newline at end of file diff --git a/test/unit/org_test.rb b/test/unit/org_test.rb index b249f88..fb999c4 100644 --- a/test/unit/org_test.rb +++ b/test/unit/org_test.rb @@ -104,13 +104,13 @@ # --------------------------------------------------- test "can manage has_many relationship with Dmptemplates" do - tmplt = Template.new(title: 'Added through test') + tmplt = Template.new(title: 'Added through test', version: 1) verify_has_many_relationship(@org, tmplt, @org.templates.count) end # --------------------------------------------------- test "can manage has_many relationship with Customisations" do - tmplt = Template.new(title: 'Testing template') + tmplt = Template.new(title: 'Testing template', version: 1) verify_has_many_relationship(@org, tmplt, @org.templates.count) end diff --git a/test/unit/phase_test.rb b/test/unit/phase_test.rb index 40670fb..33f6e6d 100644 --- a/test/unit/phase_test.rb +++ b/test/unit/phase_test.rb @@ -57,7 +57,7 @@ # --------------------------------------------------- test "can manage belongs_to relationship with Template" do - tmplt = Template.create(org: @org, title: 'Testing relationship') - verify_belongs_to_relationship(@phase, tmplt) + phase = Phase.new(title: 'Tester', number: 9) + verify_belongs_to_relationship(phase, @template) end end diff --git a/test/unit/plan_test.rb b/test/unit/plan_test.rb index 1275ab6..f620df6 100644 --- a/test/unit/plan_test.rb +++ b/test/unit/plan_test.rb @@ -8,20 +8,20 @@ @plan = Plan.create(title: 'Test Plan', template: @template, grant_number: 'Plan12345', identifier: '000912', description: 'This is a test plan', principal_investigator: 'John Doe', principal_investigator_identifier: 'ABC', - data_contact: 'john.doe@example.com', visibility: 1, users: [User.last]) + data_contact: 'john.doe@example.com', visibility: 1, + roles: [Role.new(user: User.last, creator: true)]) end # --------------------------------------------------- test "required fields are required" do assert_not Plan.new.valid? - assert_not Plan.new(users: [User.last], title: 'Testing').valid?, "expected the template field to be required" - assert_not Plan.new(template: @template, title: 'Testing').valid?, "expected at least one user to be required" + assert_not Plan.new(title: 'Testing').valid?, "expected the template field to be required" # Make sure that the Settings gem is defaulting the title for us - assert Plan.new(users: [User.last], template: @template).valid?, "expected the title field to have been set by default by the Settings gem" + assert Plan.new(template: @template).valid?, "expected the title field to have been set by default by the Settings gem" # Ensure the bare minimum and complete versions are valid - a = Plan.new(title: 'Testing', template: @template, users: [User.last]) + a = Plan.new(title: 'Testing', template: @template) assert a.valid?, "expected the 'title', 'template' and at least one 'user' fields to be enough to create an Plan! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" end @@ -39,7 +39,7 @@ # --------------------------------------------------- test "can CRUD Plan" do obj = Plan.create(title: 'Testing CRUD', template: Template.where.not(id: @template.id).first, - users: [User.last], description: "should change") + roles: [Role.new(user: User.last, creator: true)], description: "should change") assert_not obj.id.nil?, "was expecting to be able to create a new Plan! - #{obj.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" obj.description = 'changed' @@ -57,8 +57,9 @@ end # --------------------------------------------------- - test "can manage has_many relationship with Users" do - verify_has_many_relationship(@plan, User.first, @plan.users.count) + test "can manage has_many relationship with Role" do + role = Role.new(user: User.first, editor: true) + verify_has_many_relationship(@plan, role, @plan.roles.count) end # --------------------------------------------------- @@ -69,8 +70,8 @@ # --------------------------------------------------- test "can manage belongs_to relationship with Template" do - tmplt = Template.create(org: @org, title: 'Testing relationship') - verify_belongs_to_relationship(@plan, tmplt) + plan = Plan.new(title: 'Tester') + verify_belongs_to_relationship(plan, Template.first) end end diff --git a/test/unit/question_option_test.rb b/test/unit/question_option_test.rb index 52b7674..b586f0c 100644 --- a/test/unit/question_option_test.rb +++ b/test/unit/question_option_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class QuestionQuestionOptionTest < ActiveSupport::TestCase +class QuestionOptionTest < ActiveSupport::TestCase include GlobalHelpers setup do @@ -40,8 +40,8 @@ # --------------------------------------------------- test "can manage belongs_to relationship with Question" do - question = Question.new(text: 'Testing 123', section: Section.first, question_format: QuestionFormat.find_by(option_based: true)) - verify_belongs_to_relationship(@option, question) + option = QuestionOption.new(text: 'Test', number: 1) + verify_belongs_to_relationship(option, @question) end # --------------------------------------------------- diff --git a/test/unit/role_test.rb b/test/unit/role_test.rb new file mode 100644 index 0000000..3530e79 --- /dev/null +++ b/test/unit/role_test.rb @@ -0,0 +1,68 @@ +require 'test_helper' + +class RoleTest < ActiveSupport::TestCase + + setup do + @user = User.last + + scaffold_plan + + @role = Role.create(user: User.first, plan: @plan) + end + + # --------------------------------------------------- + test "required fields are required" do + assert_not Role.new.valid? + assert_not Role.new(plan: Plan.first, access: 1).valid?, "expected the 'user' field to be required" + assert_not Role.new(user: @user, access: 1).valid?, "expected the 'plan' field to be required" + + # Ensure the bar minimum and complete versions are valid + plan = Plan.create(title: 'Test Plan', template: Template.last) + a = Role.new(user: @user, plan: plan) + assert a.valid?, "expected the 'user', 'plan' and 'access' fields to be enough to create an Role! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + end + + # --------------------------------------------------- + test "access is properly defaulted" do + assert_equal 1, @role.access_level + end + + # --------------------------------------------------- + test "access_level acts a proxy to the 'access' FlagShihTzu bit flag field" do + assert @role.creator?, "expected the role to be creator" + + @role.administrator = true + assert @role.administrator?, "expected the role to be administrator after setting 'administrator'" + @role.administrator = false + + @role.access_level = 3 + assert @role.administrator?, "expected the role to be administrator after setting 'access_level' >= 3" + end + + # --------------------------------------------------- + test "can CRUD Role" do + plan = Plan.create(title: 'Test Plan', template: Template.last) + obj = Role.create(user: @user, plan: plan, access_level: 1) + assert_not obj.id.nil?, "was expecting to be able to create a new Role: #{obj.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + + obj.access_level = 2 + obj.save! + obj.reload + assert_equal 2, obj.access_level, "Was expecting to be able to update the text of the Role!" + + assert obj.destroy!, "Was unable to delete the Role!" + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with User" do + role = Role.new(plan: Plan.first, access: 3) + verify_belongs_to_relationship(role, User.first) + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with Plan" do + role = Role.new(user: User.first, access: 3) + verify_belongs_to_relationship(role, Plan.first) + end + +end \ No newline at end of file diff --git a/test/unit/section_test.rb b/test/unit/section_test.rb new file mode 100644 index 0000000..f827044 --- /dev/null +++ b/test/unit/section_test.rb @@ -0,0 +1,53 @@ +require 'test_helper' + +class SectionTest < ActiveSupport::TestCase + + setup do + scaffold_template + + @section = Section.create(title: 'Test Section', description: 'My test section', number: 99, + published: true, phase: @template.phases.first, modifiable: false) + end + + # --------------------------------------------------- + test "required fields are required" do + assert_not Section.new.valid? + assert_not Section.new(phase: @template.phases.last, number: 9).valid?, "expected the 'title' field to be required" + assert_not Section.new(title: 'Tester', number: 9).valid?, "expected the 'phase' field to be required" + assert_not Section.new(phase: @template.phases.last, title: 'Tester').valid?, "expected the 'number' field to be required" + + # Ensure the bare minimum and complete versions are valid + a = Section.new(phase: @template.phases.last, title: 'Tester', number: 9) + assert a.valid?, "expected the 'phase', 'title' and 'number' fields to be enough to create an Section! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + end + + # --------------------------------------------------- + test "to_s returns the title" do + assert_equal @section.title, @section.to_s + end + + # --------------------------------------------------- + test "can CRUD Section" do + obj = Section.create(phase: @template.phases.last, title: 'Tester', number: 9) + assert_not obj.id.nil?, "was expecting to be able to create a new Section: #{obj.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + + obj.description = 'my tester' + obj.save! + obj.reload + assert_equal 'my tester', obj.description, "Was expecting to be able to update the description of the Section!" + + assert obj.destroy!, "Was unable to delete the Section!" + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with Phase" do + section = Section.new(title: 'Tester', number: 99) + verify_belongs_to_relationship(section, @template.phases.first) + end + + # --------------------------------------------------- + test "can manage has_many relationship with Question" do + question = Question.new(text: 'Testing', number: 1) + verify_has_many_relationship(@section, question, @section.questions.count) + end +end \ No newline at end of file diff --git a/test/unit/suggested_answer_test.rb b/test/unit/suggested_answer_test.rb new file mode 100644 index 0000000..7cae6ce --- /dev/null +++ b/test/unit/suggested_answer_test.rb @@ -0,0 +1,56 @@ +require 'test_helper' + +class SuggestedAnswerTest < ActiveSupport::TestCase + + setup do + scaffold_template + + @org = Org.last + @question = @template.phases.first.sections.first.questions.first + + @suggested_answer = SuggestedAnswer.create(org: @org, question: @question, text: 'Test', + is_example: true) + end + + # --------------------------------------------------- + test "required fields are required" do + assert_not SuggestedAnswer.new.valid? + assert_not SuggestedAnswer.new(org: @org, text: 'Tester').valid?, "expected the 'question' field to be required" + assert_not SuggestedAnswer.new(question: @question, text: 'Tester').valid?, "expected the 'org' field to be required" + assert_not SuggestedAnswer.new(org: @org, question: @question).valid?, "expected the 'text' field to be required" + + # Ensure the bare minimum and complete versions are valid + a = SuggestedAnswer.new(org: @org, question: @question, text: 'Tester') + assert a.valid?, "expected the 'org', 'question' and 'text' fields to be enough to create an SuggestedAnswer! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + end + + # --------------------------------------------------- + test "to_s returns the text" do + assert_equal @suggested_answer.text, @suggested_answer.to_s + end + + # --------------------------------------------------- + test "can CRUD SuggestedAnswer" do + obj = SuggestedAnswer.create(org: @org, question: @question, text: 'Tester') + assert_not obj.id.nil?, "was expecting to be able to create a new SuggestedAnswer: #{obj.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + + obj.text = 'my tester' + obj.save! + obj.reload + assert_equal 'my tester', obj.text, "Was expecting to be able to update the text of the SuggestedAnswer!" + + assert obj.destroy!, "Was unable to delete the SuggestedAnswer!" + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with Org" do + suggested_answer = SuggestedAnswer.new(question: @question, text: 'Testing') + verify_belongs_to_relationship(suggested_answer, @org) + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with Question" do + suggested_answer = SuggestedAnswer.new(org: @org, text: 'Testing') + verify_belongs_to_relationship(suggested_answer, @question) + end +end \ No newline at end of file diff --git a/test/unit/template_test.rb b/test/unit/template_test.rb index 0db44c4..cb8f9b9 100644 --- a/test/unit/template_test.rb +++ b/test/unit/template_test.rb @@ -3,38 +3,33 @@ class TemplateTest < ActiveSupport::TestCase setup do - @template = Dmptemplate.first - - @organisation = Organisation.first + @org = Org.last + + scaffold_template end def settings(extras = {}) {margin: (@margin || { top: 10, bottom: 10, left: 10, right: 10 }), - font_face: (@font_face || Settings::Dmptemplate::VALID_FONT_FACES.first), + font_face: (@font_face || Settings::Template::VALID_FONT_FACES.first), font_size: (@font_size || 11) }.merge(extras) end def default_formatting - Settings::Dmptemplate::DEFAULT_SETTINGS[:formatting] + Settings::Template::DEFAULT_SETTINGS[:formatting] end # --------------------------------------------------- test "required fields are required" do - assert_not Dmptemplate.new.valid? - 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" + assert_not Template.new.valid? + assert_not Template.new(org: @org, title: 'Tester').valid?, "expected the 'version' field to be required" + assert_not Template.new(version: 1, title: 'Tester').valid?, "expected the 'org' field to be required" + assert_not Template.new(org: @org, version: 1).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') - assert a.valid?, "expected the 'title' and 'organisation' fields to be enough to create an Dmptemplate! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + # Ensure the bare minimum and complete versions are valid + a = Template.new(org: @org, version: 1, title: 'Tester') + assert a.valid?, "expected the 'org', 'version' and 'title' fields to be enough to create an Template! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" end - - # --------------------------------------------------- - test "to_s method returns the title" do - assert_equal @template.title, @template.to_s - end - # ---------- settings ---------- # --------------------------------------------------- @@ -230,95 +225,24 @@ assert_equal(default_formatting, @template.settings(:export).formatting) end -<<<<<<< HEAD -======= - # ---------- templates_org_type ---------- - test "templates_org_type returns all published" do - OrganisationType.find_each do |org_type| - result_templates = Dmptemplate.templates_org_type(org_type.name) - my_list = Array.new - org_type.organisations.each do |org| - my_list += org.dmptemplates - end - my_list.each do |template| - if template.published - assert_includes(result_templates, template, "Template: #{template.title}} of type #{org_type.name}, not returned by templates_org_type") - end - end - end - end - - # ---------- funders_templates ---------- - test "funders_templates returns all funder organisation templates" do - result_templates = Dmptemplate.funders_templates - 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 - end - end - - # ---------- own_institutional_templates ---------- - test "own_institutional_templates returns all templates belonging to given org_id" do - Org.find_each do |org| - result_templates = Dmptemplate.own_institutional_templates(org.id) - org.dmptemplates.each do |template| - assert_includes(result_templates, template, "Template: #{template.title} not returned by own_institutional_templates") - end - end - end - - # ---------- funders_and_own_templates ---------- - test "funders_and_own_templates returns all funder and own given org_id templates" do - Org.find_each do |org| - result_templates = Dmptemplate.funders_and_own_templates(org.id) - org.dmptemplates.each do |template| - assert_includes(result_templates, template, "Template #{template.title} not returned by funders and own templates") - end - end - 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 - end - end - - # ---------- org_type ---------- - test "org_type properly returns the name of the template's organisation's type" do - Dmptemplate.find_each do |template| - assert_equal( template.org_type, template.organisation.organisation_type.name, "Template: #{template.title} returned #{template.org_type}, instead of #{template.organisation.organisation_type.name}") - end - end - ->>>>>>> final_schema # ---------- has_customisations? ---------- test "has_customisations? correctly identifies if a given org has customised the template" do - # TODO: Impliment after understanding has_customisations + # TODO: Not sure if this is still an applicable method end - # ---------- has_published_versions? ---------- - test "has_published_versions? correctly identifies published versions" do - Dmptemplate.find_each do |template| - template.phases.each do |phase| - unless phase.latest_published_version.nil? - assert(template.has_published_versions? , "there was a published version of phase: #{phase.title}") - end - end - end - end - + # --------------------------------------------------- test "can CRUD Template" do - tmplt = Dmptemplate.create(organisation: @organisation, title: 'Testing tmeplate') - assert_not tmplt.id.nil?, "was expecting to be able to create a new Dmptemplate!" + tmplt = Template.create(org: @org, version: 1, title: 'Tester') + assert_not tmplt.id.nil?, "was expecting to be able to create a new Template!" tmplt.description = 'Testing an update' tmplt.save! tmplt.reload - assert_equal 'Testing an update', tmplt.description, "Was expecting to be able to update the description of the Dmptemplate!" + assert_equal 'Testing an update', tmplt.description, "Was expecting to be able to update the description of the Template!" - assert tmplt.destroy!, "Was unable to delete the Dmptemplate!" + assert tmplt.destroy!, "Was unable to delete the Template!" end # --------------------------------------------------- @@ -328,20 +252,15 @@ end # --------------------------------------------------- - test "can manage has_many relationship with Project" do - project = Project.new(title: 'Test Project', organisation: @organisation) - verify_has_many_relationship(@template, project, @template.projects.count) + test "can manage has_many relationship with Plan" do + plan = Plan.new(title: 'Test Plan') + verify_has_many_relationship(@template, plan, @template.plans.count) end # --------------------------------------------------- - test "can manage has_many relationship with GuidanceGroup" do - grp = GuidanceGroup.new(name: 'Test Group', organisation: @organisation) - verify_has_many_relationship(@template, grp, @template.guidance_groups.count) - end - - # --------------------------------------------------- - test "can manage belongs_to relationship with Organisation" do - verify_belongs_to_relationship(@template, @organisation) + test "can manage belongs_to relationship with Org" do + tmplt = Template.new(title: 'My test', version: 1) + verify_belongs_to_relationship(tmplt, @org) end end diff --git a/test/unit/theme_test.rb b/test/unit/theme_test.rb new file mode 100644 index 0000000..140af1b --- /dev/null +++ b/test/unit/theme_test.rb @@ -0,0 +1,48 @@ +require 'test_helper' + +class ThemeTest < ActiveSupport::TestCase + + setup do + @theme = Theme.create(title: 'Test Theme', description: 'My test theme', locale: I18n.locale) + end + + # --------------------------------------------------- + test "required fields are required" do + assert_not Theme.new.valid? + + # Ensure the bare minimum are valid + a = Theme.new(title: 'Tester') + assert a.valid?, "expected the 'title' field to be enough to create an Theme! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + end + + # --------------------------------------------------- + test "to_s returns the title" do + assert_equal @theme.title, @theme.to_s + end + + # --------------------------------------------------- + test "can CRUD Theme" do + obj = Theme.create(title: 'Tester') + assert_not obj.id.nil?, "was expecting to be able to create a new Theme!" + + obj.description = 'Testing an update' + obj.save! + obj.reload + assert_equal 'Testing an update', obj.description, "Was expecting to be able to update the description of the Theme!" + + assert obj.destroy!, "Was unable to delete the Theme!" + end + + # --------------------------------------------------- + test "can manage has_many relationship with Question" do + question = Question.new(section: Section.first, text: 'Testing', number: 7) + verify_has_many_relationship(@theme, question, @theme.questions.count) + end + + # --------------------------------------------------- + test "can manage has_many relationship with Guidance" do + guidance = Guidance.new(text: 'Testing') + verify_has_many_relationship(@theme, guidance, @theme.guidances.count) + end + +end diff --git a/test/unit/token_permission_type_test.rb b/test/unit/token_permission_type_test.rb index 58820ba..93c1446 100644 --- a/test/unit/token_permission_type_test.rb +++ b/test/unit/token_permission_type_test.rb @@ -3,14 +3,13 @@ class TokenPermissionTypeTest < ActiveSupport::TestCase def setup - @tpt = token_permission_types(:plans_token_type) - @org = organisations(:curation_center) + @tpt = TokenPermissionType.create(token_type: 'testing', text_description: 'abcd') end # --------------------------------------------------- test "required fields are required" do assert_not TokenPermissionType.new.valid?, "was expecting TokenPermissionType.token_type to be required!" - assert TokenPermissionType.new(token_type: 'testing').valid?, "was only expecting TokenPermissionType.token_type to be required!" + assert TokenPermissionType.new(token_type: 'tester').valid?, "was only expecting TokenPermissionType.token_type to be required!" end # --------------------------------------------------- @@ -20,8 +19,8 @@ # --------------------------------------------------- test "can CRUD" do - tpt = TokenPermissionType.create(token_type: 'testing') - assert_not tpt.id.nil?, "was expecting to be able to create a new TokenPermissionType" + tpt = TokenPermissionType.create(token_type: 'tester') + assert_not tpt.id.nil?, "was expecting to be able to create a new TokenPermissionType - #{tpt.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" tpt.text_description = 'testing updates' tpt.save! @@ -31,14 +30,9 @@ end # --------------------------------------------------- -<<<<<<< HEAD - test "can manage has_many relationship with Organisation" do - org = Organisation.new(name: 'Testing') -======= test "can manage has_many relationship with OrgTokenPermissions" do org = Org.new(name: 'Testing') ->>>>>>> final_schema - verify_has_many_relationship(@tpt, org, @tpt.organisations.count) + verify_has_many_relationship(@tpt, org, @tpt.orgs.count) end end \ No newline at end of file diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 194cd8b..7198708 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -3,25 +3,18 @@ class UserTest < ActiveSupport::TestCase def setup - @super = users(:cc_super) - @funder = users(:funder_admin) - @user = users(:complete_user) + scaffold_plan - @organisation = organisations(:curation_center) - @language = languages(I18n.default_locale) - - @dmptemplate = dmptemplates(:cc_template) - - @complete = User.new(email: 'me@example.edu', - password: 'password', - firstname: 'Test', - surname: 'User', - orcid_id: 'test-orcid', - shibboleth_id: 'test-shib', - accept_terms: 'true', - organisation: @organisation, - api_token: 'ABC123', - language: @language) + @user = User.create(email: 'me@example.edu', + password: 'password', + password_confirmation: 'password', + firstname: 'Test', + surname: 'User', + shibboleth_id: 'test-shib', + accept_terms: 'true', + organisation: Org.last, + api_token: 'ABC123', + language: Language.find_by(abbreviation: I18n.locale)) end # --------------------------------------------------- @@ -34,8 +27,14 @@ assert_not User.new(firstname: 'test', surname: 'user', email: 'me@example.org').valid? # Ensure the bar minimum and complete versions are valid - assert User.new(email: 'me@example.edu', password: 'password').valid? - assert @complete.valid? + a = User.new(email: 'me_testing@example.edu', password: 'password') + assert a.valid?, "expected 'email' and 'password' to be enough to create a User - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + assert @user.valid? + end + + # --------------------------------------------------- + test "email address must be unique" do + assert_not User.new(email: 'me@example.edu', password: 'password').valid? end # --------------------------------------------------- @@ -53,83 +52,90 @@ # --------------------------------------------------- test "name returns the correct value" do # Name should return 'First Last' - assert @super.name.include?(@super.firstname) - assert @super.name.include?(@super.surname) + assert @user.name.include?(@user.firstname) + assert @user.name.include?(@user.surname) # Name should return the email if no first and last are present - @super.firstname = nil - @super.surname = nil - assert_equal @super.email, @super.name + @user.firstname = nil + @user.surname = nil + assert_equal @user.email, @user.name end # --------------------------------------------------- test "only accepts valid email addresses" do - assert @super.valid? + assert @user.valid? - @super.email = 'testing' - assert_not @super.valid? - @super.email = 'testing.tester.org' - assert_not @super.valid? - @super.email = 'testing@tester' - assert_not @super.valid? + @user.email = 'testing' + assert_not @user.valid? + @user.email = 'testing.tester.org' + assert_not @user.valid? + @user.email = 'testing@tester' + assert_not @user.valid? - @super.email = 'testing@tester.org' - assert @super.valid? + @user.email = 'testing@tester.org' + assert @user.valid? end # --------------------------------------------------- test "has default Settings::PlanList" do - assert_not_equal [], @super.settings(:plan_list).columns + assert_not_equal [], @user.settings(:plan_list).columns end # --------------------------------------------------- - test "api token gets removed" do - @super.api_token = 'ABCDEFGHIJKLMNOP' - @super.save! - assert_equal 'ABCDEFGHIJKLMNOP', @super.reload.api_token, "expected the api_token to have been initialized" + test "api token is removed after call to remove_token" do + @user.api_token = 'ABCDEFGHIJKLMNOP' + @user.save! + assert_equal 'ABCDEFGHIJKLMNOP', @user.reload.api_token, "expected the api_token to have been initialized" - @super.remove_token! - assert_equal '', @super.reload.api_token, "expected the api_token to have been removed" + @user.remove_token! + assert_equal '', @user.reload.api_token, "expected the api_token to have been removed" end # --------------------------------------------------- test "api token gets kept or created" do - @super.api_token = 'ABCDEFGHIJKLMNOP' - @super.save! - assert_equal 'ABCDEFGHIJKLMNOP', @super.reload.api_token, "expected the api_token to have been initialized" + @user.api_token = 'ABCDEFGHIJKLMNOP' + @user.save! + assert_equal 'ABCDEFGHIJKLMNOP', @user.reload.api_token, "expected the api_token to have been initialized" - @super.keep_or_generate_token! - assert_equal 'ABCDEFGHIJKLMNOP', @super.reload.api_token, "expected the api_token to have been kept" + @user.keep_or_generate_token! + assert_equal 'ABCDEFGHIJKLMNOP', @user.reload.api_token, "expected the api_token to have been kept" - @super.remove_token! - assert_equal '', @super.reload.api_token, "expected the api_token to have been removed" + @user.remove_token! + assert_equal '', @user.reload.api_token, "expected the api_token to have been removed" - @super.keep_or_generate_token! - assert_not_equal '', @super.reload.api_token, "expected the api_token to have been generated" + @user.keep_or_generate_token! + assert_not_equal '', @user.reload.api_token, "expected the api_token to have been generated" end # --------------------------------------------------- test "responds to all of the authentication options" do - admin = [:can_add_orgs?, :can_change_org?, :can_grant_api_to_orgs?] + super_admins = User.joins(:perms).where('perms.name = ?', 'add_organisations').to_a + org_admins = User.joins(:perms).where('perms.name = ?', 'modify_templates').to_a + users = User.includes(:perms).where(perms: {id: nil}).to_a + + # remove all of the users who also have super_admin privileges + org_admins = org_admins.delete_if{|u| super_admins.include?(u) } + + admin_methods = [:can_add_orgs?, :can_change_org?, :can_grant_api_to_orgs?] - org_admin = [:can_grant_permissions?, :can_modify_templates?, - :can_modify_guidance?, :can_use_api?, :can_modify_org_details?] + org_admin_methods = [:can_grant_permissions?, :can_modify_templates?, + :can_modify_guidance?, :can_use_api?, :can_modify_org_details?] [:can_super_admin?, :can_org_admin?].each do |auth| - assert_respond_to @super, auth, "expected User to respond to #{auth}" + assert_respond_to super_admins.first, auth, "expected User to respond to #{auth}" end # Super Admin - permission checks - admin.each do |auth| - assert @super.send(auth), "expected that Super Admin #{auth}" - assert_not @funder.send(auth), "did NOT expect that Organisation Admin #{auth}" + admin_methods.each do |auth| + assert super_admins.first.send(auth), "expected that Super Admin #{auth}" + assert_not org_admins.first.send(auth), "did NOT expect that Organisation Admin #{auth}" assert_not @user.send(auth), "did NOT expect that User #{auth}" end # Organisational Admin - permission checks - org_admin.each do |auth| - assert @super.send(auth), "expected that the Super Admin #{auth}" - assert @funder.send(auth), "expected that the Organisational Admin #{auth}" + org_admin_methods.each do |auth| + assert super_admins.first.send(auth), "expected that the Super Admin #{auth}" + assert org_admins.first.send(auth), "expected that the Organisational Admin #{auth}" assert_not @user.send(auth), "did NOT expect that User #{auth}" end end @@ -138,16 +144,16 @@ test "can only have one identifier per IdentifierScheme" do @scheme = IdentifierScheme.first - count = @super.user_identifiers.count - @super.user_identifiers << UserIdentifier.new(identifier_scheme: @scheme, identifier: 'abc') - @super.save! - @super.reload + count = @user.user_identifiers.count + @user.user_identifiers << UserIdentifier.new(identifier_scheme: @scheme, identifier: 'abc') + @user.save! + @user.reload - assert_equal (count + 1), @super.user_identifiers.count, "Expected the initial identifier to be saved" + assert_equal (count + 1), @user.user_identifiers.count, "Expected the initial identifier to be saved" - @super.user_identifiers << UserIdentifier.new(identifier_scheme: @scheme, identifier: 'abc') - assert_not @super.valid?, "Expected to NOT be able to add more than one identifier for the same scheme" - assert_equal (count + 1), @super.user_identifiers.count, "Expected the initial identifier to be saved" + @user.user_identifiers << UserIdentifier.new(identifier_scheme: @scheme, identifier: 'abc') + assert_not @user.valid?, "Expected to NOT be able to add more than one identifier for the same scheme" + assert_equal (count + 1), @user.user_identifiers.count, "Expected the initial identifier to be saved" end # --------------------------------------------------- @@ -164,52 +170,56 @@ end # --------------------------------------------------- - test "can manage has_many relationship with Roles" do - role = Role.new(name: 'Added through test') - verify_has_many_relationship(@super, role, @super.roles.count) + test "can manage has_many relationship with Perms" do + perm = Perm.new(name: 'Added through test') + verify_has_many_relationship(@user, perm, @user.perms.count) end # --------------------------------------------------- test "can manage has_many relationship with UserIdentifiers" do id = UserIdentifier.new(identifier_scheme: IdentifierScheme.first, identifier: 'tester') - verify_has_many_relationship(@super, id, @super.user_identifiers.count) + verify_has_many_relationship(@user, id, @user.user_identifiers.count) end # --------------------------------------------------- - test "can manage has_many relationship with Projects" do - # TODO: need to change dmptemplate_id to dmptemplate after refactor of Project - project = Project.new(title: 'Test Project', dmptemplate_id: @dmptemplate.id) - verify_has_many_relationship(@super, project, @super.projects.count) + test "can manage has_many relationship with Plans" do + plan = Plan.new(title: 'Test Project', template: @template) + verify_has_many_relationship(@user, plan, @user.plans.count) end # --------------------------------------------------- test "can manage has_many relationship with Answers" do - # TODO: many need to remove this once we revise/remove locking - project = Project.new(title: 'Test Project', dmptemplate_id: @dmptemplate.id) - plan = Plan.new(project: project) - question = Question.new(text: 'testing question') - answer = Answer.new(plan: plan, question: question, text: "Here's my answer") - verify_has_many_relationship(@super, answer, @super.answers.count) + answer = Answer.new(plan: @plan, + question: @plan.template.phases.first.sections.first.questions.first, + text: 'Testing') + verify_has_many_relationship(@user, answer, @user.answers.count) end # --------------------------------------------------- - test "can manage has_many relationship with PlanSections" do - # TODO: many need to remove this once we revise/remove locking - project = Project.new(title: 'Test Project', dmptemplate_id: @dmptemplate.id) - plan = Plan.new(project: project) - section = Section.new() - ps = PlanSection.new(plan: plan, section: section) - verify_has_many_relationship(@super, ps, @super.plan_sections.count) + test "can manage has_many relationship with Notes" do + answer = Answer.create(plan: @plan, + question: @plan.template.phases.first.sections.first.questions.first, + text: 'Testing') + note = Note.new(answer: answer, text: 'Testing') + verify_has_many_relationship(@user, note, @user.notes.count) end # --------------------------------------------------- - test "can manage belongs_to relationship with Organisation" do - verify_belongs_to_relationship(@super, @organisation) + test "can manage has_many relationship with ExportedPlans" do + plan = ExportedPlan.new(plan: @plan, format: ExportedPlan::VALID_FORMATS.last) + verify_has_many_relationship(@user, plan, @user.exported_plans.count) + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with Org" do + org = Org.new(name: 'Tester', abbreviation: 'TST') + verify_belongs_to_relationship(@user, org) end # --------------------------------------------------- test "can manage belongs_to relationship with Language" do - verify_belongs_to_relationship(@super, @language) + language = Language.new(name: 'esperonto', abbreviation: 'zz') + verify_belongs_to_relationship(@user, language) end end