2022-06-07 08:32:08 +03:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
|
|
|
-- | Test if all root fields (list, pk and aggregate) are enabled by default
|
|
|
|
module Test.DisableRootFields.DefaultRootFieldsSpec (spec) where
|
|
|
|
|
2022-08-11 18:03:04 +03:00
|
|
|
import Data.List.NonEmpty qualified as NE
|
2022-06-07 08:32:08 +03:00
|
|
|
import Harness.Backend.Postgres qualified as Postgres
|
|
|
|
import Harness.Backend.Sqlserver qualified as SQLServer
|
|
|
|
import Harness.GraphqlEngine qualified as GraphqlEngine
|
2022-08-05 12:34:25 +03:00
|
|
|
import Harness.Quoter.Yaml (yaml)
|
2022-08-11 10:24:07 +03:00
|
|
|
import Harness.Test.Fixture qualified as Fixture
|
2022-06-17 11:44:04 +03:00
|
|
|
import Harness.Test.Schema (Table (..), table)
|
2022-06-07 08:32:08 +03:00
|
|
|
import Harness.Test.Schema qualified as Schema
|
|
|
|
import Harness.TestEnvironment (TestEnvironment)
|
2022-08-05 12:34:25 +03:00
|
|
|
import Harness.Yaml (shouldReturnYaml)
|
2022-06-07 08:32:08 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Test.DisableRootFields.Common
|
|
|
|
import Test.Hspec (SpecWith, describe, it)
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Preamble
|
|
|
|
|
|
|
|
spec :: SpecWith TestEnvironment
|
|
|
|
spec =
|
2022-08-11 10:24:07 +03:00
|
|
|
Fixture.run
|
2022-08-11 18:03:04 +03:00
|
|
|
( 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
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
2022-06-07 08:32:08 +03:00
|
|
|
tests
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Schema
|
|
|
|
|
|
|
|
schema :: [Schema.Table]
|
|
|
|
schema = [author]
|
|
|
|
|
|
|
|
author :: Schema.Table
|
|
|
|
author =
|
2022-06-17 11:44:04 +03:00
|
|
|
(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"]
|
|
|
|
]
|
|
|
|
}
|
2022-06-07 08:32:08 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Setting up postgres
|
|
|
|
|
2022-08-11 10:24:07 +03:00
|
|
|
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
|
|
|
|
|]
|
|
|
|
}
|
2022-06-07 08:32:08 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Setting up SQL Server
|
|
|
|
|
2022-08-11 10:24:07 +03:00
|
|
|
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
|
|
|
|
|]
|
|
|
|
}
|
2022-06-07 08:32:08 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Tests
|
|
|
|
|
|
|
|
-- Root fields are enabled and accessible by default, until specifed otherwise in metadata.
|
2022-08-11 10:24:07 +03:00
|
|
|
tests :: Fixture.Options -> SpecWith TestEnvironment
|
2022-06-07 08:32:08 +03:00
|
|
|
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
|