1 # -*- coding: utf-8 -*-
4 class UserTest < ActiveSupport::TestCase
5 include Rails::Dom::Testing::Assertions::SelectorAssertions
10 def test_invalid_with_empty_attributes
13 assert user.errors[:email].any?
14 assert user.errors[:pass_crypt].any?
15 assert user.errors[:display_name].any?
16 assert user.errors[:email].any?
17 assert !user.errors[:home_lat].any?
18 assert !user.errors[:home_lon].any?
19 assert !user.errors[:home_zoom].any?
23 existing_user = create(:user)
25 :email => existing_user.email,
27 :pass_crypt => Digest::MD5.hexdigest("test"),
28 :display_name => "new user",
30 :description => "desc"
33 assert new_user.errors[:email].include?("has already been taken")
36 def test_unique_display_name
37 existing_user = create(:user)
39 :email => "tester@openstreetmap.org",
41 :pass_crypt => Digest::MD5.hexdigest("test"),
42 :display_name => existing_user.display_name,
44 :description => "desc"
47 assert new_user.errors[:display_name].include?("has already been taken")
51 ok = %w(a@s.com test@shaunmcdonald.me.uk hello_local@ping-d.ng
52 test_local@openstreetmap.org test-local@example.com)
53 bad = %w(hi ht@ n@ @.com help@.me.uk help"hi.me.uk も対@応します
54 輕觸搖晃的遊戲@ah.com も対応します@s.name)
59 assert user.valid?(:save), user.errors.full_messages.join(",")
65 assert user.invalid?(:save), "#{name} is valid when it shouldn't be"
69 def test_display_name_length
71 user.display_name = "123"
72 assert user.valid?, " should allow nil display name"
73 user.display_name = "12"
74 assert !user.valid?, "should not allow 2 char name"
75 user.display_name = ""
77 user.display_name = nil
78 # Don't understand why it isn't allowing a nil value,
79 # when the validates statements specifically allow it
80 # It appears the database does not allow null values
84 def test_display_name_valid
85 # Due to sanitisation in the view some of these that you might not
87 # However, would they affect the xml planet dumps?
88 ok = ["Name", "'me", "he\"", "<hr>", "*ho", "\"help\"@",
89 "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲"]
90 # These need to be 3 chars in length, otherwise the length test above
92 bad = ["<hr/>", "test@example.com", "s/f", "aa/", "aa;", "aa.",
93 "aa,", "aa?", "/;.,?", "も対応します/", "#ping",
94 "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar",
95 "new", "terms", "save", "confirm", "confirm-email",
96 "go_public", "reset-password", "forgot-password", "suspended"]
97 ok.each do |display_name|
99 user.display_name = display_name
100 assert user.valid?, "#{display_name} is invalid, when it should be"
103 bad.each do |display_name|
105 user.display_name = display_name
106 assert !user.valid?, "#{display_name} is valid when it shouldn't be"
110 def test_friends_with
111 alice = create(:user, :active)
112 bob = create(:user, :active)
113 charlie = create(:user, :active)
114 create(:friend, :befriender => alice, :befriendee => bob)
116 assert alice.is_friends_with?(bob)
117 assert !alice.is_friends_with?(charlie)
118 assert !bob.is_friends_with?(alice)
119 assert !bob.is_friends_with?(charlie)
120 assert !charlie.is_friends_with?(bob)
121 assert !charlie.is_friends_with?(alice)
124 def test_users_nearby
125 alice = create(:user, :active, :home_lat => 51.0, :home_lon => 1.0, :data_public => false)
126 bob = create(:user, :active, :home_lat => 51.1, :home_lon => 1.0, :data_public => true)
127 charlie = create(:user, :active, :home_lat => 51.1, :home_lon => 1.1, :data_public => true)
128 david = create(:user, :active, :home_lat => 10.0, :home_lon => -123.0, :data_public => true)
129 _edward = create(:user, :suspended, :home_lat => 10.0, :home_lon => -123.0, :data_public => true)
130 south_pole_user = create(:user, :active, :home_lat => -90.0, :home_lon => 0.0, :data_public => true)
131 vagrant_user = create(:user, :active, :home_lat => nil, :home_lon => nil, :data_public => true)
133 # bob and charlie are both near alice
134 assert_equal [bob, charlie], alice.nearby
135 # charlie and alice are both near bob, but alice has their data private
136 assert_equal [charlie], bob.nearby
137 # david has no user nearby, since edward is not active
138 assert_equal [], david.nearby
139 # south_pole_user has no user nearby, and doesn't throw exception
140 assert_equal [], south_pole_user.nearby
141 # vagrant_user has no home location
142 assert_equal [], vagrant_user.nearby
145 def test_friend_users
146 norm = create(:user, :active)
147 sec = create(:user, :active)
148 create(:friend, :befriender => norm, :befriendee => sec)
150 assert_equal [sec], norm.friend_users
151 assert_equal 1, norm.friend_users.size
153 assert_equal [], sec.friend_users
154 assert_equal 0, sec.friend_users.size
157 def test_user_preferred_editor
159 assert_nil user.preferred_editor
160 user.preferred_editor = "potlatch"
161 assert_equal "potlatch", user.preferred_editor
164 user.preferred_editor = "invalid_editor"
165 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
169 pending = create(:user, :pending)
170 active = create(:user, :active)
171 confirmed = create(:user, :confirmed)
172 suspended = create(:user, :suspended)
173 deleted = create(:user, :deleted)
175 assert User.visible.find(pending.id)
176 assert User.visible.find(active.id)
177 assert User.visible.find(confirmed.id)
178 assert_raise ActiveRecord::RecordNotFound do
179 User.visible.find(suspended.id)
181 assert_raise ActiveRecord::RecordNotFound do
182 User.visible.find(deleted.id)
187 pending = create(:user, :pending)
188 active = create(:user, :active)
189 confirmed = create(:user, :confirmed)
190 suspended = create(:user, :suspended)
191 deleted = create(:user, :deleted)
193 assert User.active.find(active.id)
194 assert User.active.find(confirmed.id)
195 assert_raise ActiveRecord::RecordNotFound do
196 User.active.find(pending.id)
198 assert_raise ActiveRecord::RecordNotFound do
199 User.active.find(suspended.id)
201 assert_raise ActiveRecord::RecordNotFound do
202 User.active.find(deleted.id)
206 def test_identifiable
207 public_user = create(:user, :data_public => true)
208 private_user = create(:user, :data_public => false)
210 assert User.identifiable.find(public_user.id)
211 assert_raise ActiveRecord::RecordNotFound do
212 User.identifiable.find(private_user.id)
217 create(:language, :code => "en")
218 create(:language, :code => "de")
219 create(:language, :code => "sl")
221 user = create(:user, :languages => ["en"])
222 assert_equal ["en"], user.languages
223 user.languages = %w(de fr en)
224 assert_equal %w(de fr en), user.languages
225 user.languages = %w(fr de sl)
226 assert_equal "de", user.preferred_language
227 assert_equal %w(fr de sl), user.preferred_languages.map(&:to_s)
228 user = create(:user, :languages => %w(en de))
229 assert_equal %w(en de), user.languages
233 assert_equal true, build(:user, :pending).visible?
234 assert_equal true, build(:user, :active).visible?
235 assert_equal true, build(:user, :confirmed).visible?
236 assert_equal false, build(:user, :suspended).visible?
237 assert_equal false, build(:user, :deleted).visible?
241 assert_equal false, build(:user, :pending).active?
242 assert_equal true, build(:user, :active).active?
243 assert_equal true, build(:user, :confirmed).active?
244 assert_equal false, build(:user, :suspended).active?
245 assert_equal false, build(:user, :deleted).active?
249 assert_equal false, create(:user).moderator?
250 assert_equal true, create(:moderator_user).moderator?
253 def test_administrator?
254 assert_equal false, create(:user).administrator?
255 assert_equal true, create(:administrator_user).administrator?
259 assert_equal false, create(:user).has_role?("administrator")
260 assert_equal false, create(:user).has_role?("moderator")
261 assert_equal true, create(:administrator_user).has_role?("administrator")
262 assert_equal true, create(:moderator_user).has_role?("moderator")
266 user = create(:user, :with_home_location, :description => "foo")
268 assert_equal "user_#{user.id}", user.display_name
269 assert user.description.blank?
270 assert_nil user.home_lat
271 assert_nil user.home_lon
272 assert_equal false, user.image.file?
273 assert_equal "deleted", user.status
274 assert_equal false, user.visible?
275 assert_equal false, user.active?
279 user = build(:user, :with_home_location)
281 assert_select Nokogiri::XML::Document.parse(xml.to_s), "user" do
282 assert_select "[display_name=?]", user.display_name
283 assert_select "[account_created=?]", user.creation_time.xmlschema
284 assert_select "home[lat=?][lon=?][zoom=?]", user.home_lat.to_s, user.home_lon.to_s, user.home_zoom.to_s
289 user = build(:user, :with_home_location)
290 xml = user.to_xml_node
291 assert_select Nokogiri::XML::DocumentFragment.parse(xml.to_s), "user" do
292 assert_select "[display_name=?]", user.display_name
293 assert_select "[account_created=?]", user.creation_time.xmlschema
294 assert_select "home[lat=?][lon=?][zoom=?]", user.home_lat.to_s, user.home_lon.to_s, user.home_zoom.to_s