From b5f55ef3fdbaa27605779ee2e4bb14382874a245 Mon Sep 17 00:00:00 2001 From: nov Date: Tue, 12 Jul 2022 22:52:43 +0900 Subject: [PATCH] spec for revocation & change error for no token given --- lib/rack/oauth2/client.rb | 2 +- spec/rack/oauth2/client_spec.rb | 82 ++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/lib/rack/oauth2/client.rb b/lib/rack/oauth2/client.rb index a98f0e8..08c1887 100644 --- a/lib/rack/oauth2/client.rb +++ b/lib/rack/oauth2/client.rb @@ -102,7 +102,7 @@ def revoke!(*args) token_type_hint: :refresh_token } when options[:token].blank? - raise AttrRequired::AttrMissing, 'One of "token", "access_token" and "refresh_token" is required' + raise ArgumentError, 'One of "token", "access_token" and "refresh_token" is required' end params.merge! options diff --git a/spec/rack/oauth2/client_spec.rb b/spec/rack/oauth2/client_spec.rb index 4a2e4d7..806b06a 100644 --- a/spec/rack/oauth2/client_spec.rb +++ b/spec/rack/oauth2/client_spec.rb @@ -448,12 +448,86 @@ end end + describe '#revoke!' do + context 'when access_token given' do + before do + mock_response( + :post, + 'https://server.example.com/oauth2/revoke', + 'blank', + status: 200, + body: { + token: 'access_token', + token_type_hint: 'access_token' + } + ) + end + it do + client.revoke!(access_token: 'access_token').should == :success + end + end + + context 'when refresh_token given' do + before do + mock_response( + :post, + 'https://server.example.com/oauth2/revoke', + 'blank', + status: 200, + body: { + token: 'refresh_token', + token_type_hint: 'refresh_token' + } + ) + end + + context 'as argument' do + it do + client.revoke!(refresh_token: 'refresh_token').should == :success + end + end + + context 'as grant' do + it do + client.refresh_token = 'refresh_token' + client.revoke! + end + end + end + + context 'when error response given' do + before do + mock_response( + :post, + 'https://server.example.com/oauth2/revoke', + 'errors/invalid_request.json', + status: 400 + ) + end + + it do + expect do + client.revoke! access_token: 'access_token' + end.to raise_error Rack::OAuth2::Client::Error + end + end + + context 'when no token given' do + it do + expect do + client.revoke! + end.to raise_error ArgumentError + end + end + end + context 'when no host info' do let :client do Rack::OAuth2::Client.new( identifier: 'client_id', secret: 'client_secret', - redirect_uri: 'https://client.example.com/callback' + redirect_uri: 'https://client.example.com/callback', + revocation_endpoint: '/oauth2/revoke' ) end @@ -468,5 +542,11 @@ expect { client.access_token! }.to raise_error 'No Host Info' end end + + describe '#revoke!' do + it do + expect { client.revoke! access_token: 'access_token' }.to raise_error 'No Host Info' + end + end end end