osem-fcy/spec/services/mailbluster_manager_spec.rb

141 lines
4.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require 'spec_helper'
require 'webmock/rspec'
describe MailblusterManager, type: :model do
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-04-01 23:20:21 -07:00
describe 'query_api' do
it 'translates :get to a get request' do
stub_request(:get, url)
described_class.query_api(:get, '/')
expect(WebMock).to have_requested(:get, url)
end
it 'translates :post to a post request' do
stub_request(:post, url + 'path')
described_class.query_api(:post, '/path', body: { key: 'value' })
expect(WebMock).to have_requested(:post, url + 'path').with(body: { key: 'value' })
end
end
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
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-17 14:54:57 -07:00
stub_request(:post, url)
.to_return(body: response_body, status: 200)
2021-03-31 21:22:28 -07:00
response = described_class.create_lead(user)
2021-03-16 15:22:15 -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)
expect(response).to eq(response_body)
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'}
],
}
}"
old_email = user.email
2021-03-23 15:00:02 -07:00
user.email = 'new@new.org'
2021-03-23 13:45:05 -07:00
user.save
2021-03-25 16:43:18 -07:00
stub_request(:put, url + Digest::MD5.hexdigest(old_email))
.to_return(body: response_body, status: 200)
2021-03-31 21:22:28 -07:00
response = described_class.edit_lead(user, old_email: old_email)
2021-03-23 13:45:05 -07:00
expect(WebMock).to have_requested(:put, url + Digest::MD5.hexdigest(old_email)).with(body: {
2021-03-23 15:00:02 -07:00
'email': user.email,
'firstName': user.name,
2021-03-25 16:43:18 -07:00
'addTags': [],
2021-03-23 13:45:05 -07:00
'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']
2021-03-31 21:22:28 -07:00
response = described_class.edit_lead(user, add_tags: add_tags)
2021-03-23 13:45:05 -07:00
2021-03-25 16:43:18 -07:00
expect(WebMock).to have_requested(:put, url + Digest::MD5.hexdigest(user.email)).with(body: {
2021-03-23 15:00:02 -07:00
'email': user.email,
'firstName': user.name,
'addTags': add_tags,
2021-03-23 13:45:05 -07:00
'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 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 = described_class.delete_lead(user.email)
2021-03-16 15:22:28 -07:00
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
end