changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/restful_authentication/generators/authenticated/templates/unit_spec.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__) + '/../spec_helper'
2
3# Be sure to include AuthenticatedTestHelper in spec/spec_helper.rb instead.
4# Then, you can remove it from this and the functional test.
5include AuthenticatedTestHelper
6
7describe <%= class_name %> do
8 fixtures :<%= table_name %>
9
10 describe 'being created' do
11 before do
12 @<%= file_name %> = nil
13 @creating_<%= file_name %> = lambda do
14 @<%= file_name %> = create_<%= file_name %>
15 violated "#{@<%= file_name %>.errors.full_messages.to_sentence}" if @<%= file_name %>.new_record?
16 end
17 end
18
19 it 'increments User#count' do
20 @creating_<%= file_name %>.should change(<%= class_name %>, :count).by(1)
21 end
22<% if options[:include_activation] %>
23 it 'initializes #activation_code' do
24 @creating_<%= file_name %>.call
25 @<%= file_name %>.reload.activation_code.should_not be_nil
26 end
27<% end %><% if options[:stateful] %>
28 it 'starts in pending state' do
29 @creating_<%= file_name %>.call
30 @<%= file_name %>.should be_pending
31 end
32<% end %> end
33
34 it 'requires login' do
35 lambda do
36 u = create_<%= file_name %>(:login => nil)
37 u.errors.on(:login).should_not be_nil
38 end.should_not change(<%= class_name %>, :count)
39 end
40
41 it 'requires password' do
42 lambda do
43 u = create_<%= file_name %>(:password => nil)
44 u.errors.on(:password).should_not be_nil
45 end.should_not change(<%= class_name %>, :count)
46 end
47
48 it 'requires password confirmation' do
49 lambda do
50 u = create_<%= file_name %>(:password_confirmation => nil)
51 u.errors.on(:password_confirmation).should_not be_nil
52 end.should_not change(<%= class_name %>, :count)
53 end
54
55 it 'requires email' do
56 lambda do
57 u = create_<%= file_name %>(:email => nil)
58 u.errors.on(:email).should_not be_nil
59 end.should_not change(<%= class_name %>, :count)
60 end
61
62 it 'resets password' do
63 <%= table_name %>(:quentin).update_attributes(:password => 'new password', :password_confirmation => 'new password')
64 <%= class_name %>.authenticate('quentin', 'new password').should == <%= table_name %>(:quentin)
65 end
66
67 it 'does not rehash password' do
68 <%= table_name %>(:quentin).update_attributes(:login => 'quentin2')
69 <%= class_name %>.authenticate('quentin2', 'test').should == <%= table_name %>(:quentin)
70 end
71
72 it 'authenticates <%= file_name %>' do
73 <%= class_name %>.authenticate('quentin', 'test').should == <%= table_name %>(:quentin)
74 end
75
76 it 'sets remember token' do
77 <%= table_name %>(:quentin).remember_me
78 <%= table_name %>(:quentin).remember_token.should_not be_nil
79 <%= table_name %>(:quentin).remember_token_expires_at.should_not be_nil
80 end
81
82 it 'unsets remember token' do
83 <%= table_name %>(:quentin).remember_me
84 <%= table_name %>(:quentin).remember_token.should_not be_nil
85 <%= table_name %>(:quentin).forget_me
86 <%= table_name %>(:quentin).remember_token.should be_nil
87 end
88
89 it 'remembers me for one week' do
90 before = 1.week.from_now.utc
91 <%= table_name %>(:quentin).remember_me_for 1.week
92 after = 1.week.from_now.utc
93 <%= table_name %>(:quentin).remember_token.should_not be_nil
94 <%= table_name %>(:quentin).remember_token_expires_at.should_not be_nil
95 <%= table_name %>(:quentin).remember_token_expires_at.between?(before, after).should be_true
96 end
97
98 it 'remembers me until one week' do
99 time = 1.week.from_now.utc
100 <%= table_name %>(:quentin).remember_me_until time
101 <%= table_name %>(:quentin).remember_token.should_not be_nil
102 <%= table_name %>(:quentin).remember_token_expires_at.should_not be_nil
103 <%= table_name %>(:quentin).remember_token_expires_at.should == time
104 end
105
106 it 'remembers me default two weeks' do
107 before = 2.weeks.from_now.utc
108 <%= table_name %>(:quentin).remember_me
109 after = 2.weeks.from_now.utc
110 <%= table_name %>(:quentin).remember_token.should_not be_nil
111 <%= table_name %>(:quentin).remember_token_expires_at.should_not be_nil
112 <%= table_name %>(:quentin).remember_token_expires_at.between?(before, after).should be_true
113 end
114<% if options[:stateful] %>
115 it 'registers passive <%= file_name %>' do
116 <%= file_name %> = create_<%= file_name %>(:password => nil, :password_confirmation => nil)
117 <%= file_name %>.should be_passive
118 <%= file_name %>.update_attributes(:password => 'new password', :password_confirmation => 'new password')
119 <%= file_name %>.register!
120 <%= file_name %>.should be_pending
121 end
122
123 it 'suspends <%= file_name %>' do
124 <%= table_name %>(:quentin).suspend!
125 <%= table_name %>(:quentin).should be_suspended
126 end
127
128 it 'does not authenticate suspended <%= file_name %>' do
129 <%= table_name %>(:quentin).suspend!
130 <%= class_name %>.authenticate('quentin', 'test').should_not == <%= table_name %>(:quentin)
131 end
132
133 it 'deletes <%= file_name %>' do
134 <%= table_name %>(:quentin).deleted_at.should be_nil
135 <%= table_name %>(:quentin).delete!
136 <%= table_name %>(:quentin).deleted_at.should_not be_nil
137 <%= table_name %>(:quentin).should be_deleted
138 end
139
140 describe "being unsuspended" do
141 fixtures :<%= table_name %>
142
143 before do
144 @<%= file_name %> = <%= table_name %>(:quentin)
145 @<%= file_name %>.suspend!
146 end
147
148 it 'reverts to active state' do
149 @<%= file_name %>.unsuspend!
150 @<%= file_name %>.should be_active
151 end
152
153 it 'reverts to passive state if activation_code and activated_at are nil' do
154 <%= class_name %>.update_all :activation_code => nil, :activated_at => nil
155 @<%= file_name %>.reload.unsuspend!
156 @<%= file_name %>.should be_passive
157 end
158
159 it 'reverts to pending state if activation_code is set and activated_at is nil' do
160 <%= class_name %>.update_all :activation_code => 'foo-bar', :activated_at => nil
161 @<%= file_name %>.reload.unsuspend!
162 @<%= file_name %>.should be_pending
163 end
164 end
165<% end %>
166protected
167 def create_<%= file_name %>(options = {})
168 record = <%= class_name %>.new({ :login => 'quire', :email => 'quire@example.com', :password => 'quire', :password_confirmation => 'quire' }.merge(options))
169 record.<% if options[:stateful] %>register! if record.valid?<% else %>save<% end %>
170 record
171 end
172end