diff --git a/spec/integration/info_spec.rb b/spec/integration/info_spec.rb index bcf2fd115c..50eb69ed74 100644 --- a/spec/integration/info_spec.rb +++ b/spec/integration/info_spec.rb @@ -1,16 +1,58 @@ require "spec_helper" require "net/http" +require "uri" describe "Cloud controller", :type => :integration do start_nats start_cc it "responds to /info" do - result = Net::HTTP.get_response(URI.parse("http://localhost:8181/info")) - result.code.should == "200" + make_http_request("/info").tap do |r| + r.code.should == "200" + r.json_body["version"].should == 2 + end + end + + it "authenticate and authorize with valid token" do + unauthorized_token = {"Authorization" => "bearer unauthorized-token"} + make_http_request("/v2/stacks", unauthorized_token).tap do |r| + r.code.should == "401" + end + + authorized_token = {"Authorization" => "bearer #{admin_token}"} + make_http_request("/v2/stacks", authorized_token).tap do |r| + r.code.should == "200" + r.json_body["resources"].should be_a(Array) + end + end + + def admin_token + token = { + "aud" => "cloud_controller", + "exp" => Time.now.to_i + 10_000, + "client_id" => Sham.guid, + "scope" => ["cloud_controller.admin"], + } + CF::UAA::TokenCoder.encode(token, :skey => "tokensecret", :algorithm => "HS256") + end - JSON.parse(result.body).tap do |r| - r["version"].should == 2 + def make_http_request(path, headers = {}) + url = URI.parse("http://localhost:8181#{path}") + + response = Net::HTTP.new(url.host, url.port).start do |http| + request = Net::HTTP::Get.new(url.path) + headers.each do |name, value| + request.add_field(name, value) + end + http.request(request) end + + response.extend(Module.new do + def json_body + @json_body ||= JSON.parse(body) + end + end) + + response end end diff --git a/spec/vcap/uaa_util_spec.rb b/spec/vcap/uaa_util_spec.rb index cd7abaf96d..ee03ae6938 100644 --- a/spec/vcap/uaa_util_spec.rb +++ b/spec/vcap/uaa_util_spec.rb @@ -2,7 +2,6 @@ require File.expand_path("../spec_helper", __FILE__) require "vcap/uaa_util" -require "openssl" module VCAP describe UaaTokenDecoder do