From b07166f27d64a4847ab2a1c439cb94b522b876e4 Mon Sep 17 00:00:00 2001 From: Marc Florisson Date: Mon, 3 Dec 2012 17:14:25 +0100 Subject: add some specs --- .../controllers/api/v1/networks_controller_spec.rb | 71 ++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 spec/controllers/api/v1/networks_controller_spec.rb (limited to 'spec/controllers/api') 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 -- cgit v1.2.3