Skip to content

Commit

Permalink
Merge pull request #235 from input-output-hk/piotr/220/api_integratio…
Browse files Browse the repository at this point in the history
…n_tests

Integration tests for 4xx and refactor address_pool_gap tests
  • Loading branch information
KtorZ committed May 7, 2019
2 parents 1a59d86 + bc2711d commit 96430fc
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ import Data.Text
( Text )
import Data.Time
( UTCTime )
import Data.Word
( Word8 )
import GHC.TypeLits
( Symbol )
import Language.Haskell.TH.Quote
Expand Down Expand Up @@ -212,14 +210,14 @@ verify a = mapM_ (a &)
--
-- Lenses
--
addressPoolGap :: HasType (ApiT AddressPoolGap) s => Lens' s Word8
addressPoolGap :: HasType (ApiT AddressPoolGap) s => Lens' s Int
addressPoolGap =
lens _get _set
where
_get :: HasType (ApiT AddressPoolGap) s => s -> Word8
_get = getAddressPoolGap . getApiT . view typed
_set :: HasType (ApiT AddressPoolGap) s => (s, Word8) -> s
_set (s, v) = set typed (ApiT $ unsafeMkAddressPoolGap $ fromIntegral v) s
_get :: HasType (ApiT AddressPoolGap) s => s -> Int
_get = fromIntegral . getAddressPoolGap . getApiT . view typed
_set :: HasType (ApiT AddressPoolGap) s => (s, Int) -> s
_set (s, v) = set typed (ApiT $ unsafeMkAddressPoolGap v) s

balanceAvailable :: HasType (ApiT WalletBalance) s => Lens' s Natural
balanceAvailable =
Expand Down Expand Up @@ -340,8 +338,8 @@ unsafeCreateDigest s = fromMaybe
(error $ "unsafeCreateDigest failed to create digest from: " <> show s)
(digestFromByteString $ B8.pack $ T.unpack s)

unsafeMkAddressPoolGap :: Integer -> AddressPoolGap
unsafeMkAddressPoolGap g = case (mkAddressPoolGap g) of
unsafeMkAddressPoolGap :: Int -> AddressPoolGap
unsafeMkAddressPoolGap g = case (mkAddressPoolGap $ fromIntegral g) of
Right a -> a
Left _ -> error $ "unsafeMkAddressPoolGap: bad argument: " <> show g

Expand Down
175 changes: 95 additions & 80 deletions lib/http-bridge/test/integration/Test/Integration/Scenario/Wallets.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import Data.Quantity
( Quantity (..) )
import Data.Text
( Text )
import Data.Word
( Word8 )
import Test.Hspec
( SpecWith, describe, it )
import Test.Integration.Framework.DSL
Expand Down Expand Up @@ -56,7 +54,6 @@ import qualified Network.HTTP.Types.Status as HTTP
spec :: SpecWith Context
spec = do
it "WALLETS_CREATE_01 - Create a wallet" $ \ctx -> do

let payload = Json [json| {
"name": "1st Wallet",
"mnemonic_sentence": #{mnemonics15},
Expand All @@ -78,7 +75,6 @@ spec = do
]

it "WALLETS_CREATE_01 - Created a wallet can be listed" $ \ctx -> do

let payload = Json [json| {
"name": "Wallet to be listed",
"mnemonic_sentence": #{mnemonics18},
Expand All @@ -100,7 +96,7 @@ spec = do
, expectListItemFieldEqual 0 walletId "dfe87fcf0560fb57937a6468ea51e860672fad79"
]

it "WALLETS_CREATE_03 - Cannot create wallet that exists" $ \ctx -> do
it "WALLETS_CREATE_03,09 - Cannot create wallet that exists" $ \ctx -> do
let payload = Json [json| {
"name": "Some Wallet",
"mnemonic_sentence": #{mnemonics21},
Expand Down Expand Up @@ -548,10 +544,40 @@ spec = do
]
)
, ( "0 -> fail", 0
, [ expectResponseCode @IO HTTP.status400
, expectErrorMessage "An address pool gap must be a natural\
, [ expectResponseCode @IO HTTP.status400
, expectErrorMessage "An address pool gap must be a natural\
\ number between 10 and 100."
]
]
)
, ( "-1 -> fail", -1
, [ expectResponseCode @IO HTTP.status400
, expectErrorMessage "An address pool gap must be a natural\
\ number between 10 and 100."
]
)
, ( "1000 -> fail", 1000
, [ expectResponseCode @IO HTTP.status400
, expectErrorMessage "An address pool gap must be a natural\
\ number between 10 and 100."
]
)
, ( "132323000 -> fail", 132323000
, [ expectResponseCode @IO HTTP.status400
, expectErrorMessage "An address pool gap must be a natural\
\ number between 10 and 100."
]
)
, ( "-1000 -> fail", -1000
, [ expectResponseCode @IO HTTP.status400
, expectErrorMessage "An address pool gap must be a natural\
\ number between 10 and 100."
]
)
, ( "-132323000 -> fail", -132323000
, [ expectResponseCode @IO HTTP.status400
, expectErrorMessage "An address pool gap must be a natural\
\ number between 10 and 100."
]
)
]
forM_ matrix $ \(title, addrPoolGap, expectations) -> it title $ \ctx -> do
Expand All @@ -564,76 +590,6 @@ spec = do
r <- request @ApiWallet ctx ("POST", "v2/wallets") Default payload
verify r expectations

