diff --git a/server/graphql-engine.cabal b/server/graphql-engine.cabal index d8314ee2ea7f2..ed20640f5b10e 100644 --- a/server/graphql-engine.cabal +++ b/server/graphql-engine.cabal @@ -231,7 +231,7 @@ test-suite graphql-engine-test , aeson , aeson-casing , bytestring - --, directory + -- , directory --, fgl , filepath , hspec diff --git a/server/test/Main.hs b/server/test/Main.hs index 653c9fc0c7da0..3d3d2b03485f8 100644 --- a/server/test/Main.hs +++ b/server/test/Main.hs @@ -10,7 +10,6 @@ import Options.Applicative import System.Environment (withArgs) import System.Exit (exitFailure) import Test.Hspec.Core.Runner -import Test.Hspec.Formatters import Test.Hspec.Wai import qualified Data.Aeson as J @@ -43,7 +42,7 @@ resetStateTx = do ravenApp :: L.LoggerCtx -> PGQ.PGPool -> IO Application ravenApp loggerCtx pool = do - let corsCfg = CorsConfigG "*" True -- cors is disabled + let corsCfg = CorsConfigG "*" False -- cors is enabled -- spockAsApp $ spockT id $ app Q.Serializable Nothing rlogger pool AMNoAuth corsCfg True -- no access key and no webhook mkWaiApp Q.Serializable Nothing loggerCtx pool AMNoAuth corsCfg True -- no access key and no webhook @@ -60,11 +59,11 @@ main = do void $ liftIO $ runExceptT $ Q.runTx pool defTxMode resetStateTx -- intialize state for graphql-engine in the database liftIO $ initialise pool + -- generate the test specs specs <- mkSpecs loggerCtx <- L.mkLoggerCtx L.defaultLoggerSettings -- run the tests - withArgs [] $ hspecWith defaultConfig {configFormatter = Just progress} $ - with (ravenApp loggerCtx pool) specs + withArgs [] $ hspecWith defaultConfig $ with (ravenApp loggerCtx pool) specs where initialise :: Q.PGPool -> IO () diff --git a/server/test/Spec.hs b/server/test/Spec.hs index dd60a13719e4e..0f6b2edb47e23 100644 --- a/server/test/Spec.hs +++ b/server/test/Spec.hs @@ -22,15 +22,18 @@ data TestCase , tcQuery :: !J.Value , tcUrl :: !T.Text , tcStatus :: !Int + -- , tcDependsOn :: !(Maybe TestCase) } deriving (Show) $(J.deriveJSON (J.aesonDrop 2 J.snakeCase) ''TestCase) + querySpecFiles :: [FilePath] querySpecFiles = [ "create_tables.yaml" , "track_tables.yaml" , "create_author_article_relationship.yaml" + , "create_author_article_permissions.yaml" ] gqlSpecFiles :: [FilePath] @@ -46,6 +49,8 @@ gqlSpecFiles = , "insert_mutation_article_on_conflict_error_02.yaml" , "insert_mutation_article_on_conflict_error_03.yaml" , "nested_select_query_article.yaml" + , "update_mutation_author.yaml" + , "delete_mutation_article.yaml" ] readTestCase :: FilePath -> IO TestCase @@ -63,9 +68,10 @@ mkSpec tc = do url = tcUrl tc q = tcQuery tc respStatus = (fromIntegral $ tcStatus tc) :: ResponseMatcher - it (T.unpack desc) $ do + it (T.unpack desc) $ post (T.encodeUtf8 url) (J.encode q) `shouldRespondWith` respStatus + mkSpecs :: IO (SpecWith Application) mkSpecs = do ddlTc <- mapM readTestCase querySpecFiles @@ -75,6 +81,16 @@ mkSpecs = do it "responds with version" $ get "/v1/version" `shouldRespondWith` 200 + describe "console endpoint" $ + it "responds with 200" $ + get "/console" `shouldRespondWith` 200 + + describe "CORS test" $ + it "should respond with correct CORS headers" $ + request "OPTIONS" "/v1/version" [("Origin", "example.com")] "" + `shouldRespondWith` 204 + {matchHeaders = ["Access-Control-Allow-Origin" <:> "example.com"]} + describe "Query API" $ mapM_ mkSpec ddlTc describe "GraphQL API" $ mapM_ mkSpec gqlTc diff --git a/server/test/testcases/create_author_article_permissions.yaml b/server/test/testcases/create_author_article_permissions.yaml new file mode 100644 index 0000000000000..83a7a0f7357c9 --- /dev/null +++ b/server/test/testcases/create_author_article_permissions.yaml @@ -0,0 +1,14 @@ +description: Create relevant permissions +url: /v1/query +status: 200 +query: + type: create_select_permission + args: + table: article + role: user + permission: + columns: '*' + filter: + $or: + - author_id: X-HASURA-USER-ID + - is_published: true diff --git a/server/test/testcases/create_author_article_relationship.yaml b/server/test/testcases/create_author_article_relationship.yaml index b36440b25cf30..ad1aeb869aef7 100644 --- a/server/test/testcases/create_author_article_relationship.yaml +++ b/server/test/testcases/create_author_article_relationship.yaml @@ -2,9 +2,19 @@ description: Creates relationships url: /v1/query status: 200 query: - type: create_object_relationship + type: bulk args: - table: article - name: author - using: - foreign_key_constraint_on: author_id + - type: create_object_relationship + args: + table: article + name: author + using: + foreign_key_constraint_on: author_id + - type: create_array_relationship + args: + table: author + name: articles + using: + foreign_key_constraint_on: + table: article + column: author_id diff --git a/server/test/testcases/create_tables.yaml b/server/test/testcases/create_tables.yaml index 5847a718f0a68..1a421dd5602ce 100644 --- a/server/test/testcases/create_tables.yaml +++ b/server/test/testcases/create_tables.yaml @@ -14,5 +14,7 @@ query: id SERIAL PRIMARY KEY, title TEXT, content TEXT, - author_id INTEGER REFERENCES author(id) + author_id INTEGER REFERENCES author(id), + is_published BOOLEAN, + published_on TIMESTAMP ) diff --git a/server/test/testcases/delete_mutation_article.yaml b/server/test/testcases/delete_mutation_article.yaml new file mode 100644 index 0000000000000..8ae815e4e7848 --- /dev/null +++ b/server/test/testcases/delete_mutation_article.yaml @@ -0,0 +1,12 @@ +description: Delete mutation on article +url: /v1alpha1/graphql +status: 200 +query: + query: | + mutation delete_article { + delete_article ( + where: {id: {_eq: 5}} + ) { + affected_rows + } + } diff --git a/server/test/testcases/insert_mutation_article.yaml b/server/test/testcases/insert_mutation_article.yaml index 6237c33a0863d..990855292bf7e 100644 --- a/server/test/testcases/insert_mutation_article.yaml +++ b/server/test/testcases/insert_mutation_article.yaml @@ -9,17 +9,32 @@ query: { title: "Article 1", content: "Sample article content", - author_id: 1 + author_id: 1, + is_published: true }, { title: "Article 2", content: "Sample article content", - author_id: 1 + author_id: 1, + is_published: true }, { title: "Article 3", content: "Sample article content", - author_id: 2 + author_id: 2, + is_published: true + }, + { + title: "Article 4", + content: "Sample article content", + author_id: 1, + is_published: false + }, + { + title: "Article 5", + content: "Sample article content", + author_id: 2, + is_published: false } ] ) { diff --git a/server/test/testcases/nested_select_where_query_author.yaml b/server/test/testcases/nested_select_where_query_author.yaml new file mode 100644 index 0000000000000..124244c2c0286 --- /dev/null +++ b/server/test/testcases/nested_select_where_query_author.yaml @@ -0,0 +1,19 @@ +description: Select author and their articles +url: /v1alpha1/graphql +status: 200 +query: + query: | + query { + author (where: {name: {_eq: "Author 1"}}) { + id + name + articles ( + where: {is_published: {_eq: true}} + limit: 10, + offset: 1 + ) { + id + title + } + } + } diff --git a/server/test/testcases/update_mutation_author.yaml b/server/test/testcases/update_mutation_author.yaml new file mode 100644 index 0000000000000..90aa3d8629bee --- /dev/null +++ b/server/test/testcases/update_mutation_author.yaml @@ -0,0 +1,13 @@ +description: Update mutation on author +url: /v1alpha1/graphql +status: 200 +query: + query: | + mutation update_author { + update_author( + where: {id: {_eq: 1}}, + _set: {name: "Jane"} + ) { + affected_rows + } + }