user_test.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. require 'test_helper'
  2. class UserTest < ActiveSupport::TestCase
  3. def setup
  4. @user = User.new(login: "example", name: "Example User",
  5. email: "user@example.com",
  6. password: "foobar", password_confirmation: "foobar",
  7. birth_date: 30.years.ago)
  8. end
  9. test "should be valid" do
  10. assert @user.valid?
  11. end
  12. test "name should be present" do
  13. @user.name = " "
  14. assert_not @user.valid?
  15. end
  16. test "login should be present" do
  17. @user.name = " "
  18. assert_not @user.valid?
  19. end
  20. test "email should be present" do
  21. @user.email = " "
  22. assert_not @user.valid?
  23. end
  24. test "login should not be too long" do
  25. @user.login = "a" * 51
  26. assert_not @user.valid?
  27. end
  28. test "email should not be too long" do
  29. @user.email = "a" * 244 + "@example.com"
  30. assert_not @user.valid?
  31. end
  32. test "name should not be too long" do
  33. @user.name = "a" * 256
  34. assert_not @user.valid?
  35. end
  36. test "email validation should accept valid addresses" do
  37. valid_addresses = %w[user@example.com USER@foo.COM A_US-ER@foo.bar.org
  38. first.last@foo.jp alice+bob@baz.cn]
  39. valid_addresses.each do |valid_address|
  40. @user.email = valid_address
  41. assert @user.valid?, "#{valid_address.inspect} should be valid"
  42. end
  43. end
  44. test "email validation should reject invalid addresses" do
  45. invalid_addresses = %w[user@example,com user_at_foo.org user.name@example.
  46. foo@bar_baz.com foo@bar+baz.com]
  47. invalid_addresses.each do |invalid_address|
  48. @user.email = invalid_address
  49. assert_not @user.valid?, "#{invalid_address.inspect} should be invalid"
  50. end
  51. end
  52. test "login validation should accept valid login" do
  53. valid_logins = %w[foobar FooBar Foo_Bar 1234_Foo_Bar_5678]
  54. valid_logins.each do |valid_login|
  55. @user.login = valid_login
  56. assert @user.valid?, "#{valid_login.inspect} should be valid"
  57. end
  58. end
  59. test "login validation should reject invalid login" do
  60. invalid_logins = %w[foo:bar Foo*Bar Foo_^^Bar 1234!"_Foo_Bar_-5678]
  61. invalid_logins.each do |invalid_login|
  62. @user.login = invalid_login
  63. assert_not @user.valid?, "#{invalid_login.inspect} should be invalid"
  64. end
  65. end
  66. test "phone number validation should accept valid phone number" do
  67. valid_numbers = [
  68. "333-333-3333",
  69. "(333) 333-3333",
  70. "1-333-333-3333",
  71. "333.333.3333",
  72. "333-333-3333",
  73. "333-333-3333 x3333",
  74. "(333) 333-3333 x3333",
  75. "1-333-333-3333 x3333",
  76. "333.333.3333 x3333",
  77. ]
  78. valid_numbers.each do |valid_number|
  79. @user.phone = valid_number
  80. assert @user.valid?, "#{valid_number.inspect} should be valid"
  81. end
  82. end
  83. test "login should be unique" do
  84. duplicate_user = @user.dup
  85. duplicate_user.email = "foo@bar.com"
  86. @user.save
  87. assert_not duplicate_user.valid?
  88. end
  89. test "email addresses should be unique" do
  90. duplicate_user = @user.dup
  91. duplicate_user.login = "bar_foo"
  92. duplicate_user.email = @user.email.upcase
  93. @user.save
  94. assert_not duplicate_user.valid?
  95. end
  96. test "email addresses should be saved as lower-case" do
  97. mixed_case_email = "Foo@ExAMPle.CoM"
  98. @user.email = mixed_case_email
  99. @user.save
  100. assert_equal mixed_case_email.downcase, @user.reload.email
  101. end
  102. test "password should be present (nonblank)" do
  103. @user.password = @user.password_confirmation = " " * 6
  104. assert_not @user.valid?
  105. end
  106. test "password should have a minimum length" do
  107. @user.password = @user.password_confirmation = "a" * 5
  108. assert_not @user.valid?
  109. end
  110. end