Skip to content

Commit

Permalink
fix: improve detour_factory to split name and id, then test SL
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahpurcell committed Jan 10, 2025
1 parent 69b649c commit 2fd43d5
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 17 deletions.
100 changes: 92 additions & 8 deletions test/skate_web/channels/detours_channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,21 @@ defmodule SkateWeb.DetoursChannelTest do
@tag :authenticated
test "subscribes to all active detours with initial detours", %{socket: socket} do
:detour |> build() |> with_id(1) |> insert()
:detour |> build() |> activated |> with_id(2) |> with_route("57") |> insert()
:detour |> build() |> activated |> with_id(3) |> with_route("66") |> insert()

:detour
|> build()
|> activated
|> with_id(2)
|> with_route(%{name: "57", id: "57"})
|> insert()

:detour
|> build()
|> activated
|> with_id(3)
|> with_route(%{name: "66", id: "66"})
|> insert()

:detour |> build() |> deactivated |> with_id(4) |> insert()

assert {:ok,
Expand Down Expand Up @@ -64,8 +77,21 @@ defmodule SkateWeb.DetoursChannelTest do
@tag :authenticated
test "subscribes to active detours for one route", %{socket: socket} do
:detour |> build() |> with_id(1) |> insert()
:detour |> build() |> activated |> with_id(2) |> with_route("57") |> insert()
:detour |> build() |> activated |> with_id(3) |> with_route("66") |> insert()

:detour
|> build()
|> activated
|> with_id(2)
|> with_route(%{name: "57", id: "57"})
|> insert()

:detour
|> build()
|> activated
|> with_id(3)
|> with_route(%{name: "66", id: "66"})
|> insert()

:detour |> build() |> deactivated |> with_id(4) |> insert()

assert {:ok,
Expand All @@ -87,6 +113,38 @@ defmodule SkateWeb.DetoursChannelTest do
subscribe_and_join(socket, DetoursChannel, "detours:active:66")
end

@tag :authenticated
test "subscribes to active detours for SL1", %{socket: socket} do
:detour |> build() |> with_id(1) |> insert()

:detour
|> build()
|> activated
|> with_id(2)
|> with_route(%{name: "SL1", id: "741"})
|> insert()

:detour |> build() |> deactivated |> with_id(3) |> insert()

assert {:ok,
%{
data: [
%Skate.Detours.Detour.Detailed{
author_id: _,
direction: _,
id: 2,
intersection: "detour_nearest_intersection:" <> _,
name: "detour_route_pattern_headsign:" <> _,
route: "SL1",
status: :active,
updated_at: _
}
]
},
%Socket{}} =
subscribe_and_join(socket, DetoursChannel, "detours:active:741")
end

@tag :authenticated
test "subscribes to draft detours with initial detours", %{conn: conn, socket: socket} do
%{id: authenticated_user_id} = SkateWeb.AuthManager.Plug.current_resource(conn)
Expand Down Expand Up @@ -125,8 +183,21 @@ defmodule SkateWeb.DetoursChannelTest do
@tag :authenticated
test "subscribes to past detours with initial detours", %{socket: socket} do
:detour |> build() |> with_id(1) |> insert()
:detour |> build() |> activated |> with_id(2) |> with_route("57") |> insert()
:detour |> build() |> activated |> with_id(3) |> with_route("66") |> insert()

:detour
|> build()
|> activated
|> with_id(2)
|> with_route(%{name: "57", id: "57"})
|> insert()

:detour
|> build()
|> activated
|> with_id(3)
|> with_route(%{name: "66", id: "66"})
|> insert()

:detour |> build() |> deactivated |> with_id(4) |> insert()

assert {:ok,
Expand All @@ -151,8 +222,21 @@ defmodule SkateWeb.DetoursChannelTest do
@tag :authenticated
test "deny topic subscription when socket token validation fails", %{socket: socket} do
:detour |> build() |> with_id(1) |> insert()
:detour |> build() |> activated |> with_id(2) |> with_route("57") |> insert()
:detour |> build() |> activated |> with_id(3) |> with_route("66") |> insert()

:detour
|> build()
|> activated
|> with_id(2)
|> with_route(%{name: "57", id: "57"})
|> insert()

:detour
|> build()
|> activated
|> with_id(3)
|> with_route(%{name: "66", id: "66"})
|> insert()

:detour |> build() |> deactivated |> with_id(4) |> insert()

reassign_env(:skate, :valid_token_fn, fn _socket -> false end)
Expand Down
36 changes: 27 additions & 9 deletions test/support/factories/detour_factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,43 @@ defmodule Skate.DetourFactory do
put_in(state["value"], %{"Detour Drawing" => "Past"})
end

def with_route(%Skate.Detours.Db.Detour{} = detour, route) do
def with_route(%Skate.Detours.Db.Detour{} = detour, %{name: _, id: _} = route) do
%{detour | state: with_route(detour.state, route)}
end

def with_route(
%{
"context" => %{
"route" => %{"name" => _, "id" => _, "directionNames" => direction_names}
}
} = state,
route
%{"context" => %{"route" => %{"directionNames" => direction_names}}} = state,
%{name: route_name, id: route_id}
) do
put_in(state["context"]["route"], %{
"name" => route,
"id" => route,
"id" => route_id,
"name" => route_name,
"directionNames" => direction_names
})
end

def with_route_name(%Skate.Detours.Db.Detour{} = detour, name) do
%{detour | state: with_route_name(detour.state, name)}
end

def with_route_name(
%{"context" => %{"route" => %{"name" => _}}} = state,
name
) do
put_in(state["context"]["route"]["name"], name)
end

def with_route_id(%Skate.Detours.Db.Detour{} = detour, id) do
%{detour | state: with_route_id(detour.state, id)}
end

def with_route_id(
%{"context" => %{"route" => %{"id" => _}}} = state,
id
) do
put_in(state["context"]["route"]["id"], id)
end

def with_direction(%Skate.Detours.Db.Detour{} = detour, direction) do
%{
detour
Expand Down

0 comments on commit 2fd43d5

Please sign in to comment.