1 # -*- coding: utf-8 -*-
4 class UserTest < ActiveSupport::TestCase
5 include Rails::Dom::Testing::Assertions::SelectorAssertions
7 def test_invalid_with_empty_attributes
10 assert user.errors[:email].any?
11 assert user.errors[:pass_crypt].any?
12 assert user.errors[:display_name].any?
13 assert user.errors[:email].any?
14 assert !user.errors[:home_lat].any?
15 assert !user.errors[:home_lon].any?
16 assert !user.errors[:home_zoom].any?
20 existing_user = create(:user)
22 :email => existing_user.email,
24 :pass_crypt => Digest::MD5.hexdigest("test"),
25 :display_name => "new user",
27 :description => "desc"
30 assert new_user.errors[:email].include?("has already been taken")
33 def test_unique_display_name
34 existing_user = create(:user)
36 :email => "tester@openstreetmap.org",
38 :pass_crypt => Digest::MD5.hexdigest("test"),
39 :display_name => existing_user.display_name,
41 :description => "desc"
44 assert new_user.errors[:display_name].include?("has already been taken")
48 ok = %w(a@s.com test@shaunmcdonald.me.uk hello_local@ping-d.ng
49 test_local@openstreetmap.org test-local@example.com)
50 bad = %w(hi ht@ n@ @.com help@.me.uk help"hi.me.uk も対@応します
51 輕觸搖晃的遊戲@ah.com も対応します@s.name)
56 assert user.valid?(:save), user.errors.full_messages.join(",")
62 assert user.invalid?(:save), "#{name} is valid when it shouldn't be"
66 def test_display_name_length
68 user.display_name = "123"
69 assert user.valid?, " should allow nil display name"
70 user.display_name = "12"
71 assert !user.valid?, "should not allow 2 char name"
72 user.display_name = ""
74 user.display_name = nil
75 # Don't understand why it isn't allowing a nil value,
76 # when the validates statements specifically allow it
77 # It appears the database does not allow null values
81 def test_display_name_valid
82 # Due to sanitisation in the view some of these that you might not
84 # However, would they affect the xml planet dumps?
85 ok = ["Name", "'me", "he\"", "<hr>", "*ho", "\"help\"@",
86 "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲"]
87 # These need to be 3 chars in length, otherwise the length test above
89 bad = ["<hr/>", "test@example.com", "s/f", "aa/", "aa;", "aa.",
90 "aa,", "aa?", "/;.,?", "も対応します/", "#ping",
91 "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar",
92 "new", "terms", "save", "confirm", "confirm-email",
93 "go_public", "reset-password", "forgot-password", "suspended"]
94 ok.each do |display_name|
96 user.display_name = display_name
97 assert user.valid?, "#{display_name} is invalid, when it should be"
100 bad.each do |display_name|
102 user.display_name = display_name
103 assert !user.valid?, "#{display_name} is valid when it shouldn't be"
107 def test_friends_with
108 alice = create(:user, :active)
109 bob = create(:user, :active)
110 charlie = create(:user, :active)
111 create(:friend, :befriender => alice, :befriendee => bob)
113 assert alice.is_friends_with?(bob)
114 assert !alice.is_friends_with?(charlie)
115 assert !bob.is_friends_with?(alice)
116 assert !bob.is_friends_with?(charlie)
117 assert !charlie.is_friends_with?(bob)
118 assert !charlie.is_friends_with?(alice)
121 def test_users_nearby
122 alice = create(:user, :active, :home_lat => 51.0, :home_lon => 1.0, :data_public => false)
123 bob = create(:user, :active, :home_lat => 51.1, :home_lon => 1.0, :data_public => true)
124 charlie = create(:user, :active, :home_lat => 51.1, :home_lon => 1.1, :data_public => true)
125 david = create(:user, :active, :home_lat => 10.0, :home_lon => -123.0, :data_public => true)
126 _edward = create(:user, :suspended, :home_lat => 10.0, :home_lon => -123.0, :data_public => true)
127 south_pole_user = create(:user, :active, :home_lat => -90.0, :home_lon => 0.0, :data_public => true)
128 vagrant_user = create(:user, :active, :home_lat => nil, :home_lon => nil, :data_public => true)
130 # bob and charlie are both near alice
131 assert_equal [bob, charlie], alice.nearby
132 # charlie and alice are both near bob, but alice has their data private
133 assert_equal [charlie], bob.nearby
134 # david has no user nearby, since edward is not active
135 assert_equal [], david.nearby
136 # south_pole_user has no user nearby, and doesn't throw exception
137 assert_equal [], south_pole_user.nearby
138 # vagrant_user has no home location
139 assert_equal [], vagrant_user.nearby
142 def test_friend_users
143 norm = create(:user, :active)
144 sec = create(:user, :active)
145 create(:friend, :befriender => norm, :befriendee => sec)
147 assert_equal [sec], norm.friend_users
148 assert_equal 1, norm.friend_users.size
150 assert_equal [], sec.friend_users
151 assert_equal 0, sec.friend_users.size
154 def test_user_preferred_editor
156 assert_nil user.preferred_editor
157 user.preferred_editor = "potlatch"
158 assert_equal "potlatch", user.preferred_editor
161 user.preferred_editor = "invalid_editor"
162 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
166 pending = create(:user, :pending)
167 active = create(:user, :active)
168 confirmed = create(:user, :confirmed)
169 suspended = create(:user, :suspended)
170 deleted = create(:user, :deleted)
172 assert User.visible.find(pending.id)
173 assert User.visible.find(active.id)
174 assert User.visible.find(confirmed.id)
175 assert_raise ActiveRecord::RecordNotFound do
176 User.visible.find(suspended.id)
178 assert_raise ActiveRecord::RecordNotFound do
179 User.visible.find(deleted.id)
184 pending = create(:user, :pending)
185 active = create(:user, :active)
186 confirmed = create(:user, :confirmed)
187 suspended = create(:user, :suspended)
188 deleted = create(:user, :deleted)
190 assert User.active.find(active.id)
191 assert User.active.find(confirmed.id)
192 assert_raise ActiveRecord::RecordNotFound do
193 User.active.find(pending.id)
195 assert_raise ActiveRecord::RecordNotFound do
196 User.active.find(suspended.id)
198 assert_raise ActiveRecord::RecordNotFound do
199 User.active.find(deleted.id)
203 def test_identifiable
204 public_user = create(:user, :data_public => true)
205 private_user = create(:user, :data_public => false)
207 assert User.identifiable.find(public_user.id)
208 assert_raise ActiveRecord::RecordNotFound do
209 User.identifiable.find(private_user.id)
214 create(:language, :code => "en")
215 create(:language, :code => "de")
216 create(:language, :code => "sl")
218 user = create(:user, :languages => ["en"])
219 assert_equal ["en"], user.languages
220 user.languages = %w(de fr en)
221 assert_equal %w(de fr en), user.languages
222 user.languages = %w(fr de sl)
223 assert_equal "de", user.preferred_language
224 assert_equal %w(fr de sl), user.preferred_languages.map(&:to_s)
225 user = create(:user, :languages => %w(en de))
226 assert_equal %w(en de), user.languages
230 assert_equal true, build(:user, :pending).visible?
231 assert_equal true, build(:user, :active).visible?
232 assert_equal true, build(:user, :confirmed).visible?
233 assert_equal false, build(:user, :suspended).visible?
234 assert_equal false, build(:user, :deleted).visible?
238 assert_equal false, build(:user, :pending).active?
239 assert_equal true, build(:user, :active).active?
240 assert_equal true, build(:user, :confirmed).active?
241 assert_equal false, build(:user, :suspended).active?
242 assert_equal false, build(:user, :deleted).active?
246 assert_equal false, create(:user).moderator?
247 assert_equal true, create(:moderator_user).moderator?
250 def test_administrator?
251 assert_equal false, create(:user).administrator?
252 assert_equal true, create(:administrator_user).administrator?
256 assert_equal false, create(:user).has_role?("administrator")
257 assert_equal false, create(:user).has_role?("moderator")
258 assert_equal true, create(:administrator_user).has_role?("administrator")
259 assert_equal true, create(:moderator_user).has_role?("moderator")
263 user = create(:user, :with_home_location, :description => "foo")
265 assert_equal "user_#{user.id}", user.display_name
266 assert user.description.blank?
267 assert_nil user.home_lat
268 assert_nil user.home_lon
269 assert_equal false, user.image.file?
270 assert_equal "deleted", user.status
271 assert_equal false, user.visible?
272 assert_equal false, user.active?
276 user = build(:user, :with_home_location)
278 assert_select Nokogiri::XML::Document.parse(xml.to_s), "user" do
279 assert_select "[display_name=?]", user.display_name
280 assert_select "[account_created=?]", user.creation_time.xmlschema
281 assert_select "home[lat=?][lon=?][zoom=?]", user.home_lat.to_s, user.home_lon.to_s, user.home_zoom.to_s
286 user = build(:user, :with_home_location)
287 xml = user.to_xml_node
288 assert_select Nokogiri::XML::DocumentFragment.parse(xml.to_s), "user" do
289 assert_select "[display_name=?]", user.display_name
290 assert_select "[account_created=?]", user.creation_time.xmlschema
291 assert_select "home[lat=?][lon=?][zoom=?]", user.home_lat.to_s, user.home_lon.to_s, user.home_zoom.to_s