1 # -*- coding: utf-8 -*-
2 require File.dirname(__FILE__) + '/../test_helper'
4 class UserTest < ActiveSupport::TestCase
6 fixtures :friends, :languages, :user_roles
8 def test_invalid_with_empty_attributes
11 assert user.errors[:email].any?
12 assert user.errors[:pass_crypt].any?
13 assert user.errors[:display_name].any?
14 assert user.errors[:email].any?
15 assert !user.errors[:home_lat].any?
16 assert !user.errors[:home_lon].any?
17 assert !user.errors[:home_zoom].any?
22 :email => users(:normal_user).email,
24 :pass_crypt => Digest::MD5.hexdigest('test'),
25 :display_name => "new user",
27 :description => "desc"
28 }, :without_protection => true)
30 assert new_user.errors[:email].include?("has already been taken")
33 def test_unique_display_name
35 :email => "tester@openstreetmap.org",
37 :pass_crypt => Digest::MD5.hexdigest('test'),
38 :display_name => users(:normal_user).display_name,
40 :description => "desc"
41 }, :without_protection => true)
43 assert new_user.errors[:display_name].include?("has already been taken")
47 ok = %w{ a@s.com test@shaunmcdonald.me.uk hello_local@ping-d.ng
48 test_local@openstreetmap.org test-local@example.com }
49 bad = %w{ hi ht@ n@ @.com help@.me.uk help"hi.me.uk も対@応します
50 輕觸搖晃的遊戲@ah.com も対応します@s.name }
53 user = users(:normal_user)
55 assert user.valid?(:save), user.errors.full_messages
59 user = users(:normal_user)
61 assert user.invalid?(:save), "#{name} is valid when it shouldn't be"
65 def test_display_name_length
66 user = users(:normal_user)
67 user.display_name = "123"
68 assert user.valid?, " should allow nil display name"
69 user.display_name = "12"
70 assert !user.valid?, "should not allow 2 char name"
71 user.display_name = ""
73 user.display_name = nil
74 # Don't understand why it isn't allowing a nil value,
75 # when the validates statements specifically allow it
76 # It appears the database does not allow null values
80 def test_display_name_valid
81 # Due to sanitisation in the view some of these that you might not
83 # However, would they affect the xml planet dumps?
84 ok = [ "Name", "'me", "he\"", "#ping", "<hr>", "*ho", "\"help\"@",
85 "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲" ]
86 # These need to be 3 chars in length, otherwise the length test above
88 bad = [ "<hr/>", "test@example.com", "s/f", "aa/", "aa;", "aa.",
89 "aa,", "aa?", "/;.,?", "も対応します/" ]
90 ok.each do |display_name|
91 user = users(:normal_user)
92 user.display_name = display_name
93 assert user.valid?, "#{display_name} is invalid, when it should be"
96 bad.each do |display_name|
97 user = users(:normal_user)
98 user.display_name = display_name
99 assert !user.valid?, "#{display_name} is valid when it shouldn't be"
100 assert user.errors[:display_name].include?("is invalid")
105 assert_equal true, users(:normal_user).is_friends_with?(users(:public_user))
106 assert_equal false, users(:normal_user).is_friends_with?(users(:inactive_user))
107 assert_equal false, users(:public_user).is_friends_with?(users(:normal_user))
108 assert_equal false, users(:public_user).is_friends_with?(users(:inactive_user))
109 assert_equal false, users(:inactive_user).is_friends_with?(users(:normal_user))
110 assert_equal false, users(:inactive_user).is_friends_with?(users(:public_user))
113 def test_users_nearby
114 # second user has their data public and is close by normal user
115 assert_equal [users(:public_user)], users(:normal_user).nearby
116 # second_user has normal user nearby, but normal user has their data private
117 assert_equal [], users(:public_user).nearby
118 # inactive_user has no user nearby
119 assert_equal [], users(:inactive_user).nearby
122 def test_friends_with
123 # normal user is a friend of second user
124 # it should be a one way friend accossitation
125 assert_equal 1, Friend.count
126 norm = users(:normal_user)
127 sec = users(:public_user)
129 #friend.befriender = norm
130 #friend.befriendee = sec
132 assert_equal [sec], norm.nearby
133 assert_equal 1, norm.nearby.size
134 assert_equal 1, Friend.count
135 assert_equal true, norm.is_friends_with?(sec)
136 assert_equal false, sec.is_friends_with?(norm)
137 assert_equal false, users(:normal_user).is_friends_with?(users(:inactive_user))
138 assert_equal false, users(:public_user).is_friends_with?(users(:normal_user))
139 assert_equal false, users(:public_user).is_friends_with?(users(:inactive_user))
140 assert_equal false, users(:inactive_user).is_friends_with?(users(:normal_user))
141 assert_equal false, users(:inactive_user).is_friends_with?(users(:public_user))
142 #Friend.delete(friend)
143 #assert_equal 0, Friend.count
146 def test_user_preferred_editor
147 user = users(:normal_user)
148 assert_equal nil, user.preferred_editor
149 user.preferred_editor = "potlatch"
150 assert_equal "potlatch", user.preferred_editor
153 user.preferred_editor = "invalid_editor"
154 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
158 assert_equal 14, User.visible.count
159 assert_raise ActiveRecord::RecordNotFound do
160 User.visible.find(users(:suspended_user).id)
162 assert_raise ActiveRecord::RecordNotFound do
163 User.visible.find(users(:deleted_user).id)
168 assert_equal 13, User.active.count
169 assert_raise ActiveRecord::RecordNotFound do
170 User.active.find(users(:inactive_user).id)
172 assert_raise ActiveRecord::RecordNotFound do
173 User.active.find(users(:suspended_user).id)
175 assert_raise ActiveRecord::RecordNotFound do
176 User.active.find(users(:deleted_user).id)
181 assert_equal 15, User.public.count
182 assert_raise ActiveRecord::RecordNotFound do
183 User.public.find(users(:normal_user).id)
188 user = users(:normal_user)
189 assert_equal [ "en" ], user.languages
190 user.languages = [ "de", "fr", "en" ]
191 assert_equal [ "de", "fr", "en" ], user.languages
192 user.languages = [ "fr", "de", "sl" ]
193 assert_equal "de", user.preferred_language
194 assert_equal "de", user.preferred_language_from(["en", "sl", "de", "es"])
198 assert_equal true, users(:inactive_user).visible?
199 assert_equal true, users(:normal_user).visible?
200 assert_equal true, users(:confirmed_user).visible?
201 assert_equal false, users(:suspended_user).visible?
202 assert_equal false, users(:deleted_user).visible?
206 assert_equal false, users(:inactive_user).active?
207 assert_equal true, users(:normal_user).active?
208 assert_equal true, users(:confirmed_user).active?
209 assert_equal false, users(:suspended_user).active?
210 assert_equal false, users(:deleted_user).active?
214 assert_equal false, users(:normal_user).moderator?
215 assert_equal true, users(:moderator_user).moderator?
218 def test_administrator?
219 assert_equal false, users(:normal_user).administrator?
220 assert_equal true, users(:administrator_user).administrator?
224 assert_equal false, users(:normal_user).has_role?("administrator")
225 assert_equal false, users(:normal_user).has_role?("moderator")
226 assert_equal true, users(:administrator_user).has_role?("administrator")
227 assert_equal true, users(:moderator_user).has_role?("moderator")
231 user = users(:normal_user)
233 assert_equal "user_#{user.id}", user.display_name
234 assert_blank user.description
235 assert_equal nil, user.home_lat
236 assert_equal nil, user.home_lon
237 assert_equal false, user.image.file?
238 assert_equal "deleted", user.status
239 assert_equal false, user.visible?
240 assert_equal false, user.active?