diff options
| author | Marc Florisson | 2012-12-04 17:55:50 +0100 | 
|---|---|---|
| committer | Marc Florisson | 2012-12-04 17:55:50 +0100 | 
| commit | 2570573d472010249b753e404c6cad003a78ad39 (patch) | |
| tree | 44d78be5a9352bc7861b23ad39ccef77b803793a /spec/controllers/api | |
| parent | b07166f27d64a4847ab2a1c439cb94b522b876e4 (diff) | |
| download | chouette-core-2570573d472010249b753e404c6cad003a78ad39.tar.bz2 | |
refactor api/v1/networks_controller specs
Diffstat (limited to 'spec/controllers/api')
| -rw-r--r-- | spec/controllers/api/v1/networks_controller_spec.rb | 96 | 
1 files changed, 52 insertions, 44 deletions
| diff --git a/spec/controllers/api/v1/networks_controller_spec.rb b/spec/controllers/api/v1/networks_controller_spec.rb index 3047d2b63..76ff7fa44 100644 --- a/spec/controllers/api/v1/networks_controller_spec.rb +++ b/spec/controllers/api/v1/networks_controller_spec.rb @@ -4,68 +4,76 @@ require 'spec_helper'  describe Api::V1::NetworksController do    let!(:organisation) {Organisation.find_by_name("first") || create(:organisation, :name => "first")}    let!(:referential) {Referential.find_by_name("first") || create(:referential, :organisation => organisation)} +    let!(:network) {referential.networks.where(:name => "first") || create(:network, :referential => referential)} -  context "no authorization provided" do -    it "should return HTTP 401" do -      get :index -      response.response_code.should == 401 -    end +  def authorization_context(api_key) +    request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials( api_key.token) +    request.accept = "application/json"    end -  context "an invalid authorization is provided" do -    before(@each) do -      @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials("dummy") -    end -    it "should return HTTP 401" do -      get :index -      response.response_code.should == 401 +  shared_examples "authorization provided" do |format| +    before :each do +      request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials( api_key.token) +      request.accept = format +      action.call      end -  end -  context "a valid authorization is provided" do -    let!(:api_key) {Api::V1::ApiKey.create( organisation, referential)} -    before(@each) do -      request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials( api_key.token) -      request.accept = "application/json" +    def json_xml_format? +      request.accept == "application/json" || request.accept == "application/xml"      end      it "should assign expected api_key" do -      get :index -      assigns[:api_key].should eql(api_key)  +      assigns[:api_key].should eql(api_key) if json_xml_format?       end      it "should assign expected referential" do -      get :index -      assigns[:referential].should == api_key.referential +      assigns[:referential].should == api_key.referential if json_xml_format?      end -    it "should assign referntial's networks" do -      get :index -      assigns[:networks].should == api_key.referential.networks +    it "should return HTTP 200 if json or xml format, HTTP 406 otherwise" do +      response.response_code.should == (json_xml_format? ? 200 : 406)      end -    context "the request format is HTML" do -      before(@each) do -        request.accept = "application/html" -      end -      it "should return HTTP 406" do -        get :index -        response.response_code.should == 406 -      end +  end +  shared_examples "authorization required" do +    before :each do +      action.call      end -    context "the request format is XML" do -      before(@each) do -        request.accept = "application/xml" +    context "when no authorization is provided" do +      it "should return HTTP 401" do +        request.env['HTTP_AUTHORIZATION'] = nil +        response.response_code.should == 401        end -      it "should return HTTP 200" do -        get :index -        response.response_code.should == 200 +    end +    context "when an invalid authorization is provided" do +      it "should return HTTP 401" do +        request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials("dummy") +        response.response_code.should == 401        end      end -    context "the request format is JSON" do -      before(@each) do -        request.accept = "application/json" +  end +  describe "show" do +    it_behaves_like "authorization required" do +      let(:action) { Proc.new { get :show, :id => network.id } } +    end +  end +  describe "index" do +    ["application/xml", "application/json", "application/html"].each do |format| +      it_behaves_like "authorization provided", format do +        let(:api_key) {Api::V1::ApiKey.create( organisation, referential)} +        let(:action) { Proc.new { get :index } }        end -      it "should return HTTP 200" do +    end +    it_behaves_like "authorization required" do +      let(:action) { Proc.new { get :index } } +    end + +    context "when authorization is valid" do +      let(:api_key) {Api::V1::ApiKey.create( organisation, referential)} +      before :each do +        authorization_context(api_key)          get :index -        response.response_code.should == 200 +      end +      it "should assigns networks with network" do +        assigns[:networks].should == referential.networks        end      end +    end  end | 
