-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for create version event controller
- Loading branch information
Showing
4 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
lib/polar_web/controllers/publish/version/event_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule PolarWeb.Publish.Version.EventController do | ||
use PolarWeb, :controller | ||
|
||
alias Polar.Repo | ||
alias Polar.Streams.Version | ||
|
||
action_fallback PolarWeb.FallbackController | ||
|
||
def create(%{assigns: %{current_user: current_user}} = conn, %{ | ||
"version_id" => version_id, | ||
"event" => %{"name" => event_name} | ||
}) do | ||
with %Version{} = version <- Repo.get(Version, version_id), | ||
{:ok, %{event: event}} <- Eventful.Transit.perform(version, current_user, event_name) do | ||
conn | ||
|> put_status(:created) | ||
|> render(:create, %{event: event}) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
defmodule PolarWeb.Publish.Version.EventJSON do | ||
def create(%{event: event}) do | ||
%{data: %{id: event.id, name: event.name}} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
test/polar_web/controllers/publish/version/event_controller_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
defmodule PolarWeb.Publish.Version.EventControllerTest do | ||
use PolarWeb.ConnCase | ||
|
||
import Polar.AccountsFixtures | ||
import Polar.StreamsFixtures | ||
|
||
alias Polar.Accounts | ||
alias Polar.Streams | ||
|
||
setup do | ||
password = Accounts.generate_automation_password() | ||
|
||
bot = bot_fixture(%{password: password}) | ||
|
||
user = Accounts.get_user_by_email_and_password(bot.email, password) | ||
|
||
session_token = | ||
Accounts.generate_user_session_token(user) | ||
|> Base.encode64() | ||
|
||
conn = | ||
build_conn() | ||
|> put_req_header("authorization", session_token) | ||
|> put_req_header("content-type", "application/json") | ||
|
||
product_attributes = valid_product_attributes("alpine:3.19:amd64:default") | ||
|
||
{:ok, product} = Streams.create_product(product_attributes) | ||
|
||
{:ok, version} = | ||
Streams.create_version(product, valid_version_attributes(2)) | ||
|
||
{:ok, conn: conn, version: version} | ||
end | ||
|
||
describe "POST /publish/versions/:version_id/events" do | ||
test "can create transition event", %{conn: conn, version: version} do | ||
conn = | ||
post(conn, "/publish/versions/#{version.id}/events", %{ | ||
event: %{ | ||
name: "test" | ||
} | ||
}) | ||
|
||
assert %{"data" => data} = json_response(conn, 201) | ||
|
||
assert %{"id" => _id, "name" => "test"} = data | ||
end | ||
end | ||
end |