spec/acceptance/rest/base_spec.rb in ably-0.1.0 vs spec/acceptance/rest/base_spec.rb in ably-0.1.1

- old
+ new

@@ -5,14 +5,14 @@ let(:client) do Ably::Rest::Client.new(api_key: api_key, environment: environment) end describe "invalid requests in middleware" do - it "should raise a InvalidRequest exception with a valid message" do + it "should raise an InvalidRequest exception with a valid message" do invalid_client = Ably::Rest::Client.new(api_key: 'appid.keyuid:keysecret') expect { invalid_client.channel('test').publish('foo', 'choo') }.to raise_error do |error| - expect(error).to be_a(Ably::InvalidRequest) + expect(error).to be_a(Ably::Exceptions::InvalidRequest) expect(error.message).to match(/invalid credentials/) expect(error.code).to eql(40100) expect(error.status).to eql(401) end end @@ -23,20 +23,73 @@ before do stub_request(:get, "#{client.endpoint}/time").to_return(:status => 500, :body => error_response, :headers => { 'Content-Type' => 'application/json' }) end it "should raise a ServerError exception" do - expect { client.time }.to raise_error(Ably::ServerError, /Internal error/) + expect { client.time }.to raise_error(Ably::Exceptions::ServerError, /Internal error/) end end describe "server error", webmock: true do before do stub_request(:get, "#{client.endpoint}/time").to_return(:status => 500) end it "should raise a ServerError exception" do - expect { client.time }.to raise_error(Ably::ServerError, /Unknown/) + expect { client.time }.to raise_error(Ably::Exceptions::ServerError, /Unknown/) + end + end + end + + describe 'authentication failure', webmock: true do + let(:token_1) { { id: SecureRandom.hex } } + let(:token_2) { { id: SecureRandom.hex } } + let(:channel) { 'channelname' } + + before do + @token_requests = 0 + @publish_attempts = 0 + + stub_request(:post, "#{client.endpoint}/keys/#{key_id}/requestToken").to_return do + @token_requests += 1 + { + :body => { access_token: send("token_#{@token_requests}").merge(expires: Time.now.to_i + 3600) }.to_json, + :headers => { 'Content-Type' => 'application/json' } + } + end + + stub_request(:post, "#{client.endpoint}/channels/#{channel}/publish").to_return do + @publish_attempts += 1 + if [1, 3].include?(@publish_attempts) + { status: 201, :body => '[]' } + else + raise Ably::Exceptions::InvalidRequest.new('Authentication failure', status: 401, code: 40140) + end + end + end + + context 'when auth#token_renewable?' do + before do + client.auth.authorise + end + + it 'should automatically reissue a token' do + client.channel(channel).publish('evt', 'msg') + expect(@publish_attempts).to eql(1) + + client.channel(channel).publish('evt', 'msg') + expect(@publish_attempts).to eql(3) + expect(@token_requests).to eql(2) + end + end + + context 'when NOT auth#token_renewable?' do + let(:client) { Ably::Rest::Client.new(token_id: 'token ID cannot be used to create a new token', environment: environment) } + it 'should raise the exception' do + client.channel(channel).publish('evt', 'msg') + expect(@publish_attempts).to eql(1) + expect { client.channel(channel).publish('evt', 'msg') }.to raise_error Ably::Exceptions::InvalidToken + expect(@token_requests).to eql(0) end end end describe Ably::Rest::Client do