|
| 1 | +defmodule Chargebeex.CustomerEntitlementTest do |
| 2 | + use ExUnit.Case, async: true |
| 3 | + |
| 4 | + import Hammox |
| 5 | + |
| 6 | + alias Chargebeex.Fixtures.Common |
| 7 | + alias Chargebeex.CustomerEntitlement |
| 8 | + |
| 9 | + setup :verify_on_exit! |
| 10 | + |
| 11 | + describe "list" do |
| 12 | + test "with bad authentication should fail" do |
| 13 | + unauthorized = Common.unauthorized() |
| 14 | + |
| 15 | + expect( |
| 16 | + Chargebeex.HTTPClientMock, |
| 17 | + :get, |
| 18 | + fn url, body, headers -> |
| 19 | + assert url == |
| 20 | + "https://test-namespace.chargebee.com/api/v2/customers/customer_id/customer_entitlements" |
| 21 | + |
| 22 | + assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}] |
| 23 | + assert body == "" |
| 24 | + |
| 25 | + {:ok, 401, [], Jason.encode!(unauthorized)} |
| 26 | + end |
| 27 | + ) |
| 28 | + |
| 29 | + assert {:error, 401, [], ^unauthorized} = CustomerEntitlement.list("customer_id") |
| 30 | + end |
| 31 | + |
| 32 | + test "with no param, no offset should succeed" do |
| 33 | + expect( |
| 34 | + Chargebeex.HTTPClientMock, |
| 35 | + :get, |
| 36 | + fn url, body, headers -> |
| 37 | + assert url == |
| 38 | + "https://test-namespace.chargebee.com/api/v2/customers/customer_id/customer_entitlements" |
| 39 | + |
| 40 | + assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}] |
| 41 | + assert body == "" |
| 42 | + |
| 43 | + {:ok, 200, [], |
| 44 | + Jason.encode!(%{ |
| 45 | + list: [%{customer_entitlement: %{}}, %{customer_entitlement: %{}}] |
| 46 | + })} |
| 47 | + end |
| 48 | + ) |
| 49 | + |
| 50 | + assert {:ok, [%CustomerEntitlement{}, %CustomerEntitlement{}], %{"next_offset" => nil}} = |
| 51 | + CustomerEntitlement.list("customer_id") |
| 52 | + end |
| 53 | + |
| 54 | + test "with limit & offset params should succeed" do |
| 55 | + expect( |
| 56 | + Chargebeex.HTTPClientMock, |
| 57 | + :get, |
| 58 | + fn url, body, headers -> |
| 59 | + assert url == |
| 60 | + "https://test-namespace.chargebee.com/api/v2/customers/customer_id/customer_entitlements?limit=1&feature_type%5Bis%5D=switch" |
| 61 | + |
| 62 | + assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}] |
| 63 | + assert body == "" |
| 64 | + |
| 65 | + result = %{ |
| 66 | + list: [%{customer_entitlement: %{customer_id: "customer_id"}}], |
| 67 | + next_offset: "bar" |
| 68 | + } |
| 69 | + |
| 70 | + {:ok, 200, [], Jason.encode!(result)} |
| 71 | + end |
| 72 | + ) |
| 73 | + |
| 74 | + assert {:ok, [%CustomerEntitlement{}], %{"next_offset" => "bar"}} = |
| 75 | + CustomerEntitlement.list("customer_id", %{ |
| 76 | + "feature_type[is]" => "switch", |
| 77 | + limit: 1 |
| 78 | + }) |
| 79 | + end |
| 80 | + end |
| 81 | +end |
0 commit comments