2022-01-19 07:46:42 +03:00
|
|
|
module Hasura.RQL.WebhookTransformsSpec (spec) where
|
2021-09-16 14:03:01 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.Aeson
|
|
|
|
import Data.CaseInsensitive qualified as CI
|
2021-09-29 11:13:30 +03:00
|
|
|
import Data.List (nubBy)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.Set qualified as S
|
|
|
|
import Hasura.Prelude
|
2022-01-19 07:46:42 +03:00
|
|
|
import Hasura.RQL.DDL.WebhookTransforms
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hedgehog.Gen qualified as Gen
|
|
|
|
import Hedgehog.Range qualified as Range
|
|
|
|
import Test.Hspec
|
|
|
|
import Test.Hspec.Hedgehog
|
2021-09-16 14:03:01 +03:00
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = do
|
|
|
|
it "RequstMethod RoundTrip" $
|
|
|
|
hedgehog $ forAll genRequestMethod >>= trippingJSON
|
|
|
|
|
|
|
|
it "TemplateEngine RoundTrip" $
|
|
|
|
hedgehog $ forAll genTemplatingEngine >>= trippingJSON
|
|
|
|
|
|
|
|
it "TemplateText RoundTrip" $
|
|
|
|
hedgehog $ forAll genTemplateText >>= trippingJSON
|
|
|
|
|
|
|
|
it "ContentType RoundTrip" $
|
|
|
|
hedgehog $ forAll genContentType >>= trippingJSON
|
|
|
|
|
|
|
|
it "TransformHeaders" $
|
|
|
|
hedgehog $ do
|
|
|
|
headers <- forAll genTransformHeaders
|
2021-09-24 01:56:37 +03:00
|
|
|
let sortH TransformHeaders {..} = TransformHeaders (sort addHeaders) (sort removeHeaders)
|
2022-01-19 07:46:42 +03:00
|
|
|
headersMaybe = eitherDecode $ encode headers
|
|
|
|
Right (sortH headers) === fmap sortH headersMaybe
|
2021-09-16 14:03:01 +03:00
|
|
|
|
2022-01-19 07:46:42 +03:00
|
|
|
it "MetadataRequestTransform RoundTrip" $
|
2021-09-16 14:03:01 +03:00
|
|
|
hedgehog $ do
|
2022-01-19 07:46:42 +03:00
|
|
|
transform <- forAll genMetadataRequestTransform
|
2021-09-24 01:56:37 +03:00
|
|
|
let sortH TransformHeaders {..} = TransformHeaders (sort addHeaders) (sort removeHeaders)
|
2022-01-19 07:46:42 +03:00
|
|
|
sortMT mt@MetadataRequestTransform {mtRequestHeaders, mtQueryParams} = mt {mtRequestHeaders = sortH <$> mtRequestHeaders, mtQueryParams = sort <$> mtQueryParams}
|
|
|
|
transformMaybe = eitherDecode $ encode transform
|
|
|
|
Right (sortMT transform) === fmap sortMT transformMaybe
|
2021-09-16 14:03:01 +03:00
|
|
|
|
|
|
|
trippingJSON :: (Show a, Eq a, ToJSON a, FromJSON a, MonadTest m) => a -> m ()
|
|
|
|
trippingJSON val = tripping val (toJSON) (fromJSON)
|
|
|
|
|
|
|
|
genRequestMethod :: Gen RequestMethod
|
|
|
|
genRequestMethod = Gen.enumBounded @_ @RequestMethod
|
|
|
|
|
|
|
|
genTemplatingEngine :: Gen TemplatingEngine
|
|
|
|
genTemplatingEngine = Gen.enumBounded @_ @TemplatingEngine
|
|
|
|
|
|
|
|
-- NOTE: This generator is strictly useful for roundtrip aeson testing
|
|
|
|
-- and does not produce valid template snippets.
|
|
|
|
genTemplateText :: Gen TemplateText
|
2021-10-21 16:31:45 +03:00
|
|
|
genTemplateText = TemplateText . wrap <$> Gen.text (Range.constant 3 20) Gen.alphaNum
|
2021-09-29 11:13:30 +03:00
|
|
|
where
|
|
|
|
wrap txt = "\"" <> txt <> "\""
|
2021-09-16 14:03:01 +03:00
|
|
|
|
2022-01-19 07:46:42 +03:00
|
|
|
genStringTemplateText :: Gen StringTemplateText
|
|
|
|
genStringTemplateText = StringTemplateText <$> Gen.text (Range.constant 3 20) Gen.alphaNum
|
|
|
|
|
2021-09-16 14:03:01 +03:00
|
|
|
genContentType :: Gen ContentType
|
|
|
|
genContentType = Gen.enumBounded @_ @ContentType
|
|
|
|
|
|
|
|
genTransformHeaders :: Gen TransformHeaders
|
|
|
|
genTransformHeaders = do
|
|
|
|
numHeaders <- Gen.integral $ Range.constant 1 20
|
|
|
|
|
|
|
|
let genHeaderKey = CI.mk <$> Gen.text (Range.constant 1 20) Gen.alphaNum
|
2022-01-19 07:46:42 +03:00
|
|
|
genHeaderValue = genStringTemplateText
|
2021-09-16 14:03:01 +03:00
|
|
|
|
|
|
|
genKeys = S.toList <$> Gen.set (Range.singleton numHeaders) genHeaderKey
|
|
|
|
genValues = S.toList <$> Gen.set (Range.singleton numHeaders) genHeaderValue
|
|
|
|
|
|
|
|
removeHeaders <- Gen.list (Range.constant 1 10) genHeaderKey
|
|
|
|
addHeaders <- liftA2 zip genKeys genValues
|
|
|
|
pure $ TransformHeaders addHeaders removeHeaders
|
|
|
|
|
2021-10-21 16:31:45 +03:00
|
|
|
genQueryParams :: Gen [(StringTemplateText, Maybe StringTemplateText)]
|
2021-09-16 14:03:01 +03:00
|
|
|
genQueryParams = do
|
|
|
|
numParams <- Gen.integral $ Range.constant 1 20
|
2022-01-19 07:46:42 +03:00
|
|
|
let keyGen = genStringTemplateText
|
|
|
|
valueGen = Gen.maybe $ genStringTemplateText
|
2021-09-16 14:03:01 +03:00
|
|
|
keys <- Gen.list (Range.singleton numParams) keyGen
|
|
|
|
values <- Gen.list (Range.singleton numParams) valueGen
|
2021-09-29 11:13:30 +03:00
|
|
|
pure $ nubBy (\a b -> fst a == fst b) $ zip keys values
|
|
|
|
|
2021-10-21 16:31:45 +03:00
|
|
|
genUrl :: Gen StringTemplateText
|
2021-09-29 11:13:30 +03:00
|
|
|
genUrl = do
|
|
|
|
host <- Gen.text (Range.constant 3 20) Gen.alphaNum
|
|
|
|
|
2021-10-21 16:31:45 +03:00
|
|
|
pure $ StringTemplateText $ "http://www." <> host <> ".com"
|
2021-09-16 14:03:01 +03:00
|
|
|
|
2022-01-19 07:46:42 +03:00
|
|
|
genMetadataRequestTransform :: Gen MetadataRequestTransform
|
|
|
|
genMetadataRequestTransform = do
|
2021-09-16 14:03:01 +03:00
|
|
|
method <- Gen.maybe genRequestMethod
|
|
|
|
-- NOTE: At the moment no need to generate valid urls or templates
|
|
|
|
-- but such instances maybe useful in the future.
|
2021-09-29 11:13:30 +03:00
|
|
|
url <- Gen.maybe $ genUrl
|
2021-09-16 14:03:01 +03:00
|
|
|
bodyTransform <- Gen.maybe $ genTemplateText
|
|
|
|
contentType <- Gen.maybe $ genContentType
|
|
|
|
queryParams <- Gen.maybe $ genQueryParams
|
|
|
|
reqHeaders <- Gen.maybe $ genTransformHeaders
|
2022-01-19 07:46:42 +03:00
|
|
|
MetadataRequestTransform
|
2021-09-24 01:56:37 +03:00
|
|
|
method
|
|
|
|
url
|
|
|
|
bodyTransform
|
|
|
|
contentType
|
|
|
|
queryParams
|
|
|
|
reqHeaders
|
2021-09-16 14:03:01 +03:00
|
|
|
<$> genTemplatingEngine
|