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"
98 alice = create(:user, :active)
99 bob = create(:user, :active)
100 charlie = create(:user, :active)
101 create(:friendship, :befriender => alice, :befriendee => bob)
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)
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)
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
133 norm = create(:user, :active)
134 sec = create(:user, :active)
135 create(:friendship, :befriender => norm, :befriendee => sec)
137 assert_equal [sec], norm.friends
138 assert_equal 1, norm.friends.size
140 assert_empty sec.friends
141 assert_equal 0, sec.friends.size
144 def test_user_preferred_editor
146 assert_nil user.preferred_editor
147 user.preferred_editor = "id"
148 assert_equal "id", user.preferred_editor
151 user.preferred_editor = "invalid_editor"
152 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
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)
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)
168 assert_raise ActiveRecord::RecordNotFound do
169 User.visible.find(deleted.id)
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)
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)
185 assert_raise ActiveRecord::RecordNotFound do
186 User.active.find(suspended.id)
188 assert_raise ActiveRecord::RecordNotFound do
189 User.active.find(deleted.id)
193 def test_identifiable
194 public_user = create(:user, :data_public => true)
195 private_user = create(:user, :data_public => false)
197 assert User.identifiable.find(public_user.id)
198 assert_raise ActiveRecord::RecordNotFound do
199 User.identifiable.find(private_user.id)
204 create(:language, :code => "en")
205 create(:language, :code => "de")
206 create(:language, :code => "sl")
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
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?
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?
236 assert_not_predicate create(:user), :moderator?
237 assert_predicate create(:moderator_user), :moderator?
240 def test_administrator?
241 assert_not_predicate create(:user), :administrator?
242 assert_predicate create(:administrator_user), :administrator?
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")
252 def test_soft_destroy
253 user = create(:user, :with_home_location, :description => "foo")
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?
265 def test_soft_destroy_revokes_oauth1_tokens
267 access_token = create(:access_token, :user => user)
268 assert_equal 1, user.oauth_tokens.authorized.count
272 assert_equal 0, user.oauth_tokens.authorized.count
274 assert_predicate access_token, :invalidated?
277 def test_soft_destroy_revokes_oauth2_tokens
279 oauth_access_token = create(:oauth_access_token, :resource_owner_id => user.id)
280 assert_equal 1, user.access_tokens.not_expired.count
284 assert_equal 0, user.access_tokens.not_expired.count
285 oauth_access_token.reload
286 assert_predicate oauth_access_token, :revoked?
289 def test_deletion_allowed_when_no_changesets
290 with_user_account_deletion_delay(10000) do
292 assert_predicate user, :deletion_allowed?
296 def test_deletion_allowed_without_delay
297 with_user_account_deletion_delay(nil) do
299 create(:changeset, :user => user)
301 assert_predicate user, :deletion_allowed?
305 def test_deletion_allowed_past_delay
306 with_user_account_deletion_delay(10) do
308 create(:changeset, :user => user, :created_at => Time.now.utc - 12.hours, :closed_at => Time.now.utc - 10.hours)
310 assert_predicate user, :deletion_allowed?
314 def test_deletion_allowed_during_delay
315 with_user_account_deletion_delay(10) do
317 create(:changeset, :user => user, :created_at => Time.now.utc - 11.hours, :closed_at => Time.now.utc - 9.hours)
319 assert_not_predicate user, :deletion_allowed?
320 assert_equal Time.now.utc + 1.hour, user.deletion_allowed_at
324 def test_deletion_allowed_past_zero_delay
325 with_user_account_deletion_delay(0) do
327 create(:changeset, :user => user, :created_at => Time.now.utc, :closed_at => Time.now.utc + 1.hour)
330 assert_predicate user, :deletion_allowed?
335 def test_deletion_allowed_during_zero_delay
336 with_user_account_deletion_delay(0) do
338 create(:changeset, :user => user, :created_at => Time.now.utc, :closed_at => Time.now.utc + 1.hour)
341 assert_not_predicate user, :deletion_allowed?
342 assert_equal Time.now.utc + 30.minutes, user.deletion_allowed_at