Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/chargebeex/subscription/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Chargebeex.Subscription do
use TypedStruct

@resource "subscription"
use Chargebeex.Resource, resource: @resource, only: [:list, :retrieve, :update]
use Chargebeex.Resource, resource: @resource, only: [:list, :retrieve, :update, :delete]

@typedoc """
"future" | "in_trial" | "active" | "non_renewing" | "paused" | "cancelled"
Expand Down Expand Up @@ -92,6 +92,7 @@ defmodule Chargebeex.Subscription do
field :shipping_address, map(), default: %{}
field :referral_info, map(), default: %{}
field :contract_term, map(), default: %{}
field :resources, map(), default: %{}
end

use ExConstructor, :build
Expand Down
251 changes: 251 additions & 0 deletions test/chargebeex/subscription_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,257 @@ defmodule Chargebeex.SubscriptionTest do

setup :verify_on_exit!

describe "retrieve" do
test "with bad authentication should fail" do
unauthorized = Common.unauthorized()

expect(
Chargebeex.HTTPClientMock,
:get,
fn url, body, headers ->
assert url == "https://test-namespace.chargebee.com/api/v2/subscriptions/foobar"
assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to module variable @headers ?

@sam-levy sam-levy May 19, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}] headers for GET requests and
[{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}, {"Content-Type", "application/x-www-form-urlencoded"}] for POST requests. Should we create a @get_headers and a @post_headers module attributes? If so IMO we should refactor the other test modules in order to have the same pattern, WDYT @klacointe ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And yes ! 😄

assert body == ""

{:ok, 401, [], Jason.encode!(unauthorized)}
end
)

assert {:error, 401, [], ^unauthorized} = Subscription.retrieve("foobar")
end

test "with resource not found should fail" do
not_found = Common.not_found()

expect(
Chargebeex.HTTPClientMock,
:get,
fn url, body, headers ->
assert url == "https://test-namespace.chargebee.com/api/v2/subscriptions/foobar"
assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}]
assert body == ""

{:ok, 404, [], Jason.encode!(not_found)}
end
)

assert {:error, 404, [], ^not_found} = Subscription.retrieve("foobar")
end

test "with resource found should succeed" do
expect(
Chargebeex.HTTPClientMock,
:get,
fn url, body, headers ->
assert url == "https://test-namespace.chargebee.com/api/v2/subscriptions/foobar"
assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}]
assert body == ""

{:ok, 200, [], Jason.encode!(%{subscription: %{}})}
end
)

assert {:ok, %Subscription{}} == Subscription.retrieve("foobar")
end
end

describe "list" do
test "with bad authentication should fail" do
unauthorized = Common.unauthorized()

expect(
Chargebeex.HTTPClientMock,
:get,
fn url, body, headers ->
assert url == "https://test-namespace.chargebee.com/api/v2/subscriptions"
assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}]
assert body == ""

{:ok, 401, [], Jason.encode!(unauthorized)}
end
)

assert {:error, 401, [], ^unauthorized} = Subscription.list()
end

test "with no param, no offset should succeed" do
expect(
Chargebeex.HTTPClientMock,
:get,
fn url, body, headers ->
assert url == "https://test-namespace.chargebee.com/api/v2/subscriptions"
assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}]
assert body == ""

{:ok, 200, [], Jason.encode!(%{list: [%{subscription: %{}}, %{subscription: %{}}]})}
end
)

assert {:ok, [%Subscription{}, %Subscription{}], %{"next_offset" => nil}} =
Subscription.list()
end

test "with limit & offset params should succeed" do
expect(
Chargebeex.HTTPClientMock,
:get,
fn url, body, headers ->
assert url == "https://test-namespace.chargebee.com/api/v2/subscriptions?limit=1"
assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}]
assert body == ""

result = %{
list: [%{subscription: %{id: "foo"}}],
next_offset: "bar"
}

{:ok, 200, [], Jason.encode!(result)}
end
)

assert {:ok, [%Subscription{}], %{"next_offset" => "bar"}} = Subscription.list(%{limit: 1})
end
end

describe "update" do
test "with bad authentication should fail" do
unauthorized = Common.unauthorized()

expect(
Chargebeex.HTTPClientMock,
:post,
fn url, data, headers ->
assert url == "https://test-namespace.chargebee.com/api/v2/subscriptions/foobar"

assert headers == [
{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"},
{"Content-Type", "application/x-www-form-urlencoded"}
]

assert data == ""

{:ok, 401, [], Jason.encode!(unauthorized)}
end
)

assert {:error, 401, [], ^unauthorized} = Subscription.update("foobar", %{})
end

test "with invalid data should fail" do
bad_request = Common.bad_request()

expect(
Chargebeex.HTTPClientMock,
:post,
fn url, data, headers ->
assert url == "https://test-namespace.chargebee.com/api/v2/subscriptions/foobar"

assert headers == [
{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"},
{"Content-Type", "application/x-www-form-urlencoded"}
]

assert data == "cancel_reason=baz"

{:ok, 400, [], Jason.encode!(bad_request)}
end
)

assert {:error, 400, [], ^bad_request} =
Subscription.update("foobar", %{cancel_reason: "baz"})
end

test "with valid data should succeed" do
expect(
Chargebeex.HTTPClientMock,
:post,
fn url, data, headers ->
assert url == "https://test-namespace.chargebee.com/api/v2/subscriptions/foobar"

assert headers == [
{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"},
{"Content-Type", "application/x-www-form-urlencoded"}
]

assert data == "cancel_reason=baz"

{:ok, 200, [], Jason.encode!(%{subscription: %{}})}
end
)

assert {:ok, %Subscription{}} = Subscription.update("foobar", %{cancel_reason: "baz"})
end
end

describe "delete" do
test "with bad authentication should fail" do
unauthorized = Common.unauthorized()

expect(
Chargebeex.HTTPClientMock,
:post,
fn url, body, headers ->
assert url == "https://test-namespace.chargebee.com/api/v2/subscriptions/foobar/delete"

assert headers == [
{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"},
{"Content-Type", "application/x-www-form-urlencoded"}
]

assert body == ""

{:ok, 401, [], Jason.encode!(unauthorized)}
end
)

assert {:error, 401, [], ^unauthorized} = Subscription.delete("foobar")
end

test "with resource not found should fail" do
not_found = Common.not_found()

expect(
Chargebeex.HTTPClientMock,
:post,
fn url, body, headers ->
assert url == "https://test-namespace.chargebee.com/api/v2/subscriptions/foobar/delete"

assert headers == [
{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"},
{"Content-Type", "application/x-www-form-urlencoded"}
]

assert body == ""

{:ok, 404, [], Jason.encode!(not_found)}
end
)

assert {:error, 404, [], ^not_found} = Subscription.delete("foobar")
end

test "with resource found should succeed" do
expect(
Chargebeex.HTTPClientMock,
:post,
fn url, body, headers ->
assert url == "https://test-namespace.chargebee.com/api/v2/subscriptions/foobar/delete"

assert headers == [
{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"},
{"Content-Type", "application/x-www-form-urlencoded"}
]

assert body == ""

{:ok, 200, [], Jason.encode!(%{subscription: %{}})}
end
)

assert {:ok, %Subscription{}} == Subscription.delete("foobar")
end
end

describe "create_with_items" do
test "with bad authentication should fail" do
unauthorized = Common.unauthorized()
Expand Down
Loading