3 class UserTest < ActiveSupport::TestCase
4 include Rails::Dom::Testing::Assertions::SelectorAssertions
6 def test_invalid_with_empty_attributes
7 user = build(:user, :email => nil,
13 assert_not_predicate user, :valid?
14 assert_predicate user.errors[:email], :any?
15 assert_predicate user.errors[:pass_crypt], :any?
16 assert_predicate user.errors[:display_name], :any?
17 assert_predicate user.errors[:home_lat], :none?
18 assert_predicate user.errors[:home_lon], :none?
19 assert_predicate user.errors[:home_zoom], :none?
23 existing_user = create(:user)
24 new_user = build(:user, :email => existing_user.email)
25 assert_not new_user.save
26 assert_includes new_user.errors[:email], "has already been taken"
29 def test_unique_display_name
30 create(:user, :display_name => "H\u{e9}nryIV")
32 %W[H\u{e9}nryIV he\u{301}nryiv H\u{c9}nry\u2163 he\u{301}nry\u2173].each do |name|
33 new_user = build(:user, :display_name => name)
34 assert_not new_user.save
35 assert_includes new_user.errors[:display_name], "has already been taken"
40 ok = %w[a@s.com test@shaunmcdonald.me.uk hello_local@ping-d.ng
41 test_local@openstreetmap.org test-local@example.com]
42 bad = %w[hi ht@ n@ @.com help@.me.uk help"hi.me.uk も対@応します
43 輕觸搖晃的遊戲@ah.com も対応します@s.name]
48 assert user.valid?(:save), "#{name} isn't valid when it should be"
54 assert user.invalid?(:save), "#{name} is valid when it shouldn't be"
58 def test_display_name_length
60 user.display_name = "123"
61 assert_predicate user, :valid?, "should allow 3 char name name"
62 user.display_name = "12"
63 assert_not_predicate user, :valid?, "should not allow 2 char name"
64 user.display_name = ""
65 assert_not_predicate user, :valid?, "should not allow blank/0 char name"
66 user.display_name = nil
67 assert_not_predicate user, :valid?, "should not allow nil value"
70 def test_display_name_width
72 user.display_name = "123"
73 assert_predicate user, :valid?, "should allow 3 column name name"
74 user.display_name = "12"
75 assert_not_predicate user, :valid?, "should not allow 2 column name"
76 user.display_name = "1\u{200B}2"
77 assert_not_predicate user, :valid?, "should not allow 2 column name"
78 user.display_name = "\u{200B}\u{200B}\u{200B}"
79 assert_not_predicate user, :valid?, "should not allow 0 column name"
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", "ルシステムにも対応します", "輕觸搖晃的遊戲", "space space"]
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 "trailing whitespace ", " leading whitespace"]
96 ok.each do |display_name|
98 user.display_name = display_name
99 assert_predicate user, :valid?, "#{display_name} is invalid, when it should be"
102 bad.each do |display_name|
104 user.display_name = display_name
105 assert_not_predicate user, :valid?, "#{display_name} is valid when it shouldn't be"
109 def test_display_name_user_id_new
110 existing_user = create(:user)
113 user.display_name = "user_#{existing_user.id}"
114 assert_not_predicate user, :valid?, "user_<id> name is valid for existing user id when it shouldn't be"
116 user.display_name = "user_#{existing_user.id + 1}"
117 assert_not_predicate user, :valid?, "user_<id> name is valid for new user id when it shouldn't be"
120 def test_display_name_user_id_rename
121 existing_user = create(:user)
124 user.display_name = "user_#{existing_user.id}"
125 assert_not_predicate user, :valid?, "user_<id> name is valid for existing user id when it shouldn't be"
127 user.display_name = "user_#{user.id}"
128 assert_predicate user, :valid?, "user_<id> name is invalid for own id, when it should be"
131 def test_display_name_user_id_unchanged_is_valid
132 user = build(:user, :display_name => "user_0")
133 user.save(:validate => false)
136 assert_predicate user, :valid?, "user_0 display_name is invalid but it hasn't been changed"
139 def test_friends_with
140 alice = create(:user, :active)
141 bob = create(:user, :active)
142 charlie = create(:user, :active)
143 create(:friendship, :befriender => alice, :befriendee => bob)
145 assert alice.friends_with?(bob)
146 assert_not alice.friends_with?(charlie)
147 assert_not bob.friends_with?(alice)
148 assert_not bob.friends_with?(charlie)
149 assert_not charlie.friends_with?(bob)
150 assert_not charlie.friends_with?(alice)
153 def test_users_nearby
154 alice = create(:user, :active, :home_lat => 51.0, :home_lon => 1.0, :data_public => false)
155 bob = create(:user, :active, :home_lat => 51.1, :home_lon => 1.0, :data_public => true)
156 charlie = create(:user, :active, :home_lat => 51.1, :home_lon => 1.1, :data_public => true)
157 david = create(:user, :active, :home_lat => 10.0, :home_lon => -123.0, :data_public => true)
158 _edward = create(:user, :suspended, :home_lat => 10.0, :home_lon => -123.0, :data_public => true)
159 south_pole_user = create(:user, :active, :home_lat => -90.0, :home_lon => 0.0, :data_public => true)
160 vagrant_user = create(:user, :active, :home_lat => nil, :home_lon => nil, :data_public => true)
162 # bob and charlie are both near alice
163 assert_equal [bob, charlie], alice.nearby
164 # charlie and alice are both near bob, but alice has their data private
165 assert_equal [charlie], bob.nearby
166 # david has no user nearby, since edward is not active
167 assert_empty david.nearby
168 # south_pole_user has no user nearby, and doesn't throw exception
169 assert_empty south_pole_user.nearby
170 # vagrant_user has no home location
171 assert_empty vagrant_user.nearby
175 norm = create(:user, :active)
176 sec = create(:user, :active)
177 create(:friendship, :befriender => norm, :befriendee => sec)
179 assert_equal [sec], norm.friends
180 assert_equal 1, norm.friends.size
182 assert_empty sec.friends
183 assert_equal 0, sec.friends.size
186 def test_user_preferred_editor
188 assert_nil user.preferred_editor
189 user.preferred_editor = "id"
190 assert_equal "id", user.preferred_editor
193 user.preferred_editor = "invalid_editor"
194 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
198 pending = create(:user, :pending)
199 active = create(:user, :active)
200 confirmed = create(:user, :confirmed)
201 suspended = create(:user, :suspended)
202 deleted = create(:user, :deleted)
204 assert User.visible.find(pending.id)
205 assert User.visible.find(active.id)
206 assert User.visible.find(confirmed.id)
207 assert_raise ActiveRecord::RecordNotFound do
208 User.visible.find(suspended.id)
210 assert_raise ActiveRecord::RecordNotFound do
211 User.visible.find(deleted.id)
216 pending = create(:user, :pending)
217 active = create(:user, :active)
218 confirmed = create(:user, :confirmed)
219 suspended = create(:user, :suspended)
220 deleted = create(:user, :deleted)
222 assert User.active.find(active.id)
223 assert User.active.find(confirmed.id)
224 assert_raise ActiveRecord::RecordNotFound do
225 User.active.find(pending.id)
227 assert_raise ActiveRecord::RecordNotFound do
228 User.active.find(suspended.id)
230 assert_raise ActiveRecord::RecordNotFound do
231 User.active.find(deleted.id)
235 def test_identifiable
236 public_user = create(:user, :data_public => true)
237 private_user = create(:user, :data_public => false)
239 assert User.identifiable.find(public_user.id)
240 assert_raise ActiveRecord::RecordNotFound do
241 User.identifiable.find(private_user.id)
246 create(:language, :code => "en")
247 create(:language, :code => "de")
248 create(:language, :code => "sl")
250 user = create(:user, :languages => ["en"])
251 assert_equal ["en"], user.languages
252 user.languages = %w[de fr en]
253 assert_equal %w[de fr en], user.languages
254 user.languages = %w[fr de sl]
255 assert_equal "de", user.preferred_language
256 assert_equal %w[fr de sl], user.preferred_languages.map(&:to_s)
257 user = create(:user, :languages => %w[en de])
258 assert_equal %w[en de], user.languages
262 assert_predicate build(:user, :pending), :visible?
263 assert_predicate build(:user, :active), :visible?
264 assert_predicate build(:user, :confirmed), :visible?
265 assert_not_predicate build(:user, :suspended), :visible?
266 assert_not_predicate build(:user, :deleted), :visible?
270 assert_not_predicate build(:user, :pending), :active?
271 assert_predicate build(:user, :active), :active?
272 assert_predicate build(:user, :confirmed), :active?
273 assert_not_predicate build(:user, :suspended), :active?
274 assert_not_predicate build(:user, :deleted), :active?
278 assert_not_predicate create(:user), :moderator?
279 assert_predicate create(:moderator_user), :moderator?
282 def test_administrator?
283 assert_not_predicate create(:user), :administrator?
284 assert_predicate create(:administrator_user), :administrator?
288 assert_not create(:user).role?("administrator")
289 assert_not create(:user).role?("moderator")
290 assert create(:administrator_user).role?("administrator")
291 assert create(:moderator_user).role?("moderator")
294 def test_soft_destroy
295 user = create(:user, :with_home_location, :description => "foo")
297 assert_equal "user_#{user.id}", user.display_name
298 assert_predicate user.description, :blank?
299 assert_nil user.home_lat
300 assert_nil user.home_lon
301 assert_not_predicate user.avatar, :attached?
302 assert_equal "deleted", user.status
303 assert_not_predicate user, :visible?
304 assert_not_predicate user, :active?
307 def test_soft_destroy_revokes_oauth1_tokens
309 access_token = create(:access_token, :user => user)
310 assert_equal 1, user.oauth_tokens.authorized.count
314 assert_equal 0, user.oauth_tokens.authorized.count
316 assert_predicate access_token, :invalidated?
319 def test_soft_destroy_revokes_oauth2_tokens
321 oauth_access_token = create(:oauth_access_token, :resource_owner_id => user.id)
322 assert_equal 1, user.access_tokens.not_expired.count
326 assert_equal 0, user.access_tokens.not_expired.count
327 oauth_access_token.reload
328 assert_predicate oauth_access_token, :revoked?
331 def test_deletion_allowed_when_no_changesets
332 with_user_account_deletion_delay(10000) do
334 assert_predicate user, :deletion_allowed?
338 def test_deletion_allowed_without_delay
339 with_user_account_deletion_delay(nil) do
341 create(:changeset, :user => user)
343 assert_predicate user, :deletion_allowed?
347 def test_deletion_allowed_past_delay
348 with_user_account_deletion_delay(10) do
350 create(:changeset, :user => user, :created_at => Time.now.utc - 12.hours, :closed_at => Time.now.utc - 10.hours)
352 assert_predicate user, :deletion_allowed?
356 def test_deletion_allowed_during_delay
357 with_user_account_deletion_delay(10) do
359 create(:changeset, :user => user, :created_at => Time.now.utc - 11.hours, :closed_at => Time.now.utc - 9.hours)
361 assert_not_predicate user, :deletion_allowed?
362 assert_equal Time.now.utc + 1.hour, user.deletion_allowed_at
366 def test_deletion_allowed_past_zero_delay
367 with_user_account_deletion_delay(0) do
369 create(:changeset, :user => user, :created_at => Time.now.utc, :closed_at => Time.now.utc + 1.hour)
372 assert_predicate user, :deletion_allowed?
377 def test_deletion_allowed_during_zero_delay
378 with_user_account_deletion_delay(0) do
380 create(:changeset, :user => user, :created_at => Time.now.utc, :closed_at => Time.now.utc + 1.hour)
383 assert_not_predicate user, :deletion_allowed?
384 assert_equal Time.now.utc + 30.minutes, user.deletion_allowed_at