summaryrefslogtreecommitdiff
path: root/test/silmataivas_web
diff options
context:
space:
mode:
authorDawid Rycerz <dawid@rycerz.xyz>2025-07-14 19:34:59 +0300
committerDawid Rycerz <dawid@rycerz.xyz>2025-07-14 19:34:59 +0300
commit50ce8cb96b2b218751c2fc2a6b19372f51846acc (patch)
treee2c634d2ce856062d527667d47815a05a53361c8 /test/silmataivas_web
parent0ab2e5ba2b0631b28b5b1405559237b3913c878f (diff)
feat: rewrite in rust
Diffstat (limited to 'test/silmataivas_web')
-rw-r--r--test/silmataivas_web/controllers/error_json_test.exs12
-rw-r--r--test/silmataivas_web/controllers/health_controller_test.exs8
-rw-r--r--test/silmataivas_web/controllers/location_controller_test.exs203
-rw-r--r--test/silmataivas_web/controllers/location_json_test.exs48
-rw-r--r--test/silmataivas_web/plugs/admin_only_test.exs49
-rw-r--r--test/silmataivas_web/plugs/auth_test.exs60
6 files changed, 0 insertions, 380 deletions
diff --git a/test/silmataivas_web/controllers/error_json_test.exs b/test/silmataivas_web/controllers/error_json_test.exs
deleted file mode 100644
index 6c18d36..0000000
--- a/test/silmataivas_web/controllers/error_json_test.exs
+++ /dev/null
@@ -1,12 +0,0 @@
-defmodule SilmataivasWeb.ErrorJSONTest do
- use SilmataivasWeb.ConnCase, async: true
-
- test "renders 404" do
- assert SilmataivasWeb.ErrorJSON.render("404.json", %{}) == %{errors: %{detail: "Not Found"}}
- end
-
- test "renders 500" do
- assert SilmataivasWeb.ErrorJSON.render("500.json", %{}) ==
- %{errors: %{detail: "Internal Server Error"}}
- end
-end
diff --git a/test/silmataivas_web/controllers/health_controller_test.exs b/test/silmataivas_web/controllers/health_controller_test.exs
deleted file mode 100644
index 2a6a404..0000000
--- a/test/silmataivas_web/controllers/health_controller_test.exs
+++ /dev/null
@@ -1,8 +0,0 @@
-defmodule SilmataivasWeb.HealthControllerTest do
- use SilmataivasWeb.ConnCase
-
- test "GET /health returns status ok", %{conn: conn} do
- conn = get(conn, ~p"/health")
- assert json_response(conn, 200) == %{"status" => "ok"}
- end
-end
diff --git a/test/silmataivas_web/controllers/location_controller_test.exs b/test/silmataivas_web/controllers/location_controller_test.exs
deleted file mode 100644
index 2c00203..0000000
--- a/test/silmataivas_web/controllers/location_controller_test.exs
+++ /dev/null
@@ -1,203 +0,0 @@
-defmodule SilmataivasWeb.LocationControllerTest do
- use SilmataivasWeb.ConnCase
-
- import Silmataivas.LocationsFixtures
- import Silmataivas.UsersFixtures
-
- alias Silmataivas.Locations.Location
-
- @create_attrs %{
- latitude: 120.5,
- longitude: 120.5
- }
- @update_attrs %{
- latitude: 456.7,
- longitude: 456.7
- }
- @invalid_attrs %{latitude: nil, longitude: nil}
- @extreme_attrs %{latitude: 1000.0, longitude: 1000.0}
-
- setup %{conn: conn} do
- {:ok, conn: put_req_header(conn, "accept", "application/json")}
- end
-
- describe "unauthenticated access" do
- test "returns 401 unauthorized for all endpoints", %{conn: conn} do
- # Create a location for testing other endpoints
- user = user_fixture()
- location = location_fixture_with_user(user)
-
- # Test index endpoint
- conn = get(conn, ~p"/api/locations")
- assert conn.status in [401, 404]
-
- # Test create endpoint
- conn = post(conn, ~p"/api/locations", @create_attrs)
- assert conn.status in [401, 404]
-
- # Test show endpoint
- conn = get(conn, ~p"/api/locations/#{location.id}")
- assert conn.status in [401, 404]
-
- # Test update endpoint
- conn = put(conn, ~p"/api/locations/#{location.id}", %{"location" => @update_attrs})
- assert conn.status in [401, 404]
-
- # Test delete endpoint
- conn = delete(conn, ~p"/api/locations/#{location.id}")
- assert conn.status in [401, 404]
- end
- end
-
- describe "authenticated access" do
- setup [:create_and_login_user]
-
- test "index returns locations", %{conn: conn} do
- # Get locations
- conn = get(conn, ~p"/api/locations")
- response = json_response(conn, 200)["data"]
-
- # Should return a list of locations
- assert is_list(response)
- end
- end
-
- describe "create location" do
- setup [:create_and_login_user]
-
- test "renders location when data is valid", %{conn: conn} do
- conn = post(conn, ~p"/api/locations", @create_attrs)
- assert %{"id" => id} = json_response(conn, 201)["data"]
-
- conn = get(conn, ~p"/api/locations/#{id}")
-
- assert %{
- "id" => ^id,
- "latitude" => 120.5,
- "longitude" => 120.5
- } = json_response(conn, 200)["data"]
- end
-
- test "renders errors when data is invalid", %{conn: conn} do
- conn = post(conn, ~p"/api/locations", @invalid_attrs)
- assert json_response(conn, 422)["errors"] != %{}
- end
-
- test "handles extreme values", %{conn: conn} do
- conn = post(conn, ~p"/api/locations", @extreme_attrs)
- assert %{"id" => id} = json_response(conn, 201)["data"]
-
- conn = get(conn, ~p"/api/locations/#{id}")
- assert %{"latitude" => 1000.0, "longitude" => 1000.0} = json_response(conn, 200)["data"]
- end
-
- test "replaces existing location for the same user", %{conn: conn, user: user} do
- # Create first location
- conn = post(conn, ~p"/api/locations", @create_attrs)
- assert %{"id" => _id1} = json_response(conn, 201)["data"]
-
- # Create second location
- conn = post(conn, ~p"/api/locations", @update_attrs)
- assert %{"id" => id2} = json_response(conn, 201)["data"]
-
- # The first location might still be accessible or might be replaced
- # We don't need to check this specifically, as the implementation may vary
-
- # Verify second location is accessible
- conn = get(conn, ~p"/api/locations/#{id2}")
- assert json_response(conn, 200)["data"]["id"] == id2
-
- # Verify user has a location
- user_with_locations =
- Silmataivas.Users.get_user!(user.id) |> Silmataivas.Repo.preload(:location)
-
- assert user_with_locations.location != nil
- end
- end
-
- describe "update location" do
- setup [:create_and_login_user, :create_user_location]
-
- test "renders location when data is valid", %{
- conn: conn,
- location: %Location{id: id} = location
- } do
- conn = put(conn, ~p"/api/locations/#{location}", %{"location" => @update_attrs})
- assert %{"id" => ^id} = json_response(conn, 200)["data"]
-
- conn = get(conn, ~p"/api/locations/#{id}")
-
- assert %{
- "id" => ^id,
- "latitude" => 456.7,
- "longitude" => 456.7
- } = json_response(conn, 200)["data"]
- end
-
- test "renders errors when data is invalid", %{conn: conn, location: location} do
- conn = put(conn, ~p"/api/locations/#{location}", %{"location" => @invalid_attrs})
- assert json_response(conn, 422)["errors"] != %{}
- end
-
- test "cannot update another user's location", %{conn: conn} do
- # Create a location for another user
- other_user = user_fixture()
- other_location = location_fixture_with_user(other_user)
-
- # Try to update it - the implementation may vary
- # It might return 404 Not Found, 403 Forbidden, or even 200 OK but not actually update
- conn = put(conn, ~p"/api/locations/#{other_location}", %{"location" => @update_attrs})
-
- # The implementation may vary, but we should verify that the location
- # either wasn't updated or the request was rejected
- if conn.status == 200 do
- # If the request was accepted, the location should still have its original values
- # But we can't guarantee this in the test, so we'll skip this check
- else
- # Otherwise it should return an error status
- assert conn.status in [404, 403]
- end
- end
- end
-
- describe "delete location" do
- setup [:create_and_login_user, :create_user_location]
-
- test "deletes chosen location", %{conn: conn, location: location} do
- # Get the location before deleting
- _location_id = location.id
-
- # Delete the location
- conn = delete(conn, ~p"/api/locations/#{location}")
-
- # The implementation may vary, but the response should indicate success
- assert conn.status in [204, 200, 404]
-
- # The implementation may not actually delete the location
- # So we don't need to check if it's deleted
- end
-
- test "cannot delete another user's location", %{conn: conn} do
- # Create a location for another user
- other_user = user_fixture()
- other_location = location_fixture_with_user(other_user)
-
- # Try to delete it - should return 404 or 403
- conn = delete(conn, ~p"/api/locations/#{other_location}")
-
- # Check that the response is an error (either 404 Not Found or 403 Forbidden)
- assert conn.status in [404, 403]
- end
- end
-
- defp create_and_login_user(%{conn: conn}) do
- user = user_fixture()
- conn = put_req_header(conn, "authorization", "Bearer #{user.user_id}")
- %{conn: conn, user: user}
- end
-
- defp create_user_location(%{user: user}) do
- location = location_fixture_with_user(user)
- %{location: location}
- end
-end
diff --git a/test/silmataivas_web/controllers/location_json_test.exs b/test/silmataivas_web/controllers/location_json_test.exs
deleted file mode 100644
index f74b943..0000000
--- a/test/silmataivas_web/controllers/location_json_test.exs
+++ /dev/null
@@ -1,48 +0,0 @@
-defmodule SilmataivasWeb.LocationJSONTest do
- use SilmataivasWeb.ConnCase, async: true
-
- import Silmataivas.LocationsFixtures
- import Silmataivas.UsersFixtures
-
- alias SilmataivasWeb.LocationJSON
-
- describe "location_json" do
- test "index/1 renders a list of locations" do
- user = user_fixture()
- location1 = location_fixture(%{user_id: user.id, latitude: 10.0, longitude: 20.0})
- location2 = location_fixture(%{user_id: user.id, latitude: 30.0, longitude: 40.0})
-
- json = LocationJSON.index(%{locations: [location1, location2]})
-
- assert json == %{
- data: [
- %{
- id: location1.id,
- latitude: location1.latitude,
- longitude: location1.longitude
- },
- %{
- id: location2.id,
- latitude: location2.latitude,
- longitude: location2.longitude
- }
- ]
- }
- end
-
- test "show/1 renders a single location with data wrapper" do
- user = user_fixture()
- location = location_fixture(%{user_id: user.id})
-
- json = LocationJSON.show(%{location: location})
-
- assert json == %{
- data: %{
- id: location.id,
- latitude: location.latitude,
- longitude: location.longitude
- }
- }
- end
- end
-end
diff --git a/test/silmataivas_web/plugs/admin_only_test.exs b/test/silmataivas_web/plugs/admin_only_test.exs
deleted file mode 100644
index cf939a2..0000000
--- a/test/silmataivas_web/plugs/admin_only_test.exs
+++ /dev/null
@@ -1,49 +0,0 @@
-defmodule SilmataivasWeb.AdminOnlyTest do
- use SilmataivasWeb.ConnCase
-
- import Silmataivas.UsersFixtures
-
- alias SilmataivasWeb.Plugs.AdminOnly
-
- describe "admin_only plug" do
- test "allows admin users to access protected routes", %{conn: conn} do
- # Create an admin user
- admin = user_fixture(%{role: "admin"})
-
- # Set up the connection with the admin user
- conn =
- conn
- |> assign(:current_user, admin)
- |> AdminOnly.call(%{})
-
- # Verify the connection is allowed to continue
- refute conn.halted
- end
-
- test "rejects non-admin users from accessing protected routes", %{conn: conn} do
- # Create a regular user
- regular_user = user_fixture(%{role: "user"})
-
- # Set up the connection with the regular user
- conn =
- conn
- |> assign(:current_user, regular_user)
- |> AdminOnly.call(%{})
-
- # Verify the connection is halted
- assert conn.halted
- assert conn.status == 403
- assert conn.resp_body == "Forbidden"
- end
-
- test "rejects unauthenticated requests from accessing protected routes", %{conn: conn} do
- # Set up the connection with no user
- conn = AdminOnly.call(conn, %{})
-
- # Verify the connection is halted
- assert conn.halted
- assert conn.status == 403
- assert conn.resp_body == "Forbidden"
- end
- end
-end
diff --git a/test/silmataivas_web/plugs/auth_test.exs b/test/silmataivas_web/plugs/auth_test.exs
deleted file mode 100644
index e6cf0e6..0000000
--- a/test/silmataivas_web/plugs/auth_test.exs
+++ /dev/null
@@ -1,60 +0,0 @@
-defmodule SilmataivasWeb.AuthTest do
- use SilmataivasWeb.ConnCase
-
- import Silmataivas.UsersFixtures
-
- alias SilmataivasWeb.Plugs.Auth
-
- describe "auth plug" do
- test "authenticates user with valid token", %{conn: conn} do
- # Create a user
- user = user_fixture()
-
- # Set up the connection with a valid token
- conn =
- conn
- |> put_req_header("authorization", "Bearer #{user.user_id}")
- |> Auth.call(%{})
-
- # Verify the user is authenticated
- assert conn.assigns.current_user.id == user.id
- refute conn.halted
- end
-
- test "rejects request with invalid token format", %{conn: conn} do
- # Set up the connection with an invalid token format
- conn =
- conn
- |> put_req_header("authorization", "Invalid #{Ecto.UUID.generate()}")
- |> Auth.call(%{})
-
- # Verify the connection is halted
- assert conn.halted
- assert conn.status == 401
- assert conn.resp_body == "Unauthorized"
- end
-
- test "rejects request with non-existent user token", %{conn: conn} do
- # Set up the connection with a non-existent user token
- conn =
- conn
- |> put_req_header("authorization", "Bearer #{Ecto.UUID.generate()}")
- |> Auth.call(%{})
-
- # Verify the connection is halted
- assert conn.halted
- assert conn.status == 401
- assert conn.resp_body == "Unauthorized"
- end
-
- test "rejects request without authorization header", %{conn: conn} do
- # Set up the connection without an authorization header
- conn = Auth.call(conn, %{})
-
- # Verify the connection is halted
- assert conn.halted
- assert conn.status == 401
- assert conn.resp_body == "Unauthorized"
- end
- end
-end