1 # -*- coding: utf-8 -*-
4 class UserTest < ActiveSupport::TestCase
5 include Rails::Dom::Testing::Assertions::SelectorAssertions
8 fixtures :friends, :languages, :user_roles
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?
24 :email => users(:normal_user).email,
26 :pass_crypt => Digest::MD5.hexdigest("test"),
27 :display_name => "new user",
29 :description => "desc"
32 assert new_user.errors[:email].include?("has already been taken")
35 def test_unique_display_name
37 :email => "tester@openstreetmap.org",
39 :pass_crypt => Digest::MD5.hexdigest("test"),
40 :display_name => users(:normal_user).display_name,
42 :description => "desc"
45 assert new_user.errors[:display_name].include?("has already been taken")
49 ok = %w(a@s.com test@shaunmcdonald.me.uk hello_local@ping-d.ng
50 test_local@openstreetmap.org test-local@example.com)
51 bad = %w(hi ht@ n@ @.com help@.me.uk help"hi.me.uk も対@応します
52 輕觸搖晃的遊戲@ah.com も対応します@s.name)
55 user = users(:normal_user)
57 assert user.valid?(:save), user.errors.full_messages.join(",")
61 user = users(:normal_user)
63 assert user.invalid?(:save), "#{name} is valid when it shouldn't be"
67 def test_display_name_length
68 user = users(:normal_user)
69 user.display_name = "123"
70 assert user.valid?, " should allow nil display name"
71 user.display_name = "12"
72 assert !user.valid?, "should not allow 2 char name"
73 user.display_name = ""
75 user.display_name = nil
76 # Don't understand why it isn't allowing a nil value,
77 # when the validates statements specifically allow it
78 # It appears the database does not allow null values
82 def test_display_name_valid
83 # Due to sanitisation in the view some of these that you might not
85 # However, would they affect the xml planet dumps?
86 ok = ["Name", "'me", "he\"", "<hr>", "*ho", "\"help\"@",
87 "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲"]
88 # These need to be 3 chars in length, otherwise the length test above
90 bad = ["<hr/>", "test@example.com", "s/f", "aa/", "aa;", "aa.",
91 "aa,", "aa?", "/;.,?", "も対応します/", "#ping",
92 "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar",
93 "new", "terms", "save", "confirm", "confirm-email",
94 "go_public", "reset-password", "forgot-password", "suspended"]
95 ok.each do |display_name|
96 user = users(:normal_user)
97 user.display_name = display_name
98 assert user.valid?, "#{display_name} is invalid, when it should be"
101 bad.each do |display_name|
102 user = users(:normal_user)
103 user.display_name = display_name
104 assert !user.valid?, "#{display_name} is valid when it shouldn't be"
109 assert users(:normal_user).is_friends_with?(users(:public_user))
110 assert !users(:normal_user).is_friends_with?(users(:inactive_user))
111 assert !users(:public_user).is_friends_with?(users(:normal_user))
112 assert !users(:public_user).is_friends_with?(users(:inactive_user))
113 assert !users(:inactive_user).is_friends_with?(users(:normal_user))
114 assert !users(:inactive_user).is_friends_with?(users(:public_user))
117 def test_users_nearby
118 # second user has their data public and is close by normal user
119 assert_equal [users(:public_user), users(:german_user)], users(:normal_user).nearby
120 # second_user has normal user nearby, but normal user has their data private
121 assert_equal [users(:german_user)], users(:public_user).nearby
122 # inactive_user has no user nearby
123 assert_equal [], users(:inactive_user).nearby
124 # north_pole_user has no user nearby, and doesn't throw exception
125 assert_equal [], users(:north_pole_user).nearby
126 # confirmed_user has no home location
127 assert_equal [], users(:confirmed_user).nearby
130 def test_friends_with
131 # normal user is a friend of second user
132 # it should be a one way friend accossitation
133 assert_equal 1, Friend.count
134 norm = users(:normal_user)
135 sec = users(:public_user)
136 # friend = Friend.new
137 # friend.befriender = norm
138 # friend.befriendee = sec
140 assert_equal [sec], norm.friend_users
141 assert_equal 1, norm.friend_users.size
142 assert_equal 1, Friend.count
143 assert norm.is_friends_with?(sec)
144 assert !sec.is_friends_with?(norm)
145 assert !users(:normal_user).is_friends_with?(users(:inactive_user))
146 assert !users(:public_user).is_friends_with?(users(:normal_user))
147 assert !users(:public_user).is_friends_with?(users(:inactive_user))
148 assert !users(:inactive_user).is_friends_with?(users(:normal_user))
149 assert !users(:inactive_user).is_friends_with?(users(:public_user))
150 # Friend.delete(friend)
151 # assert_equal 0, Friend.count
154 def test_user_preferred_editor
155 user = users(:normal_user)
156 assert_equal 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 assert_equal 17, User.visible.count
167 assert_raise ActiveRecord::RecordNotFound do
168 User.visible.find(users(:suspended_user).id)
170 assert_raise ActiveRecord::RecordNotFound do
171 User.visible.find(users(:deleted_user).id)
176 assert_equal 16, User.active.count
177 assert_raise ActiveRecord::RecordNotFound do
178 User.active.find(users(:inactive_user).id)
180 assert_raise ActiveRecord::RecordNotFound do
181 User.active.find(users(:suspended_user).id)
183 assert_raise ActiveRecord::RecordNotFound do
184 User.active.find(users(:deleted_user).id)
188 def test_identifiable
189 assert_equal 18, User.identifiable.count
190 assert_raise ActiveRecord::RecordNotFound do
191 User.identifiable.find(users(:normal_user).id)
196 user = users(:normal_user)
197 assert_equal ["en"], user.languages
198 user.languages = %w(de fr en)
199 assert_equal %w(de fr en), user.languages
200 user.languages = %w(fr de sl)
201 assert_equal "de", user.preferred_language
202 assert_equal "de", user.preferred_language_from(%w(en sl de es))
203 user = users(:public_user)
204 assert_equal %w(en de), user.languages
208 assert_equal true, users(:inactive_user).visible?
209 assert_equal true, users(:normal_user).visible?
210 assert_equal true, users(:confirmed_user).visible?
211 assert_equal false, users(:suspended_user).visible?
212 assert_equal false, users(:deleted_user).visible?
216 assert_equal false, users(:inactive_user).active?
217 assert_equal true, users(:normal_user).active?
218 assert_equal true, users(:confirmed_user).active?
219 assert_equal false, users(:suspended_user).active?
220 assert_equal false, users(:deleted_user).active?
224 assert_equal false, users(:normal_user).moderator?
225 assert_equal true, users(:moderator_user).moderator?
228 def test_administrator?
229 assert_equal false, users(:normal_user).administrator?
230 assert_equal true, users(:administrator_user).administrator?
234 assert_equal false, users(:normal_user).has_role?("administrator")
235 assert_equal false, users(:normal_user).has_role?("moderator")
236 assert_equal true, users(:administrator_user).has_role?("administrator")
237 assert_equal true, users(:moderator_user).has_role?("moderator")
241 user = users(:normal_user)
243 assert_equal "user_#{user.id}", user.display_name
244 assert user.description.blank?
245 assert_equal nil, user.home_lat
246 assert_equal nil, user.home_lon
247 assert_equal false, user.image.file?
248 assert_equal "deleted", user.status
249 assert_equal false, user.visible?
250 assert_equal false, user.active?
254 user = users(:normal_user)
256 assert_select Nokogiri::XML::Document.parse(xml.to_s), "user" do
257 assert_select "[display_name=?]", user.display_name
258 assert_select "[account_created=?]", user.creation_time.xmlschema
259 assert_select "home[lat=?][lon=?][zoom=?]", user.home_lat.to_s, user.home_lon.to_s, user.home_zoom.to_s
264 user = users(:normal_user)
265 xml = user.to_xml_node
266 assert_select Nokogiri::XML::DocumentFragment.parse(xml.to_s), "user" do
267 assert_select "[display_name=?]", user.display_name
268 assert_select "[account_created=?]", user.creation_time.xmlschema
269 assert_select "home[lat=?][lon=?][zoom=?]", user.home_lat.to_s, user.home_lon.to_s, user.home_zoom.to_s