Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration tests clean up #1739

Merged
merged 6 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/byron/cardano-wallet-byron.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ test-suite integration
other-modules:
Cardano.Wallet.Byron.Faucet
Test.Integration.Byron.Scenario.API.Transactions
Test.Integration.Byron.Scenario.API.Migrations
Test.Integration.Byron.Scenario.CLI.Transactions

benchmark restore
Expand Down
2 changes: 2 additions & 0 deletions lib/byron/test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ import qualified Cardano.Wallet.Api.Link as Link
import qualified Data.Aeson as Aeson
import qualified Data.ByteString as BS
import qualified Data.Text as T
import qualified Test.Integration.Byron.Scenario.API.Migrations as MigrationsByron
import qualified Test.Integration.Byron.Scenario.API.Transactions as TransactionsByron
import qualified Test.Integration.Byron.Scenario.CLI.Transactions as TransactionsByronCLI
import qualified Test.Integration.Scenario.API.Byron.Addresses as AddressesByron
Expand Down Expand Up @@ -139,6 +140,7 @@ main = withUtf8Encoding $ withLogging Nothing Info $ \(_, tr) -> do
HWWalletsByron.spec @n
AddressesByron.spec @n
TransactionsByron.spec @n
MigrationsByron.spec @n
TransactionsByronCommon.spec @n
Network.spec
describe "CLI Specifications" $ specWithServer tr $ do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

module Test.Integration.Byron.Scenario.API.Migrations
( spec
) where

import Prelude

import Cardano.Wallet.Api.Types
( ApiByronWallet
, ApiTransaction
, ApiWalletMigrationInfo
, DecodeAddress (..)
, EncodeAddress (..)
, WalletStyle (..)
)
import Cardano.Wallet.Primitive.AddressDerivation
( NetworkDiscriminant (..), PaymentAddress (..) )
import Cardano.Wallet.Primitive.AddressDerivation.Byron
( ByronKey )
import Cardano.Wallet.Primitive.AddressDerivation.Icarus
( IcarusKey )
import Data.Generics.Internal.VL.Lens
( view )
import Data.Quantity
( Quantity (..) )
import Data.Text
( Text )
import Test.Hspec
( SpecWith, describe, it, shouldBe, shouldSatisfy )
import Test.Integration.Framework.DSL
( Context (..)
, Headers (..)
, Payload (..)
, emptyRandomWalletMws
, eventually
, expectErrorMessage
, expectField
, expectResponseCode
, fixtureIcarusWallet
, fixturePassphrase
, fixtureRandomWallet
, getFromResponse
, json
, randomAddresses
, request
, verify
, (.>)
)
import Test.Integration.Framework.TestData
( errMsg400ParseError )

import qualified Cardano.Wallet.Api.Link as Link
import qualified Network.HTTP.Types.Status as HTTP

spec
:: forall (n :: NetworkDiscriminant) t.
( PaymentAddress n IcarusKey
, PaymentAddress n ByronKey
, EncodeAddress n
, DecodeAddress n
)
=> SpecWith (Context t)
spec = do

describe "BYRON_MIGRATE" $ do
scenario_MIGRATE_01 @n fixtureRandomWallet
scenario_MIGRATE_02 @n fixtureRandomWallet 1
scenario_MIGRATE_02 @n fixtureRandomWallet 3
scenario_MIGRATE_02 @n fixtureRandomWallet 10
scenario_MIGRATE_02 @n fixtureIcarusWallet 1
scenario_MIGRATE_02 @n fixtureIcarusWallet 3
scenario_MIGRATE_02 @n fixtureIcarusWallet 10

--
-- Scenarios
--
scenario_MIGRATE_01
:: forall (n :: NetworkDiscriminant) t.
( DecodeAddress n
, EncodeAddress n
, PaymentAddress n ByronKey
)
=> (Context t -> IO ApiByronWallet)
-> SpecWith (Context t)
scenario_MIGRATE_01 fixtureSource = it title $ \ctx -> do
wSrc <- fixtureSource ctx

r <- request @[ApiTransaction n] ctx
(Link.migrateWallet @'Byron wSrc)
Default
(NonJson "{passphrase:,}")
expectResponseCode @IO HTTP.status400 r
expectErrorMessage errMsg400ParseError r
where
title = "BYRON_MIGRATE_01 - invalid payload, parser error"

scenario_MIGRATE_02
:: forall (n :: NetworkDiscriminant) t.
( DecodeAddress n
, EncodeAddress n
, PaymentAddress n ByronKey
)
=> (Context t -> IO ApiByronWallet)
-> Int
-> SpecWith (Context t)
scenario_MIGRATE_02 fixtureSource addrCount = it title $ \ctx -> do
-- Restore a Byron wallet with funds, to act as a source wallet:
wSrc <- fixtureSource ctx
let originalBalance =
view (#balance . #available . #getQuantity) wSrc

-- Create an empty target wallet:
(wDest, mw) <- emptyRandomWalletMws ctx
let addresses :: [Text] =
take addrCount $ encodeAddress @n <$> randomAddresses @n mw

-- Calculate the expected migration fee:
r0 <- request @ApiWalletMigrationInfo ctx
(Link.getMigrationInfo @'Byron wSrc) Default Empty
verify r0
[ expectResponseCode @IO HTTP.status200
, expectField #migrationCost (.> Quantity 0)
]
let expectedFee = getFromResponse (#migrationCost . #getQuantity) r0

-- Perform a migration from the source wallet to the target wallet:
r1 <- request @[ApiTransaction n] ctx
(Link.migrateWallet @'Byron wSrc)
Default
(Json [json|
{ passphrase: #{fixturePassphrase}
, addresses: #{addresses}
}|])
verify r1
[ expectResponseCode @IO HTTP.status202
, expectField id (`shouldSatisfy` (not . null))
]

-- Check that funds become available in the target wallet:
let expectedBalance = originalBalance - expectedFee
eventually "Wallet has expectedBalance" $ do
r2 <- request @ApiByronWallet ctx
(Link.getWallet @'Byron wDest) Default Empty
verify r2
[ expectField
(#balance . #available)
(`shouldBe` Quantity expectedBalance)
, expectField
(#balance . #total)
(`shouldBe` Quantity expectedBalance)
]
where
title = "BYRON_MIGRATE_02 - after a migration operation successfully \
\completes, the correct amount eventually becomes available \
\in the target wallet for an arbitrary number of specified addresses."
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import Cardano.Wallet.Api.Types
, ApiT (..)
, ApiTransaction
, ApiUtxoStatistics
, ApiWalletMigrationInfo
, DecodeAddress (..)
, EncodeAddress (..)
, Iso8601Time (..)
Expand Down Expand Up @@ -62,7 +61,6 @@ import Test.Integration.Framework.DSL
, emptyByronWalletFromXPrvWith
, emptyIcarusWallet
, emptyRandomWallet
, emptyRandomWalletMws
, eventually
, expectErrorMessage
, expectField
Expand All @@ -89,13 +87,11 @@ import Test.Integration.Framework.DSL
, rootPrvKeyFromMnemonics
, verify
, walletId
, (.>)
)
import Test.Integration.Framework.Request
( RequestException )
import Test.Integration.Framework.TestData
( errMsg400ParseError
, errMsg403Fee
( errMsg403Fee
, errMsg403InputsDepleted
, errMsg403NotEnoughMoney_
, errMsg403UTxO
Expand Down Expand Up @@ -176,15 +172,6 @@ spec = do
scenario_TRANS_UTXO_01 @n fixtureIcarusWallet (fixtureIcarusWalletAddrs @n)
scenario_TRANS_UTXO_01 @n fixtureRandomWallet (fixtureRandomWalletAddrs @n)

describe "BYRON_MIGRATE" $ do
scenario_MIGRATE_01 @n fixtureRandomWallet
scenario_MIGRATE_02 @n fixtureRandomWallet 1
scenario_MIGRATE_02 @n fixtureRandomWallet 3
scenario_MIGRATE_02 @n fixtureRandomWallet 10
scenario_MIGRATE_02 @n fixtureIcarusWallet 1
scenario_MIGRATE_02 @n fixtureIcarusWallet 3
scenario_MIGRATE_02 @n fixtureIcarusWallet 10

--
-- Scenarios
--
Expand Down Expand Up @@ -709,86 +696,6 @@ scenario_TRANS_UTXO_01 fixtureSource fixtureTarget = it title $ \ctx -> do
where
title = "TRANS_UTXO_01 - one recipient multiple txs received"

scenario_MIGRATE_01
:: forall (n :: NetworkDiscriminant) t.
( DecodeAddress n
, EncodeAddress n
, PaymentAddress n ByronKey
)
=> (Context t -> IO ApiByronWallet)
-> SpecWith (Context t)
scenario_MIGRATE_01 fixtureSource = it title $ \ctx -> do
wSrc <- fixtureSource ctx

r <- request @[ApiTransaction n] ctx
(Link.migrateWallet @'Byron wSrc)
Default
(NonJson "{passphrase:,}")
expectResponseCode @IO HTTP.status400 r
expectErrorMessage errMsg400ParseError r
where
title = "BYRON_MIGRATE_01 - invalid payload, parser error"

scenario_MIGRATE_02
:: forall (n :: NetworkDiscriminant) t.
( DecodeAddress n
, EncodeAddress n
, PaymentAddress n ByronKey
)
=> (Context t -> IO ApiByronWallet)
-> Int
-> SpecWith (Context t)
scenario_MIGRATE_02 fixtureSource addrCount = it title $ \ctx -> do
-- Restore a Byron wallet with funds, to act as a source wallet:
wSrc <- fixtureSource ctx
let originalBalance =
view (#balance . #available . #getQuantity) wSrc

-- Create an empty target wallet:
(wDest, mw) <- emptyRandomWalletMws ctx
let addresses :: [Text] =
take addrCount $ encodeAddress @n <$> randomAddresses @n mw

-- Calculate the expected migration fee:
r0 <- request @ApiWalletMigrationInfo ctx
(Link.getMigrationInfo @'Byron wSrc) Default Empty
verify r0
[ expectResponseCode @IO HTTP.status200
, expectField #migrationCost (.> Quantity 0)
]
let expectedFee = getFromResponse (#migrationCost . #getQuantity) r0

-- Perform a migration from the source wallet to the target wallet:
r1 <- request @[ApiTransaction n] ctx
(Link.migrateWallet @'Byron wSrc)
Default
(Json [json|
{ passphrase: #{fixturePassphrase}
, addresses: #{addresses}
}|])
verify r1
[ expectResponseCode @IO HTTP.status202
, expectField id (`shouldSatisfy` (not . null))
]

-- Check that funds become available in the target wallet:
let expectedBalance = originalBalance - expectedFee
eventually "Wallet has expectedBalance" $ do
r2 <- request @ApiByronWallet ctx
(Link.getWallet @'Byron wDest) Default Empty
verify r2
[ expectField
(#balance . #available)
(`shouldBe` Quantity expectedBalance)
, expectField
(#balance . #total)
(`shouldBe` Quantity expectedBalance)
]
where
title = "BYRON_MIGRATE_02 - after a migration operation successfully \
\completes, the correct amount eventually becomes available \
\in the target wallet for an arbitrary number of specified addresses."

scenario_TRANS_REG_1670
:: forall (n :: NetworkDiscriminant) t.
( DecodeAddress n
Expand Down
2 changes: 2 additions & 0 deletions lib/core-integration/cardano-wallet-core-integration.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ library
Test.Integration.Scenario.API.Byron.HWWallets
Test.Integration.Scenario.API.Byron.Addresses
Test.Integration.Scenario.API.Byron.Transactions
Test.Integration.Scenario.API.Byron.Migrations
Test.Integration.Scenario.API.Shelley.Addresses
Test.Integration.Scenario.API.Shelley.HWWallets
Test.Integration.Scenario.API.Shelley.Network
Test.Integration.Scenario.API.Shelley.StakePools
Test.Integration.Scenario.API.Shelley.Transactions
Test.Integration.Scenario.API.Shelley.Migrations
Test.Integration.Scenario.API.Shelley.Wallets
Test.Integration.Scenario.API.Network
Test.Integration.Scenario.CLI.Byron.Wallets
Expand Down
Loading