|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe Mailtrap::ContactImportsAPI do |
| 4 | + let(:client) { described_class.new('1111111', Mailtrap::Client.new(api_key: 'correct-api-key')) } |
| 5 | + let(:base_url) { 'https://mailtrap.io/api/accounts/1111111' } |
| 6 | + |
| 7 | + describe '#get' do |
| 8 | + let(:import_id) { 'import-123' } |
| 9 | + let(:expected_response) do |
| 10 | + { |
| 11 | + 'id' => 'import-123', |
| 12 | + 'status' => 'finished', |
| 13 | + 'created_contacts_count' => 10, |
| 14 | + 'updated_contacts_count' => 2, |
| 15 | + 'contacts_over_limit_count' => 0 |
| 16 | + } |
| 17 | + end |
| 18 | + |
| 19 | + it 'returns a specific contact import' do |
| 20 | + stub_request(:get, "#{base_url}/contacts/imports/#{import_id}") |
| 21 | + .to_return( |
| 22 | + status: 200, |
| 23 | + body: expected_response.to_json, |
| 24 | + headers: { 'Content-Type' => 'application/json' } |
| 25 | + ) |
| 26 | + |
| 27 | + response = client.get(import_id) |
| 28 | + expect(response).to have_attributes( |
| 29 | + id: 'import-123', |
| 30 | + status: 'finished', |
| 31 | + created_contacts_count: 10, |
| 32 | + updated_contacts_count: 2, |
| 33 | + contacts_over_limit_count: 0 |
| 34 | + ) |
| 35 | + end |
| 36 | + |
| 37 | + it 'raises error when contact import not found' do |
| 38 | + stub_request(:get, "#{base_url}/contacts/imports/not-found") |
| 39 | + .to_return( |
| 40 | + status: 404, |
| 41 | + body: { 'error' => 'Not Found' }.to_json, |
| 42 | + headers: { 'Content-Type' => 'application/json' } |
| 43 | + ) |
| 44 | + |
| 45 | + expect { client.get('not-found') }.to raise_error(Mailtrap::Error) |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + describe '#create' do |
| 50 | + let(:contacts) do |
| 51 | + [ |
| 52 | + { |
| 53 | + |
| 54 | + fields: { |
| 55 | + fname: 'John', |
| 56 | + age: 30, |
| 57 | + is_subscribed: true, |
| 58 | + birthday: '1990-05-15' |
| 59 | + }, |
| 60 | + list_ids_included: [1, 2], |
| 61 | + list_ids_excluded: [3] |
| 62 | + } |
| 63 | + ] |
| 64 | + end |
| 65 | + let(:expected_response) do |
| 66 | + { |
| 67 | + 'id' => 'import-456', |
| 68 | + 'status' => 'created', |
| 69 | + 'created_contacts_count' => 1, |
| 70 | + 'updated_contacts_count' => 0, |
| 71 | + 'contacts_over_limit_count' => 0 |
| 72 | + } |
| 73 | + end |
| 74 | + |
| 75 | + it 'creates a new contact import with hash' do |
| 76 | + stub_request(:post, "#{base_url}/contacts/imports") |
| 77 | + .with(body: { contacts: }.to_json) |
| 78 | + .to_return( |
| 79 | + status: 200, |
| 80 | + body: expected_response.to_json, |
| 81 | + headers: { 'Content-Type' => 'application/json' } |
| 82 | + ) |
| 83 | + |
| 84 | + response = client.create(contacts) |
| 85 | + expect(response).to have_attributes( |
| 86 | + id: 'import-456', |
| 87 | + status: 'created', |
| 88 | + created_contacts_count: 1, |
| 89 | + updated_contacts_count: 0, |
| 90 | + contacts_over_limit_count: 0 |
| 91 | + ) |
| 92 | + end |
| 93 | + |
| 94 | + it 'raises error when invalid options are provided' do |
| 95 | + invalid_contacts = [contacts.first.merge(foo: 'bar')] |
| 96 | + expect { client.create(invalid_contacts) }.to raise_error(ArgumentError, /invalid options are given/) |
| 97 | + end |
| 98 | + |
| 99 | + it 'raises error when API returns an error' do |
| 100 | + stub_request(:post, "#{base_url}/contacts/imports") |
| 101 | + .with(body: { contacts: }.to_json) |
| 102 | + .to_return( |
| 103 | + status: 422, |
| 104 | + body: { 'errors' => { 'email' => ['is invalid'] } }.to_json, |
| 105 | + headers: { 'Content-Type' => 'application/json' } |
| 106 | + ) |
| 107 | + |
| 108 | + expect { client.create(contacts) }.to raise_error(Mailtrap::Error) |
| 109 | + end |
| 110 | + end |
| 111 | +end |
0 commit comments