diff --git a/app/controllers/guidance_groups_controller.rb b/app/controllers/guidance_groups_controller.rb index 585f32c..0256cf0 100644 --- a/app/controllers/guidance_groups_controller.rb +++ b/app/controllers/guidance_groups_controller.rb @@ -29,7 +29,7 @@ if @guidance_group.save redirect_to admin_index_guidance_path, notice: _('Guidance group was successfully created.') else - render action: "new" + redirect_to admin_new_guidance_group_path(current_user.org), notice: generate_error_notice(@guidance_group) end end @@ -50,11 +50,11 @@ if @guidance_group.update_attributes(params[:guidance_group]) redirect_to admin_index_guidance_path(params[:guidance_group]), notice: _('Guidance group was successfully updated.') else - render action: "edit" + redirect_to admin_edit_guidance_group_path(@guidance_group), notice: generate_error_notice(@guidance_group) end end - +# TODO: This does not have a route in config/routes.rb and is unreachable! # PUT /guidance_groups/1 def admin_update_publish @guidance_group = GuidanceGroup.find(params[:id]) @@ -65,7 +65,7 @@ if @guidance_group.update_attributes(params[:guidance_group]) redirect_to admin_index_guidance_path(params[:guidance_group]), notice: _('Guidance group was successfully updated.') else - render action: "edit" + redirect_to admin_index_guidance_path(@guidance_group), notice: generate_error_notice(@guidance_group) end end diff --git a/test/functional/guidance_groups_controller_test.rb b/test/functional/guidance_groups_controller_test.rb index b708c93..0d27ebd 100644 --- a/test/functional/guidance_groups_controller_test.rb +++ b/test/functional/guidance_groups_controller_test.rb @@ -26,7 +26,7 @@ # admin_update_guidance_group PUT /org/admin/guidancegroup/:id/admin_update guidance_groups#admin_update setup do - @user = User.where(org: GuidanceGroup.first.org).select{|u| u.can_org_admin?}.first + @user = org_admin_from(GuidanceGroup.first.org) end # GET /org/admin/guidancegroup/:id/admin_show (admin_show_guidance_group_path) @@ -59,7 +59,26 @@ # POST /org/admin/guidancegroup/:id/admin_create (admin_create_guidance_group_path) # ---------------------------------------------------------- test 'create a new guidance_group' do + params = {org_id: @user.org.id, published: false, name: 'Testing create'} + # Should redirect user to the root path if they are not logged in! + post admin_create_guidance_group_path(@user.org), {guidance_group: params} + assert_unauthorized_redirect_to_root_path + + sign_in @user + + post admin_create_guidance_group_path(@user.org), {guidance_group: params} + assert_response :redirect + assert_redirected_to admin_index_guidance_path(@user.org) + assert_equal _('Guidance group was successfully created.'), flash[:notice] + assert assigns(:guidance_group) + + # Invalid object + post admin_create_guidance_group_path(@user.org), {guidance_group: {name: nil}} + assert_response :redirect + assert_redirected_to admin_new_guidance_group_path(@user.org) + assert assigns(:guidance_group) + assert flash[:notice].starts_with?(_('Unable to save your changes.')) end # GET /org/admin/guidancegroup/:id/admin_edit (admin_edit_guidance_group_path) @@ -78,19 +97,41 @@ # PUT /org/admin/templates/:id/admin_template (admin_update_guidance_group_path) # ---------------------------------------------------------- test 'update the guidance_group' do + params = {name: 'Testing UPDATE'} - end - - # PUT /org/admin/guidancegroup/:id/admin_update (admin_update_guidance_group_path) - # ---------------------------------------------------------- - test 'publish the guidance_group' do + # Should redirect user to the root path if they are not logged in! + put admin_update_guidance_group_path(GuidanceGroup.first), {guidance_group: params} + assert_unauthorized_redirect_to_root_path + sign_in @user + + put admin_update_guidance_group_path(GuidanceGroup.first), {guidance_group: params} + assert_response :redirect + assert_redirected_to "#{admin_index_guidance_path(@user.org)}?name=Testing+UPDATE" + assert_equal _('Guidance group was successfully updated.'), flash[:notice] + assert assigns(:guidance_group) + + # Invalid object + put admin_update_guidance_group_path(GuidanceGroup.first), {guidance_group: {name: nil}} + assert_response :redirect + assert_redirected_to admin_edit_guidance_group_path(GuidanceGroup.first) + assert assigns(:guidance_group) + assert flash[:notice].starts_with?(_('Unable to save your changes.')) end # DELETE /org/admin/guidancegroup/:id/admin_destroy (admin_destroy_guidance_group_path) # ---------------------------------------------------------- test 'delete the guidance_group' do + # Should redirect user to the root path if they are not logged in! + delete admin_destroy_guidance_group_path(GuidanceGroup.first) + assert_unauthorized_redirect_to_root_path + sign_in @user + + delete admin_destroy_guidance_group_path(GuidanceGroup.first) + assert_response :redirect + assert_redirected_to admin_index_guidance_path + assert_equal _('Guidance group was successfully deleted.'), flash[:notice] end end \ No newline at end of file diff --git a/test/functional/phases_controller_test.rb b/test/functional/phases_controller_test.rb index ca5b3b9..7bd9ff9 100644 --- a/test/functional/phases_controller_test.rb +++ b/test/functional/phases_controller_test.rb @@ -6,7 +6,7 @@ scaffold_template # Get the first Org Admin - @user = User.where(org: @template.org).select{|u| u.can_org_admin?}.first + @user = org_admin_from(Template.first.org) @plan = Plan.create(template: @template, title: 'Test Plan', roles: [Role.new(user: @user, creator: true)]) diff --git a/test/functional/questions_controller_test.rb b/test/functional/questions_controller_test.rb index 6df2125..9fad289 100644 --- a/test/functional/questions_controller_test.rb +++ b/test/functional/questions_controller_test.rb @@ -7,7 +7,7 @@ @section = @template.phases.first.sections.first # Get the first Org Admin - @user = User.where(org: @template.org).select{|u| u.can_org_admin?}.first + @user = org_admin_from(Template.first.org) @question_format = QuestionFormat.where(option_based: false).first end diff --git a/test/functional/sections_controller_test.rb b/test/functional/sections_controller_test.rb index c559d87..30cb3a6 100644 --- a/test/functional/sections_controller_test.rb +++ b/test/functional/sections_controller_test.rb @@ -7,7 +7,7 @@ @phase = @template.phases.first # Get the first Org Admin - @user = User.where(org: @template.org).select{|u| u.can_org_admin?}.first + @user = org_admin_from(Template.first.org) end # TODO: The following methods SHOULD replace the old 'admin_' prefixed methods. The routes file already has diff --git a/test/functional/templates_controller_test.rb b/test/functional/templates_controller_test.rb index 468fd0e..11487f2 100644 --- a/test/functional/templates_controller_test.rb +++ b/test/functional/templates_controller_test.rb @@ -6,7 +6,7 @@ scaffold_template # Get the first Org Admin - @user = User.where(org: @template.org).select{|u| u.can_org_admin?}.first + @user = org_admin_from(Template.first.org) end # TODO: The following methods SHOULD replace the old 'admin_' prefixed methods. The routes file already has diff --git a/test/functional/users/omniauth_callbacks_controller_test.rb b/test/functional/users/omniauth_callbacks_controller_test.rb index 924bba7..82bbfbd 100644 --- a/test/functional/users/omniauth_callbacks_controller_test.rb +++ b/test/functional/users/omniauth_callbacks_controller_test.rb @@ -74,7 +74,7 @@ assert_redirected_to "#{edit_user_registration_path}", "Expected a redirect to the edit profile page, #{edit_user_registration_path}, when omniauth returns with a valid identifier for a user that is already signed in!" # reload the user record and make sure the omniauth value was attached to their record - usr = User.find(@user) + usr = User.find(@user.id) assert_equal usr.user_identifiers.find_by(identifier_scheme: scheme).identifier, 'foo:bar' end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 9f72ecb..b23110e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -26,14 +26,23 @@ # Add more helper methods to be used by all tests here... - - # Return the user instance variable # ---------------------------------------------------------------------- def current_user return @user end + + # Get the organisational admin for the Org specified or create one + # ---------------------------------------------------------------------- + def org_admin_from(org) + usr = org.users.select{|u| u.can_org_admin?}.first + usr = User.create!(email: "admin@example.com", password: "password123", password_confirmation: "password123", + perms: Perm.where.not(name: ['admin', 'add_organisations', 'change_org_affiliation', 'grant_api_to_orgs']), + org: org, accept_terms: true, confirmed_at: Time.zone.now) if usr.nil? + usr + end + # Convert Ruby Class Names into attribute names (e.g. MyClass --> my_class) # ---------------------------------------------------------------------- def class_name_to_attribute_name(name)