Module: Clearance::Test::Functional::UsersControllerTest

Defined in:
lib/clearance/test/functional/users_controller_test.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/clearance/test/functional/users_controller_test.rb', line 6

def self.included(base)
  base.class_eval do
    public_context do

      context "on GET to /users/new" do
        setup { get :new }
        should_respond_with :success
        should_render_template :new
        should_not_set_the_flash
        should_have_form :action => "users_path",
          :method => :post,
          :fields => { :email => :text,
            :password => :password,
            :password_confirmation => :password }

        context "with params" do
          setup do
            @email = '[email protected]'
            get :new, :user => {:email => @email}
          end

          should_assign_to :user
          should "set the @user's params" do
            assert_equal @email, assigns(:user).email
          end
        end
      end

      context "on POST to /users" do
        setup do
          @email = Factory.next(:email)
          post :create, :user => {
            :email => @email,
            :password => 'skerit',
            :password_confirmation => 'skerit'
          }
        end
    
        should_set_the_flash_to /confirm/i
        should_redirect_to "@controller.send(:url_after_create)"
        should_assign_to :user
        should_change 'User.count', :by => 1
        
        should 'have a confirmation code' do
          assert_not_nil User.find_by_email(@email).confirmation_code
        end
      end

    end

    logged_in_user_context do
      context "GET to new" do
        setup { get :new }
        should_render_template 'new'
      end

      context "POST to create" do
        setup { post :create, :user => {} }
        should_render_template 'new'
      end

      should_filter_params :password
    end
    
    public_context do
      context 'A GET to #edit_password when not logged in' do
        setup do
          @user = Factory(:user)
          @user.confirm!
          get :edit_password, :id => @user.id
        end
        
        should_respond_with :redirect

        should_redirect_to "new_session_path"
      end

      context 'A PUT to #change_password when not logged in' do
        setup do
          @user = Factory(:user)
          @user.confirm!
          @new_password = 'new_password'
          put(:change_password,
              :id  => @user.id,
              :user => { :old_password => 'password',
              :password => @new_password,
              :password_confirmation => @new_password })
        end

        should_respond_with :redirect

        should_redirect_to "new_session_path"
      end
    end
    
    logged_in_user_context do
      context 'A GET to #edit_password' do
        setup do
          @user.confirm!
          get :edit_password, :id => @user.id
        end

        should 'find the user' do
          assert_equal @user, assigns(:user)
        end

        should_respond_with :success
        should_render_template "edit_password"

        should "have a form for the user's email, password, and password confirm" do
          update_path = ERB::Util.h(change_password_user_path(:id => @user))

          assert_select 'form[action=?]', update_path do
            assert_select 'input[name=_method][value=?]', 'put'
            assert_select 'input[name=?][type=password]', 'user[old_password]'
            assert_select 'input[name=?]', 'user[password]'
            assert_select 'input[name=?]', 'user[password_confirmation]'
          end
        end
      end

      context 'A PUT to #change_password' do
        setup do
          @user.confirm!
          @new_password = 'new_password'
          @encrypted_new_password = Digest::SHA1.hexdigest("--#{@user.salt}--#{@new_password}--")
          assert_not_equal @encrypted_new_password, @user.crypted_password
        end

        context 'with an existing user with a matching password and password confirmation' do
          setup do
            put(:change_password,
                :id  => @user.id,
                :user => { :old_password => 'password',
                :password => @new_password,
                :password_confirmation => @new_password })
            @user.reload
          end

          should "update the user's password" do
            assert_equal @encrypted_new_password, @user.crypted_password
          end

          should 'remove the reset_password_code' do
            assert_nil @user.reset_password_code
          end

          should_return_from_session :user_id, "@user.id"

          should_redirect_to "user_path(@user)"
        end

        context 'with a blank old_password' do
          setup do
            put(:change_password,
                :id => @user.id,
                :user => {:old_password => '',
                :password => @new_password,
                :password_confirmation => @new_password})
            @user.reload
          end

          should "not update the user's password" do
            assert_not_equal @encrypted_new_password, @user.crypted_password
          end

          should_return_from_session :user_id, "@user.id"

          should_render_template "edit_password"
        end

        context 'with password but blank password confirmation' do
          setup do
            put(:change_password,
                :id => @user.id,
                :user => {:old_password => 'password',
                :password => @new_password,
                :password_confirmation => ''})
            @user.reload
          end

          should "not update the user's password" do
            assert_not_equal @encrypted_new_password, @user.crypted_password
          end

          should_return_from_session :user_id, "@user.id"

          should_render_template "edit_password"
        end
      end
    end
    
  end
end