- add absinthe_phoenix for WebSocket subscription support - create GraphQL WebSocket socket with API token auth at /socket/graphql - add time-series queries: deviceSensors, sensorReadings, interfaceTraffic, checkResults - add subscriptions: deviceStatusChanged, alertEvent, sensorReadingsUpdated - wire subscription triggers into agent_channel and alerts contexts - add org-scoped access control for sensors/interfaces via join queries - add 17 tests covering queries, org isolation, empty data edge cases, and auth - update GraphQL API docs page with time-series and subscription sections
66 lines
1.6 KiB
Elixir
66 lines
1.6 KiB
Elixir
defmodule ToweropsWeb.GraphQLSocketTest do
|
|
use Towerops.DataCase, async: true
|
|
|
|
import Towerops.AccountsFixtures
|
|
import Towerops.OrganizationsFixtures
|
|
|
|
alias Towerops.ApiTokens
|
|
alias ToweropsWeb.GraphQLSocket
|
|
|
|
describe "connect/3" do
|
|
setup do
|
|
user = user_fixture()
|
|
organization = organization_fixture(user.id)
|
|
|
|
{:ok, {_token, raw_token}} =
|
|
ApiTokens.create_api_token(%{
|
|
organization_id: organization.id,
|
|
user_id: user.id,
|
|
name: "GraphQL Test Token"
|
|
})
|
|
|
|
%{user: user, organization: organization, raw_token: raw_token}
|
|
end
|
|
|
|
test "connects with valid API token", %{
|
|
organization: organization,
|
|
raw_token: raw_token
|
|
} do
|
|
socket = %Phoenix.Socket{
|
|
assigns: %{},
|
|
transport: :websocket
|
|
}
|
|
|
|
assert {:ok, socket} = GraphQLSocket.connect(%{"token" => raw_token}, socket, %{})
|
|
assert socket.assigns.organization_id == organization.id
|
|
end
|
|
|
|
test "rejects invalid token" do
|
|
socket = %Phoenix.Socket{
|
|
assigns: %{},
|
|
transport: :websocket
|
|
}
|
|
|
|
assert :error = GraphQLSocket.connect(%{"token" => "invalid_token"}, socket, %{})
|
|
end
|
|
|
|
test "rejects missing token" do
|
|
socket = %Phoenix.Socket{
|
|
assigns: %{},
|
|
transport: :websocket
|
|
}
|
|
|
|
assert :error = GraphQLSocket.connect(%{}, socket, %{})
|
|
end
|
|
end
|
|
|
|
describe "id/1" do
|
|
test "returns socket ID based on organization_id" do
|
|
socket = %Phoenix.Socket{
|
|
assigns: %{organization_id: "some-org-id"}
|
|
}
|
|
|
|
assert GraphQLSocket.id(socket) == "graphql_socket:some-org-id"
|
|
end
|
|
end
|
|
end
|