RSpec.describe ExpressPigeon::Contacts do # WARN: These tests are assuming that the test # account has three custom fields already # defined on the account. # # - my_custom_text_field (no default) # - my_custom_number_field (no default) # - my_custom_date_field (no default) # # There isn't a way as of this commit to # specify custom fields via the API. You'll # have to configure them manually via the UI. let(:auth_key) { ENV['EXPRESS_PIGEON_AUTH_KEY'] } let(:name) { 'TEST List Name' } let(:from_name) { 'Mike Hall' } let(:reply_to) { 'mike@just3ws.com' } let(:client) { ExpressPigeon::Contacts.new(auth_key) } let(:contacts) do [ { 'email' => 'mike@just3ws.com', 'first_name' => 'Mike', 'last_name' => 'Hall', 'my_custom_text_field' => 'some text value' } ] end before do @list_a = ExpressPigeon::Lists.new(auth_key).create( name: 'TEST List A', from_name: 'Mike Hall', reply_to: 'mike@just3ws.com' ) @list_a_id = @list_a['list']['id'] @list_b = ExpressPigeon::Lists.new(auth_key).create( name: 'TEST List B', from_name: 'Mike Hall', reply_to: 'mike@just3ws.com' ) @list_b_id = @list_b['list']['id'] end after do ExpressPigeon::Lists.new(auth_key).index.select do |list| list['name'] =~ /^TEST\s/i end.map do |list| list['id'] end.each do |list_id| ExpressPigeon::Lists.new(auth_key).delete(list_id) end end describe '#create' do it 'creates a contact' do response = client.create(@list_a_id, contacts) expect(response['code']).to eq(200) expect(response['contacts']).to eq(contacts.map { |contact| contact['email'] }) end end describe '#update' do before do client.create(@list_a_id, contacts) end it 'updates a contact' do contacts.first['email'] = 'mike@ugtastic.com' response = client.update(@list_a_id, contacts) expect(response['code']).to eq(200) expect(response['contacts']).to eq(contacts.map { |contact| contact['email'] }) end end describe '#move' do let(:email_addresses) { ['mike@just3ws.com'] } before do client.create(@list_a_id, contacts) end it 'updates a contact' do response = client.move(@list_a_id, @list_b_id, contacts) expect(response['code']).to eq(200) expect(response['message']).to eq("contacts moved from list=#{@list_a_id} to list=#{@list_b_id}") end end describe '#find' do before do client.create(@list_a_id, contacts) end it 'finds an existing contact' do response = client.find(contacts.first['email']) expect(response.code).to eq(200) expect(response['lists'].map { |list| list['id'] }).to include(@list_a_id) expect(response['email']).to eq(contacts.first['email']) expect(response['custom_fields']['my_custom_text_field']).to eq('some text value') end end end