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