Skip to content
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

feat: add time machine #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
5 changes: 4 additions & 1 deletion lib/chargebeex/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ defmodule Chargebeex.Builder do
Item,
ItemPrice,
Quote,
QuotedSubscription
QuotedSubscription,
TimeMachine
}

def build(%{"list" => resources, "next_offset" => next_offset}) do
Expand Down Expand Up @@ -45,6 +46,7 @@ defmodule Chargebeex.Builder do
def build_resource(%{"item_price" => params}), do: ItemPrice.build(params)
def build_resource(%{"quote" => params}), do: Quote.build(params)
def build_resource(%{"quoted_subscription" => params}), do: QuotedSubscription.build(params)
def build_resource(%{"time_machine" => params}), do: TimeMachine.build(params)

def build_resource("subscription", params), do: Subscription.build(params)
def build_resource("customer", params), do: Customer.build(params)
Expand All @@ -60,6 +62,7 @@ defmodule Chargebeex.Builder do
def build_resource("item_price", params), do: ItemPrice.build(params)
def build_resource("quote", params), do: Quote.build(params)
def build_resource("quoted_subscription", params), do: QuotedSubscription.build(params)
def build_resource("time_machine", params), do: TimeMachine.build(params)

def build_resource(_resource, params), do: params
end
32 changes: 32 additions & 0 deletions lib/chargebeex/time_machine/time_machine.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule Chargebeex.TimeMachine do
@resource "time_machine"

use TypedStruct
use Chargebeex.Resource, resource: @resource, only: [:retrieve]

typedstruct do
field :name, String.t()
field :time_travel_status, String.t()
field :genesis_time, integer()
field :destination_time, integer()
field :failure_code, String.t()
field :failure_reason, String.t()
field :error_json, String.t()
field :object, String.t()
field :resources, map(), defualt: %{}
end

use ExConstructor, :build

@doc """
Restart the time machine.
"""
def start_afresh(id), do: generic_action(:post, @resource, "start_afresh", id)

@doc """
Travel forward in time.
"""
def travel_forward(id, params) do
generic_action(:post, @resource, "travel_forward", id, params)
end
end
34 changes: 34 additions & 0 deletions test/chargebeex/builder/time_machine_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule Chargebeex.Builder.TimeMachineTest do
use ExUnit.Case, async: true

alias Chargebeex.Builder
alias Chargebeex.Fixtures.TimeMachine, as: TimeMachineFixture
alias Chargebeex.TimeMachine

describe "build/1" do
test "should build an time machine" do
builded =
TimeMachineFixture.retrieve()
|> Jason.decode!()
|> Builder.build()

assert %{"time_machine" => %TimeMachine{}} = builded
end

test "should have time machine params" do
time_machine =
TimeMachineFixture.retrieve()
|> Jason.decode!()
|> Builder.build()
|> Map.get("time_machine")

params = TimeMachineFixture.time_machine_params() |> Jason.decode!()

assert time_machine.destination_time == Map.get(params, "destination_time")
assert time_machine.genesis_time == Map.get(params, "genesis_time")
assert time_machine.name == Map.get(params, "name")
assert time_machine.object == Map.get(params, "object")
assert time_machine.time_travel_status == Map.get(params, "time_travel_status")
end
end
end
211 changes: 211 additions & 0 deletions test/chargebeex/time_machine_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
defmodule Chargebeex.TimeMachineTest do
use ExUnit.Case, async: true

import Hammox

alias Chargebeex.Fixtures.{Common, TimeMachine}
alias Chargebeex.TimeMachine

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/time_machines/delorean"
assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}]
assert body == ""

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

assert {:error, 401, [], ^unauthorized} = TimeMachine.retrieve("delorean")
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/time_machines/delorean"
assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}]
assert body == ""

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

assert {:error, 404, [], ^not_found} = TimeMachine.retrieve("delorean")
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/time_machines/delorean"
assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}]
assert body == ""

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

assert {:ok, %TimeMachine{}} = TimeMachine.retrieve("delorean")
end
end

describe "start_afresh" 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/time_machines/delorean/start_afresh"

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

assert body == ""

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

assert {:error, 401, [], ^unauthorized} = TimeMachine.start_afresh("delorean")
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/time_machines/delorean/start_afresh"

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} = TimeMachine.start_afresh("delorean")
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/time_machines/delorean/start_afresh"

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

assert body == ""

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

assert {:ok, %TimeMachine{}} = TimeMachine.start_afresh("delorean")
end
end

describe "travel_forward" 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/time_machines/delorean/travel_forward"

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

assert body == "destination_time=1586274640"

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

assert {:error, 401, [], ^unauthorized} =
TimeMachine.travel_forward("delorean", %{destination_time: 1_586_274_640})
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/time_machines/delorean/travel_forward"

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

assert body == "destination_time=1586274640"

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

assert {:error, 404, [], ^not_found} =
TimeMachine.travel_forward("delorean", %{destination_time: 1_586_274_640})
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/time_machines/delorean/travel_forward"

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

assert body == "destination_time=1586274640"

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

assert {:ok, %TimeMachine{}} =
TimeMachine.travel_forward("delorean", %{destination_time: 1_586_274_640})
end
end
end
21 changes: 21 additions & 0 deletions test/support/fixtures/time_machine.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Chargebeex.Fixtures.TimeMachine do
def time_machine_params() do
"""
{
"destination_time": 1585065021,
"genesis_time": 1585065021,
"name": "delorean",
"object": "time_machine",
"time_travel_status": "succeeded"
}
"""
end

def retrieve() do
"""
{
"time_machine": #{time_machine_params()}
}
"""
end
end