From 4c777106152148794a6bd63b6af60f7de18a279b Mon Sep 17 00:00:00 2001 From: nov Date: Thu, 20 Jul 2023 08:49:58 +0900 Subject: [PATCH 1/2] spec for request_phase --- spec/omniauth/strategies/oauth2_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spec/omniauth/strategies/oauth2_spec.rb b/spec/omniauth/strategies/oauth2_spec.rb index ff6176c..c57b3ee 100644 --- a/spec/omniauth/strategies/oauth2_spec.rb +++ b/spec/omniauth/strategies/oauth2_spec.rb @@ -39,6 +39,22 @@ def app end end + describe "#request_phase" do + subject(:instance) { fresh_strategy.new(app, :client_options => {"authorize_url" => "https://example.com/authorize"}) } + + before do + allow(instance).to receive(:request) do + double("Request", :scheme => 'https', :url => 'https://rp.example.com', :env => {}, :query_string => {}) + end + end + + it do + response = instance.request_phase + expect(response[0]).to be 302 + expect(response[1]['location']).to be_start_with "https://example.com/authorize" + end + end + describe "#authorize_params" do subject { fresh_strategy } From df00e16846202d8a02589b7a0615316765eb77cf Mon Sep 17 00:00:00 2001 From: nov Date: Thu, 20 Jul 2023 09:12:01 +0900 Subject: [PATCH 2/2] spec for callback_phase success case --- spec/omniauth/strategies/oauth2_spec.rb | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec/omniauth/strategies/oauth2_spec.rb b/spec/omniauth/strategies/oauth2_spec.rb index c57b3ee..423f3e3 100644 --- a/spec/omniauth/strategies/oauth2_spec.rb +++ b/spec/omniauth/strategies/oauth2_spec.rb @@ -183,6 +183,34 @@ def app end end end + + describe 'successful case' do + def app + lambda do |_env| + [200, {}, ["Hello."]] + end + end + + let(:instance) do + fresh_strategy.new(app, :client_options => {"token_url" => "https://example.com/token"}) + end + let(:params) do + {"code" => "code", "state" => state} + end + + before do + allow(instance).to receive(:env).and_return({}) + allow(instance).to receive(:request) do + double("Request", :scheme => 'https', :url => 'https://rp.example.com/callback', :env => {}, :query_string => {}, :params => params) + end + stub_request(:post, "https://example.com/token").to_return(:status => 200, :body => '{"access_token":"access_token","tokey_type":"bearer"}', :headers => {'Content-Type' => 'application/json'}) + end + + it do + instance.callback_phase + expect(instance.access_token).to be_instance_of OAuth2::AccessToken + end + end end end