diff options
| author | Marc Florisson | 2012-12-03 17:14:25 +0100 |
|---|---|---|
| committer | Marc Florisson | 2012-12-03 17:14:25 +0100 |
| commit | b07166f27d64a4847ab2a1c439cb94b522b876e4 (patch) | |
| tree | 888684a427895a5363e33ec4a4ad8bd8e2c984d4 | |
| parent | 966a9f62b3ffd8029c0104cee0d63d98582d95d9 (diff) | |
| download | chouette-core-b07166f27d64a4847ab2a1c439cb94b522b876e4.tar.bz2 | |
add some specs
| -rw-r--r-- | app/controllers/api/v1/chouette_controller.rb | 21 | ||||
| -rw-r--r-- | app/controllers/api/v1/networks_controller.rb | 2 | ||||
| -rw-r--r-- | config/routes.rb | 13 | ||||
| -rw-r--r-- | spec/controllers/api/v1/networks_controller_spec.rb | 71 |
4 files changed, 89 insertions, 18 deletions
diff --git a/app/controllers/api/v1/chouette_controller.rb b/app/controllers/api/v1/chouette_controller.rb index 401625da8..f307e6755 100644 --- a/app/controllers/api/v1/chouette_controller.rb +++ b/app/controllers/api/v1/chouette_controller.rb @@ -3,27 +3,20 @@ module Api class ChouetteController < ActionController::Base respond_to :json, :xml layout false - before_filter :restrict_access_and_switch + before_filter :authenticate def referential - @referential ||= organisation.referentials.find_by_id @referential_id - end - def organisation - @organisation ||= Organisation.find_by_id @organisation_id - end - + @referential ||= @api_key.referential + end private - def restrict_access_and_switch + def authenticate authenticate_or_request_with_http_token do |token, options| - switch_referential if key_exists?( token) + @api_key = ApiKey.new(token) + switch_referential if @api_key.exists? end end - def key_exists?( token) - @organisation_id, @referential_id = token.split('-') - organisation && referential - end def switch_referential - Apartment::Database.switch(referential.slug) + Apartment::Database.switch(@api_key.referential_slug) end end diff --git a/app/controllers/api/v1/networks_controller.rb b/app/controllers/api/v1/networks_controller.rb index c9cb6345e..c3d52348b 100644 --- a/app/controllers/api/v1/networks_controller.rb +++ b/app/controllers/api/v1/networks_controller.rb @@ -1,6 +1,6 @@ module Api module V1 - class NetworksController < ActionController::Base + class NetworksController < ChouetteController def networks @networks ||= referential.networks end diff --git a/config/routes.rb b/config/routes.rb index c6569a785..344426c6c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,10 +7,17 @@ ChouetteIhm::Application.routes.draw do namespace :api do namespace :v1 do - resources :networks - resources :lines do - resources :routes + resources :networks, :only => [:index, :show] + resources :lines, :only => [:index, :show] do + resources :journey_patterns, :only => [:index, :show] + resources :routes, :only => [:index, :show] do + resources :journey_patterns, :only => [:index, :show] + resources :stop_areas, :only => [:index, :show] + end end + resources :routes, :only => :show + resources :journey_patterns, :only => :show + resources :stop_areas, :only => :show end end diff --git a/spec/controllers/api/v1/networks_controller_spec.rb b/spec/controllers/api/v1/networks_controller_spec.rb new file mode 100644 index 000000000..3047d2b63 --- /dev/null +++ b/spec/controllers/api/v1/networks_controller_spec.rb @@ -0,0 +1,71 @@ +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 + 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 + 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" + end + it "should assign expected api_key" do + get :index + assigns[:api_key].should eql(api_key) + end + it "should assign expected referential" do + get :index + assigns[:referential].should == api_key.referential + end + it "should assign referntial's networks" do + get :index + assigns[:networks].should == api_key.referential.networks + 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 + context "the request format is XML" do + before(@each) do + request.accept = "application/xml" + end + it "should return HTTP 200" do + get :index + response.response_code.should == 200 + end + end + context "the request format is JSON" do + before(@each) do + request.accept = "application/json" + end + it "should return HTTP 200" do + get :index + response.response_code.should == 200 + end + end + end +end |
