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 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 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"
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]
45 assert user.valid?(:save), "#{name} isn't valid when it should be"
51 assert user.invalid?(:save), "#{name} is valid when it shouldn't be"
55 def test_display_name_length
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"
67 def test_display_name_valid
68 # Due to sanitisation in the view some of these that you might not
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
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|
83 user.display_name = display_name
84 assert_predicate user, :valid?, "#{display_name} is invalid, when it should be"
87 bad.each do |display_name|
89 user.display_name = display_name
90 assert_not user.valid?, "#{display_name} is valid when it shouldn't be"
95 alice = create(:user, :active)
96 bob = create(:user, :active)
97 charlie = create(:user, :active)
98 create(:friendship, :befriender => alice, :befriendee => bob)
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)
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)
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
130 norm = create(:user, :active)
131 sec = create(:user, :active)
132 create(:friendship, :befriender => norm, :befriendee => sec)
134 assert_equal [sec], norm.friends
135 assert_equal 1, norm.friends.size
137 assert_empty sec.friends
138 assert_equal 0, sec.friends.size
141 def test_user_preferred_editor
143 assert_nil user.preferred_editor
144 user.preferred_editor = "id"
145 assert_equal "id", user.preferred_editor
148 user.preferred_editor = "invalid_editor"
149 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
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)
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)
165 assert_raise ActiveRecord::RecordNotFound do
166 User.visible.find(deleted.id)
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)
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)
182 assert_raise ActiveRecord::RecordNotFound do
183 User.active.find(suspended.id)
185 assert_raise ActiveRecord::RecordNotFound do
186 User.active.find(deleted.id)
190 def test_identifiable
191 public_user = create(:user, :data_public => true)
192 private_user = create(:user, :data_public => false)
194 assert User.identifiable.find(public_user.id)
195 assert_raise ActiveRecord::RecordNotFound do
196 User.identifiable.find(private_user.id)
201 create(:language, :code => "en")
202 create(:language, :code => "de")
203 create(:language, :code => "sl")
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
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?
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?
233 assert_not create(:user).moderator?
234 assert_predicate create(:moderator_user), :moderator?
237 def test_administrator?
238 assert_not create(:user).administrator?
239 assert_predicate create(:administrator_user), :administrator?
243 assert_not create(:user).has_role?("administrator")
244 assert_not create(:user).has_role?("moderator")
245 assert create(:administrator_user).has_role?("administrator")
246 assert create(:moderator_user).has_role?("moderator")
249 def test_soft_destroy
250 user = create(:user, :with_home_location, :description => "foo")
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?