spec/keikoku/client_spec.rb in keikokuc-0.0.3 vs spec/keikoku/client_spec.rb in keikokuc-0.1
- old
+ new
@@ -1,46 +1,122 @@
require 'spec_helper'
require 'sham_rack'
module Keikokuc
- describe Client, '#post_notification' do
+ shared_context 'client specs' do
let(:fake_keikoku) { FakeKeikoku.new }
-
after { ShamRack.unmount_all }
+ end
+ describe Client, '#post_notification' do
+ include_context 'client specs'
+
it 'publishes a new notification' do
ShamRack.mount(fake_keikoku, "keikoku.herokuapp.com", 443)
fake_keikoku.register_publisher({api_key: 'abc'})
client = Client.new(producer_api_key: 'abc')
result, error = client.post_notification(message: 'hello',
severity: 'info')
- result[:id].should_not be_nil
- error.should be_nil
+ expect(result[:id]).not_to be_nil
+ expect(error).to be_nil
end
it 'handles invalid notifications' do
ShamRack.at('keikoku.herokuapp.com', 443) do |env|
[422, {}, StringIO.new(Yajl::Encoder.encode({ errors: :srorre }))]
end
response, error = Client.new.post_notification({})
- error.should == Client::InvalidNotification
- response[:errors].should == 'srorre'
+ expect(error).to be Client::InvalidNotification
+ expect(response[:errors]).to eq('srorre')
end
it 'handles authentication failures' do
ShamRack.mount(fake_keikoku, "keikoku.herokuapp.com", 443)
fake_keikoku.register_publisher({api_key: 'abc'})
client = Client.new(producer_api_key: 'bad one')
result, error = client.post_notification(message: 'hello',
severity: 'info')
- result[:id].should be_nil
- error.should == Client::Unauthorized
+ expect(result[:id]).to be_nil
+ expect(error).to eq Client::Unauthorized
end
it 'handles timeouts' do
RestClient::Resource.any_instance.stub(:post).and_raise Timeout::Error
response, error = Client.new.post_notification({})
- response.should be_nil
- error.should == Client::RequestTimeout
+ expect(response).to be_nil
+ expect(error).to eq(Client::RequestTimeout)
+ end
+ end
+
+ describe Client, '#get_notifications' do
+ include_context 'client specs'
+
+ it 'gets all notifications for a user' do
+ ShamRack.mount(fake_keikoku, "keikoku.herokuapp.com", 443)
+ fake_keikoku.register_publisher(api_key: 'abc')
+ fake_keikoku.register_user(email: 'harold@heroku.com', password: 'pass')
+ build(:notification, account_email: 'harold@heroku.com', message: 'find me!', producer_api_key: 'abc').publish
+ build(:notification, account_email: 'another@heroku.com', producer_api_key: 'abc').publish
+
+ client = Client.new(user: 'harold@heroku.com', password: 'pass')
+
+ notifications, error = client.get_notifications
+
+ expect(error).to be_nil
+ expect(notifications).to have(1).item
+
+ expect(notifications.first[:message]).to eq('find me!')
+ end
+
+ it 'handles timeouts' do
+ RestClient::Resource.any_instance.stub(:get).and_raise Timeout::Error
+ response, error = Client.new.get_notifications
+ expect(response).to be_nil
+ expect(error).to eq(Client::RequestTimeout)
+ end
+
+ it 'handles authentication failures' do
+ ShamRack.mount(fake_keikoku, "keikoku.herokuapp.com", 443)
+ fake_keikoku.register_user(email: 'harold@heroku.com', password: 'pass')
+ client = Client.new(user: 'harold@heroku.com', password: 'bad-pass')
+
+ response, error = client.get_notifications
+
+ expect(response).to be_empty
+ expect(error).to eq(Client::Unauthorized)
+ end
+ end
+
+ describe Client, '#read_notification' do
+ include_context 'client specs'
+ it 'marks the notification as read' do
+ ShamRack.mount(fake_keikoku, "keikoku.herokuapp.com", 443)
+ fake_keikoku.register_publisher(api_key: 'abc')
+ fake_keikoku.register_user(email: 'harold@heroku.com', password: 'pass')
+ client = Client.new(user: 'harold@heroku.com', password: 'pass')
+ notification = build(:notification, account_email: 'harold@heroku.com', producer_api_key: 'abc')
+ notification.publish or raise "Notification publish error"
+
+ response, error = client.read_notification(notification.remote_id)
+ expect(error).to be_nil
+
+ expect(response[:read_by]).to eq('harold@heroku.com')
+ expect(response[:read_at]).to be_within(1).of(DateTime.now)
+ end
+
+ it 'handles authentication errors' do
+ ShamRack.mount(fake_keikoku, "keikoku.herokuapp.com", 443)
+ fake_keikoku.register_user(email: 'harold@heroku.com', password: 'pass')
+ client = Client.new(user: 'harold@heroku.com', password: 'bad-pass')
+ response, error = client.read_notification(1)
+ expect(response).to be_empty
+ expect(error).to eq(Client::Unauthorized)
+ end
+
+ it 'handles timeouts' do
+ RestClient::Resource.any_instance.stub(:post).and_raise Timeout::Error
+ response, error = Client.new.read_notification(1)
+ expect(response).to be_nil
+ expect(error).to eq(Client::RequestTimeout)
end
end
end