diff options
| author | Marc Florisson | 2012-12-07 18:42:03 +0100 |
|---|---|---|
| committer | Marc Florisson | 2012-12-07 18:42:03 +0100 |
| commit | ca0118ec7947777a382840027f9a001803510382 (patch) | |
| tree | 30f1234dec6b2d74d900c4e221c9e9c913d09010 /spec/controllers/api/v1 | |
| parent | 0b24329d0fc53a226e801f7626d690a59be3d29d (diff) | |
| download | chouette-core-ca0118ec7947777a382840027f9a001803510382.tar.bz2 | |
refactor api/V1 spec
Diffstat (limited to 'spec/controllers/api/v1')
| -rw-r--r-- | spec/controllers/api/v1/networks_controller_spec.rb | 137 |
1 files changed, 78 insertions, 59 deletions
diff --git a/spec/controllers/api/v1/networks_controller_spec.rb b/spec/controllers/api/v1/networks_controller_spec.rb index 76ff7fa44..edbc15284 100644 --- a/spec/controllers/api/v1/networks_controller_spec.rb +++ b/spec/controllers/api/v1/networks_controller_spec.rb @@ -1,79 +1,98 @@ 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 "organisation and referntial" do + let!(:organisation) {Organisation.find_by_name("first") || create(:organisation, :name => "first")} + let!(:referential) {Referential.find_by_name("first") || create(:referential, :organisation => organisation)} + let!(:api_key) {Api::V1::ApiKey.create( organisation, referential)} + let!(:network) { + Apartment::Database.switch(referential.slug) + referential.networks.first || create(:network) + } - def authorization_context(api_key) - request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials( api_key.token) - request.accept = "application/json" - end - shared_examples "authorization provided" do |format| - before :each do + def provide_authorization( api_key, format) request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials( api_key.token) request.accept = format - action.call end - def json_xml_format? request.accept == "application/json" || request.accept == "application/xml" 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 HTTP 200 if json or xml format, HTTP 406 otherwise" do - response.response_code.should == (json_xml_format? ? 200 : 406) - end - end - shared_examples "authorization required" do - before :each do - action.call - end - context "when no authorization is provided" do - it "should return HTTP 401" do - request.env['HTTP_AUTHORIZATION'] = nil - response.response_code.should == 401 - end - 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 + + let(:h) { { :index => (Proc.new { get :index }), + :show => (Proc.new { get :show, :id => network.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 + request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials("dummy") + request.accept = 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 + request.env['HTTP_AUTHORIZATION'] = nil + request.accept = 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 + provide_authorization(api_key, 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 - 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 } } + + describe "GET #show" do + context "when authorization provided and request.accept is json" do + before :each do + provide_authorization(api_key, "application/json") + get :show, :id => network.objectid + end + + it "should assign expected network" do + assigns[:network].should == network + end end end - it_behaves_like "authorization required" do - let(:action) { Proc.new { get :index } } - end + describe "GET #index" do + context "when authorization provided and request.accept is json" do + before :each do + provide_authorization(api_key, "application/json") + 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 - end - it "should assigns networks with network" do - assigns[:networks].should == referential.networks + it "should assign expected networks" do + assigns[:networks].should == [network] + end end end - end end |
