towerops/test/towerops_web/graphql/middleware/organization_scope_test.exs
graham c5481c2cd3 add mobile token auth to GraphQL endpoint (#48)
Extend the GraphQL API and socket to accept mobile session tokens
alongside existing API tokens. Add OrganizationScope middleware
that extracts organization_id from query args for mobile users.
Add my_organizations query for mobile app org listing.

Reviewed-on: graham/towerops-web#48
2026-03-16 15:23:07 -05:00

72 lines
2 KiB
Elixir

defmodule ToweropsWeb.GraphQL.Middleware.OrganizationScopeTest do
use Towerops.DataCase, async: true
import Towerops.AccountsFixtures
import Towerops.OrganizationsFixtures
alias ToweropsWeb.GraphQL.Middleware.OrganizationScope
describe "call/2" do
test "passes through when context already has organization_id" do
org_id = Ecto.UUID.generate()
resolution = %Absinthe.Resolution{
context: %{organization_id: org_id, user: %{id: Ecto.UUID.generate()}},
arguments: %{}
}
result = OrganizationScope.call(resolution, [])
assert result.context.organization_id == org_id
assert result.errors == []
end
test "promotes organization_id from args for mobile user with access" do
user = user_fixture()
organization = organization_fixture(user.id)
resolution = %Absinthe.Resolution{
context: %{user: user},
arguments: %{organization_id: organization.id}
}
result = OrganizationScope.call(resolution, [])
assert result.context.organization_id == organization.id
assert result.errors == []
end
test "errors when mobile user lacks access to organization" do
user = user_fixture()
other_org_id = Ecto.UUID.generate()
resolution = %Absinthe.Resolution{
context: %{user: user},
arguments: %{organization_id: other_org_id}
}
result = OrganizationScope.call(resolution, [])
assert result.errors != []
end
test "errors when mobile user omits organization_id" do
user = user_fixture()
resolution = %Absinthe.Resolution{
context: %{user: user},
arguments: %{}
}
result = OrganizationScope.call(resolution, [])
assert result.errors != []
end
test "errors when no authentication at all" do
resolution = %Absinthe.Resolution{
context: %{},
arguments: %{}
}
result = OrganizationScope.call(resolution, [])
assert result.errors != []
end
end
end