it "WALLETS_CREATE_08 - -1 as address_pool_gap -> fail" $ \ctx -> do
let payload = Json [json| {
"name": "Secure Wallet",
"mnemonic_sentence": #{mnemonics15},
"passphrase": "Secure passphrase",
"address_pool_gap": -1
} |]
r <- request @ApiWallet ctx ("POST", "v2/wallets") Default payload
verify r
[ expectResponseCode @IO HTTP.status400
, expectErrorMessage "An address pool gap must be a natural\
\ number between 10 and 100."
]

it "WALLETS_CREATE_08 - 1000 as address_pool_gap -> fail" $ \ctx -> do
let payload = Json [json| {
"name": "Secure Wallet",
"mnemonic_sentence": #{mnemonics15},
"passphrase": "Secure passphrase",
"address_pool_gap": 1000
} |]
r <- request @ApiWallet ctx ("POST", "v2/wallets") Default payload
verify r
[ expectResponseCode @IO HTTP.status400
, expectErrorMessage "An address pool gap must be a natural\
\ number between 10 and 100."
]

it "WALLETS_CREATE_08 - 132323000 as address_pool_gap -> fail" $ \ctx -> do
let payload = Json [json| {
"name": "Secure Wallet",
"mnemonic_sentence": #{mnemonics15},
"passphrase": "Secure passphrase",
"address_pool_gap": 132323000
} |]
r <- request @ApiWallet ctx ("POST", "v2/wallets") Default payload
verify r
[ expectResponseCode @IO HTTP.status400
, expectErrorMessage "An address pool gap must be a natural\
\ number between 10 and 100."
]

it "WALLETS_CREATE_08 - -1000 as address_pool_gap -> fail" $ \ctx -> do
let payload = Json [json| {
"name": "Secure Wallet",
"mnemonic_sentence": #{mnemonics15},
"passphrase": "Secure passphrase",
"address_pool_gap": -1000
} |]
r <- request @ApiWallet ctx ("POST", "v2/wallets") Default payload
verify r
[ expectResponseCode @IO HTTP.status400
, expectErrorMessage "An address pool gap must be a natural\
\ number between 10 and 100."
]

it "WALLETS_CREATE_08 - -132323000 as address_pool_gap -> fail" $ \ctx -> do
let payload = Json [json| {
"name": "Secure Wallet",
"mnemonic_sentence": #{mnemonics15},
"passphrase": "Secure passphrase",
"address_pool_gap": -132323000
} |]
r <- request @ApiWallet ctx ("POST", "v2/wallets") Default payload
verify r
[ expectResponseCode @IO HTTP.status400
, expectErrorMessage "An address pool gap must be a natural\
\ number between 10 and 100."
]

it "WALLETS_CREATE_08 - 2.5 as address_pool_gap -> fail" $ \ctx -> do
let payload = Json [json| {
"name": "Secure Wallet",
Expand Down Expand Up @@ -698,6 +654,65 @@ spec = do
[ expectResponseCode @IO HTTP.status202
, expectFieldEqual addressPoolGap 20
]

describe "WALLETS_CREATE_09 - HTTP headers" $ do
let matrix =
[ ( "No HTTP headers -> 415", None
, [expectResponseCode @IO HTTP.status415] )
, ( "Accept: text/plain -> 406"
, Headers
[ ("Content-Type", "application/json")
, ("Accept", "text/plain") ]
, [expectResponseCode @IO HTTP.status406]
)
, ( "No Accept -> 202"
, Headers [ ("Content-Type", "application/json") ]
, [expectResponseCode @IO HTTP.status202]
)
, ( "No Content-Type -> 415"
, Headers [ ("Accept", "application/json") ]
, [expectResponseCode @IO HTTP.status415]
)
, ( "Content-Type: text/plain -> 415"
, Headers [ ("Content-Type", "text/plain") ]
, [expectResponseCode @IO HTTP.status415]
)
]

forM_ matrix $ \(title, headers, expectations) -> it title $ \ctx -> do
let payload = Json [json| {
"name": "Secure Wallet",
"mnemonic_sentence": #{mnemonics21},
"passphrase": "Secure passphrase"
} |]
r <- request @ApiWallet ctx ("POST", "v2/wallets") headers payload
verify r expectations

describe "WALLETS_CREATE_09 - Bad request" $ do
let matrix =
[ ( "empty payload", NonJson "" )
, ( "{} payload", NonJson "{}" )
, ( "non-json valid payload"
, NonJson
"{name: wallet,\
\ mnemonic_sentence: [pill, hand, ask, useless, asset,\
\ rely, above, pipe, embark, game, elder, unaware,\
\ nasty, coach, glad],\
\ passphrase: 1234567890}"
)
]

forM_ matrix $ \(name, nonJson) -> it name $ \ctx -> do
let payload = nonJson
r <- request @ApiWallet ctx ("POST", "v2/wallets") Default payload
expectResponseCode @IO HTTP.status400 r

describe "WALLETS_CREATE_09 - v2/wallets - Methods Not Allowed" $ do
let matrix = ["PUT", "DELETE"]
forM_ matrix $ \method -> it (show method) $ \ctx -> do
r <- request @ApiWallet ctx (method, "v2/wallets") Default Empty
expectResponseCode @IO HTTP.status405 r

where

mnemonics3 :: [Text]
Expand Down Expand Up @@ -806,8 +821,8 @@ spec = do
passphraseMaxLength :: Int
passphraseMaxLength = 255

addressPoolGapMin :: Word8
addressPoolGapMin :: Int
addressPoolGapMin = 10

addressPoolGapMax :: Word8
addressPoolGapMax :: Int
addressPoolGapMax = 100

0 comments on commit 96430fc

Please sign in to comment.