Skip to content

Commit

Permalink
Add User Controller Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasUCSB committed Nov 15, 2024
1 parent 5c9f9c9 commit 45a4176
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ gem 'bcrypt', '~> 3.1.7'

gem 'jwt'

gem 'rails-controller-testing'
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ GEM
activesupport (= 7.1.5)
bundler (>= 1.15.0)
railties (= 7.1.5)
rails-controller-testing (1.0.5)
actionpack (>= 5.0.1.rc1)
actionview (>= 5.0.1.rc1)
activesupport (>= 5.0.1.rc1)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
Expand Down Expand Up @@ -228,6 +232,7 @@ DEPENDENCIES
pg (~> 1.5)
puma (>= 5.0)
rails (~> 7.1.2)
rails-controller-testing
sprockets-rails
stimulus-rails
turbo-rails
Expand Down
1 change: 0 additions & 1 deletion app/controllers/user_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def handle_register
session[:user] = @user
redirect_to root_path
else
puts @user.errors.full_messages.join(', ')
render :register_form, status: :unprocessable_entity
end
end
Expand Down
6 changes: 2 additions & 4 deletions test/controllers/session_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require "test_helper"
require 'test_helper'

class SessionControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end

end
26 changes: 21 additions & 5 deletions test/controllers/user_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
require "test_helper"
require 'test_helper'

class UserControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
test "should get register_form" do
get register_path
assert_response :success
assert assigns(:user).is_a?(User)
end

test "should handle_register with valid parameters" do
post register_path, params: { user: { username: "testuser", password: "password", password_confirmation: "password" } }
assert_redirected_to root_path
assert User.exists?(username: "testuser")
assert_equal session[:user]['id'], User.find_by(username: "testuser").id
end

test "should handle_register with invalid parameters" do
post register_path, params: { user: { username: "testuser", password: "password", password_confirmation: "wrongpassword" } }
assert_template :register_form
assert_response :unprocessable_entity
assert assigns(:user).errors.full_messages.include?("Password confirmation doesn't match Password")
end
end
7 changes: 7 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
username: "testuser_yml"
password_digest: <%= BCrypt::Password.create("password") %>

two:
username: "anotheruser_yml"
password_digest: <%= BCrypt::Password.create("anotherpassword") %>

0 comments on commit 45a4176

Please sign in to comment.