1 # -*- coding: utf-8 -*-
4 class UserTest < ActiveSupport::TestCase
5 include Rails::Dom::Testing::Assertions::SelectorAssertions
8 fixtures :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 create(:friend, :befriender => users(:normal_user), :befriendee => users(:public_user))
110 assert users(:normal_user).is_friends_with?(users(:public_user))
111 assert !users(:normal_user).is_friends_with?(users(:inactive_user))
112 assert !users(:public_user).is_friends_with?(users(:normal_user))
113 assert !users(:public_user).is_friends_with?(users(:inactive_user))
114 assert !users(:inactive_user).is_friends_with?(users(:normal_user))
115 assert !users(:inactive_user).is_friends_with?(users(:public_user))
118 def test_users_nearby
119 # second user has their data public and is close by normal user
120 assert_equal [users(:public_user), users(:german_user)], users(:normal_user).nearby
121 # second_user has normal user nearby, but normal user has their data private
122 assert_equal [users(:german_user)], users(:public_user).nearby
123 # inactive_user has no user nearby
124 assert_equal [], users(:inactive_user).nearby
125 # north_pole_user has no user nearby, and doesn't throw exception
126 assert_equal [], users(:north_pole_user).nearby
127 # confirmed_user has no home location
128 assert_equal [], users(:confirmed_user).nearby
131 def test_friends_with
132 # normal user is a friend of second user
133 # it should be a one way friend associatation
134 norm = users(:normal_user)
135 sec = users(:public_user)
136 create(:friend, :befriender => norm, :befriendee => sec)
137 assert_equal 1, Friend.count
138 assert_equal [sec], norm.friend_users
139 assert_equal 1, norm.friend_users.size
140 assert_equal 1, Friend.count
141 assert norm.is_friends_with?(sec)
142 assert !sec.is_friends_with?(norm)
143 assert !users(:normal_user).is_friends_with?(users(:inactive_user))
144 assert !users(:public_user).is_friends_with?(users(:normal_user))
145 assert !users(:public_user).is_friends_with?(users(:inactive_user))
146 assert !users(:inactive_user).is_friends_with?(users(:normal_user))
147 assert !users(:inactive_user).is_friends_with?(users(:public_user))
148 # Friend.delete(friend)
149 # assert_equal 0, Friend.count
152 def test_user_preferred_editor
153 user = users(:normal_user)
154 assert_equal nil, user.preferred_editor
155 user.preferred_editor = "potlatch"
156 assert_equal "potlatch", user.preferred_editor
159 user.preferred_editor = "invalid_editor"
160 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
164 assert_equal 22, User.visible.count
165 assert_raise ActiveRecord::RecordNotFound do
166 User.visible.find(users(:suspended_user).id)
168 assert_raise ActiveRecord::RecordNotFound do
169 User.visible.find(users(:deleted_user).id)
174 assert_equal 21, User.active.count
175 assert_raise ActiveRecord::RecordNotFound do
176 User.active.find(users(:inactive_user).id)
178 assert_raise ActiveRecord::RecordNotFound do
179 User.active.find(users(:suspended_user).id)
181 assert_raise ActiveRecord::RecordNotFound do
182 User.active.find(users(:deleted_user).id)
186 def test_identifiable
187 assert_equal 23, User.identifiable.count
188 assert_raise ActiveRecord::RecordNotFound do
189 User.identifiable.find(users(:normal_user).id)
194 user = users(:normal_user)
195 assert_equal ["en"], user.languages
196 user.languages = %w(de fr en)
197 assert_equal %w(de fr en), user.languages
198 user.languages = %w(fr de sl)
199 assert_equal "de", user.preferred_language
200 assert_equal %w(fr de sl), user.preferred_languages.map(&:to_s)
201 user = users(:public_user)
202 assert_equal %w(en de), user.languages
206 assert_equal true, users(:inactive_user).visible?
207 assert_equal true, users(:normal_user).visible?
208 assert_equal true, users(:confirmed_user).visible?
209 assert_equal false, users(:suspended_user).visible?
210 assert_equal false, users(:deleted_user).visible?
214 assert_equal false, users(:inactive_user).active?
215 assert_equal true, users(:normal_user).active?
216 assert_equal true, users(:confirmed_user).active?
217 assert_equal false, users(:suspended_user).active?
218 assert_equal false, users(:deleted_user).active?
222 assert_equal false, users(:normal_user).moderator?
223 assert_equal true, users(:moderator_user).moderator?
226 def test_administrator?
227 assert_equal false, users(:normal_user).administrator?
228 assert_equal true, users(:administrator_user).administrator?
232 assert_equal false, users(:normal_user).has_role?("administrator")
233 assert_equal false, users(:normal_user).has_role?("moderator")
234 assert_equal true, users(:administrator_user).has_role?("administrator")
235 assert_equal true, users(:moderator_user).has_role?("moderator")
239 user = users(:normal_user)
241 assert_equal "user_#{user.id}", user.display_name
242 assert user.description.blank?
243 assert_equal nil, user.home_lat
244 assert_equal nil, user.home_lon
245 assert_equal false, user.image.file?
246 assert_equal "deleted", user.status
247 assert_equal false, user.visible?
248 assert_equal false, user.active?
252 user = users(:normal_user)
254 assert_select Nokogiri::XML::Document.parse(xml.to_s), "user" do
255 assert_select "[display_name=?]", user.display_name
256 assert_select "[account_created=?]", user.creation_time.xmlschema
257 assert_select "home[lat=?][lon=?][zoom=?]", user.home_lat.to_s, user.home_lon.to_s, user.home_zoom.to_s
262 user = users(:normal_user)
263 xml = user.to_xml_node
264 assert_select Nokogiri::XML::DocumentFragment.parse(xml.to_s), "user" do
265 assert_select "[display_name=?]", user.display_name
266 assert_select "[account_created=?]", user.creation_time.xmlschema
267 assert_select "home[lat=?][lon=?][zoom=?]", user.home_lat.to_s, user.home_lon.to_s, user.home_zoom.to_s