Skip to content

Commit

Permalink
Add origin host on topic creation (#40)
Browse files Browse the repository at this point in the history
* Add migration

* Create dummy implementation

* Saving topic with origin host attribute

* Formatting

* Formatting test

* Fix test
  • Loading branch information
Sergio Revilla authored Feb 5, 2020
1 parent 96f6543 commit dea3580
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 21 deletions.
3 changes: 2 additions & 1 deletion AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ Thank you!
* Imanol Cea (@lonamiaec)
* Enrique Palenque (@epalenque)
* Juanjo Ponz (@jjponz)
* Santiago Lanús (@sanntt)
* Santiago Lanús (@sanntt)
* Sergio Revilla (@elreplicante)
2 changes: 1 addition & 1 deletion lib/postoffice/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Postoffice.Application do
def start(_type, _args) do
# List all child processes to be supervised
Postoffice.MetricsSetup.setup()

children = [
# Start the Ecto repository
Postoffice.Repo,
Expand All @@ -25,7 +26,6 @@ defmodule Postoffice.Application do
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Postoffice.Supervisor]
Supervisor.start_link(children, opts)

end

# Tell Phoenix to update the endpoint configuration
Expand Down
5 changes: 3 additions & 2 deletions lib/postoffice/messaging/topic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Postoffice.Messaging.Topic do

schema "topics" do
field :name, :string, null: false
field :origin_host, :string, null: true

has_many :consumers, Postoffice.Messaging.Publisher
has_many :messages, Postoffice.Messaging.Message
Expand All @@ -14,8 +15,8 @@ defmodule Postoffice.Messaging.Topic do
@doc false
def changeset(message, attrs) do
message
|> cast(attrs, [:name])
|> validate_required([:name])
|> cast(attrs, [:name, :origin_host])
|> validate_required([:name, :origin_host])
|> unique_constraint(:name)
end
end
6 changes: 3 additions & 3 deletions lib/postoffice_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ defmodule PostofficeWeb.Endpoint do
# this means its contents can be read but not tampered with.
# Set :encryption_salt if you would also like to encrypt it.
plug Plug.Session,
store: :cookie,
key: "_my_app_key",
signing_salt: "To8iCTxd"
store: :cookie,
key: "_my_app_key",
signing_salt: "To8iCTxd"

PostofficeWeb
plug PostofficeWeb.Metrics.Exporter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Postoffice.Repo.Migrations.AddOriginHostToTopic do
use Ecto.Migration

def change do
alter table(:topics) do
add :origin_host, :string, null: true
end
end
end
3 changes: 2 additions & 1 deletion test/postoffice/handlers/http_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ defmodule Postoffice.Handlers.HttpTest do
initial_message: 0
}
@valid_topic_attrs %{
name: "test"
name: "test",
origin_host: "example.com"
}

setup [:set_mox_from_context, :verify_on_exit!]
Expand Down
3 changes: 2 additions & 1 deletion test/postoffice/handlers/pubsub_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ defmodule Postoffice.Handlers.PubsubTest do
initial_message: 0
}
@valid_topic_attrs %{
name: "test-publisher"
name: "test-publisher",
origin_host: "example.com"
}
setup [:set_mox_from_context, :verify_on_exit!]

Expand Down
3 changes: 2 additions & 1 deletion test/postoffice/messaging_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ defmodule Postoffice.MessagingTest do
alias Postoffice.Fixtures

@second_topic_attrs %{
name: "test2"
name: "test2",
origin_host: "example2.com"
}

@disabled_publisher_attrs %{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ defmodule PostofficeWeb.Api.MessageControllerTest do
}

def fixture(:message) do
{:ok, topic} = Messaging.create_topic(%{name: "test", id: 1})
{:ok, topic} = Messaging.create_topic(%{name: "test", origin_host: "example.com", id: 1})
{:ok, message} = Messaging.create_message(topic, @create_attrs)
message
end

setup %{conn: conn} do
{:ok, _topic} = Messaging.create_topic(%{name: "test"})
{:ok, _topic} = Messaging.create_topic(%{name: "test", origin_host: "example.com"})
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ defmodule PostofficeWeb.Api.PublisherControllerTest do
}

@valid_topic_attrs %{
name: "test"
name: "test",
origin_host: "example.com"
}

setup %{conn: conn} do
Expand Down
37 changes: 33 additions & 4 deletions test/postoffice_web/controllers/api/topic_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,31 +1,60 @@
defmodule PostofficeWeb.Api.TopicControllerTest do
use PostofficeWeb.ConnCase

import Ecto.Query, warn: false

alias Postoffice.Messaging
alias Postoffice.Messaging.Topic
alias Postoffice.Repo

@create_attrs %{
name: "test"
name: "test",
origin_host: "example.com"
}
@invalid_attrs %{name: ""}

@topic_without_name %{name: "", origin_host: "example.com"}
@topic_without_origin_host %{name: "test", origin_host: ""}

setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end

defp get_last_topic() do
from(t in Topic, order_by: [desc: :id], limit: 1)
|> Repo.one()
end

describe "create topic" do
test "renders created topic information when data is valid", %{conn: conn} do
conn = post(conn, Routes.api_topic_path(conn, :create), @create_attrs)

created_topic = json_response(conn, 201)["data"]

assert created_topic["name"] == "test"
end

test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, Routes.api_topic_path(conn, :create), @invalid_attrs)
test "creates the topic when data is valid", %{conn: conn} do
conn
|> post(Routes.api_topic_path(conn, :create), @create_attrs)

created_topic = get_last_topic()

assert created_topic.name == "test"
assert created_topic.origin_host == "example.com"
end

test "renders errors when name is invalid", %{conn: conn} do
conn = post(conn, Routes.api_topic_path(conn, :create), @topic_without_name)

assert json_response(conn, 400)["data"]["errors"] == %{"name" => ["can't be blank"]}
end

test "renders errors when origin_host is invalid", %{conn: conn} do
conn = post(conn, Routes.api_topic_path(conn, :create), @topic_without_origin_host)

assert json_response(conn, 400)["data"]["errors"] == %{"origin_host" => ["can't be blank"]}
end

test "do not create topic in case it already exists", %{conn: conn} do
{:ok, _topic} = Messaging.create_topic(@create_attrs)

Expand Down
6 changes: 3 additions & 3 deletions test/postoffice_web/controllers/topic_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ defmodule PostofficeWeb.TopicControllerTest do
|> html_response(200) =~ topic.name
end
end

describe "create topics" do
test "can access to topics list", %{conn: conn} do

conn
|> post(Routes.topic_path(conn, :create, [topic: %{name: "Test Topic"}]))
assert conn
|> post(Routes.topic_path(conn, :create, [topic: %{name: "Test Topic", origin_host: "example.com"}]))
|> redirected_to() == "/topics"
end
end
Expand Down
3 changes: 2 additions & 1 deletion test/support/fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ defmodule Postoffice.Fixtures do
alias Postoffice.Messaging

@topic_attrs %{
name: "test"
name: "test",
origin_host: "example.com"
}

@message_attrs %{
Expand Down

0 comments on commit dea3580

Please sign in to comment.