diff --git a/examples/inboxes_api.rb b/examples/inboxes_api.rb index 0440f80..579dec2 100644 --- a/examples/inboxes_api.rb +++ b/examples/inboxes_api.rb @@ -29,5 +29,11 @@ # Reset SMTP credentials of the inbox inboxes.reset_credentials(inbox.id) +# Turn the email address of the inbox on/off +inboxes.toggle_email_username(inbox.id) + +# Reset username of email address per inbox +inboxes.reset_email_username(inbox.id) + # Delete unbox list inboxes.delete(inbox.id) diff --git a/lib/mailtrap/inboxes_api.rb b/lib/mailtrap/inboxes_api.rb index ea8fc8f..bed615a 100644 --- a/lib/mailtrap/inboxes_api.rb +++ b/lib/mailtrap/inboxes_api.rb @@ -85,6 +85,24 @@ def reset_credentials(inbox_id) handle_response(response) end + # Turn the email address of the inbox on/off + # @param inbox_id [Integer] The Inbox identifier + # @return [Inbox] Updated Inbox object + # @!macro api_errors + def toggle_email_username(inbox_id) + response = client.patch("#{base_path}/#{inbox_id}/toggle_email_username") + handle_response(response) + end + + # Reset username of email address per inbox + # @param inbox_id [Integer] The Inbox identifier + # @return [Inbox] Updated Inbox object + # @!macro api_errors + def reset_email_username(inbox_id) + response = client.patch("#{base_path}/#{inbox_id}/reset_email_username") + handle_response(response) + end + private def wrap_request(options) diff --git a/spec/fixtures/vcr_cassettes/Mailtrap_InboxesAPI/_reset_email_username/returns_Inbox_object.yml b/spec/fixtures/vcr_cassettes/Mailtrap_InboxesAPI/_reset_email_username/returns_Inbox_object.yml new file mode 100644 index 0000000..054b188 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Mailtrap_InboxesAPI/_reset_email_username/returns_Inbox_object.yml @@ -0,0 +1,75 @@ +--- +http_interactions: +- request: + method: patch + uri: https://mailtrap.io/api/accounts/1111111/inboxes/4578672/reset_email_username + body: + encoding: UTF-8 + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - mailtrap-ruby (https://github.com/mailtrap/mailtrap-ruby) + Host: + - mailtrap.io + Authorization: + - Bearer + Content-Type: + - application/json + response: + status: + code: 200 + message: OK + headers: + Date: + - Mon, 27 Apr 2026 13:48:43 GMT + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Server: + - cloudflare + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + X-Download-Options: + - noopen + X-Permitted-Cross-Domain-Policies: + - none + Referrer-Policy: + - strict-origin-when-cross-origin + Vary: + - Accept + X-Mailtrap-Version: + - v2 + X-Ratelimit-Limit: + - '150' + X-Ratelimit-Remaining: + - '149' + Etag: + - W/"a26cb93008de50e490b8f0d44498138b" + Cache-Control: + - max-age=0, private, must-revalidate + X-Runtime: + - '0.037827' + Strict-Transport-Security: + - max-age=2592000; includeSubDomains; preload + Cf-Cache-Status: + - DYNAMIC + Alt-Svc: + - h3=":443"; ma=86400 + body: + encoding: ASCII-8BIT + string: '{"id":4578672,"name":"Ruby SDK","username": "railsware","status":"active","email_username": + "1234abcd","email_username_enabled":true,"used":false,"forward_from_email_address": + "railsware@forward.mailtrap.info","project_id":2757750,"password": "xxxxxxxx","domain":"sandbox.smtp.mailtrap.io","pop3_domain":"pop3.mailtrap.io","email_domain":"inbox.mailtrap.io","smtp_ports":[25,465,587,2525],"pop3_ports":[1100,9950],"api_domain":"sandbox.api.mailtrap.io","sent_messages_count":0,"forwarded_messages_count":0,"emails_count":0,"emails_unread_count":0,"last_message_sent_at":null,"max_size":1000,"max_message_size":26214400,"permissions":{"can_read":true,"can_update":true,"can_destroy":true,"can_leave":false}}' + recorded_at: Mon, 27 Apr 2026 13:48:43 GMT +recorded_with: VCR 6.4.0 diff --git a/spec/fixtures/vcr_cassettes/Mailtrap_InboxesAPI/_reset_email_username/when_inbox_does_not_exist/raises_not_found_error.yml b/spec/fixtures/vcr_cassettes/Mailtrap_InboxesAPI/_reset_email_username/when_inbox_does_not_exist/raises_not_found_error.yml new file mode 100644 index 0000000..663e671 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Mailtrap_InboxesAPI/_reset_email_username/when_inbox_does_not_exist/raises_not_found_error.yml @@ -0,0 +1,71 @@ +--- +http_interactions: +- request: + method: patch + uri: https://mailtrap.io/api/accounts/1111111/inboxes/-1/reset_email_username + body: + encoding: UTF-8 + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - mailtrap-ruby (https://github.com/mailtrap/mailtrap-ruby) + Host: + - mailtrap.io + Authorization: + - Bearer + Content-Type: + - application/json + response: + status: + code: 404 + message: Not Found + headers: + Date: + - Mon, 27 Apr 2026 13:48:43 GMT + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '21' + Connection: + - keep-alive + Server: + - cloudflare + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + X-Download-Options: + - noopen + X-Permitted-Cross-Domain-Policies: + - none + Referrer-Policy: + - strict-origin-when-cross-origin + Vary: + - Accept + X-Mailtrap-Version: + - v2 + X-Ratelimit-Limit: + - '150' + X-Ratelimit-Remaining: + - '148' + Cache-Control: + - no-cache + X-Runtime: + - '0.014234' + Strict-Transport-Security: + - max-age=2592000; includeSubDomains; preload + Cf-Cache-Status: + - DYNAMIC + Alt-Svc: + - h3=":443"; ma=86400 + body: + encoding: UTF-8 + string: '{"error":"Not Found"}' + recorded_at: Mon, 27 Apr 2026 13:48:43 GMT +recorded_with: VCR 6.4.0 diff --git a/spec/fixtures/vcr_cassettes/Mailtrap_InboxesAPI/_toggle_email_username/returns_Inbox_object.yml b/spec/fixtures/vcr_cassettes/Mailtrap_InboxesAPI/_toggle_email_username/returns_Inbox_object.yml new file mode 100644 index 0000000..6969828 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Mailtrap_InboxesAPI/_toggle_email_username/returns_Inbox_object.yml @@ -0,0 +1,75 @@ +--- +http_interactions: +- request: + method: patch + uri: https://mailtrap.io/api/accounts/1111111/inboxes/4578672/toggle_email_username + body: + encoding: UTF-8 + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - mailtrap-ruby (https://github.com/mailtrap/mailtrap-ruby) + Host: + - mailtrap.io + Authorization: + - Bearer + Content-Type: + - application/json + response: + status: + code: 200 + message: OK + headers: + Date: + - Mon, 27 Apr 2026 13:44:52 GMT + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Server: + - cloudflare + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + X-Download-Options: + - noopen + X-Permitted-Cross-Domain-Policies: + - none + Referrer-Policy: + - strict-origin-when-cross-origin + Vary: + - Accept + X-Mailtrap-Version: + - v2 + X-Ratelimit-Limit: + - '150' + X-Ratelimit-Remaining: + - '149' + Etag: + - W/"55b84a99a8d7012562567a66828a8dd9" + Cache-Control: + - max-age=0, private, must-revalidate + X-Runtime: + - '0.032539' + Strict-Transport-Security: + - max-age=2592000; includeSubDomains; preload + Cf-Cache-Status: + - DYNAMIC + Alt-Svc: + - h3=":443"; ma=86400 + body: + encoding: ASCII-8BIT + string: '{"id":4578672,"name":"Ruby SDK","username": "railsware","status":"active","email_username": + "1234abcd","email_username_enabled":true,"used":false,"forward_from_email_address": + "railsware@forward.mailtrap.info","project_id":2757750,"password": "xxxxxxxx","domain":"sandbox.smtp.mailtrap.io","pop3_domain":"pop3.mailtrap.io","email_domain":"inbox.mailtrap.io","smtp_ports":[25,465,587,2525],"pop3_ports":[1100,9950],"api_domain":"sandbox.api.mailtrap.io","sent_messages_count":0,"forwarded_messages_count":0,"emails_count":0,"emails_unread_count":0,"last_message_sent_at":null,"max_size":1000,"max_message_size":26214400,"permissions":{"can_read":true,"can_update":true,"can_destroy":true,"can_leave":false}}' + recorded_at: Mon, 27 Apr 2026 13:44:52 GMT +recorded_with: VCR 6.4.0 diff --git a/spec/fixtures/vcr_cassettes/Mailtrap_InboxesAPI/_toggle_email_username/when_inbox_does_not_exist/raises_not_found_error.yml b/spec/fixtures/vcr_cassettes/Mailtrap_InboxesAPI/_toggle_email_username/when_inbox_does_not_exist/raises_not_found_error.yml new file mode 100644 index 0000000..eee653f --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Mailtrap_InboxesAPI/_toggle_email_username/when_inbox_does_not_exist/raises_not_found_error.yml @@ -0,0 +1,71 @@ +--- +http_interactions: +- request: + method: patch + uri: https://mailtrap.io/api/accounts/1111111/inboxes/-1/toggle_email_username + body: + encoding: UTF-8 + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - mailtrap-ruby (https://github.com/mailtrap/mailtrap-ruby) + Host: + - mailtrap.io + Authorization: + - Bearer + Content-Type: + - application/json + response: + status: + code: 404 + message: Not Found + headers: + Date: + - Mon, 27 Apr 2026 13:44:53 GMT + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '21' + Connection: + - keep-alive + Server: + - cloudflare + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + X-Download-Options: + - noopen + X-Permitted-Cross-Domain-Policies: + - none + Referrer-Policy: + - strict-origin-when-cross-origin + Vary: + - Accept + X-Mailtrap-Version: + - v2 + X-Ratelimit-Limit: + - '150' + X-Ratelimit-Remaining: + - '148' + Cache-Control: + - no-cache + X-Runtime: + - '0.011361' + Strict-Transport-Security: + - max-age=2592000; includeSubDomains; preload + Cf-Cache-Status: + - DYNAMIC + Alt-Svc: + - h3=":443"; ma=86400 + body: + encoding: UTF-8 + string: '{"error":"Not Found"}' + recorded_at: Mon, 27 Apr 2026 13:44:53 GMT +recorded_with: VCR 6.4.0 diff --git a/spec/mailtrap/inboxes_api_spec.rb b/spec/mailtrap/inboxes_api_spec.rb index 8daa9e6..e2f730b 100644 --- a/spec/mailtrap/inboxes_api_spec.rb +++ b/spec/mailtrap/inboxes_api_spec.rb @@ -209,6 +209,57 @@ end end + describe '#toggle_email_username' do + subject(:toggle_email_username) { inboxes_api.toggle_email_username(inbox_id) } + + let(:inbox_id) { 4_578_672 } + + it 'returns Inbox object' do + expect(toggle_email_username).to be_a(Mailtrap::Inbox) + expect(toggle_email_username).to have_attributes( + id: inbox_id, + email_username_enabled: true + ) + end + + context 'when inbox does not exist' do + let(:inbox_id) { -1 } + + it 'raises not found error' do + expect { toggle_email_username }.to raise_error do |error| + expect(error).to be_a(Mailtrap::Error) + expect(error.message).to include('Not Found') + expect(error.messages.any? { |msg| msg.include?('Not Found') }).to be true + end + end + end + end + + describe '#reset_email_username' do + subject(:reset_email_username) { inboxes_api.reset_email_username(inbox_id) } + + let(:inbox_id) { 4_578_672 } + + it 'returns Inbox object' do + expect(reset_email_username).to have_attributes( + id: inbox_id, + email_username: '1234abcd' + ) + end + + context 'when inbox does not exist' do + let(:inbox_id) { -1 } + + it 'raises not found error' do + expect { reset_email_username }.to raise_error do |error| + expect(error).to be_a(Mailtrap::Error) + expect(error.message).to include('Not Found') + expect(error.messages.any? { |msg| msg.include?('Not Found') }).to be true + end + end + end + end + describe '#delete' do subject(:delete) { inboxes_api.delete(inbox_id) }