farside/test/farside_test.exs
Ben Busby e58d6e23ed
Remove Redis dep, replace w/ native Elixir lib
This removes the dependency on Redis for core app functionality. Rather
than using the key/value store provided by Redis, Farside now uses a
key/val store provided by [cubdb](https://github.com/lucaong/cubdb) for
identical functionality but without reliance on a non-Elixir service.

This solution was chosen instead of ets, because storing instance data
in memory leads to a period of broken functionality whenever the app
restarts and hasn't re-populated instance data yet. It was also chosen
instead of dets, because the documentation for dets was pretty hard to
understand at first glance.

Tests and the CI build were updated to reflect the removed dependency on
Redis.

New environment variable `FARSIDE_DATA_DIR` can be used to point to a
directory where the instance data can be stored by cubdb.

Co-authored-by: Jason Clark <mithereal@gmail.com>
2022-10-31 16:45:31 -06:00

92 lines
2.1 KiB
Elixir

defmodule FarsideTest do
use ExUnit.Case
use Plug.Test
alias Farside.Router
@opts Router.init([])
def test_conn(path) do
:timer.sleep(1000)
:get
|> conn(path, "")
|> Router.call(@opts)
end
test "throttle" do
first_conn =
:get
|> conn("/", "")
|> Router.call(@opts)
first_redirect = elem(List.last(first_conn.resp_headers), 1)
throttled_conn =
:get
|> conn("/", "")
|> Router.call(@opts)
throttled_redirect = elem(List.last(first_conn.resp_headers), 1)
assert throttled_conn.state == :sent
assert throttled_redirect == first_redirect
end
test "/" do
conn = test_conn("/")
assert conn.state == :sent
assert conn.status == 200
end
test "/:service" do
services_json = Application.fetch_env!(:farside, :services_json)
{:ok, file} = File.read(services_json)
{:ok, service_list} = Jason.decode(file)
service_names =
Enum.map(
service_list,
fn service -> service["type"] end
)
IO.puts("")
Enum.map(service_names, fn service_name ->
conn = test_conn("/#{service_name}")
first_redirect = elem(List.last(conn.resp_headers), 1)
IO.puts(" /#{service_name} (#1) -- #{first_redirect}")
assert conn.state == :set
assert conn.status == 302
conn = test_conn("/#{service_name}")
second_redirect = elem(List.last(conn.resp_headers), 1)
IO.puts(" /#{service_name} (#2) -- #{second_redirect}")
assert conn.state == :set
assert conn.status == 302
assert first_redirect != second_redirect
end)
end
test "/https://..." do
parent_service = "https://www.youtube.com"
parent_path = "watch?v=dQw4w9WgXcQ"
conn = test_conn("/#{parent_service}/#{parent_path}")
redirect = elem(List.last(conn.resp_headers), 1)
IO.puts("")
IO.puts(" /#{parent_service}/#{parent_path}")
IO.puts(" redirected to")
IO.puts(" #{redirect}")
assert conn.state == :set
assert conn.status == 302
assert redirect =~ parent_path
assert !(redirect =~ parent_service)
end
end