From 0b30e73d8e33f619bcfad464e35179daae67884b Mon Sep 17 00:00:00 2001 From: paolino Date: Fri, 6 Sep 2024 14:16:06 +0000 Subject: [PATCH] Test deposit REST module --- .../customer-deposit-wallet.cabal | 1 + .../unit/Cardano/Wallet/Deposit/RESTSpec.hs | 128 ++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/RESTSpec.hs diff --git a/lib/customer-deposit-wallet/customer-deposit-wallet.cabal b/lib/customer-deposit-wallet/customer-deposit-wallet.cabal index 725852b2a67..e0f2ddf1380 100644 --- a/lib/customer-deposit-wallet/customer-deposit-wallet.cabal +++ b/lib/customer-deposit-wallet/customer-deposit-wallet.cabal @@ -171,6 +171,7 @@ test-suite unit other-modules: Cardano.Wallet.Deposit.HTTP.JSON.JSONSpec Cardano.Wallet.Deposit.HTTP.OpenAPISpec + Cardano.Wallet.Deposit.RESTSpec Paths_customer_deposit_wallet Spec diff --git a/lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/RESTSpec.hs b/lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/RESTSpec.hs new file mode 100644 index 00000000000..4ac02d25300 --- /dev/null +++ b/lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/RESTSpec.hs @@ -0,0 +1,128 @@ +module Cardano.Wallet.Deposit.RESTSpec + ( spec + ) +where + +import Prelude + +import Cardano.Crypto.Wallet + ( XPub + , generate + , toXPub + ) +import Cardano.Wallet.Deposit.IO + ( WalletBootEnv (WalletBootEnv) + ) +import Cardano.Wallet.Deposit.IO.Resource + ( ErrResourceExists (ErrAlreadyFailedToInitialize) + , withResource + ) +import Cardano.Wallet.Deposit.REST + ( ErrCreatingDatabase (ErrDatabaseAlreadyExists) + , ErrDatabase (ErrCreatingDatabase) + , ErrWalletResource (..) + , WalletResourceM + , availableBalance + , initXPubWallet + , loadWallet + , runWalletResourceM + ) +import Control.Concurrent + ( threadDelay + ) +import Control.Monad.IO.Class + ( MonadIO (..) + ) +import Data.List + ( isPrefixOf + ) +import System.IO.Temp + ( withSystemTempDirectory + ) +import Test.Hspec + ( Spec + , describe + , it + , shouldBe + ) + +import qualified Cardano.Wallet.Deposit.Read as Read +import qualified Data.ByteString.Char8 as B8 + +fakeBootEnv :: WalletBootEnv IO +fakeBootEnv = WalletBootEnv undefined undefined undefined + +xpub :: XPub +xpub = + toXPub + $ generate (B8.pack "random seed for a testing xpub lala") B8.empty + +waitAndGetBalance :: WalletResourceM Read.Value +waitAndGetBalance = do + liftIO $ threadDelay 100000 + availableBalance + +spec :: Spec +spec = do + describe "REST Deposit interface" $ do + it "can initialize a wallet" + $ withSystemTempDirectory "deposit-rest" + $ \dir -> do + r <- + withResource + $ runWalletResourceM + $ do + initXPubWallet fakeBootEnv dir xpub 0 + waitAndGetBalance + case r of + Left e -> fail $ show e + Right cs -> cs `shouldBe` mempty + it "can load an existing wallet" + $ withSystemTempDirectory "deposit-rest" + $ \dir -> do + r <- + withResource + $ runWalletResourceM + $ do + initXPubWallet fakeBootEnv dir xpub 0 + waitAndGetBalance + case r of + Left e -> fail $ show e + Right _ -> do + r' <- + withResource + $ runWalletResourceM + $ do + loadWallet fakeBootEnv dir + waitAndGetBalance + case r' of + Left e -> fail $ show e + Right cs -> cs `shouldBe` mempty + it "cannot re-initialize a wallet" + $ withSystemTempDirectory "deposit-rest" + $ \dir -> do + r <- + withResource + $ runWalletResourceM + $ initXPubWallet fakeBootEnv dir xpub 0 + case r of + Left e -> fail $ show e + Right _ -> do + r' <- + withResource + $ runWalletResourceM + $ do + initXPubWallet fakeBootEnv dir xpub 0 + waitAndGetBalance + case r' of + Left + ( ErrWalletPresent + ( ErrAlreadyFailedToInitialize + ( ErrCreatingDatabase + (ErrDatabaseAlreadyExists fp) + ) + ) + ) + | dir `isPrefixOf` fp -> pure () + Left e -> fail $ show e + Right _ -> fail "Should have failed"