Skip to content

Commit

Permalink
Merge pull request #170 from input-output-hk/piotr/integration_tests
Browse files Browse the repository at this point in the history
1st integration test on the create wallet API endpoint
  • Loading branch information
piotr-iohk committed Apr 24, 2019
2 parents a0d5977 + f768ace commit 850d764
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 84 deletions.
6 changes: 6 additions & 0 deletions .weeder.yaml
Expand Up @@ -43,11 +43,17 @@
- </>
- expectError
- expectSuccess
- expectFieldEqual
- expectFieldNotEqual
- getFromResponse
- module:
- name: Test.Integration.Framework.Request
- identifier:
- ClientError
- DecodeFailure
- Empty
- NonJson
- None
- unsafeRequest
- section:
- name: test:unit
Expand Down
5 changes: 4 additions & 1 deletion cardano-wallet.cabal
Expand Up @@ -191,19 +191,21 @@ test-suite integration
build-depends:
base
, aeson
, aeson-qq
, async
, bytestring
, cardano-wallet
, cborg
, cryptonite
, exceptions
, fmt
, generic-lens
, hspec
, hspec-core
, hspec-expectations-lifted
, http-client
, http-api-data
, http-types
, aeson-qq
, process
, say
, template-haskell
Expand All @@ -223,6 +225,7 @@ test-suite integration
Cardano.Launcher
Test.Integration.Framework.DSL
Test.Integration.Framework.Request
Test.Integration.Scenario.Wallets
if os(windows)
build-depends: Win32
other-modules: Cardano.Launcher.Windows
Expand Down
10 changes: 5 additions & 5 deletions src/Cardano/Wallet/Api.hs
Expand Up @@ -19,12 +19,12 @@ import Servant.API
( (:<|>)
, (:>)
, Capture
, Delete
, DeleteNoContent
, Get
, JSON
, NoContent
, OctetStream
, Post
, PostAccepted
, Put
, QueryParam
, ReqBody
Expand Down Expand Up @@ -64,7 +64,7 @@ type Wallets =
-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/deleteWallet
type DeleteWallet = "wallets"
:> Capture "walletId" (ApiT WalletId)
:> Delete '[OctetStream] NoContent
:> DeleteNoContent '[OctetStream] NoContent

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/getWallet
type GetWallet = "wallets"
Expand All @@ -78,7 +78,7 @@ type ListWallets = "wallets"
-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/postWallet
type PostWallet = "wallets"
:> ReqBody '[JSON] WalletPostData
:> Post '[JSON] ApiWallet
:> PostAccepted '[JSON] ApiWallet

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/putWallet
type PutWallet = "wallets"
Expand Down Expand Up @@ -107,4 +107,4 @@ type CreateTransaction = "wallets"
:> Capture "walletId" (ApiT WalletId)
:> "transactions"
:> ReqBody '[JSON] PostTransactionData
:> Post '[JSON] ApiTransaction
:> PostAccepted '[JSON] ApiTransaction
86 changes: 11 additions & 75 deletions test/integration/Main.hs
@@ -1,7 +1,3 @@
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TypeApplications #-}


module Main where

import Prelude
Expand All @@ -14,41 +10,30 @@ import Control.Concurrent.Async
( async, cancel, link )
import Control.Monad
( void )
import Data.Aeson
( Value )
import Data.Time
( addUTCTime, defaultTimeLocale, formatTime, getCurrentTime )
import Network.HTTP.Client
( defaultManagerSettings, newManager )
import Network.HTTP.Types.Status
( status200, status404, status405 )
import Test.Hspec
( SpecWith, afterAll, beforeAll, describe, hspec, it, shouldBe )
( afterAll, beforeAll, describe, hspec )
import Test.Integration.Framework.DSL
( Context (..)
, Headers (..)
, Payload (..)
, expectResponseCode
, json
, request
)
( Context (..) )

import qualified Cardano.Wallet.Network.HttpBridgeSpec as HttpBridge
import qualified Cardano.WalletSpec as Wallet
import qualified Data.Text as T
import qualified Test.Integration.Scenario.Wallets as Wallets

main :: IO ()
main = do
hspec $ do
describe "Cardano.WalletSpec" Wallet.spec
describe "Cardano.Wallet.Network.HttpBridge" HttpBridge.spec

beforeAll startCluster $ afterAll killCluster $ do
describe "Integration test framework" dummySpec

beforeAll dummySetup $ do
describe "Test response codes" respCodesSpec
describe "Wallets API endpoint tests" Wallets.spec
where
startUpDelay :: Int
startUpDelay = 4 * 1000 * 1000 -- 4 seconds in milliseconds

-- Run a local cluster of cardano-sl nodes, a cardano-http-bridge on top and
-- a cardano wallet server connected to the bridge.
startCluster :: IO Context
Expand All @@ -66,6 +51,7 @@ main = do
link cluster
let baseURL = "http://localhost:1337/"
manager <- newManager defaultManagerSettings
threadDelay (2 * startUpDelay)
return $ Context cluster (baseURL, manager)

killCluster :: Context -> IO ()
Expand All @@ -88,58 +74,8 @@ main = do

cardanoWalletLauncher serverPort bridgePort network = Command
"cardano-wallet-launcher"
[ "--wallet-server-port", serverPort
[ "--network", network
, "--wallet-server-port", serverPort
, "--http-bridge-port", bridgePort
, "--network", network
] (threadDelay 6000000)
] (threadDelay startUpDelay)
Inherit


-- Exercise the request functions, which just fail at the moment.
dummySpec :: SpecWith Context
dummySpec = do
it "dummy spec" $ \(Context _ (url, _)) -> do
url `shouldBe` "http://localhost:1337/"

-- Temporary test setup for testing response codes
dummySetup :: IO Context
dummySetup = do
cluster <- async (return ())
let baseURL = T.pack ("http://httpbin.org")
manager <- newManager defaultManagerSettings
return $ Context cluster (baseURL, manager)

-- Exercise response codes
respCodesSpec :: SpecWith Context
respCodesSpec = do
it "GET; Response code 200" $ \ctx -> do
response <- request @Value ctx ("GET", "/get?my=arg") Default Empty
expectResponseCode @IO status200 response

it "GET; Response code 404" $ \ctx -> do
response <- request @Value ctx ("GET", "/get/nothing") Default Empty
expectResponseCode @IO status404 response

it "POST; Response code 200" $ \ctx -> do
let headers = Headers [("dummy", "header")]
let payload = Json [json| {
"addressPoolGap": 70,
"assuranceLevel": "strict",
"name": "Wallet EOS"
} |]
response <- request @Value ctx ("POST", "/post") headers payload
expectResponseCode @IO status200 response

it "POST; Response code 200" $ \ctx -> do
let headers = Headers [("dummy", "header")]
let payloadInvalid = NonJson "{\
\\"addressPoolGap: 70,\
\\"assuranceLevel\": strict,\
\\"name\": \"Wallet EOS\"\
\}"
response <- request @Value ctx ("POST", "/post") headers payloadInvalid
expectResponseCode @IO status200 response

it "POST; Response code 405" $ \ctx -> do
response <- request @Value ctx ("POST", "/get") None Empty
expectResponseCode @IO status405 response

0 comments on commit 850d764

Please sign in to comment.