Skip to content

Commit

Permalink
Add test coverage for organziation/onboarding controller
Browse files Browse the repository at this point in the history
  • Loading branch information
martinemde committed Nov 11, 2024
1 parent e4228d7 commit 0d7a94b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 25 deletions.
13 changes: 4 additions & 9 deletions app/controllers/organizations/onboarding_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
class Organizations::OnboardingController < Organizations::Onboarding::BaseController
def index
redirect_to organization_onboarding_name_path
end
class Organizations::OnboardingController < ApplicationController
before_action :redirect_to_signin, unless: :signed_in?
before_action :redirect_to_new_mfa, if: :mfa_required_not_yet_enabled?

def destroy
if @organization_onboarding.completed?
flash[:error] = "Cannot destroy a completed onboarding"
elsif @organization_onboarding.persisted?
@organization_onboarding.destroy
end
OrganizationOnboarding.destroy_by(created_by: Current.user, status: :pending)

redirect_to dashboard_path
end
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@
get '/sign_up' => 'users#new', as: 'sign_up' if Clearance.configuration.allow_sign_up?

namespace :organizations, as: :organization do
resource :onboarding, only: %i[index destroy], controller: "onboarding"
get "onboarding", to: redirect("/organizations/onboarding/name")
delete "onboarding", to: "onboarding#destroy"

namespace :onboarding do
get "name", to: "name#new"
post "name", to: "name#create"
Expand Down
42 changes: 42 additions & 0 deletions test/integration/organizations/onboarding_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "test_helper"

class Organizations::OnboardingControllerTest < ActionDispatch::IntegrationTest
setup do
@user = create(:user, remember_token_expires_at: Gemcutter::REMEMBER_FOR.from_now)
post session_path(session: { who: @user.handle, password: PasswordHelpers::SECURE_TEST_PASSWORD })
end

context "GET /organizations/onboarding" do
should "redirect to onboarding start page" do
get "/organizations/onboarding"

assert_redirected_to organization_onboarding_name_path
end
end

context "DELETE /organizations/onboarding" do
should "not destroy an OrganizationOnboarding that is already completed" do
organization_onboarding = create(:organization_onboarding, :completed, created_by: @user)

delete "/organizations/onboarding"

assert_redirected_to dashboard_path
assert OrganizationOnboarding.find_by(id: organization_onboarding.id)
end

should "destroy an existing OrganizationOnboarding created by the current user" do
organization_onboarding = create(:organization_onboarding, created_by: @user)

delete "/organizations/onboarding"

assert_redirected_to dashboard_path
assert_nil OrganizationOnboarding.find_by(id: organization_onboarding.id)
end

should "redirect to the dashboarding if the current user has not started organization onboarding" do
delete "/organizations/onboarding"

assert_redirected_to dashboard_path
end
end
end
30 changes: 15 additions & 15 deletions test/models/organization_onboarding_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class OrganizationOnboardingTest < ActiveSupport::TestCase
@onboarding.save
@onboarding.reload

assert @onboarding.invites.find_by(user_id: @other_user.id).nil?
assert_nil @onboarding.invites.find_by(user_id: @other_user.id)
end
end

Expand All @@ -78,19 +78,19 @@ class OrganizationOnboardingTest < ActiveSupport::TestCase
end

context "when the name is a valid user" do
setup do
should "be valid" do
@onboarding.organization_handle = @owner.handle
end

should :be_valid
assert_predicate @onboarding, :valid?
end
end

context "when the name is not valid" do
setup do
should "be invalid" do
@onboarding.organization_handle = "invalid"
end

should :be_invalid
assert_predicate @onboarding, :invalid?
end
end
end

Expand All @@ -100,19 +100,19 @@ class OrganizationOnboardingTest < ActiveSupport::TestCase
end

context "when the name is a valid gem that the user owns" do
setup do
@onboarding.organization_name = @rubygem.name
end
should "be valid" do
@onboarding.organization_handle = @rubygem.name

should :be_valid
assert_predicate @onboarding, :valid?
end
end

context "when the name is not valid" do
setup do
@onboarding.organization_name = "invalid"
end
should "be invalid" do
@onboarding.organization_handle = "invalid"

should :be_invalid
assert_predicate @onboarding, :invalid?
end
end
end
end
Expand Down

0 comments on commit 0d7a94b

Please sign in to comment.