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