diff options
| author | Marc Florisson | 2012-12-10 17:26:01 +0100 |
|---|---|---|
| committer | Marc Florisson | 2012-12-10 17:26:01 +0100 |
| commit | e059278a5ebaa2ccaf5547f328ecbefb06701655 (patch) | |
| tree | 3ee50a3ce60ba329bb2d9681c61105292f14bd63 /spec/support | |
| parent | f2b63fddd0e93c63a43c99afe5473e1760a6e7b2 (diff) | |
| download | chouette-core-e059278a5ebaa2ccaf5547f328ecbefb06701655.tar.bz2 | |
refactor spec
Diffstat (limited to 'spec/support')
| -rw-r--r-- | spec/support/api_key.rb | 42 | ||||
| -rw-r--r-- | spec/support/api_key_protected.rb | 51 |
2 files changed, 93 insertions, 0 deletions
diff --git a/spec/support/api_key.rb b/spec/support/api_key.rb new file mode 100644 index 000000000..8c025bbad --- /dev/null +++ b/spec/support/api_key.rb @@ -0,0 +1,42 @@ +module ApiKeyHelper + + def get_api_key + Api::V1::ApiKey.create( referential.organisation, referential) + end + def config_formatted_request_with_authorization( format) + request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials( get_api_key.token) + request.accept = format + end + def config_formatted_request_with_dummy_authorization( format) + request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials( "dummy") + request.accept = format + end + def config_formatted_request_without_authorization( format) + request.env['HTTP_AUTHORIZATION'] = nil + request.accept = format + end + def json_xml_format? + request.accept == "application/json" || request.accept == "application/xml" + end + + def self.included(base) + base.class_eval do + extend ClassMethods + alias_method :api_key, :get_api_key + end + end + + module ClassMethods + def assign_api_key + before(:each) do + assign :api_key, api_key + end + end + end + +end + +RSpec.configure do |config| + config.include ApiKeyHelper +end + diff --git a/spec/support/api_key_protected.rb b/spec/support/api_key_protected.rb new file mode 100644 index 000000000..43256716d --- /dev/null +++ b/spec/support/api_key_protected.rb @@ -0,0 +1,51 @@ +shared_examples "api key protected controller" do + + let(:h) { { :index => (Proc.new { get :index }), + :show => (Proc.new { get :show, :id => data.objectid })}} + [:index, :show].each do |http_verb| + + describe "GET ##{http_verb}" do + ["application/json","application/xml","application/html"].each do |format| + context "when an invalid authorization is provided" do + before :each do + config_formatted_request_with_dummy_authorization( format) + h[http_verb].call + end + it "should return HTTP 401" do + response.response_code.should == 401 + end + end + context "when no authorization is provided" do + before :each do + config_formatted_request_without_authorization( format) + h[http_verb].call + end + it "should return HTTP 401" do + response.response_code.should == 401 + end + end + context "when authorization provided and request.accept is #{format}," do + before :each do + config_formatted_request_with_authorization( format) + h[http_verb].call + end + + it "should assign expected api_key" do + assigns[:api_key].should eql(api_key) if json_xml_format? + end + it "should assign expected referential" do + assigns[:referential].should == api_key.referential if json_xml_format? + end + + it "should return #{(format == "application/json" || format == "application/xml") ? "success" : "failure"} response" do + if json_xml_format? + response.should be_success + else + response.should_not be_success + end + end + end + end + end + end +end |
