---
src/spec/models/oauth_client_spec.rb | 61 ++++++++++++++++++++++++++++++++++
src/spec/vcr/cassettes/oauth.yml | 61 ++++++++++++++++++++++++++++++++++
src/spec/vcr_setup.rb | 10 +++++
3 files changed, 132 insertions(+), 0 deletions(-)
create mode 100644 src/spec/models/oauth_client_spec.rb
create mode 100644 src/spec/vcr/cassettes/oauth.yml
diff --git a/src/spec/models/oauth_client_spec.rb b/src/spec/models/oauth_client_spec.rb
new file mode 100644
index 0000000..e479429
--- /dev/null
+++ b/src/spec/models/oauth_client_spec.rb
@@ -0,0 +1,61 @@
+#
+# Copyright (C) 2011 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301, USA. A copy of the GNU General Public License is
+# also available at
http://www.gnu.org/copyleft/gpl.html.
+
+require 'spec_helper'
+require 'oauth_client'
+
+describe OAuthClient do
+
+ before(:each) do
+ # Make sure this is initalized; we don't want test data in our global config
+ SETTINGS_CONFIG[:oauth] ||= {}
+ end
+
+ it "should get authenticated resource with custom header" do
+ # These are the proper configurations
+ SETTINGS_CONFIG[:oauth]['example.com'] = {
+ :consumer_key => 'valid',
+ :consumer_secret => 'good_secret'
+ }
+ client = OAuthClient.new('http://example.com:5000/api')
+ consumer = client.instance_variable_get('@consumer')
+ # We need to stub out sign! since it's otherwise unique on every request
+ consumer.stub('sign!') do |request, *args|
+ request['authorization'] = 'OAuth
oauth_consumer_key="cloud_forms",
oauth_nonce="ieQTc0gxyo6Z4PlQkgPvxdRCyvrtsLAVU9zfVdyDsnE",
oauth_signature="u%2B9yniABqqpQgTAmQQJS5tNEv4g%3D",
oauth_signature_method="HMAC-SHA1", oauth_timestamp="1317068969",
oauth_version="1.0"'
+ request['user-agent'] = 'OAuth gem v0.4.4'
+ end
+ response = client.authenticated_get('/katello/api/organizations',
{'katello-user' => 'admin'})
+ response.code.should match("200")
+ end
+
+ it "should return a 401 for invalid credentials" do
+ SETTINGS_CONFIG[:oauth]['example.com'] = {
+ :consumer_key => 'test',
+ :consumer_secret => 'failure'
+ }
+ client = OAuthClient.new('http://example.com:5000/api')
+ consumer = client.instance_variable_get('@consumer')
+ consumer.stub('sign!') do |request, *args|
+ request['authorization'] = 'OAuth oauth_consumer_key="test",
oauth_nonce="zoT39JgdwI0fjqg9ZfJ2xN4QxyJyIghW7wWUHAxfY",
oauth_signature="UFM87n9CcQXGsOUbHttPJs9NQOg%3D",
oauth_signature_method="HMAC-SHA1", oauth_timestamp="1317068970",
oauth_version="1.0"'
+ request['user-agent'] = 'OAuth gem v0.4.4'
+ end
+ response = client.authenticated_get('/katello/api/organizations',
{'katello-user' => 'admin'})
+ response.code.should match("401")
+ end
+
+end
diff --git a/src/spec/vcr/cassettes/oauth.yml b/src/spec/vcr/cassettes/oauth.yml
new file mode 100644
index 0000000..46ca6ae
--- /dev/null
+++ b/src/spec/vcr/cassettes/oauth.yml
@@ -0,0 +1,61 @@
+---
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :get
+ uri:
http://example.com:5000/katello/api/organizations
+ body:
+ headers:
+ user-agent:
+ - OAuth gem v0.4.4
+ authorization:
+ - OAuth oauth_consumer_key="cloud_forms",
oauth_nonce="ieQTc0gxyo6Z4PlQkgPvxdRCyvrtsLAVU9zfVdyDsnE",
oauth_signature="u%2B9yniABqqpQgTAmQQJS5tNEv4g%3D",
oauth_signature_method="HMAC-SHA1", oauth_timestamp="1317068969",
oauth_version="1.0"
+ katello-user:
+ - admin
+ response: !ruby/struct:VCR::Response
+ status: !ruby/struct:VCR::ResponseStatus
+ code: 200
+ message: OK
+ headers:
+ x-ua-compatible:
+ - IE=Edge,chrome=1
+ etag:
+ - "\"4f3973e2332582b0da3bec1870776fbb\""
+ content-type:
+ - application/json; charset=utf-8
+ x-runtime:
+ - "0.058187"
+ server:
+ - thin 1.2.11 codename Bat-Shit Crazy
+ cache-control:
+ - max-age=0, private, must-revalidate
+ body:
"[{\"name\":\"ACME_Corporation\",\"created_at\":\"2011-09-20T17:45:42Z\",\"updated_at\":\"2011-09-20T17:45:42Z\",\"id\":1,\"cp_key\":\"ACME_Corporation\",\"description\":\"ACME
Corporation Organization\"}]"
+ http_version: "1.1"
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :get
+ uri:
http://example.com:5000/katello/api/organizations
+ body:
+ headers:
+ user-agent:
+ - OAuth gem v0.4.4
+ katello-user:
+ - admin
+ authorization:
+ - OAuth oauth_consumer_key="test",
oauth_nonce="zoT39JgdwI0fjqg9ZfJ2xN4QxyJyIghW7wWUHAxfY",
oauth_signature="UFM87n9CcQXGsOUbHttPJs9NQOg%3D",
oauth_signature_method="HMAC-SHA1", oauth_timestamp="1317068970",
oauth_version="1.0"
+ response: !ruby/struct:VCR::Response
+ status: !ruby/struct:VCR::ResponseStatus
+ code: 401
+ message: Unauthorized
+ headers:
+ x-ua-compatible:
+ - IE=Edge,chrome=1
+ content-type:
+ - text/html; charset=utf-8
+ server:
+ - thin 1.2.11 codename Bat-Shit Crazy
+ x-runtime:
+ - "0.017754"
+ cache-control:
+ - no-cache
+ body: " "
+ http_version: "1.1"
diff --git a/src/spec/vcr_setup.rb b/src/spec/vcr_setup.rb
index 6163ac7..0b65432 100644
--- a/src/spec/vcr_setup.rb
+++ b/src/spec/vcr_setup.rb
@@ -61,3 +61,13 @@ DeployableXML.class_eval do
end
end
end
+
+require 'oauth'
+OAuth::AccessToken.class_eval do
+ def request(method, path, *args)
+ # By default, headers are ignored:
https://www.relishapp.com/myronmarston/vcr/v/1-6-0/docs/cassettes/request...
+ VCR.use_cassette('oauth', :record => :new_episodes, :match_requests_on
=> [:method, :uri, :body, :headers]) do
+ super(method, path, *args)
+ end
+ end
+end
--
1.7.6.2