changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/restful_authentication/generators/authenticated/templates/unit_test.rb

changeset 4: 43c5e6930eee
author: moriq@moriq.com
date: Wed Mar 05 01:17:41 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugin restful_authentication.
1require File.dirname(__FILE__) + '/../test_helper'
2
3class <%= class_name %>Test < Test::Unit::TestCase
4 # Be sure to include AuthenticatedTestHelper in test/test_helper.rb instead.
5 # Then, you can remove it from this and the functional test.
6 include AuthenticatedTestHelper
7 fixtures :<%= table_name %>
8
9 def test_should_create_<%= file_name %>
10 assert_difference '<%= class_name %>.count' do
11 <%= file_name %> = create_<%= file_name %>
12 assert !<%= file_name %>.new_record?, "#{<%= file_name %>.errors.full_messages.to_sentence}"
13 end
14 end
15<% if options[:include_activation] %>
16 def test_should_initialize_activation_code_upon_creation
17 <%= file_name %> = create_<%= file_name %>
18 assert_not_nil <%= file_name %>.reload.activation_code
19 end
20<% end %><% if options[:stateful] %>
21 def test_should_create_and_start_in_pending_state
22 <%= file_name %> = create_<%= file_name %>
23 assert <%= file_name %>.pending?
24 end
25
26<% end %>
27 def test_should_require_login
28 assert_no_difference '<%= class_name %>.count' do
29 u = create_<%= file_name %>(:login => nil)
30 assert u.errors.on(:login)
31 end
32 end
33
34 def test_should_require_password
35 assert_no_difference '<%= class_name %>.count' do
36 u = create_<%= file_name %>(:password => nil)
37 assert u.errors.on(:password)
38 end
39 end
40
41 def test_should_require_password_confirmation
42 assert_no_difference '<%= class_name %>.count' do
43 u = create_<%= file_name %>(:password_confirmation => nil)
44 assert u.errors.on(:password_confirmation)
45 end
46 end
47
48 def test_should_require_email
49 assert_no_difference '<%= class_name %>.count' do
50 u = create_<%= file_name %>(:email => nil)
51 assert u.errors.on(:email)
52 end
53 end
54
55 def test_should_reset_password
56 <%= table_name %>(:quentin).update_attributes(:password => 'new password', :password_confirmation => 'new password')
57 assert_equal <%= table_name %>(:quentin), <%= class_name %>.authenticate('quentin', 'new password')
58 end
59
60 def test_should_not_rehash_password
61 <%= table_name %>(:quentin).update_attributes(:login => 'quentin2')
62 assert_equal <%= table_name %>(:quentin), <%= class_name %>.authenticate('quentin2', 'test')
63 end
64
65 def test_should_authenticate_<%= file_name %>
66 assert_equal <%= table_name %>(:quentin), <%= class_name %>.authenticate('quentin', 'test')
67 end
68
69 def test_should_set_remember_token
70 <%= table_name %>(:quentin).remember_me
71 assert_not_nil <%= table_name %>(:quentin).remember_token
72 assert_not_nil <%= table_name %>(:quentin).remember_token_expires_at
73 end
74
75 def test_should_unset_remember_token
76 <%= table_name %>(:quentin).remember_me
77 assert_not_nil <%= table_name %>(:quentin).remember_token
78 <%= table_name %>(:quentin).forget_me
79 assert_nil <%= table_name %>(:quentin).remember_token
80 end
81
82 def test_should_remember_me_for_one_week
83 before = 1.week.from_now.utc
84 <%= table_name %>(:quentin).remember_me_for 1.week
85 after = 1.week.from_now.utc
86 assert_not_nil <%= table_name %>(:quentin).remember_token
87 assert_not_nil <%= table_name %>(:quentin).remember_token_expires_at
88 assert <%= table_name %>(:quentin).remember_token_expires_at.between?(before, after)
89 end
90
91 def test_should_remember_me_until_one_week
92 time = 1.week.from_now.utc
93 <%= table_name %>(:quentin).remember_me_until time
94 assert_not_nil <%= table_name %>(:quentin).remember_token
95 assert_not_nil <%= table_name %>(:quentin).remember_token_expires_at
96 assert_equal <%= table_name %>(:quentin).remember_token_expires_at, time
97 end
98
99 def test_should_remember_me_default_two_weeks
100 before = 2.weeks.from_now.utc
101 <%= table_name %>(:quentin).remember_me
102 after = 2.weeks.from_now.utc
103 assert_not_nil <%= table_name %>(:quentin).remember_token
104 assert_not_nil <%= table_name %>(:quentin).remember_token_expires_at
105 assert <%= table_name %>(:quentin).remember_token_expires_at.between?(before, after)
106 end
107<% if options[:stateful] %>
108 def test_should_register_passive_<%= file_name %>
109 <%= file_name %> = create_<%= file_name %>(:password => nil, :password_confirmation => nil)
110 assert <%= file_name %>.passive?
111 <%= file_name %>.update_attributes(:password => 'new password', :password_confirmation => 'new password')
112 <%= file_name %>.register!
113 assert <%= file_name %>.pending?
114 end
115
116 def test_should_suspend_<%= file_name %>
117 <%= table_name %>(:quentin).suspend!
118 assert <%= table_name %>(:quentin).suspended?
119 end
120
121 def test_suspended_<%= file_name %>_should_not_authenticate
122 <%= table_name %>(:quentin).suspend!
123 assert_not_equal <%= table_name %>(:quentin), <%= class_name %>.authenticate('quentin', 'test')
124 end
125
126 def test_should_unsuspend_<%= file_name %>_to_active_state
127 <%= table_name %>(:quentin).suspend!
128 assert <%= table_name %>(:quentin).suspended?
129 <%= table_name %>(:quentin).unsuspend!
130 assert <%= table_name %>(:quentin).active?
131 end
132
133 def test_should_unsuspend_<%= file_name %>_with_nil_activation_code_and_activated_at_to_passive_state
134 <%= table_name %>(:quentin).suspend!
135 <%= class_name %>.update_all :activation_code => nil, :activated_at => nil
136 assert <%= table_name %>(:quentin).suspended?
137 <%= table_name %>(:quentin).reload.unsuspend!
138 assert <%= table_name %>(:quentin).passive?
139 end
140
141 def test_should_unsuspend_<%= file_name %>_with_activation_code_and_nil_activated_at_to_pending_state
142 <%= table_name %>(:quentin).suspend!
143 <%= class_name %>.update_all :activation_code => 'foo-bar', :activated_at => nil
144 assert <%= table_name %>(:quentin).suspended?
145 <%= table_name %>(:quentin).reload.unsuspend!
146 assert <%= table_name %>(:quentin).pending?
147 end
148
149 def test_should_delete_<%= file_name %>
150 assert_nil <%= table_name %>(:quentin).deleted_at
151 <%= table_name %>(:quentin).delete!
152 assert_not_nil <%= table_name %>(:quentin).deleted_at
153 assert <%= table_name %>(:quentin).deleted?
154 end
155<% end %>
156protected
157 def create_<%= file_name %>(options = {})
158 <%= class_name %>.create({ :login => 'quire', :email => 'quire@example.com', :password => 'quire', :password_confirmation => 'quire' }.merge(options))
159 end
160end