|
| 1 | +module Spec.BotPlutusInterface.Server (tests) where |
| 2 | + |
| 3 | +import BotPlutusInterface.Server (app, initState) |
| 4 | +import BotPlutusInterface.Types ( |
| 5 | + HasDefinitions (..), |
| 6 | + PABConfig (..), |
| 7 | + SomeBuiltin (..), |
| 8 | + ) |
| 9 | + |
| 10 | +import Playground.Types (FunctionSchema) |
| 11 | +import Schema (FormSchema) |
| 12 | + |
| 13 | +import Test.Tasty (TestTree, testGroup) |
| 14 | +import Test.Tasty.HUnit (testCase, (@?=)) |
| 15 | + |
| 16 | +import Network.HTTP.Client (defaultManagerSettings, newManager) |
| 17 | +import Network.HTTP.Types.Status (status404) |
| 18 | +import Network.Wai.Handler.Warp (testWithApplication) |
| 19 | +import Servant.API (Capture, Get, JSON, (:>)) |
| 20 | +import Servant.Client (ClientEnv, ClientError (..), client, mkClientEnv, responseStatusCode, runClientM) |
| 21 | +import Servant.Client.Core.BaseUrl (BaseUrl (..), parseBaseUrl) |
| 22 | + |
| 23 | +import Data.Aeson (FromJSON, ToJSON) |
| 24 | +import Data.Default (def) |
| 25 | +import Data.Proxy (Proxy (..)) |
| 26 | +import Data.Text (Text, pack, unpack) |
| 27 | +import Data.Void (Void, absurd) |
| 28 | +import System.FilePath ((</>)) |
| 29 | +import System.IO.Temp (withSystemTempDirectory) |
| 30 | +import Prelude |
| 31 | + |
| 32 | +type RawTxEndpointResponse = Either ClientError Text |
| 33 | +type RawTxTest a = (Text -> IO RawTxEndpointResponse) -> IO a |
| 34 | + |
| 35 | +tests :: TestTree |
| 36 | +tests = |
| 37 | + testGroup |
| 38 | + "Server" |
| 39 | + [ rawTxTests |
| 40 | + ] |
| 41 | + |
| 42 | +rawTxTests :: TestTree |
| 43 | +rawTxTests = |
| 44 | + testGroup |
| 45 | + "rawTx" |
| 46 | + [ testCase "Can fetch valid tx file" fetchTx |
| 47 | + , testCase "If an extension is supplied, it is replaced by .raw" fetchSignedTx |
| 48 | + , testCase "Unable to fetch outside tx folder" fetchOutsideTxFolder |
| 49 | + , testCase "Returns 404 for valid request when the endpoint is disabled" fetchWithDefaultConfig |
| 50 | + ] |
| 51 | + where |
| 52 | + fetchTx :: IO () |
| 53 | + fetchTx = do |
| 54 | + initServerAndClient enableTxEndpointConfig $ \runRawTxClient -> do |
| 55 | + result <- runRawTxClient txHash |
| 56 | + result @?= Right (pack txFileContents) |
| 57 | + |
| 58 | + fetchSignedTx :: IO () |
| 59 | + fetchSignedTx = do |
| 60 | + initServerAndClient enableTxEndpointConfig $ \runRawTxClient -> do |
| 61 | + result <- runRawTxClient $ txHash <> ".signed" |
| 62 | + result @?= Right (pack txFileContents) |
| 63 | + |
| 64 | + fetchOutsideTxFolder :: IO () |
| 65 | + fetchOutsideTxFolder = do |
| 66 | + initServerAndClient enableTxEndpointConfig $ \runRawTxClient -> do |
| 67 | + Left (FailureResponse _ res) <- runRawTxClient "../somefile" |
| 68 | + responseStatusCode res @?= status404 |
| 69 | + |
| 70 | + fetchWithDefaultConfig :: IO () |
| 71 | + fetchWithDefaultConfig = do |
| 72 | + initServerAndClient def $ \runRawTxClient -> do |
| 73 | + Left (FailureResponse _ res) <- runRawTxClient txHash |
| 74 | + responseStatusCode res @?= status404 |
| 75 | + |
| 76 | +-- Ideally we would reuse the API type definition from BotPlutusInterface.Server but servant-client |
| 77 | +-- can not generate a client for the websocket endpoint. |
| 78 | +txProxy :: |
| 79 | + Proxy |
| 80 | + ( "rawTx" |
| 81 | + :> Capture "hash" Text |
| 82 | + :> Get '[JSON] Text |
| 83 | + ) |
| 84 | +txProxy = Proxy |
| 85 | + |
| 86 | +initServerAndClient :: PABConfig -> RawTxTest a -> IO a |
| 87 | +initServerAndClient config test = do |
| 88 | + withSystemTempDirectory "tx" $ \path -> do |
| 89 | + let pabConfig :: PABConfig |
| 90 | + pabConfig = config {pcTxFileDir = pack path} |
| 91 | + state <- initState |
| 92 | + writeFile (path </> txFileName) txFileContents |
| 93 | + testWithApplication (pure $ app @EmptyContract pabConfig state) (initClientOnPort test) |
| 94 | + where |
| 95 | + initClientOnPort :: RawTxTest a -> Int -> IO a |
| 96 | + initClientOnPort testToRun port = do |
| 97 | + baseUrl <- parseBaseUrl "http://localhost" |
| 98 | + manager <- newManager defaultManagerSettings |
| 99 | + |
| 100 | + let clientEnv :: ClientEnv |
| 101 | + clientEnv = mkClientEnv manager $ baseUrl {baseUrlPort = port} |
| 102 | + |
| 103 | + runRawTxClient :: Text -> IO RawTxEndpointResponse |
| 104 | + runRawTxClient hash = runClientM (client txProxy hash) clientEnv |
| 105 | + |
| 106 | + testToRun runRawTxClient |
| 107 | + |
| 108 | +txHash :: Text |
| 109 | +txHash = "aaaa" |
| 110 | + |
| 111 | +txFileName :: FilePath |
| 112 | +txFileName = "tx-" <> unpack txHash <> ".raw" |
| 113 | + |
| 114 | +txFileContents :: String |
| 115 | +txFileContents = "test" |
| 116 | + |
| 117 | +enableTxEndpointConfig :: PABConfig |
| 118 | +enableTxEndpointConfig = def {pcEnableTxEndpoint = True} |
| 119 | + |
| 120 | +-- Since we are not testing the contract endpoints we just use a newtype around Void as a Contract |
| 121 | +newtype EmptyContract = EmptyContract {unEmptyContract :: Void} |
| 122 | + deriving newtype (FromJSON, ToJSON) |
| 123 | + |
| 124 | +instance HasDefinitions EmptyContract where |
| 125 | + getDefinitions :: [EmptyContract] |
| 126 | + getDefinitions = [] |
| 127 | + |
| 128 | + getSchema :: EmptyContract -> [FunctionSchema FormSchema] |
| 129 | + getSchema = absurd . unEmptyContract |
| 130 | + |
| 131 | + getContract :: (EmptyContract -> SomeBuiltin) |
| 132 | + getContract = absurd . unEmptyContract |
0 commit comments