Skip to content

Commit 1f52a4f

Browse files
committed
Fix tests
1 parent 65296d8 commit 1f52a4f

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

services/app/apps/codebattle/test/codebattle_web/controllers/api/v1/stream_config_controller_test.exs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ defmodule CodebattleWeb.Api.V1.StreamConfigControllerTest do
33

44
alias Codebattle.StreamConfig
55

6-
setup do
7-
user = insert(:user)
8-
conn = put_req_header(build_conn(), "authorization", "Bearer #{user.auth_token}")
6+
setup %{conn: conn} do
7+
user = :user |> insert() |> Repo.reload()
8+
conn = put_session(conn, :user_id, user.id)
9+
910
{:ok, %{conn: conn, user: user}}
1011
end
1112

1213
describe "index" do
1314
test "returns empty list when user has no configs", %{conn: conn} do
14-
conn = get(conn, Routes.v1_stream_config_path(conn, :index))
15+
conn = get(conn, Routes.api_v1_stream_config_path(conn, :index))
1516
assert json_response(conn, 200) == %{"items" => []}
1617
end
1718

@@ -23,7 +24,7 @@ defmodule CodebattleWeb.Api.V1.StreamConfigControllerTest do
2324
%StreamConfig{} |> StreamConfig.changeset(config1) |> Repo.insert!()
2425
%StreamConfig{} |> StreamConfig.changeset(config2) |> Repo.insert!()
2526

26-
conn = get(conn, Routes.v1_stream_config_path(conn, :index))
27+
conn = get(conn, Routes.api_v1_stream_config_path(conn, :index))
2728

2829
response = json_response(conn, 200)
2930
assert length(response["items"]) == 2
@@ -37,20 +38,22 @@ defmodule CodebattleWeb.Api.V1.StreamConfigControllerTest do
3738
describe "put_all" do
3839
test "creates new configs", %{conn: conn} do
3940
configs = [
40-
%{"name" => "config1", "key" => "value1"},
41-
%{"name" => "config2", "key" => "value2"}
41+
%{"name" => "config1", "config" => %{"key" => "value1"}},
42+
%{"name" => "config2", "config" => %{"key" => "value2"}}
4243
]
4344

44-
conn = put(conn, Routes.v1_stream_config_path(conn, :put_all), %{configs: configs})
45+
conn = put(conn, Routes.api_v1_stream_config_path(conn, :put_all), %{configs: configs})
4546

4647
response = json_response(conn, 200)
4748
assert length(response["items"]) == 2
4849

4950
# Verify configs were created with correct values
50-
assert Enum.at(response["items"], 0)["name"] == "config1"
51-
assert Enum.at(response["items"], 0)["config"]["key"] == "value1"
52-
assert Enum.at(response["items"], 1)["name"] == "config2"
53-
assert Enum.at(response["items"], 1)["config"]["key"] == "value2"
51+
config1 = Enum.find(response["items"], &(&1["name"] == "config1"))
52+
config2 = Enum.find(response["items"], &(&1["name"] == "config2"))
53+
assert config1["name"] == "config1"
54+
assert config1["config"] == %{"config" => %{"key" => "value1"}, "name" => "config1"}
55+
assert config2["name"] == "config2"
56+
assert config2["config"] == %{"config" => %{"key" => "value2"}, "name" => "config2"}
5457
end
5558

5659
test "updates existing configs", %{conn: conn, user: user} do
@@ -60,15 +63,16 @@ defmodule CodebattleWeb.Api.V1.StreamConfigControllerTest do
6063

6164
# Update the config
6265
updated_configs = [
63-
%{"name" => "config1", "key" => "new_value"}
66+
%{"name" => "config1", "config" => %{"key" => "new_value"}}
6467
]
6568

66-
conn = put(conn, Routes.v1_stream_config_path(conn, :put_all), %{configs: updated_configs})
69+
conn = put(conn, Routes.api_v1_stream_config_path(conn, :put_all), %{configs: updated_configs})
6770

6871
response = json_response(conn, 200)
6972
assert length(response["items"]) == 1
70-
assert Enum.at(response["items"], 0)["name"] == "config1"
71-
assert Enum.at(response["items"], 0)["config"]["key"] == "new_value"
73+
config = Enum.at(response["items"], 0)
74+
assert config["name"] == "config1"
75+
assert config["config"] == %{"config" => %{"key" => "new_value"}, "name" => "config1"}
7276
end
7377

7478
test "deletes configs not in the list", %{conn: conn, user: user} do
@@ -81,15 +85,16 @@ defmodule CodebattleWeb.Api.V1.StreamConfigControllerTest do
8185

8286
# Only keep config1, config2 should be deleted
8387
updated_configs = [
84-
%{"name" => "config1", "key" => "updated_value"}
88+
%{"name" => "config1", "config" => %{"key" => "updated_value"}}
8589
]
8690

87-
conn = put(conn, Routes.v1_stream_config_path(conn, :put_all), %{configs: updated_configs})
91+
conn = put(conn, Routes.api_v1_stream_config_path(conn, :put_all), %{configs: updated_configs})
8892

8993
response = json_response(conn, 200)
9094
assert length(response["items"]) == 1
91-
assert Enum.at(response["items"], 0)["name"] == "config1"
92-
assert Enum.at(response["items"], 0)["config"]["key"] == "updated_value"
95+
config = Enum.at(response["items"], 0)
96+
assert config["name"] == "config1"
97+
assert config["config"] == %{"config" => %{"key" => "updated_value"}, "name" => "config1"}
9398

9499
# Verify config2 was deleted
95100
assert Repo.get_by(StreamConfig, name: "config2", user_id: user.id) == nil
@@ -101,7 +106,7 @@ defmodule CodebattleWeb.Api.V1.StreamConfigControllerTest do
101106
%StreamConfig{} |> StreamConfig.changeset(config) |> Repo.insert!()
102107

103108
# Send empty configs list (should delete all configs)
104-
conn = put(conn, Routes.v1_stream_config_path(conn, :put_all), %{configs: []})
109+
conn = put(conn, Routes.api_v1_stream_config_path(conn, :put_all), %{configs: []})
105110

106111
response = json_response(conn, 200)
107112
assert response["items"] == []

0 commit comments

Comments
 (0)