1 # -*- coding: utf-8 -*-
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"
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"
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.join(",")
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\"", "<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?", "/;.,?", "も対応します/", "#ping",
90 "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar",
91 "new", "terms", "save", "confirm", "confirm-email",
92 "go_public", "reset-password", "forgot-password", "suspended" ]
93 ok.each do |display_name|
94 user = users(:normal_user)
95 user.display_name = display_name
96 assert user.valid?, "#{display_name} is invalid, when it should be"
99 bad.each do |display_name|
100 user = users(:normal_user)
101 user.display_name = display_name
102 assert !user.valid?, "#{display_name} is valid when it shouldn't be"
107 assert users(:normal_user).is_friends_with?(users(:public_user))
108 assert !users(:normal_user).is_friends_with?(users(:inactive_user))
109 assert !users(:public_user).is_friends_with?(users(:normal_user))
110 assert !users(:public_user).is_friends_with?(users(:inactive_user))
111 assert !users(:inactive_user).is_friends_with?(users(:normal_user))
112 assert !users(:inactive_user).is_friends_with?(users(:public_user))
115 def test_users_nearby
116 # second user has their data public and is close by normal user
117 assert_equal [users(:public_user)], users(:normal_user).nearby
118 # second_user has normal user nearby, but normal user has their data private
119 assert_equal [], users(:public_user).nearby
120 # inactive_user has no user nearby
121 assert_equal [], users(:inactive_user).nearby
122 # north_pole_user has no user nearby, and doesn't throw exception
123 assert_equal [], users(:north_pole_user).nearby
126 def test_friends_with
127 # normal user is a friend of second user
128 # it should be a one way friend accossitation
129 assert_equal 1, Friend.count
130 norm = users(:normal_user)
131 sec = users(:public_user)
133 #friend.befriender = norm
134 #friend.befriendee = sec
136 assert_equal [sec], norm.nearby
137 assert_equal 1, norm.nearby.size
138 assert_equal 1, Friend.count
139 assert norm.is_friends_with?(sec)
140 assert !sec.is_friends_with?(norm)
141 assert !users(:normal_user).is_friends_with?(users(:inactive_user))
142 assert !users(:public_user).is_friends_with?(users(:normal_user))
143 assert !users(:public_user).is_friends_with?(users(:inactive_user))
144 assert !users(:inactive_user).is_friends_with?(users(:normal_user))
145 assert !users(:inactive_user).is_friends_with?(users(:public_user))
146 #Friend.delete(friend)
147 #assert_equal 0, Friend.count
150 def test_user_preferred_editor
151 user = users(:normal_user)
152 assert_equal nil, user.preferred_editor
153 user.preferred_editor = "potlatch"
154 assert_equal "potlatch", user.preferred_editor
157 user.preferred_editor = "invalid_editor"
158 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
162 assert_equal 15, User.visible.count
163 assert_raise ActiveRecord::RecordNotFound do
164 User.visible.find(users(:suspended_user).id)
166 assert_raise ActiveRecord::RecordNotFound do
167 User.visible.find(users(:deleted_user).id)
172 assert_equal 14, User.active.count
173 assert_raise ActiveRecord::RecordNotFound do
174 User.active.find(users(:inactive_user).id)
176 assert_raise ActiveRecord::RecordNotFound do
177 User.active.find(users(:suspended_user).id)
179 assert_raise ActiveRecord::RecordNotFound do
180 User.active.find(users(:deleted_user).id)
184 def test_identifiable
185 assert_equal 16, User.identifiable.count
186 assert_raise ActiveRecord::RecordNotFound do
187 User.identifiable.find(users(:normal_user).id)
192 user = users(:normal_user)
193 assert_equal [ "en" ], user.languages
194 user.languages = [ "de", "fr", "en" ]
195 assert_equal [ "de", "fr", "en" ], user.languages
196 user.languages = [ "fr", "de", "sl" ]
197 assert_equal "de", user.preferred_language
198 assert_equal "de", user.preferred_language_from(["en", "sl", "de", "es"])
202 assert_equal true, users(:inactive_user).visible?
203 assert_equal true, users(:normal_user).visible?
204 assert_equal true, users(:confirmed_user).visible?
205 assert_equal false, users(:suspended_user).visible?
206 assert_equal false, users(:deleted_user).visible?
210 assert_equal false, users(:inactive_user).active?
211 assert_equal true, users(:normal_user).active?
212 assert_equal true, users(:confirmed_user).active?
213 assert_equal false, users(:suspended_user).active?
214 assert_equal false, users(:deleted_user).active?
218 assert_equal false, users(:normal_user).moderator?
219 assert_equal true, users(:moderator_user).moderator?
222 def test_administrator?
223 assert_equal false, users(:normal_user).administrator?
224 assert_equal true, users(:administrator_user).administrator?
228 assert_equal false, users(:normal_user).has_role?("administrator")
229 assert_equal false, users(:normal_user).has_role?("moderator")
230 assert_equal true, users(:administrator_user).has_role?("administrator")
231 assert_equal true, users(:moderator_user).has_role?("moderator")
235 user = users(:normal_user)
237 assert_equal "user_#{user.id}", user.display_name
238 assert user.description.blank?
239 assert_equal nil, user.home_lat
240 assert_equal nil, user.home_lon
241 assert_equal false, user.image.file?
242 assert_equal "deleted", user.status
243 assert_equal false, user.visible?
244 assert_equal false, user.active?