Skip to content

Commit

Permalink
add core unit testing bits
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Jan 26, 2021
1 parent c36b294 commit ecdc7eb
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/core/src/Cardano/Wallet/Api.hs
Expand Up @@ -339,6 +339,7 @@ type SignMetadata = "wallets"
type PostAccountKey = "wallets"
:> Capture "walletId" (ApiT WalletId)
:> "keys"
:> Capture "index" (ApiT DerivationIndex)
:> ReqBody '[JSON] ApiPostAccountKeyData
:> PostAccepted '[JSON] ApiAccountKey

Expand Down
3 changes: 2 additions & 1 deletion lib/core/src/Cardano/Wallet/Api/Server.hs
Expand Up @@ -1933,9 +1933,10 @@ postAccountPublicKey
)
=> ctx
-> ApiT WalletId
-> ApiT DerivationIndex
-> ApiPostAccountKeyData
-> Handler ApiAccountKey
postAccountPublicKey ctx (ApiT wid) (ApiPostAccountKeyData (ApiT ix) (ApiT pwd) extd) = do
postAccountPublicKey ctx (ApiT wid) (ApiT ix) (ApiPostAccountKeyData (ApiT pwd) extd) = do
withWorkerCtx @_ @s @k ctx wid liftE liftE $ \wrk -> do
k <- liftHandler $ W.readPublicAccountKey @_ @s @k @n wrk wid pwd ix
let toBytes = if extd then xpubToBytes else xpubPublicKey
Expand Down
3 changes: 1 addition & 2 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Expand Up @@ -943,8 +943,7 @@ newtype ApiVerificationKey = ApiVerificationKey
deriving anyclass NFData

data ApiPostAccountKeyData = ApiPostAccountKeyData
{ index :: ApiT DerivationIndex
, passphrase :: ApiT (Passphrase "raw")
{ passphrase :: ApiT (Passphrase "raw")
, extended :: Bool
} deriving (Eq, Generic, Show)
deriving anyclass NFData
Expand Down
15 changes: 15 additions & 0 deletions lib/core/test/data/Cardano/Wallet/Api/ApiAccountKey.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions lib/core/test/data/Cardano/Wallet/Api/ApiPostAccountKeyData.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions lib/core/test/unit/Cardano/Wallet/Api/Malformed.hs
Expand Up @@ -54,6 +54,7 @@ import Cardano.Wallet.Api.Types
, ApiAddressInspectData
, ApiMaintenanceActionPostData
, ApiPoolId
, ApiPostAccountKeyData
, ApiPostRandomAddressData
, ApiPutAddressesData
, ApiSelectCoinsData
Expand Down Expand Up @@ -988,6 +989,48 @@ instance Malformed (BodyParam ApiWalletPassphrase) where
)
]

instance Malformed (BodyParam ApiPostAccountKeyData) where
malformed = jsonValid ++ jsonInvalid
where
jsonInvalid = first BodyParam <$>
[ ("1020344", "Error in $: parsing Cardano.Wallet.Api.Types.ApiPostAccountKeyData(ApiPostAccountKeyData) failed, expected Object, but encountered Number")
, ("\"1020344\"", "Error in $: parsing Cardano.Wallet.Api.Types.ApiPostAccountKeyData(ApiPostAccountKeyData) failed, expected Object, but encountered String")
, ("\"slot_number : \"random\"}", "trailing junk after valid JSON: endOfInput")
, ("{\"name : \"random\"}", msgJsonInvalid)
]
jsonValid = first (BodyParam . Aeson.encode) <$>
[ ( [aesonQQ| { "passphrase": #{nameTooLong}, "extended": true }|]
, "Error in $.passphrase: passphrase is too long: expected at most 255 characters"
)
, ( [aesonQQ| { "passphrase": 123, "extended": true }|]
, "Error in $.passphrase: parsing Text failed, expected String, but encountered Number"
)
, ( [aesonQQ| { "passphrase": [], "extended": true }|]
, "Error in $.passphrase: parsing Text failed, expected String, but encountered Array"
)
, ( [aesonQQ| { "passphrase": 1.5, "extended": true }|]
, "Error in $.passphrase: parsing Text failed, expected String, but encountered Number"
)
, ( [aesonQQ| { "extended": true }|]
, "Error in $: parsing Cardano.Wallet.Api.Types.ApiPostAccountKeyData(ApiPostAccountKeyData) failed, key 'passphrase' not found"
)
, ( [aesonQQ| { "passphrase": "The proper passphrase" }|]
, "Error in $: parsing Cardano.Wallet.Api.Types.ApiPostAccountKeyData(ApiPostAccountKeyData) failed, key 'extended' not found"
)
, ( [aesonQQ| { "passphrase": "The proper passphrase", "extended": 123 }|]
, "Error in $.extended: expected Bool, but encountered Number"
)
, ( [aesonQQ| { "passphrase": "The proper passphrase", "extended": [] }|]
, "Error in $.extended: expected Bool, but encountered Array"
)
, ( [aesonQQ| { "passphrase": "The proper passphrase", "extended": 1.5 }|]
, "Error in $.extended: expected Bool, but encountered Number"
)
, ( [aesonQQ| { "passphrase": "The proper passphrase", "extended": "ok" }|]
, "Error in $.extended: expected Bool, but encountered String"
)
]

instance Malformed (BodyParam (ApiSelectCoinsData ('Testnet pm))) where
malformed = jsonValid ++ jsonInvalid
where
Expand Down
21 changes: 21 additions & 0 deletions lib/core/test/unit/Cardano/Wallet/Api/TypesSpec.hs
Expand Up @@ -46,6 +46,7 @@ import Cardano.Wallet.Api.Types
( AccountPostData (..)
, AddressAmount (..)
, AnyAddress (..)
, ApiAccountKey (..)
, ApiAccountPublicKey (..)
, ApiAddress (..)
, ApiAddressData (..)
Expand Down Expand Up @@ -73,6 +74,7 @@ import Cardano.Wallet.Api.Types
, ApiNetworkInformation (..)
, ApiNetworkParameters (..)
, ApiNtpStatus (..)
, ApiPostAccountKeyData
, ApiPostRandomAddressData
, ApiPutAddressesData (..)
, ApiSelectCoinsAction (..)
Expand Down Expand Up @@ -356,6 +358,8 @@ spec = parallel $ do
jsonRoundtripAndGolden $ Proxy @ApiCredential
jsonRoundtripAndGolden $ Proxy @ApiAddressData
jsonRoundtripAndGolden $ Proxy @(ApiT DerivationIndex)
jsonRoundtripAndGolden $ Proxy @ApiPostAccountKeyData
jsonRoundtripAndGolden $ Proxy @ApiAccountKey
jsonRoundtripAndGolden $ Proxy @ApiEpochInfo
jsonRoundtripAndGolden $ Proxy @(ApiSelectCoinsData ('Testnet 0))
jsonRoundtripAndGolden $ Proxy @(ApiCoinSelection ('Testnet 0))
Expand Down Expand Up @@ -1784,6 +1788,17 @@ instance Arbitrary ApiHealthCheck where
arbitrary = genericArbitrary
shrink = genericShrink

instance Arbitrary ApiPostAccountKeyData where
arbitrary = genericArbitrary
shrink = genericShrink

instance Arbitrary ApiAccountKey where
arbitrary = do
xpubKey <- BS.pack <$> replicateM 64 arbitrary
pubKey <- BS.pack <$> replicateM 32 arbitrary
oneof [ pure $ ApiAccountKey pubKey False
, pure $ ApiAccountKey xpubKey True ]

{-------------------------------------------------------------------------------
Specification / Servant-Swagger Machinery
Expand Down Expand Up @@ -2017,6 +2032,12 @@ instance ToSchema ApiWalletSignData where
addDefinition =<< declareSchemaForDefinition "TransactionMetadataValue"
declareSchemaForDefinition "ApiWalletSignData"

instance ToSchema ApiPostAccountKeyData where
declareNamedSchema _ = declareSchemaForDefinition "ApiPostAccountKeyData"

instance ToSchema ApiAccountKey where
declareNamedSchema _ = declareSchemaForDefinition "ApiAccountKey"

-- | Utility function to provide an ad-hoc 'ToSchema' instance for a definition:
-- we simply look it up within the Swagger specification.
declareSchemaForDefinition :: Text -> Declare (Definitions Schema) NamedSchema
Expand Down
16 changes: 13 additions & 3 deletions specifications/api/swagger.yaml
Expand Up @@ -1965,11 +1965,9 @@ components:
ApiPostAccountKeyData: &ApiPostAccountKeyData
type: object
required:
- index
- passphrase
- extended
properties:
index: *derivationSegment
passphrase: *walletPassphrase
extended: *keyExtended

Expand Down Expand Up @@ -2388,6 +2386,17 @@ x-errSoftDerivationRequired: &errSoftDerivationRequired
type: string
enum: ['soft_derivation_required']

x-errHardenedDerivationRequired: &errHardenedDerivationRequired
<<: *responsesErr
title: not_found
properties:
message:
type: string
description: A descriptive error message.
code:
type: string
enum: ['hardened_derivation_required']

x-errNoSuchWallet: &errNoSuchWallet
<<: *responsesErr
title: no_such_wallet
Expand Down Expand Up @@ -3819,7 +3828,7 @@ paths:
- *parametersAddressState
responses: *responsesListAddresses

/wallets/{walletId}/keys:
/wallets/{walletId}/keys/{index}:
post:
operationId: postAccountKey
tags: ["Keys"]
Expand All @@ -3830,6 +3839,7 @@ paths:
Retrieve account public key from the wallet.
parameters:
- *parametersWalletId
- *parametersDerivationSegment
requestBody:
required: true
content:
Expand Down

0 comments on commit ecdc7eb

Please sign in to comment.