spec/keikoku/client_spec.rb in keikokuc-0.7 vs spec/keikoku/client_spec.rb in keikokuc-0.8
- old
+ new
@@ -9,12 +9,12 @@
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')
+ fake_keikoku.register_producer({:api_key => 'abc', :username => 'heroku-postgres'})
+ client = Client.new(:api_key => 'abc', :username => 'heroku-postgres')
result, error = client.post_notification(:message => 'hello',
:severity => 'info')
expect(result[:id]).not_to be_nil
expect(error).to be_nil
end
@@ -22,44 +22,46 @@
it 'handles invalid notifications' do
ShamRack.at('keikoku.herokuapp.com', 443) do |env|
[422, {}, StringIO.new(OkJson.encode({ 'errors' => 'srorre' }))]
end
- response, error = Client.new.post_notification({})
+ response, error = Client.new(:api_key => 'key').post_notification({})
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')
+ fake_keikoku.register_producer({:api_key => 'abc', :username => 'heroku-postgres'})
+ client = Client.new(:api_key => 'bad one', :username => 'heroku-postgres')
result, error = client.post_notification(:message => 'hello',
:severity => 'info')
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, error = Client.new(:api_key => 'key').post_notification({})
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
+ fake_keikoku.register_producer(:username => 'heroku-postgres', :api_key => 'abc')
+ fake_keikoku.register_user(:api_key => 'api-key', :account_email => 'harold@heroku.com')
+ build_notification(:account_email => 'harold@heroku.com', :message => 'find me!',
+ :producer_password => 'abc', :username => 'heroku-postgres').publish
+ build_notification(:account_email => 'another@heroku.com', :producer_password => 'abc',
+ :username => 'heroku-postgres').publish
- client = Client.new(:user => 'harold@heroku.com', :password => 'pass')
+ client = Client.new(:api_key => 'api-key')
notifications, error = client.get_notifications
expect(error).to be_nil
expect(notifications).to have(1).item
@@ -67,58 +69,58 @@
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
+ response, error = Client.new(:api_key => 'key').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')
+ fake_keikoku.register_user(:api_key => 'api-key', :account_email => 'harold@heroku.com')
+ client = Client.new(:api_key => 'bad-api-key')
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')
+
+ fake_keikoku.register_producer(:username => 'heroku-postgres', :api_key => 'abc')
+ fake_keikoku.register_user(:api_key => 'api-key', :account_email => 'harold@heroku.com')
+ client = Client.new(:api_key => 'api-key')
+ notification = build_notification(:account_email => 'harold@heroku.com',
+ :producer_password => 'abc',
+ :username => 'heroku-postgres')
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)
+ expect(response[:status]).to eq('ok')
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')
+ fake_keikoku.register_user(:api_key => 'api-key', :account_email => 'harold@heroku.com')
+ client = Client.new(:api_key => 'bad-api-key')
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)
+ response, error = Client.new(:api_key => 'key').read_notification(1)
expect(response).to be_nil
expect(error).to eq(Client::RequestTimeout)
end
end
end