Called create_lead in model, FactoryBot errors
This commit is contained in:
parent
09becbc3e0
commit
f98f35ec8d
3 changed files with 66 additions and 1 deletions
|
|
@ -80,6 +80,8 @@ class User < ApplicationRecord
|
||||||
|
|
||||||
after_save :touch_events
|
after_save :touch_events
|
||||||
|
|
||||||
|
after_commit :mailbluster_create_lead, on: :create
|
||||||
|
|
||||||
# add scope
|
# add scope
|
||||||
scope :comment_notifiable, ->(conference) {joins(:roles).where('roles.name IN (?)', [:organizer, :cfp]).where('roles.resource_type = ? AND roles.resource_id = ?', 'Conference', conference.id)}
|
scope :comment_notifiable, ->(conference) {joins(:roles).where('roles.name IN (?)', [:organizer, :cfp]).where('roles.resource_type = ? AND roles.resource_id = ?', 'Conference', conference.id)}
|
||||||
|
|
||||||
|
|
@ -362,6 +364,9 @@ class User < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO email_hash function for mailbluster
|
# TODO email_hash function for mailbluster
|
||||||
|
# def email_hash
|
||||||
|
# Digest::MD5.hexdigest user.email
|
||||||
|
# end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
|
@ -369,6 +374,10 @@ class User < ApplicationRecord
|
||||||
self.is_admin = true if User.empty?
|
self.is_admin = true if User.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def mailbluster_create_lead
|
||||||
|
ApplicationController.helpers.create_lead(self)
|
||||||
|
end
|
||||||
|
|
||||||
def touch_events
|
def touch_events
|
||||||
event_users.each(&:touch)
|
event_users.each(&:touch)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -80,8 +80,27 @@ FactoryBot.define do
|
||||||
last_sign_in_at { Date.today }
|
last_sign_in_at { Date.today }
|
||||||
is_disabled { false }
|
is_disabled { false }
|
||||||
|
|
||||||
|
url_mailbluster = 'https://api.mailbluster.com/api/leads/'
|
||||||
|
response_body = "{
|
||||||
|
\"message\": \"Lead created\",
|
||||||
|
\"lead\": {
|
||||||
|
\"id\": 329395,
|
||||||
|
\"firstName\": \"#{name}\",
|
||||||
|
\"lastName\": \"\",
|
||||||
|
\"fullName\": \"#{name}\",
|
||||||
|
\"email\": \"#{email}\",
|
||||||
|
\"subscribed\": true,
|
||||||
|
\"tags\": [
|
||||||
|
#{ENV['OSEM_NAME'] || 'snapcon'}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
WebMock.stub_request(:post, url_mailbluster)
|
||||||
|
# Called by every user creation
|
||||||
|
|
||||||
after(:create) do |user|
|
after(:create) do |user|
|
||||||
user.is_admin = false
|
user.is_admin = false
|
||||||
|
|
||||||
# save with bang cause we want change in DB and not just in object instance
|
# save with bang cause we want change in DB and not just in object instance
|
||||||
user.save!
|
user.save!
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe User do
|
describe User do
|
||||||
|
|
||||||
let(:user_admin) { create(:admin) }
|
let(:user_admin) { create(:admin) }
|
||||||
let(:conference) { create(:conference, short_title: 'oSC16', title: 'openSUSE Conference 2016') }
|
let(:conference) { create(:conference, short_title: 'oSC16', title: 'openSUSE Conference 2016') }
|
||||||
let(:conference2) { create(:conference, short_title: 'oSC15', title: 'openSUSE Conference 2015') }
|
let(:conference2) { create(:conference, short_title: 'oSC15', title: 'openSUSE Conference 2015') }
|
||||||
|
|
@ -530,4 +530,41 @@ describe User do
|
||||||
expect(User.omniauth_providers).to eq [:suse, :google, :facebook, :github, :discourse]
|
expect(User.omniauth_providers).to eq [:suse, :google, :facebook, :github, :discourse]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'mailbluster' do
|
||||||
|
it 'creates a Mailbluster lead on creating a user' do
|
||||||
|
user_mailbluster = User.new(name: 'John Doe', email: 'snap@qwertyuiop.org')
|
||||||
|
|
||||||
|
response_body = "{
|
||||||
|
\"message\": \"Lead created\",
|
||||||
|
\"lead\": {
|
||||||
|
\"id\": 329395,
|
||||||
|
\"firstName\": \"#{user_mailbluster.name}\",
|
||||||
|
\"lastName\": \"\",
|
||||||
|
\"fullName\": \"#{user_mailbluster.name}\",
|
||||||
|
\"email\": \"#{user_mailbluster.email}\",
|
||||||
|
\"subscribed\": true,
|
||||||
|
\"tags\": [
|
||||||
|
#{ENV['OSEM_NAME'] || 'snapcon'}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
stub_request(:post, url_mailbluster)
|
||||||
|
.to_return(body: response_body, status: 200)
|
||||||
|
|
||||||
|
# Do not request before save
|
||||||
|
expect(WebMock).not_to have_requested(:post, url)
|
||||||
|
|
||||||
|
# Request after save
|
||||||
|
user_mailbluster.save!
|
||||||
|
|
||||||
|
expect(WebMock).to have_requested(:post, url).with(body: {
|
||||||
|
'email': user_mailbluster.email,
|
||||||
|
'firstName': user_mailbluster.name,
|
||||||
|
'overrideExisting': true,
|
||||||
|
'subscribed': true,
|
||||||
|
'tags': [ENV['OSEM_NAME'] || 'snapcon']
|
||||||
|
}.to_json)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue