Skip to content

Commit

Permalink
Add test case for list proxies and endpoint to refresh proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Dec 25, 2024
1 parent 1f70355 commit c9ebc3a
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 4 deletions.
12 changes: 12 additions & 0 deletions lib/uplink/cache/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Uplink.Cache.Router do

alias Uplink.Secret
alias Uplink.Cache
alias Uplink.Routings

plug :match

Expand All @@ -23,4 +24,15 @@ defmodule Uplink.Cache.Router do

json(conn, :ok, %{message: "cache key :self deleted."})
end

delete "/routers/:router_id/proxies" do
router_id = String.to_integer(router_id)

Cache.transaction([keys: [{:proxies, router_id}]], fn ->
Cache.delete({:proxies, router_id})
Routings.list_proxies(router_id)
end)

json(conn, :ok, %{message: "cache key {:proxies, #{router_id}} refreshed."})
end
end
2 changes: 1 addition & 1 deletion lib/uplink/web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Uplink.Web do
def json(conn, status, response) do
conn
|> put_resp_header("content-type", "application/json")
|> send_resp(status, Jason.encode!(%{data: response}))
|> send_resp(status, Jason.encode_to_iodata!(%{data: response}))
end

def redirect(conn, location) do
Expand Down
53 changes: 51 additions & 2 deletions test/uplink/cache/router_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Uplink.Cache.RouterTest do
use ExUnit.Case, async: true
use ExUnit.Case
use Plug.Test

alias Uplink.Cache.Router
Expand All @@ -16,12 +16,20 @@ defmodule Uplink.Cache.RouterTest do

describe "delete self" do
setup do
bypass = Bypass.open()

Application.put_env(
:uplink,
Uplink.Clients.Instellar,
endpoint: "http://localhost:#{bypass.port}/uplink"
)

signature =
:crypto.mac(:hmac, :sha256, Uplink.Secret.get(), @valid_delete_body)
|> Base.encode16()
|> String.downcase()

{:ok, signature: signature}
{:ok, signature: signature, bypass: bypass}
end

test "can successfully delete :self", %{signature: signature} do
Expand All @@ -33,5 +41,46 @@ defmodule Uplink.Cache.RouterTest do

assert conn.status == 200
end

test "can successfully delete {:proxies, router_id}", %{
signature: signature,
bypass: bypass
} do
Bypass.expect_once(
bypass,
"GET",
"/uplink/self/routers/1/proxies",
fn conn ->
conn
|> Plug.Conn.put_resp_header("content-type", "application/json")
|> Plug.Conn.send_resp(
200,
Jason.encode_to_iodata!(%{
"data" => [
%{
"attributes" => %{
"id" => 1,
"router_id" => 1,
"hosts" => ["opsmaru.com", "www.opsmaru.com"],
"paths" => ["/how-to*"],
"tls" => true,
"target" => "proxy.webflow.com",
"port" => 80
}
}
]
})
)
end
)

conn =
conn(:delete, "/routers/1/proxies", @valid_delete_body)
|> put_req_header("x-uplink-signature-256", "sha256=#{signature}")
|> put_req_header("content-type", "application/json")
|> Router.call(@opts)

assert conn.status == 200
end
end
end
2 changes: 1 addition & 1 deletion test/uplink/clients/caddy/config/builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ defmodule Uplink.Clients.Caddy.Config.BuilderTest do
|> Plug.Conn.put_resp_header("content-type", "application/json")
|> Plug.Conn.send_resp(
200,
Jason.encode!(%{
Jason.encode_to_iodata!(%{
"data" => [
%{
"attributes" => %{
Expand Down
54 changes: 54 additions & 0 deletions test/uplink/routings/proxy/manager_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
defmodule Uplink.Routings.Proxy.ManagerTest do
use ExUnit.Case

alias Uplink.Cache
alias Uplink.Routings

setup do
bypass = Bypass.open()

Application.put_env(
:uplink,
Uplink.Clients.Instellar,
endpoint: "http://localhost:#{bypass.port}/uplink"
)

{:ok, bypass: bypass}
end

describe "list proxies" do
test "fetch and parse proxies", %{bypass: bypass} do
Cache.delete({:proxies, 1})

Bypass.expect_once(
bypass,
"GET",
"/uplink/self/routers/1/proxies",
fn conn ->
conn
|> Plug.Conn.put_resp_header("content-type", "application/json")
|> Plug.Conn.send_resp(
200,
Jason.encode_to_iodata!(%{
"data" => [
%{
"attributes" => %{
"id" => 1,
"router_id" => 1,
"hosts" => ["opsmaru.com", "www.opsmaru.com"],
"paths" => ["/how-to*"],
"tls" => true,
"target" => "proxy.webflow.com",
"port" => 80
}
}
]
})
)
end
)

assert [%Routings.Proxy{} = _proxy] = Routings.list_proxies(1)
end
end
end

0 comments on commit c9ebc3a

Please sign in to comment.