3 class UserTest < ActiveSupport::TestCase
4 include Rails::Dom::Testing::Assertions::SelectorAssertions
6 def test_invalid_with_empty_attributes
9 assert user.errors[:email].any?
10 assert user.errors[:pass_crypt].any?
11 assert user.errors[:display_name].any?
12 assert user.errors[:email].any?
13 assert user.errors[:home_lat].none?
14 assert user.errors[:home_lon].none?
15 assert user.errors[:home_zoom].none?
19 existing_user = create(:user)
21 :email => existing_user.email,
23 :pass_crypt => Digest::MD5.hexdigest("test"),
24 :display_name => "new user",
26 :description => "desc"
28 assert_not new_user.save
29 assert new_user.errors[:email].include?("has already been taken")
32 def test_unique_display_name
33 existing_user = create(:user)
35 :email => "tester@openstreetmap.org",
37 :pass_crypt => Digest::MD5.hexdigest("test"),
38 :display_name => existing_user.display_name,
40 :description => "desc"
42 assert_not new_user.save
43 assert new_user.errors[:display_name].include?("has already been taken")
47 ok = %w[a@s.com test@shaunmcdonald.me.uk hello_local@ping-d.ng
48 test_local@openstreetmap.org test-local@example.com]
49 bad = %w[hi ht@ n@ @.com help@.me.uk help"hi.me.uk も対@応します
50 輕觸搖晃的遊戲@ah.com も対応します@s.name]
55 assert user.valid?(:save), user.errors.full_messages.join(",")
61 assert user.invalid?(:save), "#{name} is valid when it shouldn't be"
65 def test_display_name_length
67 user.display_name = "123"
68 assert user.valid?, " should allow nil display name"
69 user.display_name = "12"
70 assert_not user.valid?, "should not allow 2 char name"
71 user.display_name = ""
72 assert_not user.valid?
73 user.display_name = nil
74 # Don't understand why it isn't allowing a nil value,
75 # when the validates statements specifically allow it
76 # It appears the database does not allow null values
77 assert_not user.valid?
80 def test_display_name_valid
81 # Due to sanitisation in the view some of these that you might not
83 # However, would they affect the xml planet dumps?
84 ok = ["Name", "'me", "he\"", "<hr>", "*ho", "\"help\"@",
85 "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲"]
86 # These need to be 3 chars in length, otherwise the length test above
88 bad = ["<hr/>", "test@example.com", "s/f", "aa/", "aa;", "aa.",
89 "aa,", "aa?", "/;.,?", "も対応します/", "#ping",
90 "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar",
91 "new", "terms", "save", "confirm", "confirm-email",
92 "go_public", "reset-password", "forgot-password", "suspended"]
93 ok.each do |display_name|
95 user.display_name = display_name
96 assert user.valid?, "#{display_name} is invalid, when it should be"
99 bad.each do |display_name|
101 user.display_name = display_name
102 assert_not user.valid?, "#{display_name} is valid when it shouldn't be"
106 def test_friends_with
107 alice = create(:user, :active)
108 bob = create(:user, :active)
109 charlie = create(:user, :active)
110 create(:friend, :befriender => alice, :befriendee => bob)
112 assert alice.is_friends_with?(bob)
113 assert_not alice.is_friends_with?(charlie)
114 assert_not bob.is_friends_with?(alice)
115 assert_not bob.is_friends_with?(charlie)
116 assert_not charlie.is_friends_with?(bob)
117 assert_not charlie.is_friends_with?(alice)
120 def test_users_nearby
121 alice = create(:user, :active, :home_lat => 51.0, :home_lon => 1.0, :data_public => false)
122 bob = create(:user, :active, :home_lat => 51.1, :home_lon => 1.0, :data_public => true)
123 charlie = create(:user, :active, :home_lat => 51.1, :home_lon => 1.1, :data_public => true)
124 david = create(:user, :active, :home_lat => 10.0, :home_lon => -123.0, :data_public => true)
125 _edward = create(:user, :suspended, :home_lat => 10.0, :home_lon => -123.0, :data_public => true)
126 south_pole_user = create(:user, :active, :home_lat => -90.0, :home_lon => 0.0, :data_public => true)
127 vagrant_user = create(:user, :active, :home_lat => nil, :home_lon => nil, :data_public => true)
129 # bob and charlie are both near alice
130 assert_equal [bob, charlie], alice.nearby
131 # charlie and alice are both near bob, but alice has their data private
132 assert_equal [charlie], bob.nearby
133 # david has no user nearby, since edward is not active
134 assert_equal [], david.nearby
135 # south_pole_user has no user nearby, and doesn't throw exception
136 assert_equal [], south_pole_user.nearby
137 # vagrant_user has no home location
138 assert_equal [], vagrant_user.nearby
141 def test_friend_users
142 norm = create(:user, :active)
143 sec = create(:user, :active)
144 create(:friend, :befriender => norm, :befriendee => sec)
146 assert_equal [sec], norm.friend_users
147 assert_equal 1, norm.friend_users.size
149 assert_equal [], sec.friend_users
150 assert_equal 0, sec.friend_users.size
153 def test_user_preferred_editor
155 assert_nil user.preferred_editor
156 user.preferred_editor = "potlatch"
157 assert_equal "potlatch", user.preferred_editor
160 user.preferred_editor = "invalid_editor"
161 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
165 pending = create(:user, :pending)
166 active = create(:user, :active)
167 confirmed = create(:user, :confirmed)
168 suspended = create(:user, :suspended)
169 deleted = create(:user, :deleted)
171 assert User.visible.find(pending.id)
172 assert User.visible.find(active.id)
173 assert User.visible.find(confirmed.id)
174 assert_raise ActiveRecord::RecordNotFound do
175 User.visible.find(suspended.id)
177 assert_raise ActiveRecord::RecordNotFound do
178 User.visible.find(deleted.id)
183 pending = create(:user, :pending)
184 active = create(:user, :active)
185 confirmed = create(:user, :confirmed)
186 suspended = create(:user, :suspended)
187 deleted = create(:user, :deleted)
189 assert User.active.find(active.id)
190 assert User.active.find(confirmed.id)
191 assert_raise ActiveRecord::RecordNotFound do
192 User.active.find(pending.id)
194 assert_raise ActiveRecord::RecordNotFound do
195 User.active.find(suspended.id)
197 assert_raise ActiveRecord::RecordNotFound do
198 User.active.find(deleted.id)
202 def test_identifiable
203 public_user = create(:user, :data_public => true)
204 private_user = create(:user, :data_public => false)
206 assert User.identifiable.find(public_user.id)
207 assert_raise ActiveRecord::RecordNotFound do
208 User.identifiable.find(private_user.id)
213 create(:language, :code => "en")
214 create(:language, :code => "de")
215 create(:language, :code => "sl")
217 user = create(:user, :languages => ["en"])
218 assert_equal ["en"], user.languages
219 user.languages = %w[de fr en]
220 assert_equal %w[de fr en], user.languages
221 user.languages = %w[fr de sl]
222 assert_equal "de", user.preferred_language
223 assert_equal %w[fr de sl], user.preferred_languages.map(&:to_s)
224 user = create(:user, :languages => %w[en de])
225 assert_equal %w[en de], user.languages
229 assert_equal true, build(:user, :pending).visible?
230 assert_equal true, build(:user, :active).visible?
231 assert_equal true, build(:user, :confirmed).visible?
232 assert_equal false, build(:user, :suspended).visible?
233 assert_equal false, build(:user, :deleted).visible?
237 assert_equal false, build(:user, :pending).active?
238 assert_equal true, build(:user, :active).active?
239 assert_equal true, build(:user, :confirmed).active?
240 assert_equal false, build(:user, :suspended).active?
241 assert_equal false, build(:user, :deleted).active?
245 assert_equal false, create(:user).moderator?
246 assert_equal true, create(:moderator_user).moderator?
249 def test_administrator?
250 assert_equal false, create(:user).administrator?
251 assert_equal true, create(:administrator_user).administrator?
255 assert_equal false, create(:user).has_role?("administrator")
256 assert_equal false, create(:user).has_role?("moderator")
257 assert_equal true, create(:administrator_user).has_role?("administrator")
258 assert_equal true, create(:moderator_user).has_role?("moderator")
262 user = create(:user, :with_home_location, :description => "foo")
264 assert_equal "user_#{user.id}", user.display_name
265 assert user.description.blank?
266 assert_nil user.home_lat
267 assert_nil user.home_lon
268 assert_equal false, user.image.file?
269 assert_equal "deleted", user.status
270 assert_equal false, user.visible?
271 assert_equal false, user.active?
275 user = build(:user, :with_home_location)
277 assert_select Nokogiri::XML::Document.parse(xml.to_s), "user" do
278 assert_select "[display_name=?]", user.display_name
279 assert_select "[account_created=?]", user.creation_time.xmlschema
280 assert_select "home[lat=?][lon=?][zoom=?]", user.home_lat.to_s, user.home_lon.to_s, user.home_zoom.to_s
285 user = build(:user, :with_home_location)
286 xml = user.to_xml_node
287 assert_select Nokogiri::XML::DocumentFragment.parse(xml.to_s), "user" do
288 assert_select "[display_name=?]", user.display_name
289 assert_select "[account_created=?]", user.creation_time.xmlschema
290 assert_select "home[lat=?][lon=?][zoom=?]", user.home_lat.to_s, user.home_lon.to_s, user.home_zoom.to_s