]> git.openstreetmap.org Git - rails.git/blob - test/models/user_test.rb
Move change detection to validation declaration
[rails.git] / test / models / user_test.rb
1 require "test_helper"
2
3 class UserTest < ActiveSupport::TestCase
4   include Rails::Dom::Testing::Assertions::SelectorAssertions
5
6   def test_invalid_with_empty_attributes
7     user = build(:user, :email => nil,
8                         :pass_crypt => nil,
9                         :display_name => nil,
10                         :home_lat => nil,
11                         :home_lon => nil,
12                         :home_zoom => 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?
20   end
21
22   def test_unique_email
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"
27   end
28
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"
34   end
35
36   def test_email_valid
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]
41
42     ok.each do |name|
43       user = build(:user)
44       user.email = name
45       assert user.valid?(:save), "#{name} isn't valid when it should be"
46     end
47
48     bad.each do |name|
49       user = build(:user)
50       user.email = name
51       assert user.invalid?(:save), "#{name} is valid when it shouldn't be"
52     end
53   end
54
55   def test_display_name_length
56     user = build(:user)
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"
65   end
66
67   def test_display_name_valid
68     # Due to sanitisation in the view some of these that you might not
69     # expect are allowed
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
74     # should be used.
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|
82       user = build(:user)
83       user.display_name = display_name
84       assert_predicate user, :valid?, "#{display_name} is invalid, when it should be"
85     end
86
87     bad.each do |display_name|
88       user = build(:user)
89       user.display_name = display_name
90       assert_not user.valid?, "#{display_name} is valid when it shouldn't be"
91     end
92   end
93
94   def test_display_name_user_id_new
95     existing_user = create(:user)
96     user = build(:user)
97
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"
100
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"
103   end
104
105   def test_display_name_user_id_rename
106     existing_user = create(:user)
107     user = create(:user)
108
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"
111
112     user.display_name = "user_#{user.id}"
113     assert_predicate user, :valid?, "user_<id> name is invalid for own id, when it should be"
114   end
115
116   def test_display_name_user_id_unchanged_is_valid
117     user = build(:user, :display_name => "user_0")
118     user.save(:validate => false)
119     user.reload
120
121     assert_predicate user, :valid?, "user_0 display_name is invalid but it hasn't been changed"
122   end
123
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)
129
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)
136   end
137
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)
146
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
157   end
158
159   def test_friends
160     norm = create(:user, :active)
161     sec = create(:user, :active)
162     create(:friendship, :befriender => norm, :befriendee => sec)
163
164     assert_equal [sec], norm.friends
165     assert_equal 1, norm.friends.size
166
167     assert_empty sec.friends
168     assert_equal 0, sec.friends.size
169   end
170
171   def test_user_preferred_editor
172     user = create(:user)
173     assert_nil user.preferred_editor
174     user.preferred_editor = "id"
175     assert_equal "id", user.preferred_editor
176     user.save!
177
178     user.preferred_editor = "invalid_editor"
179     assert_raise(ActiveRecord::RecordInvalid) { user.save! }
180   end
181
182   def test_visible
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)
188
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)
194     end
195     assert_raise ActiveRecord::RecordNotFound do
196       User.visible.find(deleted.id)
197     end
198   end
199
200   def test_active
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)
206
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)
211     end
212     assert_raise ActiveRecord::RecordNotFound do
213       User.active.find(suspended.id)
214     end
215     assert_raise ActiveRecord::RecordNotFound do
216       User.active.find(deleted.id)
217     end
218   end
219
220   def test_identifiable
221     public_user = create(:user, :data_public => true)
222     private_user = create(:user, :data_public => false)
223
224     assert User.identifiable.find(public_user.id)
225     assert_raise ActiveRecord::RecordNotFound do
226       User.identifiable.find(private_user.id)
227     end
228   end
229
230   def test_languages
231     create(:language, :code => "en")
232     create(:language, :code => "de")
233     create(:language, :code => "sl")
234
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
244   end
245
246   def test_visible?
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?
252   end
253
254   def test_active?
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?
260   end
261
262   def test_moderator?
263     assert_not create(:user).moderator?
264     assert_predicate create(:moderator_user), :moderator?
265   end
266
267   def test_administrator?
268     assert_not create(:user).administrator?
269     assert_predicate create(:administrator_user), :administrator?
270   end
271
272   def test_role?
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")
277   end
278
279   def test_soft_destroy
280     user = create(:user, :with_home_location, :description => "foo")
281     user.soft_destroy
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?
290   end
291
292   def test_soft_destroy_revokes_oauth1_tokens
293     user = create(:user)
294     access_token = create(:access_token, :user => user)
295     assert_equal 1, user.oauth_tokens.authorized.count
296
297     user.soft_destroy
298
299     assert_equal 0, user.oauth_tokens.authorized.count
300     access_token.reload
301     assert_predicate access_token, :invalidated?
302   end
303
304   def test_soft_destroy_revokes_oauth2_tokens
305     user = create(:user)
306     oauth_access_token = create(:oauth_access_token, :resource_owner_id => user.id)
307     assert_equal 1, user.access_tokens.not_expired.count
308
309     user.soft_destroy
310
311     assert_equal 0, user.access_tokens.not_expired.count
312     oauth_access_token.reload
313     assert_predicate oauth_access_token, :revoked?
314   end
315
316   def test_deletion_allowed_when_no_changesets
317     with_user_account_deletion_delay(10000) do
318       user = create(:user)
319       assert_predicate user, :deletion_allowed?
320     end
321   end
322
323   def test_deletion_allowed_without_delay
324     with_user_account_deletion_delay(nil) do
325       user = create(:user)
326       create(:changeset, :user => user)
327       user.reload
328       assert_predicate user, :deletion_allowed?
329     end
330   end
331
332   def test_deletion_allowed_past_delay
333     with_user_account_deletion_delay(10) do
334       user = create(:user)
335       create(:changeset, :user => user, :created_at => Time.now.utc - 12.hours, :closed_at => Time.now.utc - 10.hours)
336       user.reload
337       assert_predicate user, :deletion_allowed?
338     end
339   end
340
341   def test_deletion_allowed_during_delay
342     with_user_account_deletion_delay(10) do
343       user = create(:user)
344       create(:changeset, :user => user, :created_at => Time.now.utc - 11.hours, :closed_at => Time.now.utc - 9.hours)
345       user.reload
346       assert_not_predicate user, :deletion_allowed?
347       assert_equal Time.now.utc + 1.hour, user.deletion_allowed_at
348     end
349   end
350
351   def test_deletion_allowed_past_zero_delay
352     with_user_account_deletion_delay(0) do
353       user = create(:user)
354       create(:changeset, :user => user, :created_at => Time.now.utc, :closed_at => Time.now.utc + 1.hour)
355       travel 90.minutes do
356         user.reload
357         assert_predicate user, :deletion_allowed?
358       end
359     end
360   end
361
362   def test_deletion_allowed_during_zero_delay
363     with_user_account_deletion_delay(0) do
364       user = create(:user)
365       create(:changeset, :user => user, :created_at => Time.now.utc, :closed_at => Time.now.utc + 1.hour)
366       travel 30.minutes do
367         user.reload
368         assert_not_predicate user, :deletion_allowed?
369         assert_equal Time.now.utc + 30.minutes, user.deletion_allowed_at
370       end
371     end
372   end
373 end