Module: Clearance::Test::Functional::PasswordsControllerTest

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

Class Method Summary collapse

Class Method Details

.included(controller_test) ⇒ 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
# File 'lib/clearance/test/functional/passwords_controller_test.rb', line 6

def self.included(controller_test)
  controller_test.class_eval do

    should_route :get, '/users/1/password/edit', 
      :action  => 'edit', :user_id => '1'

    context "with a confirmed user" do
      setup do
        @user = Factory(:registered_user)
        @user.confirm_email!
      end

      context 'A GET to #new' do
        setup { get :new, :user_id => @user.to_param }

        should_respond_with :success
        should_render_template "new"
      end

      context "A POST to #create" do
        context "with an existing user's email address" do
          setup do
            ActionMailer::Base.deliveries.clear
            post :create, :password => { :email => @user.email }
          end
          
          should_send_the_change_your_password_email
          
          should "set a :notice flash" do
            assert_match /details/i, flash[:notice]
          end

          should_redirect_to_url_after_create
        end

        context "with a non-existent email address" do
          setup do
            email = "[email protected]"
            assert ! User.exists?(['email = ?', email])
            ActionMailer::Base.deliveries.clear

            post :create, :password => { :email => email }
          end
          
          should_not_send_the_change_your_password_email
          
          should "set a :notice flash" do
            assert_not_nil flash.now[:notice]
          end

          should_render_template "new"
        end
      end
      
      context 'who requested password recovery' do
        setup { @user.forgot_password! }

        context "A GET to #edit" do
          context "with an existing user's id and token" do
            setup do
              get :edit, :user_id => @user.to_param, :token => @user.token
            end

            should "find the user with the given id and token" do
              assert_equal @user, assigns(:user)
            end

            should_respond_with :success
            should_render_template "edit"
            should_display_a_password_update_form
          end

          context "with an existing user's id but not token" do
            setup do
              get :edit, :user_id => @user.to_param, :token => ""
            end

            should_respond_with :not_found
            should_render_nothing
          end
        end

        context "A PUT to #update" do
          context "with a matching password and password confirmation" do
            setup do
              new_password = "new_password"
              @encrypted_new_password = @user.encrypt(new_password)
              assert_not_equal @encrypted_new_password, @user.encrypted_password

              put(:update,
                  :user_id  => @user,
                  :token    => @user.token,
                  :user     => {
                    :password              => new_password,
                    :password_confirmation => new_password
                  })
              @user.reload
            end

            should "update the user's password" do
              assert_equal @encrypted_new_password, @user.encrypted_password
            end
            
            should "clear the token" do
              assert_nil @user.token
            end                    

            should_be_signed_in_as { @user }
            should_redirect_to_url_after_update
          end

          context "with password but blank password confirmation" do
            setup do
              new_password = "new_password"
              @encrypted_new_password = @user.encrypt(new_password)

              put(:update,
                  :user_id => @user.to_param,
                  :token   => @user.token,
                  :user    => {
                    :password => new_password,
                    :password_confirmation => ''
                  })
              @user.reload
            end

            should "not update the user's password" do
              assert_not_equal @encrypted_new_password, @user.encrypted_password
            end
            
            should "not clear the token" do
              assert_not_nil @user.token
            end                    

            should_not_be_signed_in
            should_respond_with :success
            should_render_template :edit
            
            should_display_a_password_update_form                       
          end
          
          context "with an existing user's id but not token" do
            setup do
              new_password = "new_password"
              @encrypted_new_password = @user.encrypt(new_password)                    
              put(:update, 
                  :user_id => @user.to_param, 
                  :token   => "",
                  :user    => {
                    :password => new_password,
                    :password => new_password                          
                  })
            end

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

            should_not_be_signed_in
            should_respond_with :not_found
            should_render_nothing
          end                  
        end
   
      end
      
   end
  end
end