3 class UserTest < ActiveSupport::TestCase
4 include Rails::Dom::Testing::Assertions::SelectorAssertions
6 def test_invalid_with_empty_attributes
9 assert user.errors[:email].any?
10 assert user.errors[:pass_crypt].any?
11 assert user.errors[:display_name].any?
12 assert user.errors[:email].any?
13 assert user.errors[:home_lat].none?
14 assert user.errors[:home_lon].none?
15 assert user.errors[:home_zoom].none?
19 existing_user = create(:user)
21 :email => existing_user.email,
23 :pass_crypt => Digest::MD5.hexdigest("test"),
24 :display_name => "new user",
26 :description => "desc"
28 assert_not new_user.save
29 assert_includes new_user.errors[:email], "has already been taken"
32 def test_unique_display_name
33 existing_user = create(:user)
35 :email => "tester@openstreetmap.org",
37 :pass_crypt => Digest::MD5.hexdigest("test"),
38 :display_name => existing_user.display_name,
40 :description => "desc"
42 assert_not new_user.save
43 assert_includes new_user.errors[:display_name], "has already been taken"
47 ok = %w[a@s.com test@shaunmcdonald.me.uk hello_local@ping-d.ng
48 test_local@openstreetmap.org test-local@example.com]
49 bad = %w[hi ht@ n@ @.com help@.me.uk help"hi.me.uk も対@応します
50 輕觸搖晃的遊戲@ah.com も対応します@s.name]
55 assert user.valid?(:save), user.errors.full_messages.join(",")
61 assert user.invalid?(:save), "#{name} is valid when it shouldn't be"
65 def test_display_name_length
67 user.display_name = "123"
68 assert user.valid?, "should allow 3 char name name"
69 user.display_name = "12"
70 assert_not user.valid?, "should not allow 2 char name"
71 user.display_name = ""
72 assert_not user.valid?, "should not allow blank/0 char name"
73 user.display_name = nil
74 assert_not user.valid?, "should not allow nil value"
77 def test_display_name_valid
78 # Due to sanitisation in the view some of these that you might not
80 # However, would they affect the xml planet dumps?
81 ok = ["Name", "'me", "he\"", "<hr>", "*ho", "\"help\"@",
82 "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲", "space space"]
83 # These need to be 3 chars in length, otherwise the length test above
85 bad = ["<hr/>", "test@example.com", "s/f", "aa/", "aa;", "aa.",
86 "aa,", "aa?", "/;.,?", "も対応します/", "#ping",
87 "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar",
88 "new", "terms", "save", "confirm", "confirm-email",
89 "go_public", "reset-password", "forgot-password", "suspended",
90 "trailing whitespace ", " leading whitespace"]
91 ok.each do |display_name|
93 user.display_name = display_name
94 assert user.valid?, "#{display_name} is invalid, when it should be"
97 bad.each do |display_name|
99 user.display_name = display_name
100 assert_not user.valid?, "#{display_name} is valid when it shouldn't be"
104 def test_friends_with
105 alice = create(:user, :active)
106 bob = create(:user, :active)
107 charlie = create(:user, :active)
108 create(:friendship, :befriender => alice, :befriendee => bob)
110 assert alice.is_friends_with?(bob)
111 assert_not alice.is_friends_with?(charlie)
112 assert_not bob.is_friends_with?(alice)
113 assert_not bob.is_friends_with?(charlie)
114 assert_not charlie.is_friends_with?(bob)
115 assert_not charlie.is_friends_with?(alice)
118 def test_users_nearby
119 alice = create(:user, :active, :home_lat => 51.0, :home_lon => 1.0, :data_public => false)
120 bob = create(:user, :active, :home_lat => 51.1, :home_lon => 1.0, :data_public => true)
121 charlie = create(:user, :active, :home_lat => 51.1, :home_lon => 1.1, :data_public => true)
122 david = create(:user, :active, :home_lat => 10.0, :home_lon => -123.0, :data_public => true)
123 _edward = create(:user, :suspended, :home_lat => 10.0, :home_lon => -123.0, :data_public => true)
124 south_pole_user = create(:user, :active, :home_lat => -90.0, :home_lon => 0.0, :data_public => true)
125 vagrant_user = create(:user, :active, :home_lat => nil, :home_lon => nil, :data_public => true)
127 # bob and charlie are both near alice
128 assert_equal [bob, charlie], alice.nearby
129 # charlie and alice are both near bob, but alice has their data private
130 assert_equal [charlie], bob.nearby
131 # david has no user nearby, since edward is not active
132 assert_equal [], david.nearby
133 # south_pole_user has no user nearby, and doesn't throw exception
134 assert_equal [], south_pole_user.nearby
135 # vagrant_user has no home location
136 assert_equal [], vagrant_user.nearby
140 norm = create(:user, :active)
141 sec = create(:user, :active)
142 create(:friendship, :befriender => norm, :befriendee => sec)
144 assert_equal [sec], norm.friends
145 assert_equal 1, norm.friends.size
147 assert_equal [], sec.friends
148 assert_equal 0, sec.friends.size
151 def test_user_preferred_editor
153 assert_nil user.preferred_editor
154 user.preferred_editor = "id"
155 assert_equal "id", user.preferred_editor
158 user.preferred_editor = "invalid_editor"
159 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
163 pending = create(:user, :pending)
164 active = create(:user, :active)
165 confirmed = create(:user, :confirmed)
166 suspended = create(:user, :suspended)
167 deleted = create(:user, :deleted)
169 assert User.visible.find(pending.id)
170 assert User.visible.find(active.id)
171 assert User.visible.find(confirmed.id)
172 assert_raise ActiveRecord::RecordNotFound do
173 User.visible.find(suspended.id)
175 assert_raise ActiveRecord::RecordNotFound do
176 User.visible.find(deleted.id)
181 pending = create(:user, :pending)
182 active = create(:user, :active)
183 confirmed = create(:user, :confirmed)
184 suspended = create(:user, :suspended)
185 deleted = create(:user, :deleted)
187 assert User.active.find(active.id)
188 assert User.active.find(confirmed.id)
189 assert_raise ActiveRecord::RecordNotFound do
190 User.active.find(pending.id)
192 assert_raise ActiveRecord::RecordNotFound do
193 User.active.find(suspended.id)
195 assert_raise ActiveRecord::RecordNotFound do
196 User.active.find(deleted.id)
200 def test_identifiable
201 public_user = create(:user, :data_public => true)
202 private_user = create(:user, :data_public => false)
204 assert User.identifiable.find(public_user.id)
205 assert_raise ActiveRecord::RecordNotFound do
206 User.identifiable.find(private_user.id)
211 create(:language, :code => "en")
212 create(:language, :code => "de")
213 create(:language, :code => "sl")
215 user = create(:user, :languages => ["en"])
216 assert_equal ["en"], user.languages
217 user.languages = %w[de fr en]
218 assert_equal %w[de fr en], user.languages
219 user.languages = %w[fr de sl]
220 assert_equal "de", user.preferred_language
221 assert_equal %w[fr de sl], user.preferred_languages.map(&:to_s)
222 user = create(:user, :languages => %w[en de])
223 assert_equal %w[en de], user.languages
227 assert build(:user, :pending).visible?
228 assert build(:user, :active).visible?
229 assert build(:user, :confirmed).visible?
230 assert_not build(:user, :suspended).visible?
231 assert_not build(:user, :deleted).visible?
235 assert_not build(:user, :pending).active?
236 assert build(:user, :active).active?
237 assert build(:user, :confirmed).active?
238 assert_not build(:user, :suspended).active?
239 assert_not build(:user, :deleted).active?
243 assert_not create(:user).moderator?
244 assert create(:moderator_user).moderator?
247 def test_administrator?
248 assert_not create(:user).administrator?
249 assert create(:administrator_user).administrator?
253 assert_not create(:user).has_role?("administrator")
254 assert_not create(:user).has_role?("moderator")
255 assert create(:administrator_user).has_role?("administrator")
256 assert create(:moderator_user).has_role?("moderator")
260 user = create(:user, :with_home_location, :description => "foo")
262 assert_equal "user_#{user.id}", user.display_name
263 assert user.description.blank?
264 assert_nil user.home_lat
265 assert_nil user.home_lon
266 assert_not user.avatar.attached?
267 assert_equal "deleted", user.status
268 assert_not user.visible?
269 assert_not user.active?