2021-03-13 15:04:57 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
require 'webmock/rspec'
|
|
|
|
|
|
2021-03-16 15:22:15 -07:00
|
|
|
describe External::MailblusterHelper, type: :helper do
|
2021-03-13 15:04:57 -08:00
|
|
|
let!(:user) { create(:user) }
|
2021-03-23 13:01:22 -07:00
|
|
|
|
|
|
|
|
before(:each) do
|
|
|
|
|
WebMock.reset_executed_requests!
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-19 10:11:07 -07:00
|
|
|
url = 'https://api.mailbluster.com/api/leads/'
|
2021-03-13 15:04:57 -08:00
|
|
|
|
|
|
|
|
describe 'create_lead' do
|
2021-03-16 15:22:15 -07:00
|
|
|
it 'makes a post request to Mailbluster\'s API and gets the correct response' do
|
2021-03-16 14:28:39 -07:00
|
|
|
response_body = "{
|
|
|
|
|
\"message\": \"Lead created\",
|
|
|
|
|
\"lead\": {
|
|
|
|
|
\"id\": 329395,
|
|
|
|
|
\"firstName\": \"#{user.name}\",
|
|
|
|
|
\"lastName\": \"\",
|
|
|
|
|
\"fullName\": \"#{user.name}\",
|
|
|
|
|
\"email\": \"#{user.email}\",
|
|
|
|
|
\"subscribed\": true,
|
|
|
|
|
\"tags\": [
|
2021-03-19 10:10:33 -07:00
|
|
|
#{ENV['OSEM_NAME'] || 'snapcon'}
|
2021-03-16 14:28:39 -07:00
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}"
|
2021-03-17 14:54:57 -07:00
|
|
|
stub_request(:post, url)
|
2021-03-16 14:28:39 -07:00
|
|
|
.to_return(body: response_body, status: 200)
|
|
|
|
|
response = create_lead(user)
|
2021-03-16 15:22:15 -07:00
|
|
|
|
2021-03-17 15:06:24 -07:00
|
|
|
expect(WebMock).to have_requested(:post, url).with(body: {
|
2021-03-16 15:22:15 -07:00
|
|
|
'email': user.email,
|
|
|
|
|
'firstName': user.name,
|
2021-03-14 13:54:44 -07:00
|
|
|
'overrideExisting': true,
|
2021-03-16 15:22:15 -07:00
|
|
|
'subscribed': true,
|
2021-03-19 10:10:33 -07:00
|
|
|
'tags': [ENV['OSEM_NAME'] || 'snapcon']
|
2021-03-14 13:54:44 -07:00
|
|
|
}.to_json)
|
2021-03-16 14:28:39 -07:00
|
|
|
expect(response).to eq(response_body)
|
2021-03-13 15:04:57 -08:00
|
|
|
end
|
|
|
|
|
end
|
2021-03-16 15:22:28 -07:00
|
|
|
|
2021-03-19 10:11:18 -07:00
|
|
|
describe 'edit_lead' do
|
2021-03-23 13:45:05 -07:00
|
|
|
it 'makes a put request to Mailbluster\'s API to change the email and gets the correct response' do
|
|
|
|
|
response_body = "{
|
|
|
|
|
\"message\": \"Lead updated\",
|
|
|
|
|
\"lead\": {
|
|
|
|
|
\"id\": 329395,
|
|
|
|
|
\"firstName\": \"#{user.name}\",
|
|
|
|
|
\"lastName\": \"\",
|
|
|
|
|
\"fullName\": \"#{user.name}\",
|
|
|
|
|
\"email\": \"#{user.email}\",
|
|
|
|
|
\"subscribed\": true,
|
|
|
|
|
\"tags\": [
|
|
|
|
|
#{ENV['OSEM_NAME'] || 'snapcon'}
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}"
|
|
|
|
|
stub_request(:put, url)
|
|
|
|
|
.to_return(body: response_body, status: 200)
|
|
|
|
|
add_tags = ['2021']
|
|
|
|
|
old_email = user.email
|
|
|
|
|
user.email = "new@new.org"
|
|
|
|
|
user.save
|
|
|
|
|
response = edit_lead(user, old_email: old_email)
|
|
|
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:put, url + Digest::MD5.hexdigest(old_email)).with(body: {
|
|
|
|
|
'email': user.email,
|
|
|
|
|
'firstName': user.name,
|
|
|
|
|
'addTags': add_tags,
|
|
|
|
|
'removeTags': []
|
|
|
|
|
}.to_json)
|
|
|
|
|
expect(response).to eq(response_body)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'makes a put request to Mailbluster\'s API to add a tag and gets the correct response' do
|
|
|
|
|
response_body = "{
|
|
|
|
|
\"message\": \"Lead updated\",
|
|
|
|
|
\"lead\": {
|
|
|
|
|
\"id\": 329395,
|
|
|
|
|
\"firstName\": \"#{user.name}\",
|
|
|
|
|
\"lastName\": \"\",
|
|
|
|
|
\"fullName\": \"#{user.name}\",
|
|
|
|
|
\"email\": \"#{user.email}\",
|
|
|
|
|
\"subscribed\": true,
|
|
|
|
|
\"tags\": [
|
|
|
|
|
#{ENV['OSEM_NAME'] || 'snapcon'}, '2021'
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}"
|
|
|
|
|
stub_request(:put, url + Digest::MD5.hexdigest(user.email))
|
|
|
|
|
.to_return(body: response_body, status: 200)
|
|
|
|
|
add_tags = ['2021']
|
|
|
|
|
response = edit_lead(user, add_tags: add_tags)
|
|
|
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:put, url).with(body: {
|
|
|
|
|
'email': user.email,
|
|
|
|
|
'firstName': user.name,
|
|
|
|
|
'addTags': add_tags,
|
|
|
|
|
'removeTags': []
|
|
|
|
|
}.to_json)
|
|
|
|
|
expect(response).to eq(response_body)
|
|
|
|
|
end
|
2021-03-19 10:11:18 -07:00
|
|
|
end
|
|
|
|
|
|
2021-03-16 15:22:28 -07:00
|
|
|
describe 'delete_lead' do
|
|
|
|
|
it 'correctly requests the right URL and gets a valid response' do
|
|
|
|
|
email_hash = Digest::MD5.hexdigest user.email
|
2021-03-17 14:54:57 -07:00
|
|
|
response_body = "{
|
|
|
|
|
\"message\":\"Lead deleted\",
|
|
|
|
|
\"leadHash\":\"#{email_hash}\"
|
2021-03-17 15:06:24 -07:00
|
|
|
}"
|
2021-03-17 14:54:57 -07:00
|
|
|
lead_url = url + email_hash.to_s
|
|
|
|
|
stub_request(:delete, lead_url)
|
2021-03-16 15:22:28 -07:00
|
|
|
.to_return(body: response_body)
|
|
|
|
|
response = delete_lead(user)
|
|
|
|
|
|
2021-03-17 14:54:57 -07:00
|
|
|
expect(WebMock).to have_requested(:delete, lead_url)
|
2021-03-16 15:22:28 -07:00
|
|
|
expect(response).to eq(response_body)
|
|
|
|
|
end
|
|
|
|
|
end
|
2021-03-13 15:22:19 -08:00
|
|
|
end
|