require 'spec_helper' describe "CaseblocksAPI" do describe "When authorising with CaseBlocks" do context "when I have an account" do it "should respond with and auth token" do CaseblocksAPI::Client.stub(:post).and_return({"token" => "some_token"}) username = "mark@local.me" password = "password1" caseblocks = CaseblocksAPI::Client.new(username, password, "http://localhost:3000") caseblocks.auth_token.should eql "some_token" end end context "when I don't have an account" do it "should raise an error" do CaseblocksAPI::Client.stub(:post).and_return({"message" => "You must have an account to do that"}) lambda { CaseblocksAPI::Client.new("blah@blah.com", "blahblah", "http://localhost:3000")}.should raise_error(CaseblocksAPI::AuthenticationException) end end end describe "creating a case" do let(:caseblocks_client) { CaseblocksAPI::Client.new("username", "password", "http://example.com") } before do stub_request(:post, "http://example.com/tokens?auth_token=some_token") CaseblocksAPI::Client.stub(:get).and_return({"case_types" => [ {"id" => 1, "name" => 'some_case'} ]}) stub_request(:post, "http://example.com/tokens?auth_token="). with(:body => "{\"email\":\"username\",\"password\":\"password\"}", :headers => {'Accept'=>'application/json', 'Content-Type'=>'application/json'}) end context "successfully created on CaseBlocks" do it "should call post" do stub = stub_request(:post, "example.com/case_blocks/cases?auth_token=") .with({body: { 'case' => {'my' => 'params', 'case_type_id' => 1}}.to_json }) .to_return(:status => 201, :body => "", :headers => {}) caseblocks_client.create_case({'my' => 'params'}, 'some_case') stub.should have_been_made.times(1) end end context "received non-200 response code from CaseBlocks" do it "should raise exception" do stub = stub_request(:post, "example.com/case_blocks/cases?auth_token=") .with({body: { 'case' => {'my' => 'params', 'case_type_id' => 1}}.to_json }) .to_return(:status => 500, :body => "", :headers => {}) expect{caseblocks_client.create_case({'my' => 'params'}, 'some_case')}.to raise_error(CaseblocksAPI::CaseCreationError, 'Unable to create case - received response code 500') end end end end