From 9b635c5d48c398059abfbc6087ecd26ff7f1b786 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Thu, 9 May 2019 14:27:27 +0200 Subject: [PATCH] add extra property test to verify passphrase gets updated correctly --- .../src/Cardano/Wallet/Primitive/Types.hs | 2 +- lib/core/test/unit/Cardano/WalletSpec.hs | 28 +++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/core/src/Cardano/Wallet/Primitive/Types.hs b/lib/core/src/Cardano/Wallet/Primitive/Types.hs index babc70b6230..9033e61d671 100644 --- a/lib/core/src/Cardano/Wallet/Primitive/Types.hs +++ b/lib/core/src/Cardano/Wallet/Primitive/Types.hs @@ -225,7 +225,7 @@ instance NFData poolId => NFData (WalletDelegation poolId) newtype WalletPassphraseInfo = WalletPassphraseInfo { lastUpdatedAt :: UTCTime } - deriving (Generic, Eq, Show) + deriving (Generic, Eq, Ord, Show) instance NFData WalletPassphraseInfo diff --git a/lib/core/test/unit/Cardano/WalletSpec.hs b/lib/core/test/unit/Cardano/WalletSpec.hs index 608ae62ae6c..db26ef1d4ce 100644 --- a/lib/core/test/unit/Cardano/WalletSpec.hs +++ b/lib/core/test/unit/Cardano/WalletSpec.hs @@ -49,10 +49,12 @@ import Cardano.Wallet.Primitive.Types , WalletMetadata (..) , WalletName (..) ) +import Control.Concurrent + ( threadDelay ) import Control.DeepSeq ( NFData (..) ) import Control.Monad - ( forM_, replicateM ) + ( forM_, replicateM, void ) import Control.Monad.IO.Class ( liftIO ) import Control.Monad.Trans.Except @@ -66,7 +68,7 @@ import Data.Coerce import Data.Either ( isLeft, isRight ) import Data.Maybe - ( isJust ) + ( isJust, isNothing ) import GHC.Generics ( Generic ) import Test.Hspec @@ -118,6 +120,8 @@ spec = do (property walletUpdatePassphraseWrong) it "Can't change passphrase if wallet doesn't exist" (property walletUpdatePassphraseNoSuchWallet) + it "Passphrase info is up-to-date after wallet passphrase update" + (property walletUpdatePassphraseDate) {------------------------------------------------------------------------------- Properties @@ -241,6 +245,26 @@ walletUpdatePassphraseNoSuchWallet wallet@(wid', _, _) wid (old, new) = let err = ErrUpdatePassphraseWithRootKey (ErrWithRootKeyNoRootKey wid) attempt `shouldBe` Left err +walletUpdatePassphraseDate + :: (WalletId, WalletName, DummyState) + -> (Key 'RootK XPrv, Passphrase "encryption") + -> Property +walletUpdatePassphraseDate wallet (xprv, pwd) = monadicIO $ liftIO $ do + (WalletLayerFixture _ wl [wid]) <- liftIO $ setupFixture wallet + let infoShouldSatisfy predicate = do + info <- (passphraseInfo . snd) <$> unsafeRunExceptT (readWallet wl wid) + info `shouldSatisfy` predicate + return info + + void $ infoShouldSatisfy isNothing + unsafeRunExceptT $ attachPrivateKey wl wid (xprv, pwd) + info <- infoShouldSatisfy isJust + pause + unsafeRunExceptT $ updateWalletPassphrase wl wid (coerce pwd, coerce pwd) + void $ infoShouldSatisfy (\info' -> isJust info' && info' > info) + where + pause = threadDelay 500 + {------------------------------------------------------------------------------- Tests machinery, Arbitrary instances -------------------------------------------------------------------------------}