diff --git a/Gemfile b/Gemfile index 6ce6006..27647d1 100644 --- a/Gemfile +++ b/Gemfile @@ -33,3 +33,4 @@ gem 'bcrypt', '~> 3.1.7' gem 'jwt' +gem 'rails-controller-testing' diff --git a/Gemfile.lock b/Gemfile.lock index af7fcaa..4a14368 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 @@ -228,6 +232,7 @@ DEPENDENCIES pg (~> 1.5) puma (>= 5.0) rails (~> 7.1.2) + rails-controller-testing sprockets-rails stimulus-rails turbo-rails diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 967c5d3..62f8249 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -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 diff --git a/test/controllers/session_controller_test.rb b/test/controllers/session_controller_test.rb index 9e4649a..0178d1b 100644 --- a/test/controllers/session_controller_test.rb +++ b/test/controllers/session_controller_test.rb @@ -1,7 +1,5 @@ -require "test_helper" +require 'test_helper' class SessionControllerTest < ActionDispatch::IntegrationTest - # test "the truth" do - # assert true - # end + end diff --git a/test/controllers/user_controller_test.rb b/test/controllers/user_controller_test.rb index f067a70..475f9c1 100644 --- a/test/controllers/user_controller_test.rb +++ b/test/controllers/user_controller_test.rb @@ -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 \ No newline at end of file diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index a905039..93b2d4d 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -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") %> \ No newline at end of file