graphql-engine/server/tests-hspec/Test/DisableRootFields/DefaultRootFieldsSpec.hs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

178 lines
5.4 KiB
Haskell
Raw Normal View History

{-# LANGUAGE QuasiQuotes #-}
-- | Test if all root fields (list, pk and aggregate) are enabled by default
module Test.DisableRootFields.DefaultRootFieldsSpec (spec) where
import Data.List.NonEmpty qualified as NE
import Harness.Backend.Postgres qualified as Postgres
import Harness.Backend.Sqlserver qualified as SQLServer
import Harness.GraphqlEngine qualified as GraphqlEngine
import Harness.Quoter.Yaml (yaml)
import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.TestEnvironment (TestEnvironment)
import Harness.Yaml (shouldReturnYaml)
import Hasura.Prelude
import Test.DisableRootFields.Common
import Test.Hspec (SpecWith, describe, it)
--------------------------------------------------------------------------------
-- Preamble
spec :: SpecWith TestEnvironment
spec =
Fixture.run
( NE.fromList
[ (Fixture.fixture $ Fixture.Backend Fixture.Postgres)
{ Fixture.setupTeardown = \(testEnv, _) ->
[ Postgres.setupTablesAction schema testEnv,
postgresSetupPermissions testEnv
]
},
(Fixture.fixture $ Fixture.Backend Fixture.SQLServer)
{ Fixture.setupTeardown = \(testEnv, _) ->
[ SQLServer.setupTablesAction schema testEnv,
sqlserverSetupPermissions testEnv
]
}
]
)
tests
--------------------------------------------------------------------------------
-- Schema
schema :: [Schema.Table]
schema = [author]
author :: Schema.Table
author =
(table "author")
{ tableColumns =
[ Schema.column "id" Schema.TInt,
Schema.column "name" Schema.TStr
],
tablePrimaryKey = ["id"],
tableData =
[ [Schema.VInt 1, Schema.VStr "Author 1"],
[Schema.VInt 2, Schema.VStr "Author 2"]
]
}
--------------------------------------------------------------------------------
-- Setting up postgres
postgresSetupPermissions :: TestEnvironment -> Fixture.SetupAction
postgresSetupPermissions testEnv =
Fixture.SetupAction
{ setupAction =
GraphqlEngine.postMetadata_
testEnv
[yaml|
type: pg_create_select_permission
args:
source: postgres
table:
schema: hasura
name: author
role: user
permission:
filter:
id: X-Hasura-User-Id
allow_aggregations: true
columns: '*'
|],
teardownAction = \_ ->
GraphqlEngine.postMetadata_
testEnv
[yaml|
type: pg_drop_select_permission
args:
source: postgres
table:
schema: hasura
name: author
role: user
|]
}
--------------------------------------------------------------------------------
-- Setting up SQL Server
sqlserverSetupPermissions :: TestEnvironment -> Fixture.SetupAction
sqlserverSetupPermissions testEnv =
Fixture.SetupAction
{ setupAction =
GraphqlEngine.postMetadata_
testEnv
[yaml|
type: mssql_create_select_permission
args:
source: mssql
table:
schema: hasura
name: author
role: user
permission:
filter:
id: X-Hasura-User-Id
allow_aggregations: true
columns: '*'
|],
teardownAction = \_ ->
GraphqlEngine.postMetadata_
testEnv
[yaml|
type: mssql_drop_select_permission
args:
source: mssql
table:
schema: hasura
name: author
role: user
|]
}
--------------------------------------------------------------------------------
-- Tests
-- Root fields are enabled and accessible by default, until specifed otherwise in metadata.
tests :: Fixture.Options -> SpecWith TestEnvironment
tests opts = describe "DefaultRootFieldSpec" $ do
let userHeaders = [("X-Hasura-Role", "user"), ("X-Hasura-User-Id", "1")]
it "'list' root field is enabled and accessible" $ \testEnvironment -> do
shouldReturnYaml
opts
(GraphqlEngine.postGraphqlWithHeaders testEnvironment userHeaders listQuery)
listRFEnabledExpectedResponse
it "'pk' root field is enabled and accessible" $ \testEnvironment -> do
shouldReturnYaml
opts
(GraphqlEngine.postGraphqlWithHeaders testEnvironment userHeaders pkQuery)
pkRFEnabledExpectedResponse
it "'aggregate' root field is enabled and accessible" $ \testEnvironment -> do
shouldReturnYaml
opts
(GraphqlEngine.postGraphqlWithHeaders testEnvironment userHeaders aggregateQuery)
aggRFEnabledExpectedResponse
it "introspection query: all root fields are enabled and accessible for query" $ \testEnvironment -> do
let expectedResponse =
[yaml|
data:
__schema:
queryType:
fields:
- name: hasura_author
- name: hasura_author_aggregate
- name: hasura_author_by_pk
|]
shouldReturnYaml
opts
(GraphqlEngine.postGraphqlWithHeaders testEnvironment userHeaders queryTypesIntrospection)
expectedResponse