|
| 1 | +RSpec.describe Mailtrap::ContactList do |
| 2 | + let(:client) { described_class.new('1111111', Mailtrap::Client.new(api_key: 'correct-api-key')) } |
| 3 | + let(:base_url) { 'https://mailtrap.io/api/accounts/1111111' } |
| 4 | + |
| 5 | + around do |example| |
| 6 | + VCR.turned_off { example.run } |
| 7 | + end |
| 8 | + |
| 9 | + describe '#' do |
| 10 | + let(:expected_response) do |
| 11 | + [ |
| 12 | + { 'id' => 1, 'name' => 'List 1' }, |
| 13 | + { 'id' => 2, 'name' => 'List 2' } |
| 14 | + ] |
| 15 | + end |
| 16 | + |
| 17 | + it 'returns all contact lists' do |
| 18 | + stub_request(:get, "#{base_url}/contacts/lists") |
| 19 | + .to_return( |
| 20 | + status: 200, |
| 21 | + body: expected_response.to_json, |
| 22 | + headers: { 'Content-Type' => 'application/json' } |
| 23 | + ) |
| 24 | + |
| 25 | + response = client.list |
| 26 | + expect(response).to be_a(Array) |
| 27 | + expect(response.length).to eq(2) |
| 28 | + expect(response.first).to have_attributes(id: 1, name: 'List 1') |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + describe '#get' do |
| 33 | + let(:contact_list_id) { 1 } |
| 34 | + let(:expected_response) do |
| 35 | + { 'id' => 1, 'name' => 'List 1' } |
| 36 | + end |
| 37 | + |
| 38 | + it 'returns a specific contact list' do |
| 39 | + stub_request(:get, "#{base_url}/contacts/lists/#{contact_list_id}") |
| 40 | + .to_return( |
| 41 | + status: 200, |
| 42 | + body: expected_response.to_json, |
| 43 | + headers: { 'Content-Type' => 'application/json' } |
| 44 | + ) |
| 45 | + |
| 46 | + response = client.get(contact_list_id) |
| 47 | + expect(response).to have_attributes(id: 1, name: 'List 1') |
| 48 | + end |
| 49 | + |
| 50 | + it 'raises error when contact list not found' do |
| 51 | + stub_request(:get, "#{base_url}/contacts/lists/999") |
| 52 | + .to_return( |
| 53 | + status: 404, |
| 54 | + body: { 'error' => 'Not Found' }.to_json, |
| 55 | + headers: { 'Content-Type' => 'application/json' } |
| 56 | + ) |
| 57 | + |
| 58 | + expect { client.get(999) }.to raise_error(Mailtrap::Error) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + describe '#create' do |
| 63 | + let(:contact_list_name) { 'List 1' } |
| 64 | + let(:expected_response) do |
| 65 | + { 'id' => 1, 'name' => contact_list_name } |
| 66 | + end |
| 67 | + |
| 68 | + it 'creates a new contact list' do |
| 69 | + stub_request(:post, "#{base_url}/contacts/lists") |
| 70 | + .with( |
| 71 | + body: { name: contact_list_name }.to_json |
| 72 | + ) |
| 73 | + .to_return( |
| 74 | + status: 200, |
| 75 | + body: expected_response.to_json, |
| 76 | + headers: { 'Content-Type' => 'application/json' } |
| 77 | + ) |
| 78 | + |
| 79 | + response = client.create(name: contact_list_name) |
| 80 | + expect(response).to have_attributes(id: 1, name: contact_list_name) |
| 81 | + end |
| 82 | + |
| 83 | + it 'raises error when rate limit exceeded' do |
| 84 | + stub_request(:post, "#{base_url}/contacts/lists") |
| 85 | + .with( |
| 86 | + body: { name: contact_list_name }.to_json |
| 87 | + ) |
| 88 | + .to_return( |
| 89 | + status: 429, |
| 90 | + body: { 'errors' => 'Rate limit exceeded' }.to_json, |
| 91 | + headers: { 'Content-Type' => 'application/json' } |
| 92 | + ) |
| 93 | + |
| 94 | + expect { client.create(name: contact_list_name) }.to raise_error(Mailtrap::Error) |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + describe '#update' do |
| 99 | + let(:contact_list_id) { 2 } |
| 100 | + let(:new_name) { 'List 2' } |
| 101 | + let(:expected_response) do |
| 102 | + { 'id' => 2, 'name' => new_name } |
| 103 | + end |
| 104 | + |
| 105 | + it 'updates a contact list' do |
| 106 | + stub_request(:patch, "#{base_url}/contacts/lists/#{contact_list_id}") |
| 107 | + .with( |
| 108 | + body: { name: new_name }.to_json |
| 109 | + ) |
| 110 | + .to_return( |
| 111 | + status: 200, |
| 112 | + body: expected_response.to_json, |
| 113 | + headers: { 'Content-Type' => 'application/json' } |
| 114 | + ) |
| 115 | + |
| 116 | + response = client.update(contact_list_id, name: new_name) |
| 117 | + expect(response).to have_attributes(id: 2, name: new_name) |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + describe '#delete' do |
| 122 | + let(:contact_list_id) { 1 } |
| 123 | + |
| 124 | + it 'deletes a contact list' do |
| 125 | + stub_request(:delete, "#{base_url}/contacts/lists/#{contact_list_id}") |
| 126 | + .to_return(status: 204) |
| 127 | + |
| 128 | + response = client.delete(contact_list_id) |
| 129 | + expect(response).to be true |
| 130 | + end |
| 131 | + end |
| 132 | +end |
0 commit comments