aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers/api/v1
diff options
context:
space:
mode:
authorMarc Florisson2012-12-10 17:26:01 +0100
committerMarc Florisson2012-12-10 17:26:01 +0100
commite059278a5ebaa2ccaf5547f328ecbefb06701655 (patch)
tree3ee50a3ce60ba329bb2d9681c61105292f14bd63 /spec/controllers/api/v1
parentf2b63fddd0e93c63a43c99afe5473e1760a6e7b2 (diff)
downloadchouette-core-e059278a5ebaa2ccaf5547f328ecbefb06701655.tar.bz2
refactor spec
Diffstat (limited to 'spec/controllers/api/v1')
-rw-r--r--spec/controllers/api/v1/lines_controller_spec.rb15
-rw-r--r--spec/controllers/api/v1/networks_controller_spec.rb105
2 files changed, 36 insertions, 84 deletions
diff --git a/spec/controllers/api/v1/lines_controller_spec.rb b/spec/controllers/api/v1/lines_controller_spec.rb
new file mode 100644
index 000000000..5bd8cac57
--- /dev/null
+++ b/spec/controllers/api/v1/lines_controller_spec.rb
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+describe Api::V1::LinesController do
+ let!(:line) { referential.lines.first || create(:line) }
+
+ it_behaves_like "api key protected controller" do
+ let(:data){line}
+ end
+ describe "GET #index" do
+ it "test" do
+ puts referential.inspect
+ puts "in spec api_key=#{api_key.inspect}"
+ end
+ end
+end
diff --git a/spec/controllers/api/v1/networks_controller_spec.rb b/spec/controllers/api/v1/networks_controller_spec.rb
index edbc15284..01e3ee35d 100644
--- a/spec/controllers/api/v1/networks_controller_spec.rb
+++ b/spec/controllers/api/v1/networks_controller_spec.rb
@@ -1,98 +1,35 @@
require 'spec_helper'
describe Api::V1::NetworksController do
+ let!(:network) { referential.networks.first || create(:network) }
- 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 provide_authorization( api_key, format)
- request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials( api_key.token)
- request.accept = format
- end
- def json_xml_format?
- request.accept == "application/json" || request.accept == "application/xml"
- end
-
- 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_behaves_like "api key protected controller" do
+ let(:data){network}
+ 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
+ describe "GET #show" do
+ context "when authorization provided and request.accept is json" do
+ before :each do
+ config_formatted_request_with_authorization( "application/json")
+ get :show, :id => network.objectid
end
- end
-
- 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
+ it "should assign expected network" do
+ assigns[:network].should == network
end
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
+ end
+ describe "GET #index" do
+ context "when authorization provided and request.accept is json" do
+ before :each do
+ config_formatted_request_with_authorization( "application/json")
+ get :index
+ end
- it "should assign expected networks" do
- assigns[:networks].should == [network]
- end
+ it "should assign expected networks" do
+ assigns[:networks].should == [network]
end
end
end
+
end