Refactored some tests code to bypass VCR request matching problem
https://bugzilla.redhat.com/show_bug.cgi?id=791216 --- spec/models/factory/base_spec.rb | 74 ++++++++++++++++------------ spec/spec_helper.rb | 1 + spec/vcr/cassettes/oauth_fail_invalid.yml | 30 +++++++++++ spec/vcr/cassettes/oauth_fail_no.yml | 22 ++++++++ spec/vcr/cassettes/oauth_success_valid.yml | 30 +++++++++++ spec/vcr_setup.rb | 8 --- 6 files changed, 126 insertions(+), 39 deletions(-) create mode 100644 spec/vcr/cassettes/oauth_fail_invalid.yml create mode 100644 spec/vcr/cassettes/oauth_fail_no.yml create mode 100644 spec/vcr/cassettes/oauth_success_valid.yml
diff --git a/spec/models/factory/base_spec.rb b/spec/models/factory/base_spec.rb index 86b8a70..7dadc1a 100644 --- a/spec/models/factory/base_spec.rb +++ b/spec/models/factory/base_spec.rb @@ -42,41 +42,53 @@ describe Aeolus::Image::Factory::Base do Aeolus::Image::Factory::Base.use_oauth?.should be_true end
- it "should succeed with valid OAuth credentials" do - Aeolus::Image::Factory::Base.config = { - :site => 'http://127.0.0.1:8075/imagefactory', - :consumer_key => 'mock-key', - :consumer_secret => 'mock-secret' - } - template = "<template>\n <name>f14jeos</name>\n <os>\n <name>Fedora</name>\n <version>14</version>\n <arch>x86_64</arch>\n <install type='url'>\n <url>http://download.fedoraproject.org/pub/fedora/linux/releases/14/Fedora/x86_64...</url>\n </install>\n </os>\n <description>Fedora 14</description>\n</template>\n" - img = Aeolus::Image::Factory::Image.new(:targets => 'ec2', :template => template) - img.save!.should be_true + context do + use_vcr_cassette "oauth_success_valid" + + it "should succeed with valid OAuth credentials" do + Aeolus::Image::Factory::Base.config = { + :site => 'http://127.0.0.1:8075/imagefactory', + :consumer_key => 'mock-key', + :consumer_secret => 'mock-secret' + } + template = "<template>\n <name>f14jeos</name>\n <os>\n <name>Fedora</name>\n <version>14</version>\n <arch>x86_64</arch>\n <install type='url'>\n <url>http://download.fedoraproject.org/pub/fedora/linux/releases/14/Fedora/x86_64...</url>\n </install>\n </os>\n <description>Fedora 14</description>\n</template>\n" + img = Aeolus::Image::Factory::Image.new(:targets => 'ec2', :template => template) + img.save!.should be_true + end end
- it "should fail with invalid OAuth credentials" do - Aeolus::Image::Factory::Base.config = { - :site => 'http://127.0.0.1:8075/imagefactory', - :consumer_key => 'mock-key', - :consumer_secret => 'wrong-secret' - } - template = "<template>\n <name>f14jeos</name>\n <os>\n <name>Fedora 2</name>\n <version>14</version>\n <arch>x86_64</arch>\n <install type='url'>\n <url>http://download.fedoraproject.org/pub/fedora/linux/releases/14/Fedora/x86_64...</url>\n </install>\n </os>\n <description>Fedora 14</description>\n</template>\n" - img = Aeolus::Image::Factory::Image.new(:targets => 'ec2', :template => template) - lambda { - img.save! - }.should raise_error(ActiveResource::UnauthorizedAccess) + context do + use_vcr_cassette "oauth_fail_invalid" + + it "should fail with invalid OAuth credentials" do + Aeolus::Image::Factory::Base.config = { + :site => 'http://127.0.0.1:8075/imagefactory', + :consumer_key => 'mock-key', + :consumer_secret => 'wrong-secret' + } + template = "<template>\n <name>f14jeos</name>\n <os>\n <name>Fedora 2</name>\n <version>14</version>\n <arch>x86_64</arch>\n <install type='url'>\n <url>http://download.fedoraproject.org/pub/fedora/linux/releases/14/Fedora/x86_64...</url>\n </install>\n </os>\n <description>Fedora 14</description>\n</template>\n" + img = Aeolus::Image::Factory::Image.new(:targets => 'ec2', :template => template) + lambda { + img.save! + }.should raise_error(ActiveResource::UnauthorizedAccess) + end end
- it "should fail with no OAuth credentials" do - Aeolus::Image::Factory::Base.config = { - :site => 'http://127.0.0.1:8075/imagefactory' - } - template = "<template>\n <name>f14jeos</name>\n <os>\n <name>Fedora 3</name>\n <version>14</version>\n <arch>x86_64</arch>\n <install type='url'>\n <url>http://download.fedoraproject.org/pub/fedora/linux/releases/14/Fedora/x86_64...</url>\n </install>\n </os>\n <description>Fedora 14</description>\n</template>\n" - img = Aeolus::Image::Factory::Image.new(:targets => 'ec2', :template => template) - # ServerError is actually not what _should_ be returned, but a bug means that's what we get at the moment. - # When the bug is fixed, we need to update to test for that. - lambda { - img.save!.should be_false - }.should raise_error(ActiveResource::ServerError) + context do + use_vcr_cassette "oauth_fail_no" + + it "should fail with no OAuth credentials" do + Aeolus::Image::Factory::Base.config = { + :site => 'http://127.0.0.1:8075/imagefactory' + } + template = "<template>\n <name>f14jeos</name>\n <os>\n <name>Fedora 3</name>\n <version>14</version>\n <arch>x86_64</arch>\n <install type='url'>\n <url>http://download.fedoraproject.org/pub/fedora/linux/releases/14/Fedora/x86_64...</url>\n </install>\n </os>\n <description>Fedora 14</description>\n</template>\n" + img = Aeolus::Image::Factory::Image.new(:targets => 'ec2', :template => template) + # ServerError is actually not what _should_ be returned, but a bug means that's what we get at the moment. + # When the bug is fixed, we need to update to test for that. + lambda { + img.save!.should be_false + }.should raise_error(ActiveResource::ServerError) + end end
end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bd4a318..7761ead 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -28,6 +28,7 @@ require 'warehouse/provider_image' require 'warehouse/template'
RSpec.configure do |config| + config.extend VCR::RSpec::Macros config.before(:all) do # Aeolus::Image::BaseCommand.class_eval do # def load_config diff --git a/spec/vcr/cassettes/oauth_fail_invalid.yml b/spec/vcr/cassettes/oauth_fail_invalid.yml new file mode 100644 index 0000000..d7488e4 --- /dev/null +++ b/spec/vcr/cassettes/oauth_fail_invalid.yml @@ -0,0 +1,30 @@ +--- +- !ruby/struct:VCR::HTTPInteraction + request: !ruby/struct:VCR::Request + method: :post + uri: http://127.0.0.1:8075/imagefactory/images + body: "{"image":{"targets":"ec2","template":"<template>\n <name>f14jeos</name>\n <os>\n <name>Fedora 2</name>\n <version>14</version>\n <arch>x86_64</arch>\n <install type='url'>\n <url>http://download.fedoraproject.org/pub/fedora/linux/releases/14/Fedora/x86_64...</url>\n </install>\n </os>\n <description>Fedora 14</description>\n</template>\n"}}" + headers: + content-type: + - application/json + user-agent: + - OAuth gem v0.4.4 + authorization: + - OAuth oauth_body_hash="tEAlC3IwCGiB%2FNSQRUOwBifTHmY%3D", oauth_consumer_key="mock-key", oauth_nonce="jeQp5OlYxoCbbflwdM9nfg306BUVfmidPYKV7qPOs", oauth_signature="ERZpwVUR4qdBjfeBTJQtem%2FnKTo%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1318873100", oauth_version="1.0" + content-length: + - "367" + response: !ruby/struct:VCR::Response + status: !ruby/struct:VCR::ResponseStatus + code: 401 + message: Unauthorized + headers: + content-type: + - text/html; charset=UTF-8 + date: + - Mon, 17 Oct 2011 17:38:15 GMT + server: + - PasteWSGIServer/0.5 Python/2.7.1 + content-length: + - "0" + body: + http_version: "1.1" diff --git a/spec/vcr/cassettes/oauth_fail_no.yml b/spec/vcr/cassettes/oauth_fail_no.yml new file mode 100644 index 0000000..3490391 --- /dev/null +++ b/spec/vcr/cassettes/oauth_fail_no.yml @@ -0,0 +1,22 @@ +--- +- !ruby/struct:VCR::HTTPInteraction + request: !ruby/struct:VCR::Request + method: :post + uri: http://127.0.0.1:8075/imagefactory/images + body: "{"image":{"targets":"ec2","template":"<template>\n <name>f14jeos</name>\n <os>\n <name>Fedora 3</name>\n <version>14</version>\n <arch>x86_64</arch>\n <install type='url'>\n <url>http://download.fedoraproject.org/pub/fedora/linux/releases/14/Fedora/x86_64...</url>\n </install>\n </os>\n <description>Fedora 14</description>\n</template>\n"}}" + headers: + content-type: + - application/json + response: !ruby/struct:VCR::Response + status: !ruby/struct:VCR::ResponseStatus + code: 500 + message: Internal Server Error + headers: + content-type: + - text/html; charset=UTF-8 + server: + - PasteWSGIServer/0.5 Python/2.7.1 + date: + - Mon, 17 Oct 2011 17:38:15 GMT + body: "'NoneType' object has no attribute '_get_timestamp_nonce'" + http_version: "1.1" diff --git a/spec/vcr/cassettes/oauth_success_valid.yml b/spec/vcr/cassettes/oauth_success_valid.yml new file mode 100644 index 0000000..17da22d --- /dev/null +++ b/spec/vcr/cassettes/oauth_success_valid.yml @@ -0,0 +1,30 @@ +--- +- !ruby/struct:VCR::HTTPInteraction + request: !ruby/struct:VCR::Request + method: :post + uri: http://127.0.0.1:8075/imagefactory/images + body: "{"image":{"targets":"ec2","template":"<template>\n <name>f14jeos</name>\n <os>\n <name>Fedora</name>\n <version>14</version>\n <arch>x86_64</arch>\n <install type='url'>\n <url>http://download.fedoraproject.org/pub/fedora/linux/releases/14/Fedora/x86_64...</url>\n </install>\n </os>\n <description>Fedora 14</description>\n</template>\n"}}" + headers: + user-agent: + - OAuth gem v0.4.4 + content-type: + - application/json + authorization: + - OAuth oauth_body_hash="iLFFmVptnUF4tv9PvDNdNxK%2FSOs%3D", oauth_consumer_key="mock-key", oauth_nonce="dESbWyTyKA0UTHekLdt7DOSOFh6tYU39Mw7LpFkts", oauth_signature="ydhBMjipuQ7ARZT5xfOrvShZfHI%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1318873100", oauth_version="1.0" + content-length: + - "365" + response: !ruby/struct:VCR::Response + status: !ruby/struct:VCR::ResponseStatus + code: 202 + message: Accepted + headers: + content-type: + - application/json + server: + - PasteWSGIServer/0.5 Python/2.7.1 + date: + - Mon, 17 Oct 2011 17:38:15 GMT + content-length: + - "640" + body: "{"_type": "image", "href": "http://127.0.0.1:8075/imagefactory/images/18f1b17e-3fdc-4931-a5d6-f4581e5f5f...", "id": "18f1b17e-3fdc-4931-a5d6-f4581e5f5f85", "build": {"target_images": [{"_type": "target_image", "href": "http://127.0.0.1:8075/imagefactory/images/18f1b17e-3fdc-4931-a5d6-f4581e5f5f...", "id": "dd9c27c5-0bdb-4670-b172-46a3cf2101a6"}], "_type": "build", "href": "http://127.0.0.1:8075/imagefactory/images/18f1b17e-3fdc-4931-a5d6-f4581e5f5f...", "id": "6d715c2f-8dc5-46eb-86ad-509366e64252"}}" + http_version: "1.1" diff --git a/spec/vcr_setup.rb b/spec/vcr_setup.rb index 0216c9b..b07d4ed 100644 --- a/spec/vcr_setup.rb +++ b/spec/vcr_setup.rb @@ -24,11 +24,3 @@ VCR.config do |config| config.allow_http_connections_when_no_cassette = true end
-ActiveResource::Connection.class_eval do - def request(method, path, *args) - # We want to add :body, but not :headers because they're too fickle with OAuth - VCR.use_cassette('oauth', :record => :new_episodes, :match_requests_on => [:method, :uri, :body]) do - request_with_oauth(method, path, *args) - end - end -end