From bfccf79caaa321b1e8220c0a6df8e6f3231df9cb Mon Sep 17 00:00:00 2001 From: Piotr Stachyra Date: Tue, 7 May 2019 08:51:45 +0200 Subject: [PATCH 1/2] refactor tests: put some address_pool_gap cases into the 'matrix' --- .../Test/Integration/Framework/DSL.hs | 16 ++- .../Test/Integration/Scenario/Wallets.hs | 112 ++++++------------ 2 files changed, 42 insertions(+), 86 deletions(-) diff --git a/lib/http-bridge/test/integration/Test/Integration/Framework/DSL.hs b/lib/http-bridge/test/integration/Test/Integration/Framework/DSL.hs index f99616837e4..7334b1e3ce2 100644 --- a/lib/http-bridge/test/integration/Test/Integration/Framework/DSL.hs +++ b/lib/http-bridge/test/integration/Test/Integration/Framework/DSL.hs @@ -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 @@ -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 = @@ -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 diff --git a/lib/http-bridge/test/integration/Test/Integration/Scenario/Wallets.hs b/lib/http-bridge/test/integration/Test/Integration/Scenario/Wallets.hs index de08df52928..5a669d65c98 100644 --- a/lib/http-bridge/test/integration/Test/Integration/Scenario/Wallets.hs +++ b/lib/http-bridge/test/integration/Test/Integration/Scenario/Wallets.hs @@ -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 @@ -548,10 +546,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 @@ -564,76 +592,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", @@ -806,8 +764,8 @@ spec = do passphraseMaxLength :: Int passphraseMaxLength = 255 - addressPoolGapMin :: Word8 + addressPoolGapMin :: Int addressPoolGapMin = 10 - addressPoolGapMax :: Word8 + addressPoolGapMax :: Int addressPoolGapMax = 100 From bc2711d9addd3c02a216d3d0e95aa01fb8086e1b Mon Sep 17 00:00:00 2001 From: Piotr Stachyra Date: Tue, 7 May 2019 16:49:32 +0200 Subject: [PATCH 2/2] WALLETS_CREATE_09 - tests excersizing 4xx responses --- .../Test/Integration/Scenario/Wallets.hs | 63 ++++++++++++++++++- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/lib/http-bridge/test/integration/Test/Integration/Scenario/Wallets.hs b/lib/http-bridge/test/integration/Test/Integration/Scenario/Wallets.hs index 5a669d65c98..4f7142c898c 100644 --- a/lib/http-bridge/test/integration/Test/Integration/Scenario/Wallets.hs +++ b/lib/http-bridge/test/integration/Test/Integration/Scenario/Wallets.hs @@ -54,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}, @@ -76,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}, @@ -98,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}, @@ -656,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]