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" ]
91 ok.each do |display_name|
92 user = users(:normal_user)
93 user.display_name = display_name
94 assert user.valid?, "#{display_name} is invalid, when it should be"
97 bad.each do |display_name|
98 user = users(:normal_user)
99 user.display_name = display_name
100 assert !user.valid?, "#{display_name} is valid when it shouldn't be"
101 assert user.errors[:display_name].include?("is invalid")
106 assert users(:normal_user).is_friends_with?(users(:public_user))
107 assert !users(:normal_user).is_friends_with?(users(:inactive_user))
108 assert !users(:public_user).is_friends_with?(users(:normal_user))
109 assert !users(:public_user).is_friends_with?(users(:inactive_user))
110 assert !users(:inactive_user).is_friends_with?(users(:normal_user))
111 assert !users(:inactive_user).is_friends_with?(users(:public_user))
114 def test_users_nearby
115 # second user has their data public and is close by normal user
116 assert_equal [users(:public_user)], users(:normal_user).nearby
117 # second_user has normal user nearby, but normal user has their data private
118 assert_equal [], users(:public_user).nearby
119 # inactive_user has no user nearby
120 assert_equal [], users(:inactive_user).nearby
121 # north_pole_user has no user nearby, and doesn't throw exception
122 assert_equal [], users(:north_pole_user).nearby
125 def test_friends_with
126 # normal user is a friend of second user
127 # it should be a one way friend accossitation
128 assert_equal 1, Friend.count
129 norm = users(:normal_user)
130 sec = users(:public_user)
132 #friend.befriender = norm
133 #friend.befriendee = sec
135 assert_equal [sec], norm.nearby
136 assert_equal 1, norm.nearby.size
137 assert_equal 1, Friend.count
138 assert norm.is_friends_with?(sec)
139 assert !sec.is_friends_with?(norm)
140 assert !users(:normal_user).is_friends_with?(users(:inactive_user))
141 assert !users(:public_user).is_friends_with?(users(:normal_user))
142 assert !users(:public_user).is_friends_with?(users(:inactive_user))
143 assert !users(:inactive_user).is_friends_with?(users(:normal_user))
144 assert !users(:inactive_user).is_friends_with?(users(:public_user))
145 #Friend.delete(friend)
146 #assert_equal 0, Friend.count
149 def test_user_preferred_editor
150 user = users(:normal_user)
151 assert_equal nil, user.preferred_editor
152 user.preferred_editor = "potlatch"
153 assert_equal "potlatch", user.preferred_editor
156 user.preferred_editor = "invalid_editor"
157 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
161 assert_equal 15, User.visible.count
162 assert_raise ActiveRecord::RecordNotFound do
163 User.visible.find(users(:suspended_user).id)
165 assert_raise ActiveRecord::RecordNotFound do
166 User.visible.find(users(:deleted_user).id)
171 assert_equal 14, User.active.count
172 assert_raise ActiveRecord::RecordNotFound do
173 User.active.find(users(:inactive_user).id)
175 assert_raise ActiveRecord::RecordNotFound do
176 User.active.find(users(:suspended_user).id)
178 assert_raise ActiveRecord::RecordNotFound do
179 User.active.find(users(:deleted_user).id)
184 assert_equal 16, User.public.count
185 assert_raise ActiveRecord::RecordNotFound do
186 User.public.find(users(:normal_user).id)
191 user = users(:normal_user)
192 assert_equal [ "en" ], user.languages
193 user.languages = [ "de", "fr", "en" ]
194 assert_equal [ "de", "fr", "en" ], user.languages
195 user.languages = [ "fr", "de", "sl" ]
196 assert_equal "de", user.preferred_language
197 assert_equal "de", user.preferred_language_from(["en", "sl", "de", "es"])
201 assert_equal true, users(:inactive_user).visible?
202 assert_equal true, users(:normal_user).visible?
203 assert_equal true, users(:confirmed_user).visible?
204 assert_equal false, users(:suspended_user).visible?
205 assert_equal false, users(:deleted_user).visible?
209 assert_equal false, users(:inactive_user).active?
210 assert_equal true, users(:normal_user).active?
211 assert_equal true, users(:confirmed_user).active?
212 assert_equal false, users(:suspended_user).active?
213 assert_equal false, users(:deleted_user).active?
217 assert_equal false, users(:normal_user).moderator?
218 assert_equal true, users(:moderator_user).moderator?
221 def test_administrator?
222 assert_equal false, users(:normal_user).administrator?
223 assert_equal true, users(:administrator_user).administrator?
227 assert_equal false, users(:normal_user).has_role?("administrator")
228 assert_equal false, users(:normal_user).has_role?("moderator")
229 assert_equal true, users(:administrator_user).has_role?("administrator")
230 assert_equal true, users(:moderator_user).has_role?("moderator")
234 user = users(:normal_user)
236 assert_equal "user_#{user.id}", user.display_name
237 assert user.description.blank?
238 assert_equal nil, user.home_lat
239 assert_equal nil, user.home_lon
240 assert_equal false, user.image.file?
241 assert_equal "deleted", user.status
242 assert_equal false, user.visible?
243 assert_equal false, user.active?