Skip to content

Commit

Permalink
SCP-5025 implement tests for GET /contract/{contractId}
Browse files Browse the repository at this point in the history
  • Loading branch information
ladamesny committed Mar 17, 2023
1 parent 6f690da commit 61ab1e6
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions marlowe-integration-tests/marlowe-integration-tests.cabal
Expand Up @@ -66,6 +66,7 @@ executable marlowe-integration-tests
Language.Marlowe.Runtime.WebSpec
Language.Marlowe.Runtime.Web.Common
Language.Marlowe.Runtime.Web.GetContracts
Language.Marlowe.Runtime.Web.GetContract
Language.Marlowe.Runtime.CliSpec
build-depends:
base >= 4.9 && < 5
Expand Down
@@ -0,0 +1,88 @@
module Language.Marlowe.Runtime.Web.GetContract
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 (getContract)
import Language.Marlowe.Runtime.Web.Common (createCloseContract, waitUntilConfirmed)
import Language.Marlowe.Runtime.Web.Server.DTO (ToDTO(toDTO))
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}" do
getContractValidSpec
getContractInvalidSpec

getContractValidSpec :: Spec
getContractValidSpec = describe "Valid GET /contract" do
getsFirstContractValidSpec
getsSecondContractValidSpec
getsThirdContractValidSpec

getContractInvalidSpec :: Spec
getContractInvalidSpec = describe "Invalid GET /contract" do
invalidTxIdSpec

getsFirstContractValidSpec :: Spec
getsFirstContractValidSpec = it "returns the first contract" $ withLocalMarloweRuntime $ runIntegrationTest do
wallet1 <- getGenesisWallet 0
wallet2 <- getGenesisWallet 1
wallet3 <- getGenesisWallet 2

either throw pure =<< runWebClient do
expectedContractId1 <- createCloseContract wallet1
_ <- createCloseContract wallet2
_ <- createCloseContract wallet3

Web.ContractState{..}<- waitUntilConfirmed (\Web.ContractState{status} -> status) $ getContract expectedContractId1

liftIO $ contractId `shouldBe` expectedContractId1

getsSecondContractValidSpec :: Spec
getsSecondContractValidSpec = it "returns the second contract" $ withLocalMarloweRuntime $ runIntegrationTest do
wallet1 <- getGenesisWallet 0
wallet2 <- getGenesisWallet 1
wallet3 <- getGenesisWallet 2

either throw pure =<< runWebClient do
_ <- createCloseContract wallet1
expectedContractId2 <- createCloseContract wallet2
_ <- createCloseContract wallet3

Web.ContractState{..}<- waitUntilConfirmed (\Web.ContractState{status} -> status) $ getContract expectedContractId2

liftIO $ contractId `shouldBe` expectedContractId2

getsThirdContractValidSpec :: Spec
getsThirdContractValidSpec = it "returns the third contract" $ withLocalMarloweRuntime $ runIntegrationTest do
wallet1 <- getGenesisWallet 0
wallet2 <- getGenesisWallet 1
wallet3 <- getGenesisWallet 2

either throw pure =<< runWebClient do
_ <- createCloseContract wallet1
_ <- createCloseContract wallet2
expectedContractId3 <- createCloseContract wallet3

Web.ContractState{..}<- waitUntilConfirmed (\Web.ContractState{status} -> status) $ getContract expectedContractId3

liftIO $ contractId `shouldBe` expectedContractId3

invalidTxIdSpec :: Spec
invalidTxIdSpec = it "returns not found for invalid contract id" $ withLocalMarloweRuntime $ runIntegrationTest do
result <- runWebClient do
let
invalidTxId = Web.TxOutRef (toDTO @Chain.TxId "0000000000000000000000000000000000000000000000000000000000000000") 1
getContract invalidTxId

case result of
Left (FailureResponse _ Response { responseStatusCode = Status { statusCode = 404 } } ) -> pure ()
_ -> fail $ "Expected 404 response code - got " <> show result
@@ -1,9 +1,11 @@
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 Test.Hspec (Spec, describe)

spec :: Spec
spec = describe "Marlowe runtime Web API" do
GetContracts.spec
GetContract.spec

0 comments on commit 61ab1e6

Please sign in to comment.