Skip to content

Instantly share code, notes, and snippets.

@mamantoha
Created January 30, 2015 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamantoha/3311a4287e383a8f6efe to your computer and use it in GitHub Desktop.
Save mamantoha/3311a4287e383a8f6efe to your computer and use it in GitHub Desktop.
CSN Notifier test
# app/mailers/action_notifier.rb
public
def generate_csn_notifications_and_deliver(users, template)
if @email_notification # maybe instead for @csn_email and @csn_online we pass an argument instead
users.each do |user|
m = mail(:to => user.email) do |format|
format.html { render :text => template }
end
# if user.notification_preferences.deliver_by_email == true # -- what this?
mail.deliver
# end
end
end
if @online_notification
# no need to check notification preferences here.
csn_api_helper.deliver(json)
end
end
# app/mailers/csn_notifier.rb
class CsnNotifier < ActionNotifier
def approved(invoice)
supplier = invoice.supplier
# TODO add needed values for email template
@title = "TITLE"
if supplier.linked_to_connect?
users = supplier.supplier_users.active
@email_notification = render "email_template"
pp @email_notification
#@online_notification = invoice.get_csn_json # uncomment this later
generate_csn_notifications_and_deliver(users, @email_notification)
else
# do the thing that we currently do fo approved logic in notifier.rb
# maybe just call InvoiceNotify.approve
end
end
end
# spec/mailers/csn_test_spec.rb
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "Test CSN invoice_approved notifications" do
before do
User.current_user = CoupaLib::Factory.create_user
end
describe "notifiers" do
describe CsnNotifier do
it "an invoice state changes " do
expect {
supplier = FactoryGirl.create(:supplier, :status => 'active', :coupa_connect_status => 'Linked')
supplier_user1 = FactoryGirl.create(:supplier_user, :email => 'test1@example.com', :supplier => supplier)
supplier_user2 = FactoryGirl.create(:supplier_user, :email => 'test2@example.com', :supplier => supplier)
invoice = CoupaLib::Factory.create_invoice_header(:supplier => supplier)
# call from a model when an invoice state changes we do
CsnNotifier.approved(invoice)
}.to change { ActionMailer::Base.deliveries.size }.by(2)
end
end
end
end
<html>
<head>
</head>
<body>
<%= @title%>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment