From 4e02de31df5cd84cc2a5adae50128dc6f48e8453 Mon Sep 17 00:00:00 2001 From: vangberg Date: Mon, 15 Jun 2026 11:57:09 +0200 Subject: [PATCH 1/2] Add names controller test --- test/controllers/names_controller_test.rb | 48 ++++++----------------- test/fixtures/users.yml | 22 ++++++----- 2 files changed, 25 insertions(+), 45 deletions(-) diff --git a/test/controllers/names_controller_test.rb b/test/controllers/names_controller_test.rb index 549a4110..7a44decf 100644 --- a/test/controllers/names_controller_test.rb +++ b/test/controllers/names_controller_test.rb @@ -1,48 +1,24 @@ require 'test_helper' class NamesControllerTest < ActionDispatch::IntegrationTest + include Devise::Test::IntegrationHelpers + setup do @name = names(:one) + @user = users(:contributor) + sign_in(@user) end - test 'should get index' do - get names_url - assert_response :success - end - - test 'should get new' do - get new_name_url - assert_response :success - end - - test 'should create name' do - assert_difference('Name.count') do - post names_url, params: { name: { name: @name.name } } + test 'submits a new name' do + assert_difference('Name.count', 1) do + post names_url, params: { name: { name: 'Testimonas exampleensis' } } end - assert_redirected_to name_url(Name.last) - end - - test 'should show name' do - get name_url(@name) - assert_response :success - end - - test 'should get edit' do - get edit_name_url(@name) - assert_response :success - end - - test 'should update name' do - patch name_url(@name), params: { name: { name: @name.name } } - assert_redirected_to name_url(@name) - end - - test 'should destroy name' do - assert_difference('Name.count', -1) do - delete name_url(@name) - end + name = Name.find_by!(name: 'Testimonas exampleensis') - assert_redirected_to names_url + assert_equal 'Draft', name.status_name + assert_equal @user, name.created_by + assert name.observing?(@user) + assert_redirected_to name_url(name) end end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 80aed36e..75f52bd2 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -1,11 +1,15 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html -# This model initially had no columns defined. If you add columns to the -# model remove the '{}' from the fixture names and add the columns immediately -# below each fixture, per the syntax in the comments below -# -one: {} -# column: value -# -two: {} -# column: value +one: + username: user_one + email: user_one@example.com + +two: + username: user_two + email: user_two@example.com + +contributor: + username: contributor + email: contributor@example.com + contributor: true + confirmed_at: 2026-01-01 00:00:00 From 20570e9c22bea54f21256fbe393fe875e0c3d82e Mon Sep 17 00:00:00 2001 From: vangberg Date: Mon, 15 Jun 2026 11:10:40 +0200 Subject: [PATCH 2/2] Add signup controller test --- config/environments/test.rb | 3 ++ test/integration/user_signup_test.rb | 54 ++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 test/integration/user_signup_test.rb diff --git a/config/environments/test.rb b/config/environments/test.rb index 0cb24249..17b79321 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # The test environment is used exclusively to run your application's # test suite. You never need to work with it otherwise. Remember that # your test database is "scratch space" for the test suite and is wiped @@ -40,6 +42,7 @@ # The :test delivery method accumulates sent emails in the # ActionMailer::Base.deliveries array. config.action_mailer.delivery_method = :test + config.action_mailer.default_url_options = { host: 'www.example.com' } # Print deprecation notices to the stderr. config.active_support.deprecation = :stderr diff --git a/test/integration/user_signup_test.rb b/test/integration/user_signup_test.rb new file mode 100644 index 00000000..7f7d789e --- /dev/null +++ b/test/integration/user_signup_test.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require 'test_helper' + +class UserSignupTest < ActionDispatch::IntegrationTest + test 'signs up and confirms a user' do # rubocop:disable Metrics/BlockLength + user_count = User.count + email_count = ActionMailer::Base.deliveries.size + + post user_registration_url, params: { + user: { + email: 'new-user@example.com', + username: 'new_user', + password: 'password123', + password_confirmation: 'password123', + family: 'User', + given: 'New', + affiliation: 'Primary institution', + # rubocop:disable Naming/VariableNumber + affiliation_2: 'Secondary institution', + # rubocop:enable Naming/VariableNumber + opt_regular_email: false, + opt_message_email: false, + opt_notification: false + } + } + + assert_equal user_count + 1, User.count + assert_equal email_count + 1, ActionMailer::Base.deliveries.size + + user = User.find_by!(email: 'new-user@example.com') + assert_equal 'new_user', user.username + assert_equal 'User', user.family + assert_equal 'New', user.given + assert_equal 'Primary institution', user.affiliation + assert_equal 'Secondary institution', user.affiliation_2 + assert_not user.opt_regular_email + assert_not user.opt_message_email + assert_not user.opt_notification + assert_not user.confirmed? + assert_response :redirect + + confirmation_link = Nokogiri::HTML( + ActionMailer::Base.deliveries.last.body.to_s + ).at_css('a[href*="/users/confirmation?confirmation_token="]') + + assert confirmation_link, 'confirmation email has no confirmation link' + + get confirmation_link['href'] + + assert user.reload.confirmed? + assert_response :redirect + end # rubocop:enable Metrics/BlockLength +end