]> git.openstreetmap.org Git - rails.git/blob - test/models/user_test.rb
Merge remote-tracking branch 'upstream/pull/4482'
[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_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?
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     create(:user, :display_name => "H\u{e9}nryIV")
31
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"
36     end
37   end
38
39   def test_email_valid
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]
44
45     ok.each do |name|
46       user = build(:user)
47       user.email = name
48       assert user.valid?(:save), "#{name} isn't valid when it should be"
49     end
50
51     bad.each do |name|
52       user = build(:user)
53       user.email = name
54       assert user.invalid?(:save), "#{name} is valid when it shouldn't be"
55     end
56   end
57
58   def test_display_name_length
59     user = build(:user)
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"
68   end
69
70   def test_display_name_valid
71     # Due to sanitisation in the view some of these that you might not
72     # expect are allowed
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
77     # should be used.
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|
85       user = build(:user)
86       user.display_name = display_name
87       assert_predicate user, :valid?, "#{display_name} is invalid, when it should be"
88     end
89
90     bad.each do |display_name|
91       user = build(:user)
92       user.display_name = display_name
93       assert_not_predicate user, :valid?, "#{display_name} is valid when it shouldn't be"
94     end
95   end
96
97   def test_friends_with
98     alice = create(:user, :active)
99     bob = create(:user, :active)
100     charlie = create(:user, :active)
101     create(:friendship, :befriender => alice, :befriendee => bob)
102
103     assert alice.friends_with?(bob)
104     assert_not alice.friends_with?(charlie)
105     assert_not bob.friends_with?(alice)
106     assert_not bob.friends_with?(charlie)
107     assert_not charlie.friends_with?(bob)
108     assert_not charlie.friends_with?(alice)
109   end
110
111   def test_users_nearby
112     alice = create(:user, :active, :home_lat => 51.0, :home_lon => 1.0, :data_public => false)
113     bob = create(:user, :active, :home_lat => 51.1, :home_lon => 1.0, :data_public => true)
114     charlie = create(:user, :active, :home_lat => 51.1, :home_lon => 1.1, :data_public => true)
115     david = create(:user, :active, :home_lat => 10.0, :home_lon => -123.0, :data_public => true)
116     _edward = create(:user, :suspended, :home_lat => 10.0, :home_lon => -123.0, :data_public => true)
117     south_pole_user = create(:user, :active, :home_lat => -90.0, :home_lon => 0.0, :data_public => true)
118     vagrant_user = create(:user, :active, :home_lat => nil, :home_lon => nil, :data_public => true)
119
120     # bob and charlie are both near alice
121     assert_equal [bob, charlie], alice.nearby
122     # charlie and alice are both near bob, but alice has their data private
123     assert_equal [charlie], bob.nearby
124     # david has no user nearby, since edward is not active
125     assert_empty david.nearby
126     # south_pole_user has no user nearby, and doesn't throw exception
127     assert_empty south_pole_user.nearby
128     # vagrant_user has no home location
129     assert_empty vagrant_user.nearby
130   end
131
132   def test_friends
133     norm = create(:user, :active)
134     sec = create(:user, :active)
135     create(:friendship, :befriender => norm, :befriendee => sec)
136
137     assert_equal [sec], norm.friends
138     assert_equal 1, norm.friends.size
139
140     assert_empty sec.friends
141     assert_equal 0, sec.friends.size
142   end
143
144   def test_user_preferred_editor
145     user = create(:user)
146     assert_nil user.preferred_editor
147     user.preferred_editor = "id"
148     assert_equal "id", user.preferred_editor
149     user.save!
150
151     user.preferred_editor = "invalid_editor"
152     assert_raise(ActiveRecord::RecordInvalid) { user.save! }
153   end
154
155   def test_visible
156     pending = create(:user, :pending)
157     active = create(:user, :active)
158     confirmed = create(:user, :confirmed)
159     suspended = create(:user, :suspended)
160     deleted = create(:user, :deleted)
161
162     assert User.visible.find(pending.id)
163     assert User.visible.find(active.id)
164     assert User.visible.find(confirmed.id)
165     assert_raise ActiveRecord::RecordNotFound do
166       User.visible.find(suspended.id)
167     end
168     assert_raise ActiveRecord::RecordNotFound do
169       User.visible.find(deleted.id)
170     end
171   end
172
173   def test_active
174     pending = create(:user, :pending)
175     active = create(:user, :active)
176     confirmed = create(:user, :confirmed)
177     suspended = create(:user, :suspended)
178     deleted = create(:user, :deleted)
179
180     assert User.active.find(active.id)
181     assert User.active.find(confirmed.id)
182     assert_raise ActiveRecord::RecordNotFound do
183       User.active.find(pending.id)
184     end
185     assert_raise ActiveRecord::RecordNotFound do
186       User.active.find(suspended.id)
187     end
188     assert_raise ActiveRecord::RecordNotFound do
189       User.active.find(deleted.id)
190     end
191   end
192
193   def test_identifiable
194     public_user = create(:user, :data_public => true)
195     private_user = create(:user, :data_public => false)
196
197     assert User.identifiable.find(public_user.id)
198     assert_raise ActiveRecord::RecordNotFound do
199       User.identifiable.find(private_user.id)
200     end
201   end
202
203   def test_languages
204     create(:language, :code => "en")
205     create(:language, :code => "de")
206     create(:language, :code => "sl")
207
208     user = create(:user, :languages => ["en"])
209     assert_equal ["en"], user.languages
210     user.languages = %w[de fr en]
211     assert_equal %w[de fr en], user.languages
212     user.languages = %w[fr de sl]
213     assert_equal "de", user.preferred_language
214     assert_equal %w[fr de sl], user.preferred_languages.map(&:to_s)
215     user = create(:user, :languages => %w[en de])
216     assert_equal %w[en de], user.languages
217   end
218
219   def test_visible?
220     assert_predicate build(:user, :pending), :visible?
221     assert_predicate build(:user, :active), :visible?
222     assert_predicate build(:user, :confirmed), :visible?
223     assert_not_predicate build(:user, :suspended), :visible?
224     assert_not_predicate build(:user, :deleted), :visible?
225   end
226
227   def test_active?
228     assert_not_predicate build(:user, :pending), :active?
229     assert_predicate build(:user, :active), :active?
230     assert_predicate build(:user, :confirmed), :active?
231     assert_not_predicate build(:user, :suspended), :active?
232     assert_not_predicate build(:user, :deleted), :active?
233   end
234
235   def test_moderator?
236     assert_not_predicate create(:user), :moderator?
237     assert_predicate create(:moderator_user), :moderator?
238   end
239
240   def test_administrator?
241     assert_not_predicate create(:user), :administrator?
242     assert_predicate create(:administrator_user), :administrator?
243   end
244
245   def test_role?
246     assert_not create(:user).role?("administrator")
247     assert_not create(:user).role?("moderator")
248     assert create(:administrator_user).role?("administrator")
249     assert create(:moderator_user).role?("moderator")
250   end
251
252   def test_soft_destroy
253     user = create(:user, :with_home_location, :description => "foo")
254     user.soft_destroy
255     assert_equal "user_#{user.id}", user.display_name
256     assert_predicate user.description, :blank?
257     assert_nil user.home_lat
258     assert_nil user.home_lon
259     assert_not_predicate user.avatar, :attached?
260     assert_equal "deleted", user.status
261     assert_not_predicate user, :visible?
262     assert_not_predicate user, :active?
263   end
264
265   def test_soft_destroy_revokes_oauth1_tokens
266     user = create(:user)
267     access_token = create(:access_token, :user => user)
268     assert_equal 1, user.oauth_tokens.authorized.count
269
270     user.soft_destroy
271
272     assert_equal 0, user.oauth_tokens.authorized.count
273     access_token.reload
274     assert_predicate access_token, :invalidated?
275   end
276
277   def test_soft_destroy_revokes_oauth2_tokens
278     user = create(:user)
279     oauth_access_token = create(:oauth_access_token, :resource_owner_id => user.id)
280     assert_equal 1, user.access_tokens.not_expired.count
281
282     user.soft_destroy
283
284     assert_equal 0, user.access_tokens.not_expired.count
285     oauth_access_token.reload
286     assert_predicate oauth_access_token, :revoked?
287   end
288
289   def test_deletion_allowed_when_no_changesets
290     with_user_account_deletion_delay(10000) do
291       user = create(:user)
292       assert_predicate user, :deletion_allowed?
293     end
294   end
295
296   def test_deletion_allowed_without_delay
297     with_user_account_deletion_delay(nil) do
298       user = create(:user)
299       create(:changeset, :user => user)
300       user.reload
301       assert_predicate user, :deletion_allowed?
302     end
303   end
304
305   def test_deletion_allowed_past_delay
306     with_user_account_deletion_delay(10) do
307       user = create(:user)
308       create(:changeset, :user => user, :created_at => Time.now.utc - 12.hours, :closed_at => Time.now.utc - 10.hours)
309       user.reload
310       assert_predicate user, :deletion_allowed?
311     end
312   end
313
314   def test_deletion_allowed_during_delay
315     with_user_account_deletion_delay(10) do
316       user = create(:user)
317       create(:changeset, :user => user, :created_at => Time.now.utc - 11.hours, :closed_at => Time.now.utc - 9.hours)
318       user.reload
319       assert_not_predicate user, :deletion_allowed?
320       assert_equal Time.now.utc + 1.hour, user.deletion_allowed_at
321     end
322   end
323
324   def test_deletion_allowed_past_zero_delay
325     with_user_account_deletion_delay(0) do
326       user = create(:user)
327       create(:changeset, :user => user, :created_at => Time.now.utc, :closed_at => Time.now.utc + 1.hour)
328       travel 90.minutes do
329         user.reload
330         assert_predicate user, :deletion_allowed?
331       end
332     end
333   end
334
335   def test_deletion_allowed_during_zero_delay
336     with_user_account_deletion_delay(0) do
337       user = create(:user)
338       create(:changeset, :user => user, :created_at => Time.now.utc, :closed_at => Time.now.utc + 1.hour)
339       travel 30.minutes do
340         user.reload
341         assert_not_predicate user, :deletion_allowed?
342         assert_equal Time.now.utc + 30.minutes, user.deletion_allowed_at
343       end
344     end
345   end
346 end