mirror of
https://github.com/TracksApp/tracks.git
synced 2025-12-16 23:30:12 +01:00
Make email address voluntary in preferences; update tests to handle the added email field better
This commit is contained in:
parent
f9f7e6b9ea
commit
efee4976b0
5 changed files with 71 additions and 21 deletions
|
|
@ -107,7 +107,7 @@ class User < ApplicationRecord
|
||||||
validates_length_of :login, within: 3..80
|
validates_length_of :login, within: 3..80
|
||||||
validates_uniqueness_of :login, on: :create
|
validates_uniqueness_of :login, on: :create
|
||||||
validate :validate_auth_type
|
validate :validate_auth_type
|
||||||
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }
|
validates :email, :allow_blank => true, format: { with: URI::MailTo::EMAIL_REGEXP }
|
||||||
|
|
||||||
before_create :crypt_password, :generate_token
|
before_create :crypt_password, :generate_token
|
||||||
before_update :crypt_password
|
before_update :crypt_password
|
||||||
|
|
|
||||||
11
test/fixtures/users.yml
vendored
11
test/fixtures/users.yml
vendored
|
|
@ -19,6 +19,17 @@ other_user:
|
||||||
last_name: Doe
|
last_name: Doe
|
||||||
auth_type: database
|
auth_type: database
|
||||||
|
|
||||||
|
other_user_email:
|
||||||
|
id: 3
|
||||||
|
login: joe
|
||||||
|
crypted_password: <%= BCrypt::Password.create("open") %>
|
||||||
|
token: <%= Digest::SHA1.hexdigest("joeSun Feb 19 14:42:45 GMT 20060.408173979260027") %>
|
||||||
|
is_admin: false
|
||||||
|
first_name: Jane
|
||||||
|
last_name: Doe
|
||||||
|
email: joe@example.org
|
||||||
|
auth_type: database
|
||||||
|
|
||||||
ldap_user:
|
ldap_user:
|
||||||
id: 3
|
id: 3
|
||||||
login: john
|
login: john
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ require 'support/stub_site_config_helper'
|
||||||
|
|
||||||
class StoriesTest < ActionDispatch::IntegrationTest
|
class StoriesTest < ActionDispatch::IntegrationTest
|
||||||
include StubSiteConfigHelper
|
include StubSiteConfigHelper
|
||||||
|
|
||||||
# ####################################################
|
# ####################################################
|
||||||
# Testing login and signup by different kinds of users
|
# Testing login and signup by different kinds of users
|
||||||
# ####################################################
|
# ####################################################
|
||||||
|
|
@ -11,10 +11,11 @@ class StoriesTest < ActionDispatch::IntegrationTest
|
||||||
admin = new_session_as(:admin_user,"abracadabra")
|
admin = new_session_as(:admin_user,"abracadabra")
|
||||||
admin.goes_to_signup
|
admin.goes_to_signup
|
||||||
admin.signs_up_with(:user => {:login => "newbie",
|
admin.signs_up_with(:user => {:login => "newbie",
|
||||||
|
:email => "test.person@example.org",
|
||||||
:password => "newbiepass",
|
:password => "newbiepass",
|
||||||
:password_confirmation => "newbiepass"})
|
:password_confirmation => "newbiepass"})
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_signup_new_user_by_nonadmin
|
def test_signup_new_user_by_nonadmin
|
||||||
stub_site_config do
|
stub_site_config do
|
||||||
SITE_CONFIG['open_signups'] = false
|
SITE_CONFIG['open_signups'] = false
|
||||||
|
|
@ -22,7 +23,7 @@ class StoriesTest < ActionDispatch::IntegrationTest
|
||||||
other_user.goes_to_signup_as_nonadmin
|
other_user.goes_to_signup_as_nonadmin
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_open_signup_new_user
|
def test_open_signup_new_user
|
||||||
stub_site_config do
|
stub_site_config do
|
||||||
SITE_CONFIG['open_signups'] = true
|
SITE_CONFIG['open_signups'] = true
|
||||||
|
|
@ -30,6 +31,7 @@ class StoriesTest < ActionDispatch::IntegrationTest
|
||||||
assert_response :success
|
assert_response :success
|
||||||
assert_template "users/new"
|
assert_template "users/new"
|
||||||
post "/users", params: { :user => {:login => "newbie",
|
post "/users", params: { :user => {:login => "newbie",
|
||||||
|
:email => "test.person@example.org",
|
||||||
:password => "newbiepass",
|
:password => "newbiepass",
|
||||||
:password_confirmation => "newbiepass"} }
|
:password_confirmation => "newbiepass"} }
|
||||||
assert_response :redirect
|
assert_response :redirect
|
||||||
|
|
@ -37,8 +39,8 @@ class StoriesTest < ActionDispatch::IntegrationTest
|
||||||
assert_response :success
|
assert_response :success
|
||||||
assert_template "todos/index"
|
assert_template "todos/index"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
module CustomAssertions
|
module CustomAssertions
|
||||||
|
|
@ -67,7 +69,7 @@ class StoriesTest < ActionDispatch::IntegrationTest
|
||||||
assert_response :success
|
assert_response :success
|
||||||
assert_template "users/new"
|
assert_template "users/new"
|
||||||
end
|
end
|
||||||
|
|
||||||
def goes_to_signup_as_nonadmin
|
def goes_to_signup_as_nonadmin
|
||||||
get "/signup"
|
get "/signup"
|
||||||
assert_response :success
|
assert_response :success
|
||||||
|
|
@ -81,7 +83,6 @@ class StoriesTest < ActionDispatch::IntegrationTest
|
||||||
assert_response :success
|
assert_response :success
|
||||||
assert_template "todos/index"
|
assert_template "todos/index"
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def new_session_as(user,plainpass)
|
def new_session_as(user,plainpass)
|
||||||
|
|
@ -92,5 +93,4 @@ class StoriesTest < ActionDispatch::IntegrationTest
|
||||||
yield sess if block_given?
|
yield sess if block_given?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,22 @@
|
||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class UsersXmlApiTest < ActionDispatch::IntegrationTest
|
class UsersXmlApiTest < ActionDispatch::IntegrationTest
|
||||||
|
|
||||||
@@foobar_postdata = "<user><login>foo</login><password>bar</password></user>"
|
@@foobar_postdata = "<user><login>foo</login><password>bar</password></user>"
|
||||||
|
@@barfoo_postdata = "<user><login>bar</login><email>barfoo@example.org</email><password>foo</password></user>"
|
||||||
@@johnny_postdata = "<user><login>johnny</login><password>barracuda</password></user>"
|
@@johnny_postdata = "<user><login>johnny</login><password>barracuda</password></user>"
|
||||||
|
@@barracuda_postdata = "<user><login>barracuda</login><email>barracuda@example.org</email><password>johnny</password></user>"
|
||||||
|
|
||||||
def test_fails_with_401_if_not_authorized_user
|
def test_fails_with_401_if_not_authorized_user
|
||||||
authenticated_post_xml_to_user_create @@foobar_postdata, 'nobody', 'nohow'
|
authenticated_post_xml_to_user_create @@foobar_postdata, 'nobody', 'nohow'
|
||||||
assert_401_unauthorized_admin
|
assert_401_unauthorized_admin
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_fails_with_401_if_not_admin_user
|
def test_fails_with_401_if_not_admin_user
|
||||||
authenticated_post_xml_to_user_create @@foobar_postdata, users(:other_user).login, 'sesame'
|
authenticated_post_xml_to_user_create @@foobar_postdata, users(:other_user).login, 'sesame'
|
||||||
assert_401_unauthorized_admin
|
assert_401_unauthorized_admin
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_content_type_must_be_xml
|
def test_content_type_must_be_xml
|
||||||
authenticated_post_xml_to_user_create @@foobar_postdata, users(:admin_user).login, 'abracadabra', {'CONTENT_TYPE' => "application/x-www-form-urlencoded"}
|
authenticated_post_xml_to_user_create @@foobar_postdata, users(:admin_user).login, 'abracadabra', {'CONTENT_TYPE' => "application/x-www-form-urlencoded"}
|
||||||
assert_response 400, "Expected response 400"
|
assert_response 400, "Expected response 400"
|
||||||
|
|
@ -25,12 +27,12 @@ class UsersXmlApiTest < ActionDispatch::IntegrationTest
|
||||||
# authenticated_post_xml_to_user_create "<foo></bar>"
|
# authenticated_post_xml_to_user_create "<foo></bar>"
|
||||||
# assert_equal 500, @integration_session.status
|
# assert_equal 500, @integration_session.status
|
||||||
# end
|
# end
|
||||||
|
|
||||||
def test_fails_with_invalid_xml_format2
|
def test_fails_with_invalid_xml_format2
|
||||||
authenticated_post_xml_to_user_create "<username>foo</username>"
|
authenticated_post_xml_to_user_create "<username>foo</username>"
|
||||||
assert_response_and_body 400, "Expected post format is valid xml like so: <user><login>username</login><password>abc123</password></user>."
|
assert_response_and_body 400, "Expected post format is valid xml like so: <user><login>username</login><password>abc123</password></user>."
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_xml_simple_param_parsing
|
def test_xml_simple_param_parsing
|
||||||
authenticated_post_xml_to_user_create
|
authenticated_post_xml_to_user_create
|
||||||
assert @controller.params.has_key?(:user)
|
assert @controller.params.has_key?(:user)
|
||||||
|
|
@ -39,18 +41,18 @@ class UsersXmlApiTest < ActionDispatch::IntegrationTest
|
||||||
assert_equal 'foo', @controller.params['user'][:login]
|
assert_equal 'foo', @controller.params['user'][:login]
|
||||||
assert_equal 'bar', @controller.params['user'][:password]
|
assert_equal 'bar', @controller.params['user'][:password]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_fails_with_too_short_password
|
def test_fails_with_too_short_password
|
||||||
authenticated_post_xml_to_user_create
|
authenticated_post_xml_to_user_create
|
||||||
assert_responses_with_error "Password is too short (minimum is 5 characters"
|
assert_responses_with_error "Password is too short (minimum is 5 characters"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_fails_with_nonunique_login
|
def test_fails_with_nonunique_login
|
||||||
existing_login = users(:other_user).login
|
existing_login = users(:other_user).login
|
||||||
authenticated_post_xml_to_user_create "<user><login>#{existing_login}</login><password>barracuda</password></user>"
|
authenticated_post_xml_to_user_create "<user><login>#{existing_login}</login><password>barracuda</password></user>"
|
||||||
assert_responses_with_error "Login has already been taken"
|
assert_responses_with_error "Login has already been taken"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_creates_new_user
|
def test_creates_new_user
|
||||||
assert_difference 'User.count' do
|
assert_difference 'User.count' do
|
||||||
authenticated_post_xml_to_user_create @@johnny_postdata
|
authenticated_post_xml_to_user_create @@johnny_postdata
|
||||||
|
|
@ -61,11 +63,22 @@ class UsersXmlApiTest < ActionDispatch::IntegrationTest
|
||||||
johnny2 = User.authenticate('johnny','barracuda')
|
johnny2 = User.authenticate('johnny','barracuda')
|
||||||
assert_not_nil johnny2, "expected user johnny to be authenticated"
|
assert_not_nil johnny2, "expected user johnny to be authenticated"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_creates_new_user
|
||||||
|
assert_difference 'User.count' do
|
||||||
|
authenticated_post_xml_to_user_create @@barracuda_postdata
|
||||||
|
assert_response_and_body 200, "User created."
|
||||||
|
end
|
||||||
|
barracuda1 = User.where(:login => 'barracuda').first
|
||||||
|
assert_not_nil barracuda1, "expected user barracuda to be created"
|
||||||
|
johnny2 = User.authenticate('barracuda','johnny')
|
||||||
|
assert_not_nil barracuda2, "expected user barracuda to be authenticated"
|
||||||
|
end
|
||||||
|
|
||||||
def test_fails_with_get_verb
|
def test_fails_with_get_verb
|
||||||
authenticated_get_xml "/users.xml", users(:admin_user).login, 'abracadabra', {}
|
authenticated_get_xml "/users.xml", users(:admin_user).login, 'abracadabra', {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_get_users_as_xml
|
def test_get_users_as_xml
|
||||||
get '/users.xml', params: {}, headers: basic_auth_headers()
|
get '/users.xml', params: {}, headers: basic_auth_headers()
|
||||||
assert_response :success
|
assert_response :success
|
||||||
|
|
@ -79,9 +92,18 @@ class UsersXmlApiTest < ActionDispatch::IntegrationTest
|
||||||
get "/users/#{users(:other_user).id}.xml", params: {}, headers: basic_auth_headers()
|
get "/users/#{users(:other_user).id}.xml", params: {}, headers: basic_auth_headers()
|
||||||
assert_response :success
|
assert_response :success
|
||||||
assert_select 'user'
|
assert_select 'user'
|
||||||
|
assert_select 'email', false
|
||||||
assert_select 'password', false
|
assert_select 'password', false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_get_email_user_as_xml
|
||||||
|
get "/users/#{users(:other_user_email).id}.xml", params: {}, headers: basic_auth_headers()
|
||||||
|
assert_response :success
|
||||||
|
assert_select 'user'
|
||||||
|
assert_select 'email'
|
||||||
|
assert_select 'password', false
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def basic_auth_headers(username = users(:admin_user).login, password = 'abracadabra')
|
def basic_auth_headers(username = users(:admin_user).login, password = 'abracadabra')
|
||||||
|
|
@ -91,4 +113,8 @@ class UsersXmlApiTest < ActionDispatch::IntegrationTest
|
||||||
def authenticated_post_xml_to_user_create(postdata = @@foobar_postdata, user = users(:admin_user).login, password = 'abracadabra', headers = {})
|
def authenticated_post_xml_to_user_create(postdata = @@foobar_postdata, user = users(:admin_user).login, password = 'abracadabra', headers = {})
|
||||||
authenticated_post_xml "/users.xml", user, password, postdata, headers
|
authenticated_post_xml "/users.xml", user, password, postdata, headers
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def authenticated_post_xml_to_user_create_with_email(postdata = @@barfoo_postdata, user = users(:admin_user).login, password = 'abracadabra', headers = {})
|
||||||
|
authenticated_post_xml "/users.xml", user, password, postdata, headers
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -89,6 +89,19 @@ class UserTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_validate_correct_email
|
||||||
|
assert_difference 'User.count' do
|
||||||
|
create_user :email=> 'testi@example.org'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_validate_email_format
|
||||||
|
assert_no_difference 'User.count' do
|
||||||
|
u = create_user :email=> 'test'
|
||||||
|
assert_equal "is not valid", u.errors[:email]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_display_name_with_first_and_last_name_set
|
def test_display_name_with_first_and_last_name_set
|
||||||
@other_user.first_name = "Jane"
|
@other_user.first_name = "Jane"
|
||||||
@other_user.last_name = "Doe"
|
@other_user.last_name = "Doe"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue