1 # -*- coding: utf-8 -*-
2 require File.dirname(__FILE__) + '/../test_helper'
4 class UserTest < ActiveSupport::TestCase
8 def test_invalid_with_empty_attributes
11 assert user.errors[:email].any?
12 assert user.errors[:pass_crypt].any?
13 assert user.errors[:display_name].any?
14 assert user.errors[:email].any?
15 assert !user.errors[:home_lat].any?
16 assert !user.errors[:home_lon].any?
17 assert !user.errors[:home_zoom].any?
22 :email => users(:normal_user).email,
24 :pass_crypt => Digest::MD5.hexdigest('test'),
25 :display_name => "new user",
27 :description => "desc"
28 }, :without_protection => true)
30 assert new_user.errors[:email].include?("has already been taken")
33 def test_unique_display_name
35 :email => "tester@openstreetmap.org",
37 :pass_crypt => Digest::MD5.hexdigest('test'),
38 :display_name => users(:normal_user).display_name,
40 :description => "desc"
41 }, :without_protection => true)
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 }
53 user = users(:normal_user)
55 assert user.valid?(:save), user.errors.full_messages
59 user = users(:normal_user)
61 assert user.invalid?(:save), "#{name} is valid when it shouldn't be"
65 def test_display_name_length
66 user = users(:normal_user)
67 user.display_name = "123"
68 assert user.valid?, " should allow nil display name"
69 user.display_name = "12"
70 assert !user.valid?, "should not allow 2 char name"
71 user.display_name = ""
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
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\"", "#ping", "<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?", "/;.,?", "も対応します/" ]
90 ok.each do |display_name|
91 user = users(:normal_user)
92 user.display_name = display_name
93 assert user.valid?, "#{display_name} is invalid, when it should be"
96 bad.each do |display_name|
97 user = users(:normal_user)
98 user.display_name = display_name
99 assert !user.valid?, "#{display_name} is valid when it shouldn't be"
100 assert user.errors[:display_name].include?("is invalid")
105 assert_equal true, users(:normal_user).is_friends_with?(users(:public_user))
106 assert_equal false, users(:normal_user).is_friends_with?(users(:inactive_user))
107 assert_equal false, users(:public_user).is_friends_with?(users(:normal_user))
108 assert_equal false, users(:public_user).is_friends_with?(users(:inactive_user))
109 assert_equal false, users(:inactive_user).is_friends_with?(users(:normal_user))
110 assert_equal false, users(:inactive_user).is_friends_with?(users(:public_user))
113 def test_users_nearby
114 # second user has their data public and is close by normal user
115 assert_equal [users(:public_user)], users(:normal_user).nearby
116 # second_user has normal user nearby, but normal user has their data private
117 assert_equal [], users(:public_user).nearby
118 # inactive_user has no user nearby
119 assert_equal [], users(:inactive_user).nearby
122 def test_friends_with
123 # normal user is a friend of second user
124 # it should be a one way friend accossitation
125 assert_equal 1, Friend.count
126 norm = users(:normal_user)
127 sec = users(:public_user)
129 #friend.befriender = norm
130 #friend.befriendee = sec
132 assert_equal [sec], norm.nearby
133 assert_equal 1, norm.nearby.size
134 assert_equal 1, Friend.count
135 assert_equal true, norm.is_friends_with?(sec)
136 assert_equal false, sec.is_friends_with?(norm)
137 assert_equal false, users(:normal_user).is_friends_with?(users(:inactive_user))
138 assert_equal false, users(:public_user).is_friends_with?(users(:normal_user))
139 assert_equal false, users(:public_user).is_friends_with?(users(:inactive_user))
140 assert_equal false, users(:inactive_user).is_friends_with?(users(:normal_user))
141 assert_equal false, users(:inactive_user).is_friends_with?(users(:public_user))
142 #Friend.delete(friend)
143 #assert_equal 0, Friend.count
146 def test_user_preferred_editor
147 user = users(:normal_user)
148 assert_equal nil, user.preferred_editor
149 user.preferred_editor = "potlatch"
150 assert_equal "potlatch", user.preferred_editor
153 user.preferred_editor = "invalid_editor"
154 assert_raise(ActiveRecord::RecordInvalid) { user.save! }