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
72 lines
2 KiB
Elixir
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
|