1 # -*- coding: utf-8 -*-
4 class UserTest < ActiveSupport::TestCase
5 include Rails::Dom::Testing::Assertions::SelectorAssertions
10 def test_invalid_with_empty_attributes
13 assert user.errors[:email].any?
14 assert user.errors[:pass_crypt].any?
15 assert user.errors[:display_name].any?
16 assert user.errors[:email].any?
17 assert !user.errors[:home_lat].any?
18 assert !user.errors[:home_lon].any?
19 assert !user.errors[:home_zoom].any?
24 :email => users(:normal_user).email,
26 :pass_crypt => Digest::MD5.hexdigest("test"),
27 :display_name => "new user",
29 :description => "desc"
32 assert new_user.errors[:email].include?("has already been taken")
35 def test_unique_display_name
37 :email => "tester@openstreetmap.org",
39 :pass_crypt => Digest::MD5.hexdigest("test"),
40 :display_name => users(:normal_user).display_name,
42 :description => "desc"
45 assert new_user.errors[:display_name].include?("has already been taken")
49 ok = %w(a@s.com test@shaunmcdonald.me.uk hello_local@ping-d.ng
50 test_local@openstreetmap.org test-local@example.com)
51 bad = %w(hi ht@ n@ @.com help@.me.uk help"hi.me.uk も対@応します
52 輕觸搖晃的遊戲@ah.com も対応します@s.name)
57 assert user.valid?(:save), user.errors.full_messages.join(",")
63 assert user.invalid?(:save), "#{name} is valid when it shouldn't be"
67 def test_display_name_length
69 user.display_name = "123"
70 assert user.valid?, " should allow nil display name"
71 user.display_name = "12"
72 assert !user.valid?, "should not allow 2 char name"
73 user.display_name = ""
75 user.display_name = nil
76 # Don't understand why it isn't allowing a nil value,
77 # when the validates statements specifically allow it
78 # It appears the database does not allow null values
82 def test_display_name_valid
83 # Due to sanitisation in the view some of these that you might not
85 # However, would they affect the xml planet dumps?
86 ok = ["Name", "'me", "he\"", "<hr>", "*ho", "\"help\"@",
87 "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲"]
88 # These need to be 3 chars in length, otherwise the length test above
90 bad = ["<hr/>", "test@example.com", "s/f", "aa/", "aa;", "aa.",
91 "aa,", "aa?", "/;.,?", "も対応します/", "#ping",
92 "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar",
93 "new", "terms", "save", "confirm", "confirm-email",
94 "go_public", "reset-password", "forgot-password", "suspended"]
95 ok.each do |display_name|
97 user.display_name = display_name
98 assert user.valid?, "#{display_name} is invalid, when it should be"
101 bad.each do |display_name|
103 user.display_name = display_name
104 assert !user.valid?, "#{display_name} is valid when it shouldn't be"
109 create(:friend, :befriender => users(:normal_user), :befriendee => users(:public_user))
110 assert users(:normal_user).is_friends_with?(users(:public_user))
111 assert !users(:normal_user).is_friends_with?(users(:inactive_user))
112 assert !users(:public_user).is_friends_with?(users(:normal_user))
113 assert !users(:public_user).is_friends_with?(users(:inactive_user))
114 assert !users(:inactive_user).is_friends_with?(users(:normal_user))
115 assert !users(:inactive_user).is_friends_with?(users(:public_user))
118 def test_users_nearby
119 # second user has their data public and is close by normal user
120 assert_equal [users(:public_user), users(:german_user)], users(:normal_user).nearby
121 # second_user has normal user nearby, but normal user has their data private
122 assert_equal [users(:german_user)], users(:public_user).nearby
123 # inactive_user has no user nearby
124 assert_equal [], users(:inactive_user).nearby
125 # north_pole_user has no user nearby, and doesn't throw exception
126 assert_equal [], users(:north_pole_user).nearby
127 # confirmed_user has no home location
128 assert_equal [], users(:confirmed_user).nearby
131 def test_friends_with
132 # normal user is a friend of second user
133 # it should be a one way friend associatation
134 norm = users(:normal_user)
135 sec = users(:public_user)
136 create(:friend, :befriender => norm, :befriendee => sec)
137 assert_equal 1, Friend.count
138 assert_equal [sec], norm.friend_users
139 assert_equal 1, norm.friend_users.size
140 assert_equal 1, Friend.count
141 assert norm.is_friends_with?(sec)
142 assert !sec.is_friends_with?(norm)
143 assert !users(:normal_user).is_friends_with?(users(:inactive_user))
144 assert !users(:public_user).is_friends_with?(users(:normal_user))
145 assert !users(:public_user).is_friends_with?(users(:inactive_user))
146 assert !users(:inactive_user).is_friends_with?(users(:normal_user))
147 assert !users(:inactive_user).is_friends_with?(users(:public_user))
148 # Friend.delete(friend)
149 # assert_equal 0, Friend.count
152 def test_user_preferred_editor
154 assert_nil user.preferred_editor
155 user.preferred_editor = "potlatch"
156 assert_equal "potlatch", user.preferred_editor
159 user.preferred_editor = "invalid_editor"
160 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
164 pending = create(:user, :pending)
165 active = create(:user, :active)
166 confirmed = create(:user, :confirmed)
167 suspended = create(:user, :suspended)
168 deleted = create(:user, :deleted)
170 assert User.visible.find(pending.id)
171 assert User.visible.find(active.id)
172 assert User.visible.find(confirmed.id)
173 assert_raise ActiveRecord::RecordNotFound do
174 User.visible.find(suspended.id)
176 assert_raise ActiveRecord::RecordNotFound do
177 User.visible.find(deleted.id)
182 pending = create(:user, :pending)
183 active = create(:user, :active)
184 confirmed = create(:user, :confirmed)
185 suspended = create(:user, :suspended)
186 deleted = create(:user, :deleted)
188 assert User.active.find(active.id)
189 assert User.active.find(confirmed.id)
190 assert_raise ActiveRecord::RecordNotFound do
191 User.active.find(pending.id)
193 assert_raise ActiveRecord::RecordNotFound do
194 User.active.find(suspended.id)
196 assert_raise ActiveRecord::RecordNotFound do
197 User.active.find(deleted.id)
201 def test_identifiable
202 public_user = create(:user, :data_public => true)
203 private_user = create(:user, :data_public => false)
205 assert User.identifiable.find(public_user.id)
206 assert_raise ActiveRecord::RecordNotFound do
207 User.identifiable.find(private_user.id)
212 create(:language, :code => "en")
213 create(:language, :code => "de")
214 create(:language, :code => "sl")
216 user = users(:normal_user)
217 assert_equal ["en"], user.languages
218 user.languages = %w(de fr en)
219 assert_equal %w(de fr en), user.languages
220 user.languages = %w(fr de sl)
221 assert_equal "de", user.preferred_language
222 assert_equal %w(fr de sl), user.preferred_languages.map(&:to_s)
223 user = users(:public_user)
224 assert_equal %w(en de), user.languages
228 assert_equal true, build(:user, :pending).visible?
229 assert_equal true, build(:user, :active).visible?
230 assert_equal true, build(:user, :confirmed).visible?
231 assert_equal false, build(:user, :suspended).visible?
232 assert_equal false, build(:user, :deleted).visible?
236 assert_equal false, build(:user, :pending).active?
237 assert_equal true, build(:user, :active).active?
238 assert_equal true, build(:user, :confirmed).active?
239 assert_equal false, build(:user, :suspended).active?
240 assert_equal false, build(:user, :deleted).active?
244 assert_equal false, create(:user).moderator?
245 assert_equal true, create(:moderator_user).moderator?
248 def test_administrator?
249 assert_equal false, create(:user).administrator?
250 assert_equal true, create(:administrator_user).administrator?
254 assert_equal false, create(:user).has_role?("administrator")
255 assert_equal false, create(:user).has_role?("moderator")
256 assert_equal true, create(:administrator_user).has_role?("administrator")
257 assert_equal true, create(:moderator_user).has_role?("moderator")
261 user = create(:user, :with_home_location, :description => "foo")
263 assert_equal "user_#{user.id}", user.display_name
264 assert user.description.blank?
265 assert_nil user.home_lat
266 assert_nil user.home_lon
267 assert_equal false, user.image.file?
268 assert_equal "deleted", user.status
269 assert_equal false, user.visible?
270 assert_equal false, user.active?
274 user = build(:user, :with_home_location)
276 assert_select Nokogiri::XML::Document.parse(xml.to_s), "user" do
277 assert_select "[display_name=?]", user.display_name
278 assert_select "[account_created=?]", user.creation_time.xmlschema
279 assert_select "home[lat=?][lon=?][zoom=?]", user.home_lat.to_s, user.home_lon.to_s, user.home_zoom.to_s
284 user = build(:user, :with_home_location)
285 xml = user.to_xml_node
286 assert_select Nokogiri::XML::DocumentFragment.parse(xml.to_s), "user" do
287 assert_select "[display_name=?]", user.display_name
288 assert_select "[account_created=?]", user.creation_time.xmlschema
289 assert_select "home[lat=?][lon=?][zoom=?]", user.home_lat.to_s, user.home_lon.to_s, user.home_zoom.to_s