Skip to content

Instantly share code, notes, and snippets.

@jccarbonfive
Last active December 12, 2015 03:08
Show Gist options
  • Save jccarbonfive/4704826 to your computer and use it in GitHub Desktop.
Save jccarbonfive/4704826 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe 'Signing in', vcr: { match_requests_on: [:path] } do
before do
@user = create :user
visit '/users/sign_in'
fill_in 'Email', with: @user.email
fill_in 'Password', with: @user.password
click_button 'Sign in'
end
it 'publishes a sign in event to pusherapp.com' do
expected_body = {
name: 'sign-in',
channels: ['users'],
data: {
email: @user.email
}.to_json
}.to_json
WebMock.should have_requested(:post, /api.pusherapp.com/)
.with {|request| request.body == expected_body}
end
end
class ApplicationController < ActionController::Base
protect_from_forgery
def after_sign_in_path_for(user)
user.publish_sign_in
super
end
end
require 'spec_helper'
describe User do
describe 'sign in event publishing' do
before do
@user = build :user
channel = double 'Pusher channel'
channel
.should_receive(:trigger)
.with('sign-in', email: @user.email)
Pusher
.should_receive(:[])
.with('users')
.and_return(channel)
end
it 'publishes the event using Pusher' do
@user.publish_sign_in
end
end
end
require 'spec_helper'
describe User do
describe 'sign in event publishing' do
before do
@user = build :user
@event_publisher = double 'event publisher'
@event_publisher
.should_receive(:publish)
.with('sign-in',
channel: 'users',
data: {
email: @user.email
})
end
it 'asks an event publisher to publish the event' do
@user.publish_sign_in @event_publisher
end
end
end
class User < ActiveRecord::Base
devise :validatable,
:database_authenticatable,
:registerable,
:rememberable
attr_accessible :email,
:password,
:password_confirmation,
:remember_me
def publish_sign_in(event_publisher)
event_publisher.publish 'sign-in',
channel: 'users',
data: {
email: email
}
end
end
class User < ActiveRecord::Base
devise :validatable,
:database_authenticatable,
:registerable,
:rememberable
attr_accessible :email,
:password,
:password_confirmation,
:remember_me
def publish_sign_in(event_publisher = PusherEventPublisher.new)
event_publisher.publish 'sign-in',
channel: 'users',
data: {
email: email
}
end
end
require 'spec_helper'
describe PusherEventPublisher do
describe '#publish', vcr: { match_requests_on: [:path] } do
before do
pusher_event_publisher = PusherEventPublisher.new
pusher_event_publisher.publish 'example-event',
channel: 'example-channel',
data: {
name: 'value'
}
end
it 'publishes the event to pusherapp.com' do
expected_body = {
name: 'example-event',
channels: ['example-channel'],
data: {
name: 'value'
}.to_json
}.to_json
WebMock.should have_requested(:post, /api.pusherapp.com/)
.with {|request| request.body == expected_body}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment