-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/RESTSpec.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
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 | ||
( ErrResourceMissing (..) | ||
, withResource | ||
) | ||
import Cardano.Wallet.Deposit.REST | ||
( ErrCreatingDatabase (..) | ||
, ErrDatabase (..) | ||
, ErrLoadingDatabase (..) | ||
, ErrWalletResource (..) | ||
, WalletResourceM | ||
, availableBalance | ||
, initXPubWallet | ||
, loadWallet | ||
, runWalletResourceM | ||
, walletExists | ||
) | ||
import Control.Concurrent | ||
( threadDelay | ||
) | ||
import Control.Monad.IO.Class | ||
( MonadIO (..) | ||
) | ||
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 | ||
|
||
letItInitialize :: WalletResourceM () | ||
letItInitialize = liftIO $ threadDelay 100000 | ||
|
||
onSuccess :: (Show e, MonadFail m) => Either e a -> (a -> m b) -> m b | ||
onSuccess (Left e) _ = fail $ show e | ||
onSuccess (Right a) f = f a | ||
|
||
matchEmptyValue :: Show e => Either e Read.Value -> IO () | ||
matchEmptyValue x = onSuccess x $ \v -> v `shouldBe` mempty | ||
|
||
withWallet :: WalletResourceM a -> IO (Either ErrWalletResource a) | ||
withWallet f = withResource $ runWalletResourceM f | ||
|
||
withInitializedWallet | ||
:: FilePath | ||
-> WalletResourceM a | ||
-> IO (Either ErrWalletResource a) | ||
withInitializedWallet dir f = withWallet $ do | ||
initXPubWallet fakeBootEnv dir xpub 0 | ||
letItInitialize | ||
f | ||
|
||
withLoadedWallet | ||
:: FilePath | ||
-> WalletResourceM a | ||
-> IO (Either ErrWalletResource a) | ||
withLoadedWallet dir f = withWallet $ do | ||
loadWallet fakeBootEnv dir | ||
letItInitialize | ||
f | ||
|
||
doNothing :: WalletResourceM () | ||
doNothing = pure () | ||
|
||
inADirectory :: (FilePath -> IO a) -> IO a | ||
inADirectory = withSystemTempDirectory "deposit-rest" | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "REST Deposit interface" $ do | ||
it "can initialize a wallet" | ||
$ inADirectory | ||
$ \dir -> do | ||
val <- withInitializedWallet dir availableBalance | ||
matchEmptyValue val | ||
it "can load an existing wallet" | ||
$ inADirectory | ||
$ \dir -> do | ||
val <- withInitializedWallet dir availableBalance | ||
onSuccess val $ \_ -> do | ||
val' <- withLoadedWallet dir availableBalance | ||
matchEmptyValue val' | ||
it "cannot re-initialize a wallet" | ||
$ inADirectory | ||
$ \dir -> do | ||
val <- withInitializedWallet dir doNothing | ||
onSuccess val $ \_ -> do | ||
val' <- withInitializedWallet dir availableBalance | ||
case val' of | ||
Left | ||
( ErrNoWallet | ||
( ErrFailedToInitialize | ||
( ErrCreatingDatabase | ||
(ErrDatabaseAlreadyExists fp) | ||
) | ||
) | ||
) | ||
| dir == fp -> pure () | ||
Left e -> fail $ show e | ||
Right _ -> fail "Should have failed the query on re-init" | ||
it "cannot load a non-existing wallet" | ||
$ inADirectory | ||
$ \dir -> do | ||
val <- withLoadedWallet dir availableBalance | ||
case val of | ||
Left | ||
( ErrNoWallet | ||
( ErrFailedToInitialize | ||
( ErrLoadingDatabase | ||
(ErrDatabaseNotFound fp) | ||
) | ||
) | ||
) | ||
| dir == fp -> pure () | ||
Left e -> fail $ show e | ||
Right _ -> fail "Should have failed the query on load" | ||
it "can check if a wallet is present" | ||
$ inADirectory | ||
$ \dir -> do | ||
r <- withInitializedWallet dir doNothing | ||
onSuccess r $ \_ -> do | ||
presence <- withWallet $ walletExists dir | ||
onSuccess presence $ \p -> p `shouldBe` True |