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 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 existing_user = create(:user)
31 new_user = build(:user, :display_name => existing_user.display_name)
32 assert_not new_user.save
33 assert_includes new_user.errors[:display_name], "has already been taken"
37 ok = %w[a@s.com test@shaunmcdonald.me.uk hello_local@ping-d.ng
38 test_local@openstreetmap.org test-local@example.com]
39 bad = %w[hi ht@ n@ @.com help@.me.uk help"hi.me.uk も対@応します
40 輕觸搖晃的遊戲@ah.com も対応します@s.name]
45 assert user.valid?(:save), "#{name} isn't valid when it should be"
51 assert user.invalid?(:save), "#{name} is valid when it shouldn't be"
55 def test_display_name_length
57 user.display_name = "123"
58 assert_predicate user, :valid?, "should allow 3 char name name"
59 user.display_name = "12"
60 assert_not user.valid?, "should not allow 2 char name"
61 user.display_name = ""
62 assert_not user.valid?, "should not allow blank/0 char name"
63 user.display_name = nil
64 assert_not user.valid?, "should not allow nil value"
67 def test_display_name_valid
68 # Due to sanitisation in the view some of these that you might not
70 # However, would they affect the xml planet dumps?
71 ok = ["Name", "'me", "he\"", "<hr>", "*ho", "\"help\"@",
72 "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲", "space space"]
73 # These need to be 3 chars in length, otherwise the length test above
75 bad = ["<hr/>", "test@example.com", "s/f", "aa/", "aa;", "aa.",
76 "aa,", "aa?", "/;.,?", "も対応します/", "#ping",
77 "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar",
78 "new", "terms", "save", "confirm", "confirm-email",
79 "go_public", "reset-password", "forgot-password", "suspended",
80 "trailing whitespace ", " leading whitespace"]
81 ok.each do |display_name|
83 user.display_name = display_name
84 assert_predicate user, :valid?, "#{display_name} is invalid, when it should be"
87 bad.each do |display_name|
89 user.display_name = display_name
90 assert_not user.valid?, "#{display_name} is valid when it shouldn't be"
94 def test_display_name_user_id_new
95 existing_user = create(:user)
98 user.display_name = "user_#{existing_user.id}"
99 assert_not user.valid?, "user_<id> name is valid for existing user id when it shouldn't be"
101 user.display_name = "user_#{existing_user.id + 1}"
102 assert_not user.valid?, "user_<id> name is valid for new user id when it shouldn't be"
105 def test_display_name_user_id_rename
106 existing_user = create(:user)
109 user.display_name = "user_#{existing_user.id}"
110 assert_not user.valid?, "user_<id> name is valid for existing user id when it shouldn't be"
112 user.display_name = "user_#{user.id}"
113 assert_predicate user, :valid?, "user_<id> name is invalid for own id, when it should be"
116 def test_display_name_user_id_unchanged_is_valid
117 user = build(:user, :display_name => "user_0")
118 user.save(:validate => false)
121 assert_predicate user, :valid?, "user_0 display_name is invalid but it hasn't been changed"
124 def test_friends_with
125 alice = create(:user, :active)
126 bob = create(:user, :active)
127 charlie = create(:user, :active)
128 create(:friendship, :befriender => alice, :befriendee => bob)
130 assert alice.friends_with?(bob)
131 assert_not alice.friends_with?(charlie)
132 assert_not bob.friends_with?(alice)
133 assert_not bob.friends_with?(charlie)
134 assert_not charlie.friends_with?(bob)
135 assert_not charlie.friends_with?(alice)
138 def test_users_nearby
139 alice = create(:user, :active, :home_lat => 51.0, :home_lon => 1.0, :data_public => false)
140 bob = create(:user, :active, :home_lat => 51.1, :home_lon => 1.0, :data_public => true)
141 charlie = create(:user, :active, :home_lat => 51.1, :home_lon => 1.1, :data_public => true)
142 david = create(:user, :active, :home_lat => 10.0, :home_lon => -123.0, :data_public => true)
143 _edward = create(:user, :suspended, :home_lat => 10.0, :home_lon => -123.0, :data_public => true)
144 south_pole_user = create(:user, :active, :home_lat => -90.0, :home_lon => 0.0, :data_public => true)
145 vagrant_user = create(:user, :active, :home_lat => nil, :home_lon => nil, :data_public => true)
147 # bob and charlie are both near alice
148 assert_equal [bob, charlie], alice.nearby
149 # charlie and alice are both near bob, but alice has their data private
150 assert_equal [charlie], bob.nearby
151 # david has no user nearby, since edward is not active
152 assert_empty david.nearby
153 # south_pole_user has no user nearby, and doesn't throw exception
154 assert_empty south_pole_user.nearby
155 # vagrant_user has no home location
156 assert_empty vagrant_user.nearby
160 norm = create(:user, :active)
161 sec = create(:user, :active)
162 create(:friendship, :befriender => norm, :befriendee => sec)
164 assert_equal [sec], norm.friends
165 assert_equal 1, norm.friends.size
167 assert_empty sec.friends
168 assert_equal 0, sec.friends.size
171 def test_user_preferred_editor
173 assert_nil user.preferred_editor
174 user.preferred_editor = "id"
175 assert_equal "id", user.preferred_editor
178 user.preferred_editor = "invalid_editor"
179 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
183 pending = create(:user, :pending)
184 active = create(:user, :active)
185 confirmed = create(:user, :confirmed)
186 suspended = create(:user, :suspended)
187 deleted = create(:user, :deleted)
189 assert User.visible.find(pending.id)
190 assert User.visible.find(active.id)
191 assert User.visible.find(confirmed.id)
192 assert_raise ActiveRecord::RecordNotFound do
193 User.visible.find(suspended.id)
195 assert_raise ActiveRecord::RecordNotFound do
196 User.visible.find(deleted.id)
201 pending = create(:user, :pending)
202 active = create(:user, :active)
203 confirmed = create(:user, :confirmed)
204 suspended = create(:user, :suspended)
205 deleted = create(:user, :deleted)
207 assert User.active.find(active.id)
208 assert User.active.find(confirmed.id)
209 assert_raise ActiveRecord::RecordNotFound do
210 User.active.find(pending.id)
212 assert_raise ActiveRecord::RecordNotFound do
213 User.active.find(suspended.id)
215 assert_raise ActiveRecord::RecordNotFound do
216 User.active.find(deleted.id)
220 def test_identifiable
221 public_user = create(:user, :data_public => true)
222 private_user = create(:user, :data_public => false)
224 assert User.identifiable.find(public_user.id)
225 assert_raise ActiveRecord::RecordNotFound do
226 User.identifiable.find(private_user.id)
231 create(:language, :code => "en")
232 create(:language, :code => "de")
233 create(:language, :code => "sl")
235 user = create(:user, :languages => ["en"])
236 assert_equal ["en"], user.languages
237 user.languages = %w[de fr en]
238 assert_equal %w[de fr en], user.languages
239 user.languages = %w[fr de sl]
240 assert_equal "de", user.preferred_language
241 assert_equal %w[fr de sl], user.preferred_languages.map(&:to_s)
242 user = create(:user, :languages => %w[en de])
243 assert_equal %w[en de], user.languages
247 assert_predicate build(:user, :pending), :visible?
248 assert_predicate build(:user, :active), :visible?
249 assert_predicate build(:user, :confirmed), :visible?
250 assert_not build(:user, :suspended).visible?
251 assert_not build(:user, :deleted).visible?
255 assert_not build(:user, :pending).active?
256 assert_predicate build(:user, :active), :active?
257 assert_predicate build(:user, :confirmed), :active?
258 assert_not build(:user, :suspended).active?
259 assert_not build(:user, :deleted).active?
263 assert_not create(:user).moderator?
264 assert_predicate create(:moderator_user), :moderator?
267 def test_administrator?
268 assert_not create(:user).administrator?
269 assert_predicate create(:administrator_user), :administrator?
273 assert_not create(:user).role?("administrator")
274 assert_not create(:user).role?("moderator")
275 assert create(:administrator_user).role?("administrator")
276 assert create(:moderator_user).role?("moderator")
279 def test_soft_destroy
280 user = create(:user, :with_home_location, :description => "foo")
282 assert_equal "user_#{user.id}", user.display_name
283 assert_predicate user.description, :blank?
284 assert_nil user.home_lat
285 assert_nil user.home_lon
286 assert_not user.avatar.attached?
287 assert_equal "deleted", user.status
288 assert_not user.visible?
289 assert_not user.active?
292 def test_soft_destroy_revokes_oauth1_tokens
294 access_token = create(:access_token, :user => user)
295 assert_equal 1, user.oauth_tokens.authorized.count
299 assert_equal 0, user.oauth_tokens.authorized.count
301 assert_predicate access_token, :invalidated?
304 def test_soft_destroy_revokes_oauth2_tokens
306 oauth_access_token = create(:oauth_access_token, :resource_owner_id => user.id)
307 assert_equal 1, user.access_tokens.not_expired.count
311 assert_equal 0, user.access_tokens.not_expired.count
312 oauth_access_token.reload
313 assert_predicate oauth_access_token, :revoked?
316 def test_deletion_allowed_when_no_changesets
317 with_user_account_deletion_delay(10000) do
319 assert_predicate user, :deletion_allowed?
323 def test_deletion_allowed_without_delay
324 with_user_account_deletion_delay(nil) do
326 create(:changeset, :user => user)
328 assert_predicate user, :deletion_allowed?
332 def test_deletion_allowed_past_delay
333 with_user_account_deletion_delay(10) do
335 create(:changeset, :user => user, :created_at => Time.now.utc - 12.hours, :closed_at => Time.now.utc - 10.hours)
337 assert_predicate user, :deletion_allowed?
341 def test_deletion_allowed_during_delay
342 with_user_account_deletion_delay(10) do
344 create(:changeset, :user => user, :created_at => Time.now.utc - 11.hours, :closed_at => Time.now.utc - 9.hours)
346 assert_not_predicate user, :deletion_allowed?
347 assert_equal Time.now.utc + 1.hour, user.deletion_allowed_at
351 def test_deletion_allowed_past_zero_delay
352 with_user_account_deletion_delay(0) do
354 create(:changeset, :user => user, :created_at => Time.now.utc, :closed_at => Time.now.utc + 1.hour)
357 assert_predicate user, :deletion_allowed?
362 def test_deletion_allowed_during_zero_delay
363 with_user_account_deletion_delay(0) do
365 create(:changeset, :user => user, :created_at => Time.now.utc, :closed_at => Time.now.utc + 1.hour)
368 assert_not_predicate user, :deletion_allowed?
369 assert_equal Time.now.utc + 30.minutes, user.deletion_allowed_at