From 887eba8c8dda1eaea0a6d4785a443798da6f80b7 Mon Sep 17 00:00:00 2001 From: Shaun McDonald Date: Sun, 7 Jun 2009 21:07:07 +0000 Subject: [PATCH] More tests to check that user creation works, and the url in the e-mail is the same. Seems that we can only test that a particular page is loading fine in all locales in the integration tests, and not the functional tests. --- .../functional/diary_entry_controller_test.rb | 24 ++---- test/functional/user_controller_test.rb | 37 +++++++- test/integration/user_creation_test.rb | 86 ++++++++++++++++++- test/test_helper.rb | 8 +- 4 files changed, 130 insertions(+), 25 deletions(-) diff --git a/test/functional/diary_entry_controller_test.rb b/test/functional/diary_entry_controller_test.rb index 709404045..d060af416 100644 --- a/test/functional/diary_entry_controller_test.rb +++ b/test/functional/diary_entry_controller_test.rb @@ -146,13 +146,9 @@ class DiaryEntryControllerTest < ActionController::TestCase end def test_edit_diary_entry_i18n - I18n.available_locales.each do |locale| - set_locale locale - - get(:edit, {:id => diary_entries(:normal_user_entry_1).id}, {'user' => users(:normal_user).id}) - assert_response :success - assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry" - end + get(:edit, {:id => diary_entries(:normal_user_entry_1).id}, {'user' => users(:normal_user).id}) + assert_response :success + assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry" end def test_create_diary_entry @@ -166,20 +162,16 @@ class DiaryEntryControllerTest < ActionController::TestCase # Check that you can get the expected response and template for all available languages # Should test that there are no def test_listing_diary_entries - I18n.available_locales.each do |locale| - set_locale locale - get :list - assert_response :success, "Should be able to list the diary entries in #{locale}" - assert_template 'list', "Should use the list template in #{locale}" + assert_response :success, "Should be able to list the diary entries in locale" + assert_template 'list', "Should use the list template in locale" assert_select "span[class=translation_missing]", false, "Missing translation in list of diary entries" # Now try to find a specific user's diary entry get :list, {:display_name => users(:normal_user).display_name} - assert_response :success, "Should be able to list the diary entries for a user in #{locale}" - assert_template 'list', "Should use the list template for a user in #{locale}" - assert_select "span[class=translation_missing]", false, "Missing translation in list of diary entries for user" - end + assert_response :success, "Should be able to list the diary entries for a user in locale" + assert_template 'list', "Should use the list template for a user in locale" + assert_no_missing_translations end def test_rss diff --git a/test/functional/user_controller_test.rb b/test/functional/user_controller_test.rb index 8d90b2bcf..5c1d6f47e 100644 --- a/test/functional/user_controller_test.rb +++ b/test/functional/user_controller_test.rb @@ -4,7 +4,7 @@ class UserControllerTest < ActionController::TestCase fixtures :users # The user creation page loads - def test_user_create + def test_user_create_view get :new assert_response :success @@ -27,6 +27,41 @@ class UserControllerTest < ActionController::TestCase end end + def test_user_create_success + new_email = "newtester@osm.org" + display_name = "new_tester" + assert_difference('User.count') do + assert_difference('ActionMailer::Base.deliveries.size') do + post :save, {:user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}} + end + end + + # Check the e-mail + register_email = ActionMailer::Base.deliveries.first + + assert_equal register_email.to[0], new_email + assert_match /#{@url}/, register_email.body + + # Check the page + assert_redirected_to :action => 'login' + + ActionMailer::Base.deliveries.clear + end + + def test_user_create_submit_duplicate_email + dup_email = users(:public_user).email + display_name = "new_tester" + assert_difference('User.count', 0) do + assert_difference('ActionMailer::Base.deliveries.size', 0) do + post :save, :user => { :email => dup_email, :email_confirmation => dup_email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"} + end + end + assert_response :success + assert_template 'new' + assert_select "div#errorExplanation" + assert_select "table#loginForm > tr > td > div[class=fieldWithErrors] > input#user_email" + end + # Check that the user account page will display and contains some relevant # information for the user def test_view_user_account diff --git a/test/integration/user_creation_test.rb b/test/integration/user_creation_test.rb index 5f664b5c8..e6ac49e8d 100644 --- a/test/integration/user_creation_test.rb +++ b/test/integration/user_creation_test.rb @@ -1,10 +1,88 @@ -require 'test_helper' +require File.dirname(__FILE__) + '/../test_helper' class UserCreationTest < ActionController::IntegrationTest fixtures :users - def test_create_user_duplicate - get '/user/new' - assert_response :success + def test_create_user_form + I18n.available_locales.each do |locale| + get '/user/new', {}, {"accept_language" => locale.to_s} + assert_response :success + assert_template 'user/new' + end + end + + def test_user_create_submit_duplicate_email + I18n.available_locales.each do |localer| + dup_email = users(:public_user).email + display_name = "#{localer.to_s}_new_tester" + assert_difference('User.count', 0) do + assert_difference('ActionMailer::Base.deliveries.size', 0) do + post '/user/save', + {:user => { :email => dup_email, :email_confirmation => dup_email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}}, + {"accept_language" => localer.to_s} + end + end + assert_response :success + assert_template 'user/new' + assert_equal response.headers['Content-Language'][0..1], localer.to_s[0..1] unless localer == :root + assert_select "div#errorExplanation" + assert_select "table#loginForm > tr > td > div[class=fieldWithErrors] > input#user_email" + assert_no_missing_translations + end + end + + def test_user_create_submit_duplicate_username + I18n.available_locales.each do |locale| + dup_display_name = users(:public_user).display_name + email = "#{locale.to_s}_new_tester" + assert_difference('User.count', 0) do + assert_difference('ActionMailer::Base.deliveries.size', 0) do + post '/user/save', + {:user => {:email => email, :email_confirmation => email, :display_name => dup_display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}}, + {"accept_language" => locale.to_s} + end + end + assert_response :success + assert_template 'user/new' + assert_select "div#errorExplanation" + assert_select "table#loginForm > tr > td > div[class=fieldWithErrors] > input#user_display_name" + assert_no_missing_translations + end + end + + def test_user_create_success + I18n.available_locales.each do |locale| + new_email = "#{locale.to_s}newtester@osm.org" + display_name = "#{locale.to_s}_new_tester" + assert_difference('User.count') do + assert_difference('ActionMailer::Base.deliveries.size', 1) do + post_via_redirect "/user/save", + {:user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}}, + {"accept_language" => "#{locale.to_s}"} + end + end + + # Check the e-mail + register_email = ActionMailer::Base.deliveries.first + + assert_equal register_email.to[0], new_email + # Check that the confirm account url is correct + assert_match /#{@url}/, register_email.body + + # Check the page + assert_response :success + assert_template 'login' + + ActionMailer::Base.deliveries.clear + end + end + + # Check that the user can successfully recover their password + def lost_password_recovery_success + # Open the lost password form + # Submit the lost password form + # Check the e-mail + # Submit the reset password token + # Check that the password has changed, and the user can login end end diff --git a/test/test_helper.rb b/test/test_helper.rb index c210315a9..97c749000 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -123,10 +123,6 @@ class ActiveSupport::TestCase @request.env["RAW_POST_DATA"] = c.to_s end - def set_locale(l) - @request.env["HTTP_ACCEPT_LANGUAGE"] = l.to_s - end - # Used to check that the error header and the forbidden responses are given # when the owner of the changset has their data not marked as public def assert_require_public_data(msg = "Shouldn't be able to use API when the user's data is not public") @@ -140,5 +136,9 @@ class ActiveSupport::TestCase #assert_equal @response.headers['Error'], "" end + def assert_no_missing_translations(msg="") + assert_select "span[class=translation_missing]", false, "Missing translation #{msg}" + end + # Add more helper methods to be used by all tests here... end -- 2.43.2