defmodule SettlementSimulator.Controllers.SettlementControllerTest do use ExUnit.Case doctest SettlementSimulator.Controllers.SettlementController setup do {:ok, %{}} end test "successful settlement with valid merchant tag from database" do # This test assumes merchant with code AE_REST_001 exists in the merchants table request_body = Jason.encode!(%{ "type" => "qr_payment_settlement_summary", "merchantTag" => "AE_REST_001", "bankUserId" => "Mercury_UAE_PARTNER_001", "settlementDate" => "2026-06-11", "totalTransactionCount" => "3", "grossSettlementAmount" => %{ "value" => "66.00", "currency" => "AED" }, "mdrCharges" => %{ "value" => "0.33", "currency" => "AED" }, "taxOnMdr" => %{ "value" => "0.06", "currency" => "AED" }, "netSettlementAmount" => %{ "value" => "65.61", "currency" => "AED" }, "settlementTimestamp" => "2026-06-11T21:29:59+04:00" }) conn = :post |> Plug.Test.conn("/api/v1/upi/settlement-summary", request_body) |> Plug.Test.put_req_header("authorization", "Bearer jfx2ozaJQpszIuek2b5NHagH93Z61sNVk_lYTWyVAlk") |> Plug.Test.put_req_header("content-type", "application/json") response = SettlementSimulator.Controllers.SettlementController.handle_settlement_summary(conn) assert response.status == 200 {:ok, body} = Jason.decode(response.resp_body) assert body["settlementStatus"] == "COMPLETED" assert body["mismatchDetected"] == false end test "successful settlement with merchant tag without underscores" do # AEREST001 should match AE_REST_001 in database request_body = Jason.encode!(%{ "type" => "qr_payment_settlement_summary", "merchantTag" => "AEREST001", "bankUserId" => "Mercury_UAE_PARTNER_001", "settlementDate" => "2026-06-11", "totalTransactionCount" => "3", "grossSettlementAmount" => %{ "value" => "66.00", "currency" => "AED" } }) conn = :post |> Plug.Test.conn("/api/v1/upi/settlement-summary", request_body) |> Plug.Test.put_req_header("authorization", "Bearer jfx2ozaJQpszIuek2b5NHagH93Z61sNVk_lYTWyVAlk") |> Plug.Test.put_req_header("content-type", "application/json") response = SettlementSimulator.Controllers.SettlementController.handle_settlement_summary(conn) assert response.status == 200 {:ok, body} = Jason.decode(response.resp_body) assert body["settlementStatus"] == "COMPLETED" end test "failure response when merchant not found in database" do request_body = Jason.encode!(%{ "type" => "qr_payment_settlement_summary", "merchantTag" => "INVALID_MERCHANT", "bankUserId" => "Mercury_UAE_PARTNER_001", "settlementDate" => "2026-06-11", "totalTransactionCount" => "3", "grossSettlementAmount" => %{ "value" => "66.00", "currency" => "AED" } }) conn = :post |> Plug.Test.conn("/api/v1/upi/settlement-summary", request_body) |> Plug.Test.put_req_header("authorization", "Bearer jfx2ozaJQpszIuek2b5NHagH93Z61sNVk_lYTWyVAlk") |> Plug.Test.put_req_header("content-type", "application/json") response = SettlementSimulator.Controllers.SettlementController.handle_settlement_summary(conn) assert response.status == 404 {:ok, body} = Jason.decode(response.resp_body) assert body["settlementStatus"] == "FAILED" assert body["mismatchDetected"] == true assert body["error"] == "Merchant not found" end test "unauthorized when bearer token missing" do request_body = Jason.encode!(%{ "merchantTag" => "AE_REST_001", "bankUserId" => "Mercury_UAE_PARTNER_001", "settlementDate" => "2026-06-11", "grossSettlementAmount" => %{ "value" => "66.00", "currency" => "AED" } }) conn = :post |> Plug.Test.conn("/api/v1/upi/settlement-summary", request_body) |> Plug.Test.put_req_header("content-type", "application/json") response = SettlementSimulator.Controllers.SettlementController.handle_settlement_summary(conn) assert response.status == 401 {:ok, body} = Jason.decode(response.resp_body) assert body["message"] == "Unauthorized" end test "missing required fields validation" do request_body = Jason.encode!(%{ "merchantTag" => "AE_REST_001" }) conn = :post |> Plug.Test.conn("/api/v1/upi/settlement-summary", request_body) |> Plug.Test.put_req_header("authorization", "Bearer jfx2ozaJQpszIuek2b5NHagH93Z61sNVk_lYTWyVAlk") |> Plug.Test.put_req_header("content-type", "application/json") response = SettlementSimulator.Controllers.SettlementController.handle_settlement_summary(conn) assert response.status == 400 {:ok, body} = Jason.decode(response.resp_body) assert body["message"] == "Validation failed" assert is_list(body["errors"]) end end