source: subversion/sites/rails_port/test/unit/user_test.rb @ 15534

Last change on this file since 15534 was 14971, checked in by Shaun McDonald, 10 years ago

fix the user unit test so that it doesn't use a depricated method in rails 2.2.2

File size: 5.2 KB
Line 
1require File.dirname(__FILE__) + '/../test_helper'
2
3class UserTest < Test::Unit::TestCase
4  api_fixtures
5  fixtures :friends
6
7  def test_invalid_with_empty_attributes
8    user = User.new
9    assert !user.valid?
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)
17  end
18 
19  def test_unique_email
20    new_user = User.new(:email => users(:normal_user).email,
21      :active => 1, 
22      :pass_crypt => Digest::MD5.hexdigest('test'),
23      :display_name => "new user",
24      :data_public => 1,
25      :description => "desc")
26    assert !new_user.save
27    assert_equal "has already been taken", new_user.errors.on(:email)
28  end
29 
30  def test_unique_display_name
31    new_user = User.new(:email => "tester@openstreetmap.org",
32      :active => 0,
33      :pass_crypt => Digest::MD5.hexdigest('test'),
34      :display_name => users(:normal_user).display_name, 
35      :data_public => 1,
36      :description => "desc")
37    assert !new_user.save
38    assert_equal "has already been taken", new_user.errors.on(:display_name)
39  end
40 
41  def test_email_valid
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 も対@応します }
46   
47    ok.each do |name|
48      user = users(:normal_user)
49      user.email = name
50      assert user.valid?, user.errors.full_messages
51    end
52   
53    bad.each do |name|
54      user = users(:normal_user)
55      user.email = name
56      assert !user.valid?, "#{name} is valid when it shouldn't be" 
57    end
58  end
59 
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 = ""
67    assert !user.valid?
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
72    assert !user.valid?
73  end
74 
75  def test_display_name_valid
76    # Due to sanitisation in the view some of these that you might not
77    # expact are allowed
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
82    # should be used.
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"
89    end
90   
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)
96    end
97  end
98 
99  def test_friend_with
100    assert_equal true, users(:normal_user).is_friends_with?(users(:public_user))
101    assert_equal false, users(:normal_user).is_friends_with?(users(:inactive_user))
102    assert_equal false, users(:public_user).is_friends_with?(users(:normal_user))
103    assert_equal false, users(:public_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(:public_user))
106  end
107 
108  def test_users_nearby
109    # second user has their data public and is close by normal user
110    assert_equal [users(:public_user)], users(:normal_user).nearby
111    # second_user has normal user nearby, but normal user has their data private
112    assert_equal [], users(:public_user).nearby
113    # inactive_user has no user nearby
114    assert_equal [], users(:inactive_user).nearby
115  end
116 
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(:public_user)
123    #friend = Friend.new
124    #friend.befriender = norm
125    #friend.befriendee = sec
126    #friend.save
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(:public_user).is_friends_with?(users(:normal_user))
134    assert_equal false, users(:public_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(:public_user))
137    #Friend.delete(friend)
138    #assert_equal 0, Friend.count
139  end
140end
Note: See TracBrowser for help on using the repository browser.