From 39f704c256fc3e71a1dc009acc77796a1efffead Mon Sep 17 00:00:00 2001 From: Adam Cooke Date: Thu, 18 Apr 2024 14:30:42 +0100 Subject: [PATCH] fix(legacy-api): allow _expansions to be provided as true to return all expansions closes #2932 --- .../legacy_api/messages_controller.rb | 18 ++++---- spec/apis/legacy_api/messages/message_spec.rb | 43 +++++++++++++++++++ 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/app/controllers/legacy_api/messages_controller.rb b/app/controllers/legacy_api/messages_controller.rb index 84493fe5..6d78c161 100644 --- a/app/controllers/legacy_api/messages_controller.rb +++ b/app/controllers/legacy_api/messages_controller.rb @@ -24,7 +24,7 @@ def message message_hash = { id: message.id, token: message.token } expansions = api_params["_expansions"] - if expansions.include?("status") + if expansions == true || (expansions.is_a?(Array) && expansions.include?("status")) message_hash[:status] = { status: message.status, last_delivery_attempt: message.last_delivery_attempt&.to_f, @@ -33,7 +33,7 @@ def message } end - if expansions.include?("details") + if expansions == true || (expansions.is_a?(Array) && expansions.include?("details")) message_hash[:details] = { rcpt_to: message.rcpt_to, mail_from: message.mail_from, @@ -49,7 +49,7 @@ def message } end - if expansions.include?("inspection") + if expansions == true || (expansions.is_a?(Array) && expansions.include?("inspection")) message_hash[:inspection] = { inspected: message.inspected, spam: message.spam, @@ -59,15 +59,15 @@ def message } end - if expansions.include?("plain_body") + if expansions == true || (expansions.is_a?(Array) && expansions.include?("plain_body")) message_hash[:plain_body] = message.plain_body end - if expansions.include?("html_body") + if expansions == true || (expansions.is_a?(Array) && expansions.include?("html_body")) message_hash[:html_body] = message.html_body end - if expansions.include?("attachments") + if expansions == true || (expansions.is_a?(Array) && expansions.include?("attachments")) message_hash[:attachments] = message.attachments.map do |attachment| { filename: attachment.filename.to_s, @@ -79,15 +79,15 @@ def message end end - if expansions.include?("headers") + if expansions == true || (expansions.is_a?(Array) && expansions.include?("headers")) message_hash[:headers] = message.headers end - if expansions.include?("raw_message") + if expansions == true || (expansions.is_a?(Array) && expansions.include?("raw_message")) message_hash[:raw_message] = Base64.encode64(message.raw_message) end - if expansions.include?("activity_entries") + if expansions == true || (expansions.is_a?(Array) && expansions.include?("activity_entries")) message_hash[:activity_entries] = { loads: message.loads, clicks: message.clicks diff --git a/spec/apis/legacy_api/messages/message_spec.rb b/spec/apis/legacy_api/messages/message_spec.rb index 4140f9ae..202b66b5 100644 --- a/spec/apis/legacy_api/messages/message_spec.rb +++ b/spec/apis/legacy_api/messages/message_spec.rb @@ -88,6 +88,49 @@ end end + context "when all expansions are requested" do + let(:expansions) { true } + + it "returns details about the message" do + expect(response.status).to eq 200 + parsed_body = JSON.parse(response.body) + expect(parsed_body["status"]).to eq "success" + expect(parsed_body["data"]).to match({ + "id" => message.id, + "token" => message.token, + "status" => { "held" => false, + "hold_expiry" => nil, + "last_delivery_attempt" => nil, + "status" => "Pending" }, + "details" => { "bounce" => false, + "bounce_for_id" => 0, + "direction" => "outgoing", + "mail_from" => "test@example.com", + "message_id" => message.message_id, + "rcpt_to" => "john@example.com", + "received_with_ssl" => nil, + "size" => kind_of(String), + "subject" => "An example message", + "tag" => nil, + "timestamp" => kind_of(Float) }, + "inspection" => { "inspected" => false, + "spam" => false, + "spam_score" => 0.0, + "threat" => false, + "threat_details" => nil }, + "plain_body" => message.plain_body, + "html_body" => message.html_body, + "attachments" => [], + "headers" => message.headers, + "raw_message" => Base64.encode64(message.raw_message), + "activity_entries" => { + "loads" => [], + "clicks" => [] + } + }) + end + end + context "when the status expansion is requested" do let(:expansions) { ["status"] }