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"
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 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 users(:normal_user).is_friends_with?(users(:public_user))
106 assert !users(:normal_user).is_friends_with?(users(:inactive_user))
107 assert !users(:public_user).is_friends_with?(users(:normal_user))
108 assert !users(:public_user).is_friends_with?(users(:inactive_user))
109 assert !users(:inactive_user).is_friends_with?(users(:normal_user))
110 assert !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
120 # north_pole_user has no user nearby, and doesn't throw exception
121 assert_equal [], users(:north_pole_user).nearby
124 def test_friends_with
125 # normal user is a friend of second user
126 # it should be a one way friend accossitation
127 assert_equal 1, Friend.count
128 norm = users(:normal_user)
129 sec = users(:public_user)
131 #friend.befriender = norm
132 #friend.befriendee = sec
134 assert_equal [sec], norm.nearby
135 assert_equal 1, norm.nearby.size
136 assert_equal 1, Friend.count
137 assert norm.is_friends_with?(sec)
138 assert !sec.is_friends_with?(norm)
139 assert !users(:normal_user).is_friends_with?(users(:inactive_user))
140 assert !users(:public_user).is_friends_with?(users(:normal_user))
141 assert !users(:public_user).is_friends_with?(users(:inactive_user))
142 assert !users(:inactive_user).is_friends_with?(users(:normal_user))
143 assert !users(:inactive_user).is_friends_with?(users(:public_user))
144 #Friend.delete(friend)
145 #assert_equal 0, Friend.count
148 def test_user_preferred_editor
149 user = users(:normal_user)
150 assert_equal nil, user.preferred_editor
151 user.preferred_editor = "potlatch"
152 assert_equal "potlatch", user.preferred_editor
155 user.preferred_editor = "invalid_editor"
156 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
160 assert_equal 15, User.visible.count
161 assert_raise ActiveRecord::RecordNotFound do
162 User.visible.find(users(:suspended_user).id)
164 assert_raise ActiveRecord::RecordNotFound do
165 User.visible.find(users(:deleted_user).id)
170 assert_equal 14, User.active.count
171 assert_raise ActiveRecord::RecordNotFound do
172 User.active.find(users(:inactive_user).id)
174 assert_raise ActiveRecord::RecordNotFound do
175 User.active.find(users(:suspended_user).id)
177 assert_raise ActiveRecord::RecordNotFound do
178 User.active.find(users(:deleted_user).id)
183 assert_equal 16, User.public.count
184 assert_raise ActiveRecord::RecordNotFound do
185 User.public.find(users(:normal_user).id)
190 user = users(:normal_user)
191 assert_equal [ "en" ], user.languages
192 user.languages = [ "de", "fr", "en" ]
193 assert_equal [ "de", "fr", "en" ], user.languages
194 user.languages = [ "fr", "de", "sl" ]
195 assert_equal "de", user.preferred_language
196 assert_equal "de", user.preferred_language_from(["en", "sl", "de", "es"])
200 assert_equal true, users(:inactive_user).visible?
201 assert_equal true, users(:normal_user).visible?
202 assert_equal true, users(:confirmed_user).visible?
203 assert_equal false, users(:suspended_user).visible?
204 assert_equal false, users(:deleted_user).visible?
208 assert_equal false, users(:inactive_user).active?
209 assert_equal true, users(:normal_user).active?
210 assert_equal true, users(:confirmed_user).active?
211 assert_equal false, users(:suspended_user).active?
212 assert_equal false, users(:deleted_user).active?
216 assert_equal false, users(:normal_user).moderator?
217 assert_equal true, users(:moderator_user).moderator?
220 def test_administrator?
221 assert_equal false, users(:normal_user).administrator?
222 assert_equal true, users(:administrator_user).administrator?
226 assert_equal false, users(:normal_user).has_role?("administrator")
227 assert_equal false, users(:normal_user).has_role?("moderator")
228 assert_equal true, users(:administrator_user).has_role?("administrator")
229 assert_equal true, users(:moderator_user).has_role?("moderator")
233 user = users(:normal_user)
235 assert_equal "user_#{user.id}", user.display_name
236 assert user.description.blank?
237 assert_equal nil, user.home_lat
238 assert_equal nil, user.home_lon
239 assert_equal false, user.image.file?
240 assert_equal "deleted", user.status
241 assert_equal false, user.visible?
242 assert_equal false, user.active?