From 6fe34eb3af9f27f6297d8daf9629fae991c1deb5 Mon Sep 17 00:00:00 2001 From: Larry Adames Date: Sat, 25 Mar 2023 11:04:22 -0400 Subject: [PATCH] PLT-4186 - implement e2e tests for get transaction --- .../marlowe-integration-tests.cabal | 1 + .../Marlowe/Runtime/Web/GetTransaction.hs | 95 +++++++++++++++++++ .../test/Language/Marlowe/Runtime/WebSpec.hs | 3 + 3 files changed, 99 insertions(+) create mode 100644 marlowe-integration-tests/test/Language/Marlowe/Runtime/Web/GetTransaction.hs diff --git a/marlowe-integration-tests/marlowe-integration-tests.cabal b/marlowe-integration-tests/marlowe-integration-tests.cabal index 62b2e7c0d0..c3e5e9b5ae 100644 --- a/marlowe-integration-tests/marlowe-integration-tests.cabal +++ b/marlowe-integration-tests/marlowe-integration-tests.cabal @@ -70,6 +70,7 @@ executable marlowe-integration-tests Language.Marlowe.Runtime.Web.GetContracts Language.Marlowe.Runtime.Web.GetContract Language.Marlowe.Runtime.Web.GetTransactions + Language.Marlowe.Runtime.Web.GetTransaction Language.Marlowe.Runtime.CliSpec build-depends: base >= 4.9 && < 5 diff --git a/marlowe-integration-tests/test/Language/Marlowe/Runtime/Web/GetTransaction.hs b/marlowe-integration-tests/test/Language/Marlowe/Runtime/Web/GetTransaction.hs new file mode 100644 index 0000000000..1128550e7e --- /dev/null +++ b/marlowe-integration-tests/test/Language/Marlowe/Runtime/Web/GetTransaction.hs @@ -0,0 +1,95 @@ +{-# OPTIONS_GHC -Wno-unused-imports #-} +module Language.Marlowe.Runtime.Web.GetTransaction + where + +import Control.Monad.IO.Class (MonadIO(liftIO)) + +import Control.Exception (throw) +import qualified Language.Marlowe.Runtime.ChainSync.Api as Chain +import Language.Marlowe.Runtime.Integration.Common (getGenesisWallet, runIntegrationTest, runWebClient) +import qualified Language.Marlowe.Runtime.Web as Web +import Language.Marlowe.Runtime.Web.Client (getTransaction) +import Language.Marlowe.Runtime.Web.Common (createCloseContract, waitUntilConfirmed) +import Language.Marlowe.Runtime.Web.Server.DTO (ToDTO(toDTO)) +import Language.Marlowe.Runtime.Web.StandardContract (createFullyExecutedStandardContract) +import Network.HTTP.Types (Status(..)) +import Servant.Client (ClientError(FailureResponse)) +import Servant.Client.Streaming (ResponseF(Response, responseStatusCode)) +import Test.Hspec (Spec, describe, it, shouldBe) +import Test.Integration.Marlowe.Local (withLocalMarloweRuntime) + +spec :: Spec +spec = describe "GET /contract/{contractId}/transactions/{transactionId}" do + getTransactionValidSpec + -- getTransactionInvalidSpec + +getTransactionValidSpec :: Spec +getTransactionValidSpec = describe "Valid GET /contract/{contractId}/transactions/{transactionId}" do + getsFirstTransactionValidSpec + getsSecondTransactionValidSpec + getsThirdTransactionValidSpec + +-- getTransactionInvalidSpec :: Spec +-- getTransactionInvalidSpec = describe "Invalid GET /contract/{contractId}/transactions/{transactionId}" do + -- invalidTxIdSpec + +getsFirstTransactionValidSpec :: Spec +getsFirstTransactionValidSpec = it "returns the first transaction" $ withLocalMarloweRuntime $ runIntegrationTest do + wallet1 <- getGenesisWallet 0 + wallet2 <- getGenesisWallet 1 + + either throw pure =<< runWebClient do + (contractId, transactionIds) <- createFullyExecutedStandardContract wallet1 wallet2 + case transactionIds of + (txId1:_) -> do + Web.Tx{transactionId = actualTransactionId } <- getTransaction contractId txId1 + + liftIO $ actualTransactionId `shouldBe` txId1 + + _ -> do + liftIO $ fail "Expected at least two transactions" + +getsSecondTransactionValidSpec :: Spec +getsSecondTransactionValidSpec = it "returns the second transaction" $ withLocalMarloweRuntime $ runIntegrationTest do + wallet1 <- getGenesisWallet 0 + wallet2 <- getGenesisWallet 1 + + either throw pure =<< runWebClient do + (contractId, transactionIds) <- createFullyExecutedStandardContract wallet1 wallet2 + case transactionIds of + (_:txId2:_) -> do + Web.Tx{transactionId = actualTransactionId } <- getTransaction contractId txId2 + + liftIO $ actualTransactionId `shouldBe` txId2 + + _ -> do + liftIO $ fail "Expected at least two transactions" + + +getsThirdTransactionValidSpec :: Spec +getsThirdTransactionValidSpec = it "returns the third transaction" $ withLocalMarloweRuntime $ runIntegrationTest do + wallet1 <- getGenesisWallet 0 + wallet2 <- getGenesisWallet 1 + + either throw pure =<< runWebClient do + (contractId, transactionIds) <- createFullyExecutedStandardContract wallet1 wallet2 + case transactionIds of + (_:_:txId3:_) -> do + Web.Tx{transactionId = actualTransactionId } <- getTransaction contractId txId3 + + liftIO $ actualTransactionId `shouldBe` txId3 + + _ -> do + liftIO $ fail "Expected at least three transactions" + + +-- invalidTxIdSpec :: Spec +-- invalidTxIdSpec = it "returns not found for invalid transaction id" $ withLocalMarloweRuntime $ runIntegrationTest do +-- result <- runWebClient do +-- let +-- invalidTxId = Web.TxOutRef (toDTO @Chain.TxId "0000000000000000000000000000000000000000000000000000000000000000") 1 +-- getTransaction invalidTxId + +-- case result of +-- Left (FailureResponse _ Response { responseStatusCode = Status { statusCode = 404 } } ) -> pure () +-- _ -> fail $ "Expected 404 response code - got " <> show result diff --git a/marlowe-integration-tests/test/Language/Marlowe/Runtime/WebSpec.hs b/marlowe-integration-tests/test/Language/Marlowe/Runtime/WebSpec.hs index 44366322d1..f09e9d0ae6 100644 --- a/marlowe-integration-tests/test/Language/Marlowe/Runtime/WebSpec.hs +++ b/marlowe-integration-tests/test/Language/Marlowe/Runtime/WebSpec.hs @@ -1,8 +1,10 @@ +{-# OPTIONS_GHC -Wno-unused-imports #-} module Language.Marlowe.Runtime.WebSpec where import qualified Language.Marlowe.Runtime.Web.GetContract as GetContract import qualified Language.Marlowe.Runtime.Web.GetContracts as GetContracts +import qualified Language.Marlowe.Runtime.Web.GetTransactions as GetTransaction import qualified Language.Marlowe.Runtime.Web.GetTransactions as GetTransactions import Test.Hspec (Spec, describe) @@ -11,3 +13,4 @@ spec = describe "Marlowe runtime Web API" do GetContracts.spec GetContract.spec GetTransactions.spec + GetTransaction.spec