1 require File.dirname(__FILE__) + '/../test_helper'
3 class UserTest < Test::Unit::TestCase
7 def test_invalid_with_empty_attributes
10 assert user.errors.invalid?(:email)
11 assert user.errors.invalid?(:pass_crypt)
12 assert user.errors.invalid?(:display_name)
13 assert user.errors.invalid?(:email)
14 assert !user.errors.invalid?(:home_lat)
15 assert !user.errors.invalid?(:home_lon)
16 assert !user.errors.invalid?(:home_zoom)
20 new_user = User.new(:email => users(:normal_user).email,
22 :pass_crypt => Digest::MD5.hexdigest('test'),
23 :display_name => "new user",
25 :description => "desc")
27 assert_equal ActiveRecord::Errors.default_error_messages[:taken], new_user.errors.on(:email)
30 def test_unique_display_name
31 new_user = User.new(:email => "tester@openstreetmap.org",
33 :pass_crypt => Digest::MD5.hexdigest('test'),
34 :display_name => users(:normal_user).display_name,
36 :description => "desc")
38 assert_equal ActiveRecord::Errors.default_error_messages[:taken], new_user.errors.on(:display_name)
42 ok = %w{ a@s.com test@shaunmcdonald.me.uk hello_local@ping-d.ng
43 test_local@openstreetmap.org test-local@example.com
44 輕觸搖晃的遊戲@ah.com も対応します@s.name }
45 bad = %w{ hi ht@ n@ @.com help@.me.uk help"hi.me.uk も対@応します }
48 user = users(:normal_user)
50 assert user.valid?, user.errors.full_messages
54 user = users(:normal_user)
56 assert !user.valid?, "#{name} is valid when it shouldn't be"
60 def test_display_name_length
61 user = users(:normal_user)
62 user.display_name = "123"
63 assert user.valid?, " should allow nil display name"
64 user.display_name = "12"
65 assert !user.valid?, "should not allow 2 char name"
66 user.display_name = ""
68 user.display_name = nil
69 # Don't understand why it isn't allowing a nil value,
70 # when the validates statements specifically allow it
71 # It appears the database does not allow null values
75 def test_display_name_valid
76 # Due to sanitisation in the view some of these that you might not
78 # However, would they affect the xml planet dumps?
79 ok = [ "Name", "'me", "he\"", "#ping", "<hr>", "*ho", "\"help\"@",
80 "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲" ]
81 # These need to be 3 chars in length, otherwise the length test above
83 bad = [ "<hr/>", "test@example.com", "s/f", "aa/", "aa;", "aa.",
84 "aa,", "aa?", "/;.,?", "も対応します/" ]
85 ok.each do |display_name|
86 user = users(:normal_user)
87 user.display_name = display_name
88 assert user.valid?, "#{display_name} is invalid, when it should be"
91 bad.each do |display_name|
92 user = users(:normal_user)
93 user.display_name = display_name
94 assert !user.valid?, "#{display_name} is valid when it shouldn't be"
95 assert_equal "is invalid", user.errors.on(:display_name)
100 assert_equal true, users(:normal_user).is_friends_with?(users(:second_user))
101 assert_equal false, users(:normal_user).is_friends_with?(users(:inactive_user))
102 assert_equal false, users(:second_user).is_friends_with?(users(:normal_user))
103 assert_equal false, users(:second_user).is_friends_with?(users(:inactive_user))
104 assert_equal false, users(:inactive_user).is_friends_with?(users(:normal_user))
105 assert_equal false, users(:inactive_user).is_friends_with?(users(:second_user))
108 def test_users_nearby
109 # second user has their data public and is close by normal user
110 assert_equal [users(:second_user)], users(:normal_user).nearby
111 # second_user has normal user nearby, but normal user has their data private
112 assert_equal [], users(:second_user).nearby
113 # inactive_user has no user nearby
114 assert_equal [], users(:inactive_user).nearby
117 def test_friends_with
118 # normal user is a friend of second user
119 # it should be a one way friend accossitation
120 assert_equal 1, Friend.count
121 norm = users(:normal_user)
122 sec = users(:second_user)
124 #friend.befriender = norm
125 #friend.befriendee = sec
127 assert_equal [sec], norm.nearby
128 assert_equal 1, norm.nearby.size
129 assert_equal 1, Friend.count
130 assert_equal true, norm.is_friends_with?(sec)
131 assert_equal false, sec.is_friends_with?(norm)
132 assert_equal false, users(:normal_user).is_friends_with?(users(:inactive_user))
133 assert_equal false, users(:second_user).is_friends_with?(users(:normal_user))
134 assert_equal false, users(:second_user).is_friends_with?(users(:inactive_user))
135 assert_equal false, users(:inactive_user).is_friends_with?(users(:normal_user))
136 assert_equal false, users(:inactive_user).is_friends_with?(users(:second_user))
137 #Friend.delete(friend)
138 #assert_equal 0, Friend.count