forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayParamPermissionSpec.hs
158 lines (138 loc) · 3.77 KB
/
ArrayParamPermissionSpec.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
{-# LANGUAGE QuasiQuotes #-}
-- | Test case for permissions with array params
-- https://github.com/hasura/graphql-engine-mono/pull/4651
module Test.ArrayParamPermissionSpec (spec) where
import Harness.Backend.Postgres qualified as Postgres
import Harness.GraphqlEngine qualified as GraphqlEngine
import Harness.Quoter.Graphql (graphql)
import Harness.Quoter.Yaml (yaml)
import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..))
import Harness.Test.Schema qualified as Schema
import Harness.TestEnvironment (TestEnvironment)
import Harness.Yaml (shouldReturnYaml)
import Hasura.Prelude
import Test.Hspec (SpecWith, it)
--------------------------------------------------------------------------------
-- ** Preamble
spec :: SpecWith TestEnvironment
spec =
Fixture.run
( [ (Fixture.fixture $ Fixture.Backend Fixture.Postgres)
{ Fixture.setupTeardown = \(testEnv, _) ->
[postgresSetupTeardown testEnv]
}
]
)
tests
--------------------------------------------------------------------------------
-- ** Schema
schema :: [Schema.Table]
schema = [author]
author :: Schema.Table
author =
(Schema.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"]
]
}
--------------------------------------------------------------------------------
-- ** Setup and teardown
postgresSetupTeardown :: TestEnvironment -> Fixture.SetupAction
postgresSetupTeardown testEnv =
Fixture.SetupAction
(postgresSetup (testEnv, ()))
(const $ postgresTeardown (testEnv, ()))
postgresSetup :: (TestEnvironment, ()) -> IO ()
postgresSetup (testEnvironment, localTestEnvironment) = do
Postgres.setup schema (testEnvironment, localTestEnvironment)
postgresCreatePermissions testEnvironment
postgresCreatePermissions :: TestEnvironment -> IO ()
postgresCreatePermissions testEnvironment = do
GraphqlEngine.postMetadata_
testEnvironment
[yaml|
type: pg_create_select_permission
args:
source: postgres
table:
schema: hasura
name: author
role: user
permission:
filter:
id:
_in: X-Hasura-Allowed-Ids
columns: '*'
|]
postgresTeardown :: (TestEnvironment, ()) -> IO ()
postgresTeardown (testEnvironment, ()) = do
-- teardown permissions
GraphqlEngine.postMetadata_ testEnvironment $
[yaml|
type: bulk
args:
- type: pg_drop_select_permission
args:
source: postgres
table:
schema: hasura
name: author
role: user
|]
-- and then rest of the teardown
Postgres.teardown schema (testEnvironment, ())
--------------------------------------------------------------------------------
-- * Tests
tests :: Fixture.Options -> SpecWith TestEnvironment
tests opts = do
it "non-matching X-Hasura-Allowed-Ids should return no data" $ \testEnvironment -> do
let userHeaders = [("X-Hasura-Role", "user"), ("X-Hasura-Allowed-Ids", "{}")]
shouldReturnYaml
opts
( GraphqlEngine.postGraphqlWithHeaders
testEnvironment
userHeaders
[graphql|
query {
hasura_author {
id
name
}
}
|]
)
[yaml|
data:
hasura_author: []
|]
it "matching X-Hasura-Allowed-Ids should return data" $ \testEnvironment -> do
let userHeaders = [("X-Hasura-Role", "user"), ("X-Hasura-Allowed-Ids", "{1,2,3}")]
shouldReturnYaml
opts
( GraphqlEngine.postGraphqlWithHeaders
testEnvironment
userHeaders
[graphql|
query {
hasura_author {
id
name
}
}
|]
)
[yaml|
data:
hasura_author:
- name: Author 1
id: 1
- name: Author 2
id: 2
|]