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_valid
71 # Due to sanitisation in the view some of these that you might not
73 # However, would they affect the xml planet dumps?
74 ok = ["Name", "'me", "he\"", "<hr>", "*ho", "\"help\"@",
75 "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲", "space space"]
76 # These need to be 3 chars in length, otherwise the length test above
78 bad = ["<hr/>", "test@example.com", "s/f", "aa/", "aa;", "aa.",
79 "aa,", "aa?", "/;.,?", "も対応します/", "#ping",
80 "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar",
81 "new", "terms", "save", "confirm", "confirm-email",
82 "go_public", "reset-password", "forgot-password", "suspended",
83 "trailing whitespace ", " leading whitespace"]
84 ok.each do |display_name|
86 user.display_name = display_name
87 assert_predicate user, :valid?, "#{display_name} is invalid, when it should be"
90 bad.each do |display_name|
92 user.display_name = display_name
93 assert_not_predicate user, :valid?, "#{display_name} is valid when it shouldn't be"
97 def test_display_name_user_id_new
98 existing_user = create(:user)
101 user.display_name = "user_#{existing_user.id}"
102 assert_not user.valid?, "user_<id> name is valid for existing user id when it shouldn't be"
104 user.display_name = "user_#{existing_user.id + 1}"
105 assert_not user.valid?, "user_<id> name is valid for new user id when it shouldn't be"
108 def test_display_name_user_id_rename
109 existing_user = create(:user)
112 user.display_name = "user_#{existing_user.id}"
113 assert_not user.valid?, "user_<id> name is valid for existing user id when it shouldn't be"
115 user.display_name = "user_#{user.id}"
116 assert_predicate user, :valid?, "user_<id> name is invalid for own id, when it should be"
119 def test_display_name_user_id_unchanged_is_valid
120 user = build(:user, :display_name => "user_0")
121 user.save(:validate => false)
124 assert_predicate user, :valid?, "user_0 display_name is invalid but it hasn't been changed"
127 def test_friends_with
128 alice = create(:user, :active)
129 bob = create(:user, :active)
130 charlie = create(:user, :active)
131 create(:friendship, :befriender => alice, :befriendee => bob)
133 assert alice.friends_with?(bob)
134 assert_not alice.friends_with?(charlie)
135 assert_not bob.friends_with?(alice)
136 assert_not bob.friends_with?(charlie)
137 assert_not charlie.friends_with?(bob)
138 assert_not charlie.friends_with?(alice)
141 def test_users_nearby
142 alice = create(:user, :active, :home_lat => 51.0, :home_lon => 1.0, :data_public => false)
143 bob = create(:user, :active, :home_lat => 51.1, :home_lon => 1.0, :data_public => true)
144 charlie = create(:user, :active, :home_lat => 51.1, :home_lon => 1.1, :data_public => true)
145 david = create(:user, :active, :home_lat => 10.0, :home_lon => -123.0, :data_public => true)
146 _edward = create(:user, :suspended, :home_lat => 10.0, :home_lon => -123.0, :data_public => true)
147 south_pole_user = create(:user, :active, :home_lat => -90.0, :home_lon => 0.0, :data_public => true)
148 vagrant_user = create(:user, :active, :home_lat => nil, :home_lon => nil, :data_public => true)
150 # bob and charlie are both near alice
151 assert_equal [bob, charlie], alice.nearby
152 # charlie and alice are both near bob, but alice has their data private
153 assert_equal [charlie], bob.nearby
154 # david has no user nearby, since edward is not active
155 assert_empty david.nearby
156 # south_pole_user has no user nearby, and doesn't throw exception
157 assert_empty south_pole_user.nearby
158 # vagrant_user has no home location
159 assert_empty vagrant_user.nearby
163 norm = create(:user, :active)
164 sec = create(:user, :active)
165 create(:friendship, :befriender => norm, :befriendee => sec)
167 assert_equal [sec], norm.friends
168 assert_equal 1, norm.friends.size
170 assert_empty sec.friends
171 assert_equal 0, sec.friends.size
174 def test_user_preferred_editor
176 assert_nil user.preferred_editor
177 user.preferred_editor = "id"
178 assert_equal "id", user.preferred_editor
181 user.preferred_editor = "invalid_editor"
182 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
186 pending = create(:user, :pending)
187 active = create(:user, :active)
188 confirmed = create(:user, :confirmed)
189 suspended = create(:user, :suspended)
190 deleted = create(:user, :deleted)
192 assert User.visible.find(pending.id)
193 assert User.visible.find(active.id)
194 assert User.visible.find(confirmed.id)
195 assert_raise ActiveRecord::RecordNotFound do
196 User.visible.find(suspended.id)
198 assert_raise ActiveRecord::RecordNotFound do
199 User.visible.find(deleted.id)
204 pending = create(:user, :pending)
205 active = create(:user, :active)
206 confirmed = create(:user, :confirmed)
207 suspended = create(:user, :suspended)
208 deleted = create(:user, :deleted)
210 assert User.active.find(active.id)
211 assert User.active.find(confirmed.id)
212 assert_raise ActiveRecord::RecordNotFound do
213 User.active.find(pending.id)
215 assert_raise ActiveRecord::RecordNotFound do
216 User.active.find(suspended.id)
218 assert_raise ActiveRecord::RecordNotFound do
219 User.active.find(deleted.id)
223 def test_identifiable
224 public_user = create(:user, :data_public => true)
225 private_user = create(:user, :data_public => false)
227 assert User.identifiable.find(public_user.id)
228 assert_raise ActiveRecord::RecordNotFound do
229 User.identifiable.find(private_user.id)
234 create(:language, :code => "en")
235 create(:language, :code => "de")
236 create(:language, :code => "sl")
238 user = create(:user, :languages => ["en"])
239 assert_equal ["en"], user.languages
240 user.languages = %w[de fr en]
241 assert_equal %w[de fr en], user.languages
242 user.languages = %w[fr de sl]
243 assert_equal "de", user.preferred_language
244 assert_equal %w[fr de sl], user.preferred_languages.map(&:to_s)
245 user = create(:user, :languages => %w[en de])
246 assert_equal %w[en de], user.languages
250 assert_predicate build(:user, :pending), :visible?
251 assert_predicate build(:user, :active), :visible?
252 assert_predicate build(:user, :confirmed), :visible?
253 assert_not_predicate build(:user, :suspended), :visible?
254 assert_not_predicate build(:user, :deleted), :visible?
258 assert_not_predicate build(:user, :pending), :active?
259 assert_predicate build(:user, :active), :active?
260 assert_predicate build(:user, :confirmed), :active?
261 assert_not_predicate build(:user, :suspended), :active?
262 assert_not_predicate build(:user, :deleted), :active?
266 assert_not_predicate create(:user), :moderator?
267 assert_predicate create(:moderator_user), :moderator?
270 def test_administrator?
271 assert_not_predicate create(:user), :administrator?
272 assert_predicate create(:administrator_user), :administrator?
276 assert_not create(:user).role?("administrator")
277 assert_not create(:user).role?("moderator")
278 assert create(:administrator_user).role?("administrator")
279 assert create(:moderator_user).role?("moderator")
282 def test_soft_destroy
283 user = create(:user, :with_home_location, :description => "foo")
285 assert_equal "user_#{user.id}", user.display_name
286 assert_predicate user.description, :blank?
287 assert_nil user.home_lat
288 assert_nil user.home_lon
289 assert_not_predicate user.avatar, :attached?
290 assert_equal "deleted", user.status
291 assert_not_predicate user, :visible?
292 assert_not_predicate user, :active?
295 def test_soft_destroy_revokes_oauth1_tokens
297 access_token = create(:access_token, :user => user)
298 assert_equal 1, user.oauth_tokens.authorized.count
302 assert_equal 0, user.oauth_tokens.authorized.count
304 assert_predicate access_token, :invalidated?
307 def test_soft_destroy_revokes_oauth2_tokens
309 oauth_access_token = create(:oauth_access_token, :resource_owner_id => user.id)
310 assert_equal 1, user.access_tokens.not_expired.count
314 assert_equal 0, user.access_tokens.not_expired.count
315 oauth_access_token.reload
316 assert_predicate oauth_access_token, :revoked?
319 def test_deletion_allowed_when_no_changesets
320 with_user_account_deletion_delay(10000) do
322 assert_predicate user, :deletion_allowed?
326 def test_deletion_allowed_without_delay
327 with_user_account_deletion_delay(nil) do
329 create(:changeset, :user => user)
331 assert_predicate user, :deletion_allowed?
335 def test_deletion_allowed_past_delay
336 with_user_account_deletion_delay(10) do
338 create(:changeset, :user => user, :created_at => Time.now.utc - 12.hours, :closed_at => Time.now.utc - 10.hours)
340 assert_predicate user, :deletion_allowed?
344 def test_deletion_allowed_during_delay
345 with_user_account_deletion_delay(10) do
347 create(:changeset, :user => user, :created_at => Time.now.utc - 11.hours, :closed_at => Time.now.utc - 9.hours)
349 assert_not_predicate user, :deletion_allowed?
350 assert_equal Time.now.utc + 1.hour, user.deletion_allowed_at
354 def test_deletion_allowed_past_zero_delay
355 with_user_account_deletion_delay(0) do
357 create(:changeset, :user => user, :created_at => Time.now.utc, :closed_at => Time.now.utc + 1.hour)
360 assert_predicate user, :deletion_allowed?
365 def test_deletion_allowed_during_zero_delay
366 with_user_account_deletion_delay(0) do
368 create(:changeset, :user => user, :created_at => Time.now.utc, :closed_at => Time.now.utc + 1.hour)
371 assert_not_predicate user, :deletion_allowed?
372 assert_equal Time.now.utc + 30.minutes, user.deletion_allowed_at