Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Fix and simplify shared resources OAuth specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
tszolar committed Nov 12, 2015
1 parent d7a839a commit e645916
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 36 deletions.
31 changes: 0 additions & 31 deletions spec/support/shared_context_for_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,3 @@ def auth_get(path, **params)
get path, params.merge(access_token: access_token)
end
end

shared_context 'authenticated via oauth', authenticated: :oauth do
let(:access_token) { retrieve_oauth_token }

def auth_get(path, **params)
get path, params.merge(access_token: access_token)
end

def infer_cassette_name
group_descriptions = []
klass = self

while klass.respond_to?(:metadata) && klass.metadata
group_descriptions << klass.metadata[:example_group][:description]
klass = klass.superclass
end

group_descriptions.compact.reverse.join('/')
end

def retrieve_oauth_token
client = Faraday.new do |c|
c.response :json, content_type: /\bjson$/
c.request :url_encoded
c.adapter Faraday.default_adapter
end
client.basic_auth(Config.oauth_client_id, Config.oauth_client_secret)
resp = client.post(Config.oauth_token_uri, { 'grant_type' => 'client_credentials' })
resp.body['access_token']
end
end
13 changes: 8 additions & 5 deletions spec/support/shared_examples_for_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@

shared_examples 'secured resource' do

let(:dummy_access_token) { 'eecea925-7ce1-4297-8ac4-d82446a9caa7' }
let(:token_info) { double(:token_info, user_name: '', scope: ['cvut:sirius:all:read'], status: 200, client_id: 'dummy', exp: Time.now + 1.hour) }

it 'is not accessible without access token' do
get path_for(path)
expect(status).to eql 401
Expand All @@ -40,11 +43,11 @@
expect(status).to eql 200
end

it 'is accessible with oauth token', authenticated: :oauth do
VCR.use_cassette(infer_cassette_name, :record => :new_episodes) do
auth_get path_for(path)
expect(status).to eql 200
end
it 'is accessible with oauth token' do
# XXX: Using `allow_any_instance_of` stub is dirty. Could it be handled some other way?
allow_any_instance_of(SiriusApi::Strategies::RemoteOAuthServer).to receive(:request_token_info).and_return(token_info)
get path_for(path), access_token: dummy_access_token
expect(status).to eql 200
end
end

Expand Down

0 comments on commit e645916

Please sign in to comment.