Skip to content

Commit

Permalink
Add test for create version event controller
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Jun 19, 2024
1 parent b3dd153 commit f24b81d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/polar_web/controllers/publish/version/event_controller.ex
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
5 changes: 5 additions & 0 deletions lib/polar_web/controllers/publish/version/event_json.ex
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
4 changes: 4 additions & 0 deletions lib/polar_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ defmodule PolarWeb.Router do
resources "/products", ProductController, only: [:show] do
resources "/versions", VersionController, only: [:create]
end

scope "/versions/:version_id", Version, as: :version do
resources "/events", EventController, only: [:create]
end
end
end

Expand Down
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

0 comments on commit f24b81d

Please sign in to comment.