mike-burns (owner)

Revisions

gist: 23084 Download_button fork
public
Public Clone URL: git://gist.github.com/23084.git
Embed All Files: show embed
openids_controller_test.rb #
1
2
3
4
5
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
require 'test_helper'
 
class OpenidsControllerTest < ActionController::TestCase
  def setup
    @openid_identity = Factory.next(:openid_identity)
  end
 
  should_route :get, '/openid', :controller => 'openids', :action => 'show'
  should_route :put, '/openid', :controller => 'openids', :action => 'update'
 
  should_ensure_logged_in(:get, :show)
  should_ensure_logged_in(:put, :update, :user => {:openid_identity => @openid_identity})
 
  logged_in do
    context "a User with new_openid_identity set" do
      setup do
        @user.update_attributes(:new_openid_identity => @openid_identity)
      end
 
      context "when authenticating with openid would succeed" do
        setup do
          @result = stub('successful_result',:successful? => true)
          @controller.stubs(:authenticate_with_open_id).
            with(@openid_identity).yields(@result, @openid_identity)
        end
 
        context "successful update_attributes" do
          setup do
            get :show
            @user.reload
          end
 
          before_should "attempt to authenticate with openid" do
            @controller.expects(:authenticate_with_open_id).
              with(@openid_identity).yields(@result, @openid_identity)
          end
 
          before_should "not verify authenticity" do
            @controller.expects(:verify_authenticity_token).never
          end
 
          should_redirect_to 'edit_account_path'
 
          should "set the User's openid_identity to the new_openid_identity" do
            assert_equal @openid_identity, @user.openid_identity
          end
 
          should "set the User's new_openid_identity to nil" do
            assert_nil @user.new_openid_identity
          end
        end
 
        context "failed update_attributes" do
          setup do
            @user.expects(:update_attributes).
              with(:openid_identity => @openid_identity,
                   :new_openid_identity => nil).returns(false)
            errors = mock('errors', :full_messages => ['invalid OpenID'])
            @user.expects(:errors).returns(errors)
            get :show
            @user.reload
          end
 
          before_should "attempt to authenticate with openid" do
            @controller.expects(:authenticate_with_open_id).
              with(@openid_identity).yields(@result, @openid_identity)
          end
 
          before_should "not verify authenticity" do
            @controller.expects(:verify_authenticity_token).never
          end
 
          should_redirect_to 'edit_account_path'
          should_set_the_flash_to /invalid/
          should_not_change '@user.openid_identity'
          should_not_change '@user.new_openid_identity'
        end
      end
 
      context "when authenticating with openid would fail" do
        setup do
          @message = 'no good'
          @result = stub('successful_result', :successful? => false,
                         :message => @message)
          @controller.
            stubs(:authenticate_with_open_id).
            yields(@result, @openid_identity)
            get :show
            @user.reload
        end
 
        should_set_the_flash_to 'no good'
        should_redirect_to 'edit_account_path'
        should_not_change '@user.openid_identity'
        should_not_change '@user.new_openid_identity'
      end
    end
 
    context "PUT to update" do
      context "successful" do
        setup do
          put :update, :user => {:new_openid_identity => @openid_identity}
          @user.reload
        end
 
        should_not_change '@user.openid_identity'
        should_redirect_to 'openid_path'
 
        should "set the new_openid_identity" do
          assert_equal @openid_identity, @user.new_openid_identity
        end
      end
 
      context "failed" do
        setup do
          @invalid_params = {'new_openid_identity' => 'foo'}
          @user.expects(:update_attributes).with(@invalid_params).returns(false)
          errors = mock('errors', :full_messages => ['invalid OpenID'])
          @user.expects(:errors).returns(errors)
          put :update, :user => @invalid_params
        end
 
        should_set_the_flash_to /invalid/
        should_redirect_to 'edit_account_path'
      end
    end
  end
end