1 # frozen_string_literal: true
5 class UserTest < ActiveSupport::TestCase
6 include Rails::Dom::Testing::Assertions::SelectorAssertions
8 def test_invalid_with_empty_attributes
9 user = build(:user, :email => nil,
15 assert_not_predicate user, :valid?
16 assert_predicate user.errors[:email], :any?
17 assert_predicate user.errors[:pass_crypt], :any?
18 assert_predicate user.errors[:display_name], :any?
19 assert_predicate user.errors[:home_lat], :none?
20 assert_predicate user.errors[:home_lon], :none?
21 assert_predicate user.errors[:home_zoom], :none?
25 existing_user = create(:user)
26 new_user = build(:user, :email => existing_user.email)
27 assert_not new_user.save
28 assert_includes new_user.errors[:email], "has already been taken"
31 def test_unique_display_name
32 create(:user, :display_name => "H\u{e9}nryIV")
34 %W[H\u{e9}nryIV he\u{301}nryiv H\u{c9}nry\u2163 he\u{301}nry\u2173].each do |name|
35 new_user = build(:user, :display_name => name)
36 assert_not new_user.save
37 assert_includes new_user.errors[:display_name], "has already been taken"
42 ok = %w[a@s.com test@shaunmcdonald.me.uk hello_local@ping-d.ng
43 test_local@openstreetmap.org test-local@example.com]
44 bad = %w[hi ht@ n@ @.com help@.me.uk help"hi.me.uk も対@応します
45 輕觸搖晃的遊戲@ah.com も対応します@s.name]
50 assert user.valid?(:save), "#{name} isn't valid when it should be"
56 assert user.invalid?(:save), "#{name} is valid when it shouldn't be"
60 def test_display_name_length
62 user.display_name = "123"
63 assert_predicate user, :valid?, "should allow 3 char name name"
64 user.display_name = "12"
65 assert_not_predicate user, :valid?, "should not allow 2 char name"
66 user.display_name = ""
67 assert_not_predicate user, :valid?, "should not allow blank/0 char name"
68 user.display_name = nil
69 assert_not_predicate user, :valid?, "should not allow nil value"
72 def test_display_name_width
74 user.display_name = "123"
75 assert_predicate user, :valid?, "should allow 3 column name name"
76 user.display_name = "12"
77 assert_not_predicate user, :valid?, "should not allow 2 column name"
78 user.display_name = "1\u{200B}2"
79 assert_not_predicate user, :valid?, "should not allow 2 column name"
80 user.display_name = "\u{200B}\u{200B}\u{200B}"
81 assert_not_predicate user, :valid?, "should not allow 0 column name"
84 def test_display_name_valid
85 # Due to sanitisation in the view some of these that you might not
87 # However, would they affect the xml planet dumps?
88 ok = ["Name", "'me", "he\"", "<hr>", "*ho", "\"help\"@",
89 "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲", "space space"]
90 # These need to be 3 chars in length, otherwise the length test above
92 bad = ["<hr/>", "test@example.com", "s/f", "aa/", "aa;", "aa.",
93 "aa,", "aa?", "/;.,?", "も対応します/", "#ping",
94 "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar",
95 "new", "terms", "save", "confirm", "confirm-email",
96 "go_public", "reset-password", "forgot-password", "suspended",
97 "trailing whitespace ", " leading whitespace"]
98 ok.each do |display_name|
100 user.display_name = display_name
101 assert_predicate user, :valid?, "#{display_name} is invalid, when it should be"
104 bad.each do |display_name|
106 user.display_name = display_name
107 assert_not_predicate user, :valid?, "#{display_name} is valid when it shouldn't be"
111 def test_display_name_user_id_new
112 existing_user = create(:user)
115 user.display_name = "user_#{existing_user.id}"
116 assert_not_predicate user, :valid?, "user_<id> name is valid for existing user id when it shouldn't be"
118 user.display_name = "user_#{existing_user.id + 1}"
119 assert_not_predicate user, :valid?, "user_<id> name is valid for new user id when it shouldn't be"
122 def test_display_name_user_id_rename
123 existing_user = create(:user)
126 user.display_name = "user_#{existing_user.id}"
127 assert_not_predicate user, :valid?, "user_<id> name is valid for existing user id when it shouldn't be"
129 user.display_name = "user_#{user.id}"
130 assert_predicate user, :valid?, "user_<id> name is invalid for own id, when it should be"
133 def test_display_name_user_id_unchanged_is_valid
134 user = build(:user, :display_name => "user_0")
135 user.save(:validate => false)
138 assert_predicate user, :valid?, "user_0 display_name is invalid but it hasn't been changed"
141 def test_description_length
143 user.description = "x" * 65536
144 assert_predicate user, :valid?, "should allow 65536 char description"
145 user.description = "x" * 65537
146 assert_not_predicate user, :valid?, "should not allow 65537 char description"
147 user.description = ""
148 assert_predicate user, :valid?, "should allow blank/0 char description"
149 user.description = nil
150 assert_predicate user, :valid?, "should allow nil value"
154 user = build(:user, :description => "foo [bar](http://example.com/) baz")
155 assert_equal 12, user.spam_score
159 alice = create(:user, :active)
160 bob = create(:user, :active)
161 charlie = create(:user, :active)
162 create(:follow, :follower => alice, :following => bob)
164 assert alice.follows?(bob)
165 assert_not alice.follows?(charlie)
166 assert_not bob.follows?(alice)
167 assert_not bob.follows?(charlie)
168 assert_not charlie.follows?(bob)
169 assert_not charlie.follows?(alice)
172 def test_users_nearby
173 alice = create(:user, :active, :home_lat => 51.0, :home_lon => 1.0, :data_public => false)
174 bob = create(:user, :active, :home_lat => 51.1, :home_lon => 1.0, :data_public => true)
175 charlie = create(:user, :active, :home_lat => 51.1, :home_lon => 1.1, :data_public => true)
176 david = create(:user, :active, :home_lat => 10.0, :home_lon => -123.0, :data_public => true)
177 _edward = create(:user, :suspended, :home_lat => 10.0, :home_lon => -123.0, :data_public => true)
178 south_pole_user = create(:user, :active, :home_lat => -90.0, :home_lon => 0.0, :data_public => true)
179 vagrant_user = create(:user, :active, :home_lat => nil, :home_lon => nil, :data_public => true)
181 # bob and charlie are both near alice
182 assert_equal [bob, charlie], alice.nearby
183 # charlie and alice are both near bob, but alice has their data private
184 assert_equal [charlie], bob.nearby
185 # david has no user nearby, since edward is not active
186 assert_empty david.nearby
187 # south_pole_user has no user nearby, and doesn't throw exception
188 assert_empty south_pole_user.nearby
189 # vagrant_user has no home location
190 assert_empty vagrant_user.nearby
194 norm = create(:user, :active)
195 sec = create(:user, :active)
196 create(:follow, :follower => norm, :following => sec)
198 assert_equal [sec], norm.followings
199 assert_equal 1, norm.followings.size
201 assert_empty sec.followings
202 assert_equal 0, sec.followings.size
205 def test_user_preferred_editor
207 assert_nil user.preferred_editor
208 user.preferred_editor = "id"
209 assert_equal "id", user.preferred_editor
212 user.preferred_editor = "invalid_editor"
213 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
217 pending = create(:user, :pending)
218 active = create(:user, :active)
219 confirmed = create(:user, :confirmed)
220 suspended = create(:user, :suspended)
221 deleted = create(:user, :deleted)
223 assert User.visible.find(pending.id)
224 assert User.visible.find(active.id)
225 assert User.visible.find(confirmed.id)
226 assert_raise ActiveRecord::RecordNotFound do
227 User.visible.find(suspended.id)
229 assert_raise ActiveRecord::RecordNotFound do
230 User.visible.find(deleted.id)
235 pending = create(:user, :pending)
236 active = create(:user, :active)
237 confirmed = create(:user, :confirmed)
238 suspended = create(:user, :suspended)
239 deleted = create(:user, :deleted)
241 assert User.active.find(active.id)
242 assert User.active.find(confirmed.id)
243 assert_raise ActiveRecord::RecordNotFound do
244 User.active.find(pending.id)
246 assert_raise ActiveRecord::RecordNotFound do
247 User.active.find(suspended.id)
249 assert_raise ActiveRecord::RecordNotFound do
250 User.active.find(deleted.id)
254 def test_identifiable
255 public_user = create(:user, :data_public => true)
256 private_user = create(:user, :data_public => false)
258 assert User.identifiable.find(public_user.id)
259 assert_raise ActiveRecord::RecordNotFound do
260 User.identifiable.find(private_user.id)
265 user = create(:user, :languages => ["en"])
266 assert_equal ["en"], user.languages
267 user.languages = %w[de fr en]
268 assert_equal %w[de fr en], user.languages
269 user.languages = %w[fr de sl]
270 assert_equal %w[fr de sl], user.preferred_languages.map(&:to_s)
271 user = create(:user, :languages => %w[en de])
272 assert_equal %w[en de], user.languages
275 def test_preferred_color_scheme_nil_if_nothing_selected
277 assert_nil user.preferred_color_scheme(:map, :site)
280 def test_preferred_color_scheme_as_selected
282 create(:user_preference, :k => "map.color_scheme", :v => "dark"),
283 create(:user_preference, :k => "site.color_scheme", :v => "light")
285 user = create(:user, :preferences => preferences)
287 assert_equal "dark", user.preferred_color_scheme(:map, :site)
290 def test_preferred_color_scheme_fallback_if_auto
292 create(:user_preference, :k => "map.color_scheme", :v => "auto"),
293 create(:user_preference, :k => "site.color_scheme", :v => "light")
295 user = create(:user, :preferences => preferences)
297 assert_nil user.preferred_color_scheme(:map)
298 assert_equal "light", user.preferred_color_scheme(:map, :site)
301 def test_preferred_color_scheme_fallback_if_missing
303 create(:user_preference, :k => "site.color_scheme", :v => "light")
305 user = create(:user, :preferences => preferences)
307 assert_nil user.preferred_color_scheme(:map)
308 assert_equal "light", user.preferred_color_scheme(:map, :site)
311 def test_default_diary_language_undefined
312 create(:language, :code => "en")
313 user = create(:user, :languages => [])
314 assert_nil user.default_diary_language
317 def test_default_diary_language_known
318 create(:language, :code => "en")
319 user = create(:user, :languages => ["en"])
320 assert_equal "en", user.default_diary_language
323 def test_default_diary_language_known_with_fallback
324 create(:language, :code => "en")
325 create(:language, :code => "fr")
326 user = create(:user, :languages => ["fr en"])
327 assert_equal "fr", user.default_diary_language
330 def test_default_diary_language_unknown
331 create(:language, :code => "en")
332 user = create(:user, :languages => ["unknown"])
333 assert_nil user.default_diary_language
336 def test_default_diary_language_unknown_with_known_fallback
337 create(:language, :code => "en")
338 user = create(:user, :languages => ["unknown en"])
339 assert_equal "en", user.default_diary_language
342 def test_default_diary_language_set
343 create(:language, :code => "en")
344 user = create(:user, :languages => [])
346 assert_difference "user.preferences.count", 1 do
347 assert_equal "en", (user.default_diary_language = "en")
351 assert_equal "en", user.default_diary_language
352 preference = user.preferences.find_by(:k => "diary.default_language")
353 assert_equal "en", preference.v
356 def test_default_diary_language_set_twice
357 create(:language, :code => "en")
358 create(:language, :code => "fr")
359 user = create(:user, :languages => [])
361 assert_difference "user.preferences.count", 1 do
362 assert_equal "en", (user.default_diary_language = "en")
364 assert_difference "user.preferences.count", 0 do
365 assert_equal "fr", (user.default_diary_language = "fr")
369 assert_equal "fr", user.default_diary_language
370 preference = user.preferences.find_by(:k => "diary.default_language")
371 assert_equal "fr", preference.v
375 assert_predicate build(:user, :pending), :visible?
376 assert_predicate build(:user, :active), :visible?
377 assert_predicate build(:user, :confirmed), :visible?
378 assert_not_predicate build(:user, :suspended), :visible?
379 assert_not_predicate build(:user, :deleted), :visible?
383 assert_not_predicate build(:user, :pending), :active?
384 assert_predicate build(:user, :active), :active?
385 assert_predicate build(:user, :confirmed), :active?
386 assert_not_predicate build(:user, :suspended), :active?
387 assert_not_predicate build(:user, :deleted), :active?
391 assert_not_predicate create(:user), :moderator?
392 assert_predicate create(:moderator_user), :moderator?
395 def test_administrator?
396 assert_not_predicate create(:user), :administrator?
397 assert_predicate create(:administrator_user), :administrator?
401 assert_not create(:user).role?("administrator")
402 assert_not create(:user).role?("moderator")
403 assert create(:administrator_user).role?("administrator")
404 assert create(:moderator_user).role?("moderator")
410 assert_equal "suspended", user.status
413 def test_suspend_closes_issues
415 issue = create(:issue, :reportable => user)
417 assert_equal "suspended", user.status
418 assert_equal "resolved", issue.reload.status
424 assert_equal "deleted", user.status
427 def test_hide_closes_issues
429 issue = create(:issue, :reportable => user)
431 assert_equal "deleted", user.status
432 assert_equal "resolved", issue.reload.status
435 def test_soft_destroy
436 user = create(:user, :with_home_location, :description => "foo")
438 assert_equal "user_#{user.id}", user.display_name
439 assert_predicate user.description, :blank?
440 assert_nil user.home_lat
441 assert_nil user.home_lon
442 assert_not_predicate user.avatar, :attached?
443 assert_equal "deleted", user.status
444 assert_not_predicate user, :visible?
445 assert_not_predicate user, :active?
448 def test_soft_destroy_closes_issues
450 issue = create(:issue, :reportable => user)
452 assert_equal "deleted", user.status
453 assert_equal "resolved", issue.reload.status
456 def test_soft_destroy_revokes_oauth2_tokens
458 oauth_access_token = create(:oauth_access_token, :user => user)
459 assert_equal 1, user.access_tokens.not_expired.count
463 assert_equal 0, user.access_tokens.not_expired.count
464 oauth_access_token.reload
465 assert_predicate oauth_access_token, :revoked?
468 def test_deletion_allowed_when_no_changesets
469 with_user_account_deletion_delay(10000) do
471 assert_predicate user, :deletion_allowed?
475 def test_deletion_allowed_without_delay
476 with_user_account_deletion_delay(nil) do
478 create(:changeset, :user => user)
480 assert_predicate user, :deletion_allowed?
484 def test_deletion_allowed_past_delay
485 with_user_account_deletion_delay(10) do
487 create(:changeset, :user => user, :created_at => Time.now.utc - 12.hours, :closed_at => Time.now.utc - 10.hours)
489 assert_predicate user, :deletion_allowed?
493 def test_deletion_allowed_during_delay
494 with_user_account_deletion_delay(10) do
496 create(:changeset, :user => user, :created_at => Time.now.utc - 11.hours, :closed_at => Time.now.utc - 9.hours)
498 assert_not_predicate user, :deletion_allowed?
499 assert_equal Time.now.utc + 1.hour, user.deletion_allowed_at
503 def test_deletion_allowed_past_zero_delay
504 with_user_account_deletion_delay(0) do
506 create(:changeset, :user => user, :created_at => Time.now.utc, :closed_at => Time.now.utc + 1.hour)
509 assert_predicate user, :deletion_allowed?
514 def test_deletion_allowed_during_zero_delay
515 with_user_account_deletion_delay(0) do
517 create(:changeset, :user => user, :created_at => Time.now.utc, :closed_at => Time.now.utc + 1.hour)
520 assert_not_predicate user, :deletion_allowed?
521 assert_equal Time.now.utc + 30.minutes, user.deletion_allowed_at