-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add delete/1 to Subscription
#162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
4ab6a17
7366ae5
8e3d634
174c469
4013429
2d3559c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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/1234" | ||
| assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to module variable
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(1234) | ||
| 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/1234" | ||
| assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}] | ||
| assert body == "" | ||
|
|
||
| {:ok, 404, [], Jason.encode!(not_found)} | ||
| end | ||
| ) | ||
|
|
||
| assert {:error, 404, [], ^not_found} = Subscription.retrieve(1234) | ||
| 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/1234" | ||
| assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}] | ||
| assert body == "" | ||
|
|
||
| {:ok, 200, [], Jason.encode!(%{subscription: %{}})} | ||
| end | ||
| ) | ||
|
|
||
| assert {:ok, %Subscription{}} == Subscription.retrieve(1234) | ||
| 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/1234/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("1234") | ||
| 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/1234/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("1234") | ||
| 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/1234/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("1234") | ||
| end | ||
| end | ||
|
|
||
| describe "create_with_items" do | ||
| test "with bad authentication should fail" do | ||
| unauthorized = Common.unauthorized() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to module variable
@url?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are testing different endpoints in this module:
/v2/subscriptions;/v2/subscriptions/foobar;/v2/subscriptions/foobar/delete;/v2/customers/foobar/subscription_for_items;/v2/subscriptions/foobar/update_for_items.Should we create one module attribute for each?
If we decide to use module attributes for these values I guess we should also refactor the other test modules in the project in order to have the same pattern, WDYT @klacointe ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this would impact multiple test modules, IMO this refactoring is outside the scope of this PR. I think it should be done in a separate PR, WDYT @klacointe ?