|
| 1 | +{-# LANGUAGE AllowAmbiguousTypes #-} |
| 2 | +{-# LANGUAGE ExistentialQuantification #-} |
| 3 | +{-# LANGUAGE FlexibleContexts #-} |
| 4 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 5 | +{-# LANGUAGE TupleSections #-} |
| 6 | +{-# LANGUAGE TypeApplications #-} |
| 7 | +module ObsHelper where |
| 8 | + |
| 9 | +import qualified Data.ByteString.Base64 as B64 (decodeLenient) |
| 10 | +import qualified Data.ByteString.Char8 as BS |
| 11 | +import qualified Data.ByteString.Lazy as BL |
| 12 | +import qualified Jose.Jwa as JWT |
| 13 | +import qualified Jose.Jws as JWT |
| 14 | +import qualified Jose.Jwt as JWT |
| 15 | + |
| 16 | +import PostgREST.Config (AppConfig (..), |
| 17 | + JSPathExp (..), |
| 18 | + LogLevel (..), |
| 19 | + OpenAPIMode (..), |
| 20 | + Verbosity (..), parseSecret) |
| 21 | +import PostgREST.SchemaCache.Identifiers (QualifiedIdentifier (..)) |
| 22 | + |
| 23 | +import Data.List.NonEmpty (fromList) |
| 24 | +import Data.String (String) |
| 25 | +import Network.HTTP.Types |
| 26 | +import Prometheus (Counter, getCounter) |
| 27 | +import Protolude hiding (get, toS) |
| 28 | +import Test.Hspec |
| 29 | +import Test.Hspec.Expectations.Contrib (annotate) |
| 30 | +import Test.Hspec.Wai |
| 31 | + |
| 32 | + |
| 33 | +baseCfg :: AppConfig |
| 34 | +baseCfg = let secret = encodeUtf8 "reallyreallyreallyreallyverysafe" in |
| 35 | + AppConfig { |
| 36 | + configAppSettings = [ ("app.settings.app_host", "localhost") , ("app.settings.external_api_secret", "0123456789abcdef") ] |
| 37 | + , configClientErrorVerbosity = Verbose |
| 38 | + , configDbAggregates = False |
| 39 | + , configDbAnonRole = Just "postgrest_test_anonymous" |
| 40 | + , configDbChannel = mempty |
| 41 | + , configDbChannelEnabled = True |
| 42 | + , configDbExtraSearchPath = [] |
| 43 | + , configDbHoistedTxSettings = ["default_transaction_isolation","plan_filter.statement_cost_limit","statement_timeout"] |
| 44 | + , configDbMaxRows = Nothing |
| 45 | + , configDbPlanEnabled = False |
| 46 | + , configDbPoolSize = 10 |
| 47 | + , configDbPoolAcquisitionTimeout = 10 |
| 48 | + , configDbPoolMaxLifetime = 1800 |
| 49 | + , configDbPoolMaxIdletime = 600 |
| 50 | + , configDbPoolAutomaticRecovery = True |
| 51 | + , configDbPreRequest = Just $ QualifiedIdentifier "test" "switch_role" |
| 52 | + , configDbPreparedStatements = True |
| 53 | + , configDbRootSpec = Nothing |
| 54 | + , configDbSchemas = fromList ["test"] |
| 55 | + , configDbConfig = False |
| 56 | + , configDbPreConfig = Nothing |
| 57 | + , configDbUri = "postgresql://" |
| 58 | + , configFilePath = Nothing |
| 59 | + , configJWKS = rightToMaybe $ parseSecret secret |
| 60 | + , configJwtAudience = Nothing |
| 61 | + , configJwtRoleClaimKey = [JSPKey "role"] |
| 62 | + , configJwtSecret = Just secret |
| 63 | + , configJwtSecretIsBase64 = False |
| 64 | + , configJwtCacheMaxEntries = 10 |
| 65 | + , configLogLevel = LogCrit |
| 66 | + , configLogQuery = False |
| 67 | + , configOpenApiMode = OAFollowPriv |
| 68 | + , configOpenApiSecurityActive = False |
| 69 | + , configOpenApiServerProxyUri = Nothing |
| 70 | + , configServerCorsAllowedOrigins = Nothing |
| 71 | + , configServerHost = "localhost" |
| 72 | + , configServerPort = 3000 |
| 73 | + , configServerTraceHeader = Nothing |
| 74 | + , configServerUnixSocket = Nothing |
| 75 | + , configServerUnixSocketMode = 432 |
| 76 | + , configDbTxAllowOverride = True |
| 77 | + , configDbTxRollbackAll = True |
| 78 | + , configAdminServerHost = "localhost" |
| 79 | + , configAdminServerPort = Nothing |
| 80 | + , configRoleSettings = mempty |
| 81 | + , configRoleIsoLvl = mempty |
| 82 | + , configInternalSCQuerySleep = Nothing |
| 83 | + , configInternalSCLoadSleep = Nothing |
| 84 | + , configInternalSCRelLoadSleep = Nothing |
| 85 | + , configServerTimingEnabled = True |
| 86 | + } |
| 87 | + |
| 88 | +testCfg :: AppConfig |
| 89 | +testCfg = baseCfg |
| 90 | + |
| 91 | +testCfgJwtCache :: AppConfig |
| 92 | +testCfgJwtCache = |
| 93 | + baseCfg { |
| 94 | + configJwtSecret = Just generateSecret |
| 95 | + , configJWKS = rightToMaybe $ parseSecret generateSecret |
| 96 | + , configJwtCacheMaxEntries = 2 |
| 97 | + } |
| 98 | + |
| 99 | +authHeader :: BS.ByteString -> BS.ByteString -> Header |
| 100 | +authHeader typ creds = |
| 101 | + (hAuthorization, typ <> " " <> creds) |
| 102 | + |
| 103 | +authHeaderJWT :: BS.ByteString -> Header |
| 104 | +authHeaderJWT = authHeader "Bearer" |
| 105 | + |
| 106 | +generateSecret :: ByteString |
| 107 | +generateSecret = B64.decodeLenient "cmVhbGx5cmVhbGx5cmVhbGx5cmVhbGx5dmVyeXNhZmU=" |
| 108 | + |
| 109 | +generateJWT :: BL.ByteString -> ByteString |
| 110 | +generateJWT claims = |
| 111 | + either mempty JWT.unJwt $ JWT.hmacEncode JWT.HS256 generateSecret (BL.toStrict claims) |
| 112 | + |
| 113 | +-- state check helpers |
| 114 | + |
| 115 | +data StateCheck st m = forall a. StateCheck (st -> (String, m a)) (a -> a -> Expectation) |
| 116 | + |
| 117 | +stateCheck :: (Show a, Eq a) => (c -> m a) -> (st -> (String, c)) -> (a -> a) -> StateCheck st m |
| 118 | +stateCheck extractValue extractComponent expect = StateCheck (second extractValue . extractComponent) (flip shouldBe . expect) |
| 119 | + |
| 120 | +expectField :: forall s st a c m. (KnownSymbol s, Show a, Eq a, HasField s st c) => (c -> m a) -> (a -> a) -> StateCheck st m |
| 121 | +expectField extractValue = stateCheck extractValue ((symbolVal (Proxy @s),) . getField @s) |
| 122 | + |
| 123 | +checkState :: (Traversable t) => t (StateCheck st (WaiSession st)) -> WaiSession st b -> WaiSession st () |
| 124 | +checkState checks act = getState >>= flip (`checkState'` checks) act |
| 125 | + |
| 126 | +checkState' :: (Traversable t, MonadIO m) => st -> t (StateCheck st m) -> m b -> m () |
| 127 | +checkState' initialState checks act = do |
| 128 | + expectations <- traverse (\(StateCheck g expect) -> let (msg, m) = g initialState in m >>= createExpectation msg m . expect) checks |
| 129 | + void act |
| 130 | + sequenceA_ expectations |
| 131 | + where |
| 132 | + createExpectation msg metrics expect = pure $ metrics >>= liftIO . annotate msg . expect |
| 133 | + |
| 134 | +expectCounter :: forall s st m. (KnownSymbol s, HasField s st Counter, MonadIO m) => (Int -> Int) -> StateCheck st m |
| 135 | +expectCounter = expectField @s intCounter |
| 136 | + where |
| 137 | + intCounter = ((round @Double @Int) <$>) . getCounter |
0 commit comments