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