Skip to content

Commit 45df2cc

Browse files
Remove unnecessary checks
Add url encoding tests
1 parent 6ba0726 commit 45df2cc

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

bot-plutus-interface.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ library
118118
, plutus-tx
119119
, plutus-tx-plugin
120120
, process
121+
, QuickCheck
121122
, row-types
122123
, serialise
123124
, servant
@@ -186,6 +187,7 @@ test-suite bot-plutus-interface-test
186187
, quickcheck-instances
187188
, row-types
188189
, serialise
190+
, servant
189191
, servant-client
190192
, servant-client-core
191193
, stm

src/BotPlutusInterface/Server.hs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import Data.Map qualified as Map
3434
import Data.Maybe (catMaybes)
3535
import Data.Proxy (Proxy (Proxy))
3636
import Data.Row (Row)
37+
import Data.String (fromString)
3738
import Data.Text (Text, pack, unpack)
3839
import Data.Text.Encoding (encodeUtf8)
3940
import Data.UUID.V4 qualified as UUID
@@ -75,8 +76,9 @@ import Servant.API (
7576
)
7677
import Servant.API.WebSocket (WebSocketPending)
7778
import Servant.Server (Application, Handler, Server, err404, serve)
78-
import System.Directory (canonicalizePath, doesFileExist, makeAbsolute)
79-
import System.FilePath (takeDirectory, (</>))
79+
import System.Directory (doesFileExist, makeAbsolute)
80+
import System.FilePath ((</>))
81+
import Test.QuickCheck (Arbitrary (arbitrary), elements, vectorOf)
8082
import Wallet.Types (ContractInstanceId (..))
8183
import Prelude
8284

@@ -105,6 +107,7 @@ type RawTxEndpoint =
105107
:> Get '[JSON] RawTx
106108

107109
newtype TxIdCapture = TxIdCapture TxId
110+
deriving newtype (Eq, Show)
108111

109112
instance FromHttpApiData TxIdCapture where
110113
parseUrlPiece :: Text -> Either Text TxIdCapture
@@ -121,6 +124,9 @@ instance FromHttpApiData TxIdCapture where
121124
instance ToHttpApiData TxIdCapture where
122125
toUrlPiece (TxIdCapture txId) = txIdToText txId
123126

127+
instance Arbitrary TxIdCapture where
128+
arbitrary = TxIdCapture . fromString <$> vectorOf 64 (elements "0123456789abcdefABCDEF")
129+
124130
server :: HasDefinitions t => PABConfig -> AppState -> Server (API t)
125131
server pabConfig state =
126132
websocketHandler state
@@ -274,13 +280,10 @@ rawTxHandler config (TxIdCapture txId) = do
274280
-- Absolute path to pcTxFileDir that is specified in the config
275281
txFolderPath <- liftIO $ makeAbsolute (unpack config.pcTxFileDir)
276282

277-
-- Add/Set .raw extension on path
278-
let suppliedPath :: FilePath
279-
suppliedPath = txFolderPath </> unpack (txFileName txId "raw")
280-
-- Resolve path indirections
281-
path <- liftIO $ canonicalizePath suppliedPath
282-
-- ensure it does not try to escape txFolderPath
283-
assert (takeDirectory path == txFolderPath)
283+
-- Create full path
284+
let path :: FilePath
285+
path = txFolderPath </> unpack (txFileName txId "raw")
286+
284287
-- ensure file exists
285288
fileExists <- liftIO $ doesFileExist path
286289
assert fileExists

test/Spec/BotPlutusInterface/Server.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ import Schema (FormSchema)
1515

1616
import Test.Tasty (TestTree, testGroup)
1717
import Test.Tasty.HUnit (testCase, (@?=))
18+
import Test.Tasty.QuickCheck (Property, testProperty, (===))
1819

1920
import Network.HTTP.Client (defaultManagerSettings, newManager)
2021
import Network.HTTP.Types.Status (status404)
2122
import Network.Wai.Handler.Warp (testWithApplication)
23+
import Servant.API (
24+
FromHttpApiData (parseUrlPiece),
25+
ToHttpApiData (toUrlPiece),
26+
)
2227
import Servant.Client (ClientEnv, ClientError (..), client, mkClientEnv, responseStatusCode, runClientM)
2328
import Servant.Client.Core.BaseUrl (BaseUrl (..), parseBaseUrl)
2429

@@ -49,6 +54,7 @@ rawTxTests =
4954
[ testCase "Can fetch valid tx file" fetchTx
5055
, testCase "Returns 404 for missing txs" fetchMissingTx
5156
, testCase "Returns 404 for valid request when the endpoint is disabled" fetchWithDefaultConfig
57+
, testProperty "TxId URL encoding reversible" txIdReversible
5258
]
5359
where
5460
fetchTx :: IO ()
@@ -69,6 +75,9 @@ rawTxTests =
6975
Left (FailureResponse _ res) <- runRawTxClient txHash
7076
responseStatusCode res @?= status404
7177

78+
txIdReversible :: TxIdCapture -> Property
79+
txIdReversible txId = parseUrlPiece (toUrlPiece txId) === Right txId
80+
7281
txProxy :: Proxy RawTxEndpoint
7382
txProxy = Proxy
7483

0 commit comments

Comments
 (0)