Skip to content

Commit

Permalink
test: added missing tests for all controllers and models
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellberg committed Apr 4, 2024
1 parent aa1090e commit a670587
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 24 deletions.
20 changes: 3 additions & 17 deletions app/controllers/accounts/members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@ class Accounts::MembersController < ApplicationController
before_action :ensure_team_account
before_action :setup_account

def index
end

def edit
@account_user = @account.account_users.find_puid!(params[:id])
end

def destroy
@account_user = @account.account_users.find_puid!(params[:id])
if @account_user.destroy
flash[:notice] = I18n.t("account_users.destroy.success")
redirect_to members_path
else
flash[:alert] = I18n.t("account_users.destroy.failure")
redirect_to members_path, status: :unprocessable_entity
end
@account_user.destroy!
flash[:notice] = I18n.t("account_users.destroy.success")
redirect_to members_path
end

private
Expand All @@ -30,11 +23,4 @@ def setup_account
@account = current_account
@members = current_account.account_users
end

def options_for_roles
AccountUser::ROLES.map do |role|
[I18n.t("account_users.roles.#{role}"), role]
end
end
helper_method :options_for_roles
end
2 changes: 1 addition & 1 deletion app/controllers/users/two_factor_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def verify
redirect_to edit_two_factor_path, notice: I18n.t("users.two_factor.setup.success")
else
@user.errors.add(:otp_attempt, I18n.t("users.two_factor.setup.invalid_code"))
render turbo_stream: turbo_stream.replace("two_factor_form", partial: "users/two_factor/form", locals: {user: @user})
render turbo_stream: turbo_stream.replace("two_factor_form", partial: "users/two_factor/form", locals: {user: @user}), status: :unprocessable_entity
end
end

Expand Down
14 changes: 14 additions & 0 deletions test/controllers/accounts/members_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ class Accounts::MembersControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to edit_account_path(account_id: nil)
end

test "can show edit member page" do
user = create(:user)
some_user = create(:user)

account = create(:account, name: "Team account")
account.account_users << AccountUser.create(user: user, role: "owner")
account.account_users << AccountUser.create(user: some_user, role: "admin")

sign_in user
get edit_member_path(account_id: account, id: some_user.account_users.first)

assert_response :success
end

test "can show members as team account" do
user = create(:user)
account = create(:account, name: "Team account")
Expand Down
28 changes: 27 additions & 1 deletion test/controllers/accounts_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
require "test_helper"

class AccountsControllerTest < ActionDispatch::IntegrationTest
test "can render edit page" do
test "can render new account page" do
sign_in create(:user)
get new_account_path
assert_response :success
end

test "can render edit account page" do
sign_in create(:user)
get edit_account_path
assert_response :success
end

test "can create new team account" do
user = create(:user)
sign_in user

post accounts_path, params: {account: {name: "Foobar team"}}
new_team = Account.find_by(name: "Foobar team")

assert_redirected_to dashboard_path(account_id: Account.last)
assert user.reload.accounts.include?(new_team)
end

test "shows error on invalid team account creation" do
user = create(:user)
sign_in user

post accounts_path, params: {account: {name: "no"}}
assert_response :unprocessable_entity
assert_template :new
end

test "can update personal accounts" do
user = create(:user)
sign_in user
Expand Down
6 changes: 6 additions & 0 deletions test/controllers/users/cancellation_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
require "test_helper"

class Users::RegistrationControllerTest < ActionDispatch::IntegrationTest
test "can view cancellation page" do
sign_in create(:user)
get delete_user_registration_path
assert_response :success
end

test "can delete a user without teams" do
user = create(:user)
sign_in user
Expand Down
13 changes: 13 additions & 0 deletions test/controllers/users/onboarding_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,17 @@ class Users::OnboardingControllerTest < ActionDispatch::IntegrationTest
get onboarding_path
assert_response :success
end

test "can onboard user" do
sign_in create(:user, personal_account: nil)
post onboarding_path, params: {account: {name: "Personal Account"}}
assert_redirected_to dashboard_path
end

test "validates user onboarding" do
sign_in create(:user, personal_account: nil)
post onboarding_path, params: {account: {name: "no"}}
assert_response :unprocessable_entity
assert_template :new
end
end
21 changes: 16 additions & 5 deletions test/controllers/users/sessions_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
require "test_helper"

class Users::SessionsControllerTest < ActionDispatch::IntegrationTest
test "require otp if otp is enabled" do
test "can sign in if two factor is disabled" do
user = create(:user)
post user_session_path, params: {user: {email: user.email, password: user.password}}
assert_response :redirect
assert_redirected_to dashboard_path
end

test "can sign in with otp if two factor is enabled" do
user = create(:user, :otp_enabled)
post user_session_path, params: {user: {email: user.email, password: user.password}}
assert_response :unprocessable_entity
assert_template "users/sessions/otp"

post user_session_path, params: {user: {otp_attempt: user.current_otp}}
assert_redirected_to dashboard_path
end

test "can sign in if otp is disabled" do
user = create(:user)
test "can't sign in with invalid otp if two factor is enabled" do
user = create(:user, :otp_enabled)
post user_session_path, params: {user: {email: user.email, password: user.password}}
assert_response :redirect
assert_redirected_to dashboard_path
post user_session_path, params: {user: {otp_attempt: "123456"}}
assert_response :unprocessable_entity
assert_template "users/sessions/otp"
end

test "renders form again if invalid email" do
Expand Down
20 changes: 20 additions & 0 deletions test/controllers/users/two_factor_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class Users::TwoFactorControllerTest < ActionDispatch::IntegrationTest
assert_not_equal user.otp_secret, user.reload.otp_secret
end

test "can view setup page" do
sign_in create(:user)
get setup_two_factor_path
assert_response :success
end

test "redirects to show page if two factor is alredy disabled" do
user = create(:user)
sign_in user
Expand All @@ -29,6 +35,20 @@ class Users::TwoFactorControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to edit_two_factor_path
end

test "does not activate 2fa with invalid verification code" do
user = create(:user, otp_secret: User.generate_otp_secret)
sign_in user
post verify_two_factor_path, params: {user: {otp_attempt: "123456"}}
assert_response :unprocessable_entity
end

test "activates 2fa with valid verification code" do
user = create(:user, otp_secret: User.generate_otp_secret)
sign_in user
post verify_two_factor_path, params: {user: {otp_attempt: user.current_otp}}
assert_redirected_to edit_two_factor_path
end

test "requires valid otp code to disable two factor authentication" do
user = create(:user, :otp_enabled)
sign_in user
Expand Down
5 changes: 5 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

require "simplecov"
SimpleCov.start "rails" do
add_filter %r{^/app/channels/}
add_filter %r{^/app/components/}
add_filter %r{^/app/jobs/}
add_filter %r{^/app/mailers/}
add_filter %r{^/lib/generators/view_component/}
add_filter %r{^/test/}
end

Expand Down

0 comments on commit a670587

Please sign in to comment.