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.invalid?(:email)
12 assert user.errors.invalid?(:pass_crypt)
13 assert user.errors.invalid?(:display_name)
14 assert user.errors.invalid?(:email)
15 assert !user.errors.invalid?(:home_lat)
16 assert !user.errors.invalid?(:home_lon)
17 assert !user.errors.invalid?(:home_zoom)
21 new_user = User.new(:email => users(:normal_user).email,
23 :pass_crypt => Digest::MD5.hexdigest('test'),
24 :display_name => "new user",
26 :description => "desc")
28 assert_equal "has already been taken", new_user.errors.on(:email)
31 def test_unique_display_name
32 new_user = User.new(:email => "tester@openstreetmap.org",
34 :pass_crypt => Digest::MD5.hexdigest('test'),
35 :display_name => users(:normal_user).display_name,
37 :description => "desc")
39 assert_equal "has already been taken", new_user.errors.on(:display_name)
43 ok = %w{ a@s.com test@shaunmcdonald.me.uk hello_local@ping-d.ng
44 test_local@openstreetmap.org test-local@example.com }
45 bad = %w{ hi ht@ n@ @.com help@.me.uk help"hi.me.uk も対@応します
46 輕觸搖晃的遊戲@ah.com も対応します@s.name }
49 user = users(:normal_user)
51 assert user.valid?, user.errors.full_messages
55 user = users(:normal_user)
57 assert !user.valid?, "#{name} is valid when it shouldn't be"
61 def test_display_name_length
62 user = users(:normal_user)
63 user.display_name = "123"
64 assert user.valid?, " should allow nil display name"
65 user.display_name = "12"
66 assert !user.valid?, "should not allow 2 char name"
67 user.display_name = ""
69 user.display_name = nil
70 # Don't understand why it isn't allowing a nil value,
71 # when the validates statements specifically allow it
72 # It appears the database does not allow null values
76 def test_display_name_valid
77 # Due to sanitisation in the view some of these that you might not
79 # However, would they affect the xml planet dumps?
80 ok = [ "Name", "'me", "he\"", "#ping", "<hr>", "*ho", "\"help\"@",
81 "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲" ]
82 # These need to be 3 chars in length, otherwise the length test above
84 bad = [ "<hr/>", "test@example.com", "s/f", "aa/", "aa;", "aa.",
85 "aa,", "aa?", "/;.,?", "も対応します/" ]
86 ok.each do |display_name|
87 user = users(:normal_user)
88 user.display_name = display_name
89 assert user.valid?, "#{display_name} is invalid, when it should be"
92 bad.each do |display_name|
93 user = users(:normal_user)
94 user.display_name = display_name
95 assert !user.valid?, "#{display_name} is valid when it shouldn't be"
96 assert_equal "is invalid", user.errors.on(:display_name)
101 assert_equal true, users(:normal_user).is_friends_with?(users(:public_user))
102 assert_equal false, users(:normal_user).is_friends_with?(users(:inactive_user))
103 assert_equal false, users(:public_user).is_friends_with?(users(:normal_user))
104 assert_equal false, users(:public_user).is_friends_with?(users(:inactive_user))
105 assert_equal false, users(:inactive_user).is_friends_with?(users(:normal_user))
106 assert_equal false, users(:inactive_user).is_friends_with?(users(:public_user))
109 def test_users_nearby
110 # second user has their data public and is close by normal user
111 assert_equal [users(:public_user)], users(:normal_user).nearby
112 # second_user has normal user nearby, but normal user has their data private
113 assert_equal [], users(:public_user).nearby
114 # inactive_user has no user nearby
115 assert_equal [], users(:inactive_user).nearby
118 def test_friends_with
119 # normal user is a friend of second user
120 # it should be a one way friend accossitation
121 assert_equal 1, Friend.count
122 norm = users(:normal_user)
123 sec = users(:public_user)
125 #friend.befriender = norm
126 #friend.befriendee = sec
128 assert_equal [sec], norm.nearby
129 assert_equal 1, norm.nearby.size
130 assert_equal 1, Friend.count
131 assert_equal true, norm.is_friends_with?(sec)
132 assert_equal false, sec.is_friends_with?(norm)
133 assert_equal false, users(:normal_user).is_friends_with?(users(:inactive_user))
134 assert_equal false, users(:public_user).is_friends_with?(users(:normal_user))
135 assert_equal false, users(:public_user).is_friends_with?(users(:inactive_user))
136 assert_equal false, users(:inactive_user).is_friends_with?(users(:normal_user))
137 assert_equal false, users(:inactive_user).is_friends_with?(users(:public_user))
138 #Friend.delete(friend)
139 #assert_equal 0, Friend.count
142 def test_user_preferred_editor
143 user = users(:normal_user)
144 assert_equal nil, user.preferred_editor
145 user.preferred_editor = "potlatch"
146 assert_equal "potlatch", user.preferred_editor
149 user.preferred_editor = "invalid_editor"
150 assert_raise(ActiveRecord::RecordInvalid) { user.save! }