Skip to content

Commit

Permalink
Merge pull request #399 from input-output-hk/piotr/397/fix_faulty_exi…
Browse files Browse the repository at this point in the history
…t_code

Small adjustment in CLI
  • Loading branch information
KtorZ committed Jun 12, 2019
2 parents 43818b1 + 0dd4c0e commit 8cd48bb
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 50 deletions.
39 changes: 30 additions & 9 deletions exe/wallet/Main.hs
Expand Up @@ -64,6 +64,8 @@ import Control.Monad
( when )
import Data.Aeson
( (.:) )
import Data.Either
( isRight )
import Data.Functor
( (<&>) )
import qualified Data.List.NonEmpty as NE
Expand Down Expand Up @@ -121,7 +123,6 @@ import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.IO as TIO


cli :: Docopt
cli = [docopt|Cardano Wallet CLI.

Expand Down Expand Up @@ -245,24 +246,28 @@ exec execServer manager args
| args `isPresent` command "wallet" &&
args `isPresent` command "delete" = do
wId <- args `parseArg` argument "wallet-id"
runClient (const "") $ deleteWallet (ApiT wId)
runClient (const "") (deleteWallet (ApiT wId))

| args `isPresent` command "transaction" &&
args `isPresent` command "create" = do
wId <- args `parseArg` argument "wallet-id"
ts <- args `parseAllArgs` longOption "payment"
wPwd <- getPassphrase
runClient Aeson.encodePretty $ createTransaction (ApiT wId) $
PostTransactionData
ts
(ApiT wPwd)
res <- sendRequest $ getWallet $ ApiT wId
if (isRight res) then do
wPwd <- getPassphrase
runClient Aeson.encodePretty $ createTransaction (ApiT wId) $
PostTransactionData
ts
(ApiT wPwd)
else
handleResponse Aeson.encodePretty res

| args `isPresent` command "address" &&
args `isPresent` command "list" = do
wId <- args `parseArg` argument "wallet-id"
maybeState <- args `parseOptionalArg` longOption "state"
runClient Aeson.encodePretty
$ listAddresses (ApiT wId) (ApiT <$> maybeState)
(listAddresses (ApiT wId) (ApiT <$> maybeState))

| args `isPresent` longOption "version" = do
putStrLn (showVersion version)
Expand Down Expand Up @@ -313,9 +318,24 @@ exec execServer manager args
-> ClientM a
-> IO ()
runClient encode cmd = do
res <- sendRequest cmd
handleResponse encode res

sendRequest
:: forall a. ()
=> ClientM a
-> IO (Either ServantError a)
sendRequest cmd = do
port <- args `parseArg` longOption "port"
let env = mkClientEnv manager (BaseUrl Http "localhost" port "")
res <- runClientM cmd env
runClientM cmd env

handleResponse
:: forall a. ()
=> (a -> BL.ByteString)
-> Either ServantError a
-> IO ()
handleResponse encode res = do
case res of
Right a -> do
TIO.hPutStrLn stderr "Ok."
Expand All @@ -330,6 +350,7 @@ exec execServer manager args
_ ->
T.pack $ show e
putErrLn msg
exitFailure

-- | Start a web-server to serve the wallet backend API on the given port.
execHttpBridge
Expand Down
Expand Up @@ -165,18 +165,13 @@ spec = do
forM_ falseWalletIds $ \(title, walId) -> it title $ \_ -> do
(Exit c, Stdout o, Stderr e) <- listAddressesViaCLI [walId]
o `shouldBe` ""

c `shouldBe` ExitFailure 1
if (title == "40 chars hex") then
e `shouldBe` errMsg404NoWallet "1111111111111111111111111111111111111111\n"
else
e `shouldBe` "wallet id should be an hex-encoded string of\
\ 40 characters\n"

if (title == "40 chars hex") then
c `shouldBe` ExitSuccess
else
c `shouldBe` ExitFailure 1

it "ADDRESS_LIST_04 - 'almost' valid walletId" $ \ctx -> do
wid <- emptyWallet' ctx
(Exit c, Stdout o, Stderr e) <- listAddressesViaCLI [wid ++ "0"]
Expand All @@ -192,7 +187,7 @@ spec = do
(Exit c, Stdout o, Stderr e) <- listAddressesViaCLI [wid]
e `shouldBe` errMsg404NoWallet (T.pack wid <> "\n")
o `shouldBe` ""
c `shouldBe` ExitSuccess
c `shouldBe` ExitFailure 1

where
emptyWallet' :: Context t -> IO String
Expand Down
Expand Up @@ -27,7 +27,7 @@ import Data.Proxy
( Proxy (..) )
import qualified Data.Text as T
import System.Command
( Exit (..), Stdout (..) )
( Exit (..), Stderr (..), Stdout (..) )
import System.Exit
( ExitCode (..) )
import Test.Hspec
Expand All @@ -39,6 +39,7 @@ import Test.Integration.Framework.DSL
, amount
, balanceAvailable
, balanceTotal
, cardanoWalletCLI
, deleteWalletViaCLI
, direction
, emptyWallet
Expand Down Expand Up @@ -237,7 +238,7 @@ spec = do
(c, out, err) <- postTransactionViaCLI "cardano-wallet" args
(T.unpack err) `shouldContain` errMsg403UTxO
out `shouldBe` ""
c `shouldBe` ExitSuccess
c `shouldBe` ExitFailure 1

it "TRANS_CREATE_03 - 0 balance after transaction" $ \ctx -> do
let balance = 168434
Expand Down Expand Up @@ -292,7 +293,7 @@ spec = do
(c, out, err) <- postTransactionViaCLI "Secure Passphrase" args
(T.unpack err) `shouldContain` errMsg403Fee
out `shouldBe` ""
c `shouldBe` ExitSuccess
c `shouldBe` ExitFailure 1

it "TRANS_CREATE_04 - Not enough money" $ \ctx -> do
wSrc <- fixtureWalletWith ctx [100_000, 1000]
Expand All @@ -308,7 +309,7 @@ spec = do
(c, out, err) <- postTransactionViaCLI "Secure Passphrase" args
(T.unpack err) `shouldContain` (errMsg403NotEnoughMoney 101_000 1000_000)
out `shouldBe` ""
c `shouldBe` ExitSuccess
c `shouldBe` ExitFailure 1

it "TRANS_CREATE_04 - Wrong password" $ \ctx -> do
wSrc <- fixtureWallet ctx
Expand All @@ -324,7 +325,7 @@ spec = do
(c, out, err) <- postTransactionViaCLI "This password is wrong" args
(T.unpack err) `shouldContain` errMsg403WrongPass
out `shouldBe` ""
c `shouldBe` ExitSuccess
c `shouldBe` ExitFailure 1

describe "TRANS_CREATE_05 - Invalid addresses" $ do
let longAddr = replicate 10000 '1'
Expand Down Expand Up @@ -354,7 +355,7 @@ spec = do
(c, out, err) <- postTransactionViaCLI "Secure Passphrase" args
(T.unpack err) `shouldContain` errMsg
out `shouldBe` ""
c `shouldBe` (ExitFailure 1)
c `shouldBe` ExitFailure 1

describe "TRANS_CREATE_06 - Invalid amount" $ do
let errNum = "Expecting natural number"
Expand All @@ -381,31 +382,26 @@ spec = do
(c, out, err) <- postTransactionViaCLI "cardano-wallet" args
(T.unpack err) `shouldContain` errMsg
out `shouldBe` ""
if (title == "0") then
c `shouldBe` ExitSuccess
else
c `shouldBe` (ExitFailure 1)
c `shouldBe` ExitFailure 1

describe "TRANS_CREATE_07 - False wallet ids" $ do
forM_ falseWalletIds $ \(title, walId) -> it title $ \ctx -> do
wDest <- emptyWallet ctx
addrs:_ <- listAddresses ctx wDest
let addr =
encodeAddress (Proxy @t) (getApiT $ fst $ addrs ^. #id)
let args = [ walId, "--payment", "12@" ++ (T.unpack addr) ]

(c, out, err) <- postTransactionViaCLI "Secure Passphrase" args
let args = [ "transaction", "create", "--port", "1337",
walId, "--payment", "12@" ++ (T.unpack addr) ]
-- make sure CLI returns error before asking for passphrase
(Exit c, Stdout out, Stderr err) <- cardanoWalletCLI args
out `shouldBe` ""
c `shouldBe` ExitFailure 1
if (title == "40 chars hex") then
(T.unpack err) `shouldContain` "I couldn't find a wallet with \
err `shouldContain` "I couldn't find a wallet with \
\the given id: 1111111111111111111111111111111111111111"
else
(T.unpack err) `shouldContain` "wallet id should be an \
err `shouldContain` "wallet id should be an \
\hex-encoded string of 40 characters"
if (title == "40 chars hex") then
c `shouldBe` ExitSuccess
else
c `shouldBe` (ExitFailure 1)

it "TRANSCLI_CREATE_07 - 'almost' valid walletId" $ \ctx -> do
wSrc <- fixtureWallet ctx
Expand All @@ -414,13 +410,14 @@ spec = do
let addr =
encodeAddress (Proxy @t) (getApiT $ fst $ addrs ^. #id)
let args = T.unpack <$>
[ (T.append (wSrc ^. walletId) "0"), "--payment", "11@" <> addr ]

(c, out, err) <- postTransactionViaCLI "cardano-wallet" args
(T.unpack err) `shouldContain` "wallet id should be an hex-encoded\
[ "transaction", "create", "--port", "1337",
(T.append (wSrc ^. walletId) "0"), "--payment", "11@" <> addr ]
-- make sure CLI returns error before asking for passphrase
(Exit c, Stdout out, Stderr err) <- cardanoWalletCLI args
err `shouldContain` "wallet id should be an hex-encoded\
\ string of 40 characters"
out `shouldBe` ""
c `shouldBe` (ExitFailure 1)
c `shouldBe` ExitFailure 1

it "TRANS_CREATE_07 - Deleted wallet" $ \ctx -> do
wSrc <- fixtureWallet ctx
Expand All @@ -431,10 +428,12 @@ spec = do
addrs:_ <- listAddresses ctx wDest
let addr =
encodeAddress (Proxy @t) (getApiT $ fst $ addrs ^. #id)
let args = T.unpack <$> [ wSrc ^. walletId, "--payment", "11@" <> addr ]

(c, out, err) <- postTransactionViaCLI "cardano-wallet" args
(T.unpack err) `shouldContain` "I couldn't find a wallet with \
let args = T.unpack <$>
[ "transaction", "create", "--port", "1337", wSrc ^. walletId,
"--payment", "11@" <> addr ]
-- make sure CLI returns error before asking for passphrase
(Exit c, Stdout out, Stderr err) <- cardanoWalletCLI args
err `shouldContain` "I couldn't find a wallet with \
\the given id: " ++ T.unpack ( wSrc ^. walletId )
out `shouldBe` ""
c `shouldBe` ExitSuccess
c `shouldBe` ExitFailure 1
Expand Up @@ -111,18 +111,14 @@ spec = do
forM_ falseWalletIds $ \(title, walId) -> it title $ \_ -> do
(Exit c, Stdout out, Stderr err) <- getWalletViaCLI walId
out `shouldBe` ""
c `shouldBe` ExitFailure 1
if (title == "40 chars hex") then
err `shouldBe` "I couldn't find a wallet with the given id:\
\ 1111111111111111111111111111111111111111\n"
else
err `shouldBe` "wallet id should be an hex-encoded string of\
\ 40 characters\n"

if (title == "40 chars hex") then
c `shouldBe` ExitSuccess
else
c `shouldBe` ExitFailure 1

it "WALLETS_LIST_01 - Can list wallets" $ \ctx -> do
emptyWallet' ctx $> () <* emptyWallet' ctx
(Exit c, Stdout out, Stderr err) <- listWalletsViaCLI
Expand Down

0 comments on commit 8cd48bb

Please sign in to comment.