Skip to content

Commit 6309702

Browse files
authored
stbx-service-rest: Create and enable integration tests #271 (#277)
* stbx-service-rest: Create and enable client-server integration tests. #271 * stbx-service-rest: Fix permissions on CI test suite. #271 * stbx-service-rest: Create PureScript test suite for tx API POSTs and GETs. #271 * Chain tests with && so one failure will fail the whole suite. #271 * Make sure projects in test suite are built before they are tested. #271 * stbx-service-rest: Add index-test.js test runner. #271
1 parent f214105 commit 6309702

File tree

9 files changed

+136
-15
lines changed

9 files changed

+136
-15
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ language: node_js
22
node_js: stable
33
script:
44
- scripts/build.sh
5+
- scripts/test.sh

scripts/build.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
cd studio && npm install && npm run build
2-
cd ..
3-
cd stbx-service-rest && npm install && npm run build
4-
cd ..
5-
cd halogen-petrinet-editor-example && npm install && npm run build
1+
cd studio && npm install && npm run build && \
2+
cd .. && \
3+
cd stbx-core && npm install && npm run build && \
4+
cd .. && \
5+
cd stbx-service-rest && npm install && npm run build && \
6+
cd .. && \
7+
cd stbx-lang && npm install && npm run build && \
8+
cd .. && \
9+
cd halogen-petrinet-editor-example && npm install && npm run build && \
610
cd ..

scripts/test.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cd stbx-core && npm run test && \
2+
cd .. && \
3+
cd stbx-service-rest && npm run test && \
4+
cd .. && \
5+
cd stbx-lang && npm run test && \
6+
cd ..

stbx-client-rest/src/Statebox/Client.purs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@ import Affjax as Affjax
55
import Affjax (URL, Response)
66
import Affjax.ResponseFormat as ResponseFormat
77
import Affjax.ResponseFormat (ResponseFormatError)
8+
import Affjax.RequestBody as RequestBody
89
import Control.Coroutine (Producer)
910
import Control.Coroutine.Aff (emit, close, produceAff, Emitter)
1011
import Control.Monad.Rec.Class (Step(Loop, Done), tailRecM)
1112
import Control.Monad.Free.Trans (hoistFreeT)
1213
import Data.Argonaut.Core (Json)
14+
import Data.Argonaut.Encode (encodeJson)
1315
import Data.Either (Either(..))
1416
import Data.Either.Nested (type (\/))
1517
import Data.Profunctor.Choice ((|||), (+++))
16-
import Data.HTTP.Method (Method(GET))
18+
import Data.HTTP.Method (Method(GET, POST))
1719
import Effect.Aff.Class (class MonadAff, liftAff)
1820
import Effect.Aff (Aff)
1921

2022
import Statebox.Core.Transaction (HashTx, TxId, TxSum(..), evalTxSum, isUberRootHash, attachTxId)
2123
import Statebox.Core.Transaction.Codec (decodeTxTxSum, DecodingError(..))
24+
import Statebox.Core.Types (HexStr)
2225

2326

2427
-- | A convenience function for processing API responses.
@@ -104,3 +107,13 @@ fetchAndEmitTxStep emitter apiBaseUrl hash = liftAff $ do
104107
pure $ Loop x.previous)
105108
tx
106109
)
110+
111+
--------------------------------------------------------------------------------
112+
113+
postTransactionHex :: URL -> HexStr -> Aff (Response (ResponseFormatError \/ Json))
114+
postTransactionHex apiBaseUrl txHex =
115+
Affjax.request $ Affjax.defaultRequest { url = apiBaseUrl <> "/tx"
116+
, method = Left POST
117+
, responseFormat = ResponseFormat.json
118+
, content = pure $ RequestBody.json $ encodeJson {tx: txHex}
119+
}

stbx-service-rest/index-test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
require('xhr2')
3+
require('./output/Test.Main').main()

stbx-service-rest/package.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,26 @@
77
"postinstall": "spago install",
88
"init": "spago init",
99
"build": "spago build",
10-
"test": "sh scripts/test.sh",
1110
"repl": "spago repl",
1211
"docs": "spago docs",
1312
"watch": "spago build --watch",
14-
"serve": "node index.js"
13+
"serve": "node index.js",
14+
"test": "npm run test:ps && npm run test:sh",
15+
"test:sh": "npm-run-all --print-label --race --parallel 'serve' 'test:waitThenRequests:sh'",
16+
"test:requests:sh": "sh scripts/test.sh",
17+
"test:waitThenRequests:sh": "npm run wait && npm run test:requests:sh",
18+
"test:ps": "spago build && npm-run-all --print-label --race --parallel 'serve' 'test:waitThenRequests:ps'",
19+
"test:requests:ps": "node index-test.js",
20+
"test:waitThenRequests:ps": "npm run wait && npm run test:requests:ps",
21+
"wait": "sleep 1"
1522
},
1623
"author": "Erik Post <erik@shinsetsu.nl>",
1724
"license": "ISC",
1825
"devDependencies": {
26+
"npm-run-all": "^4.1.5",
1927
"purescript": "^0.13.3",
20-
"spago": "^0.9.0"
28+
"spago": "^0.9.0",
29+
"xhr2": "^0.2.0"
2130
},
2231
"dependencies": {
2332
"@statebox/stbx-js": "0.0.31",

stbx-service-rest/scripts/test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ curl -X GET \
44
http://localhost:8080/tx \
55
-H 'Cache-Control: no-cache'
66

7-
--------------------------------------------------------------------------------
7+
################################################################################
88

99
echo "\n\ntest: POSTing root tx\n"
1010

@@ -66,7 +66,7 @@ curl -X POST \
6666

6767
sleep 1
6868

69-
--------------------------------------------------------------------------------
69+
################################################################################
7070

7171
echo "\n\ntest: GETting /tx\n"
7272
curl -X GET \

stbx-service-rest/test/Common.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Test.Common where
2+
3+
import Prelude
4+
import Control.Monad.Error.Class (class MonadThrow)
5+
import Effect.Exception (Error)
6+
7+
succeed :: forall m. MonadThrow Error m => m Unit
8+
succeed = pure unit

stbx-service-rest/test/Main.purs

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,86 @@ module Test.Main where
22

33
import Prelude
44

5+
import Affjax (URL)
6+
import Affjax.ResponseFormat (ResponseFormatError(..))
7+
import Data.Either (Either(..))
8+
import Debug.Trace (spy)
9+
import Effect.Aff (Fiber, launchAff)
510
import Effect (Effect)
6-
import Effect.Console (log)
11+
import Test.Spec (Spec, describe, it, pending)
12+
import Test.Spec.Assertions (shouldEqual, shouldSatisfy, fail)
13+
import Test.Spec.Runner (runSpec)
14+
import Test.Spec.Reporter.Console (consoleReporter)
715

8-
main :: Effect Unit
9-
main = do
10-
log "No PureScript tests defined yet, please see the ./scripts for tests."
16+
import Statebox.Core.Transaction (TxId)
17+
import Statebox.Core.Transaction.Codec (DecodingError(..))
18+
import Statebox.Client as Stbx
19+
import Statebox.Client (evalTransactionResponse)
20+
21+
import Test.Common
22+
23+
endpointUrl :: URL
24+
endpointUrl = "http://127.0.0.1:8080"
25+
26+
main :: Effect (Fiber Unit)
27+
main = launchAff $ runSpec [consoleReporter] do
28+
29+
-- first we post our example transactions to the API
30+
postExampleTransactionsSpec
31+
32+
-- then we try to read them back in
33+
getExampleTransactionsSpec
34+
35+
36+
postExampleTransactionsSpec :: Spec Unit
37+
postExampleTransactionsSpec =
38+
describe "Statebox transaction API HTTP service" do
39+
it "should respond to POST <root tx> correctly" do
40+
res <- Stbx.postTransactionHex endpointUrl "0a0022200a1e47756172616e746565642d456e7472616e63652d546f6b656e2e74657374"
41+
let dump1 = spy "POST response: " res
42+
todo
43+
it "should respond to POST <wiring tx> correctly" do
44+
res <- Stbx.postTransactionHex endpointUrl "0a20dce4021c8f117e89c479665f6d61ff650b150af375d6498b593da6afa8d2ca9f1afa010add010a0a70726976696c656467651001100010021000100210001006100010011000100310001003100010011000100210001004100010031000100510001004100010051000100110001005100010021000100510001006100010021000100610001003100010061000100510001000100310001a036275791a07666f7253616c651a05626c6f636b1a07756e626c6f636b1a047363616e1a086e6f74536f6c64321a0873686f774f7665721a076e6f74536f6c641a066e6f53686f771a04627579271a076275794261636b1a096e6f745265736f6c641a0663726561746512160a046d61696e10011801220a70726976696c656467651800"
45+
let dump1 = spy "POST response: " res
46+
todo
47+
it "should respond to POST <0th firing tx (the 'execution')> correctly" do
48+
res <- Stbx.postTransactionHex endpointUrl "0a20dce4021cacc5f351d54402799977d7e4f7b86805359aec724805c80ec0b4d546120710001a03aa0003"
49+
let dump1 = spy "POST response: " res
50+
todo
51+
it "should respond to POST <1st firing tx> correctly" do
52+
res <- Stbx.postTransactionHex endpointUrl "0a20dce4021c1447c0b50a5ce982dd4e78650ca3cc642004b4408eac0264da1ca5b812240a20dce4021c1447c0b50a5ce982dd4e78650ca3cc642004b4408eac0264da1ca5b81004"
53+
let dump1 = spy "POST response: " res
54+
todo
55+
it "should respond to POST <2nd firing tx> correctly" do
56+
res <- Stbx.postTransactionHex endpointUrl "0a20dce4021cd649d3a9d1f69832f26739c1d81c873ca5f343ec2dd92d335adfc805122a0a20dce4021c1447c0b50a5ce982dd4e78650ca3cc642004b4408eac0264da1ca5b810011a04111aaa11"
57+
let dump1 = spy "POST response: " res
58+
todo
59+
it "should respond to POST <3rd firing tx> correctly" do
60+
res <- Stbx.postTransactionHex endpointUrl "0a20dce4021c15fda2dfd9ec2ef4413b9e5a4ac5cbd8def33c0ca2c071f75a71464b122b0a20dce4021c1447c0b50a5ce982dd4e78650ca3cc642004b4408eac0264da1ca5b810051a05222bbbb222"
61+
let dump1 = spy "POST response: " res
62+
todo
63+
64+
getExampleTransactionsSpec :: Spec Unit
65+
getExampleTransactionsSpec =
66+
describe "Statebox transaction API HTTP service" do
67+
pending "TODO: GETting root transaction fails"
68+
-- requestTransactionSpec "root" "zFsGM27VMNWZne1SSkWnDQTzr6TdjmsKpbxGkJKKaEC8e"
69+
requestTransactionSpec "wiring" "zFsGM27o59f9Lu8bWjNHBG7Wbq5iftQA6uDt14zRdjCrH"
70+
requestTransactionSpec "firing 0 (execution)" "zFsGM26E6xAuYMXox2zMGUChk3HmbEAMGXBiWG3UL7KF5"
71+
requestTransactionSpec "firing 1" "zFsGM28DqZKjjGbfCEsjsXTj8xJAqWaBXpDSc1CqR6ihi"
72+
requestTransactionSpec "firing 2" "zFsGM26F88jGH8HtpdSCBdgRWSVJEWbyDoH1HRRWXTZyC"
73+
requestTransactionSpec "firing 3" "zFsGM27HNS66qmGp1Y1STK48FUA1F12VHLRB51RGWNYWV"
74+
75+
--------------------------------------------------------------------------------
76+
77+
requestTransactionSpec :: String -> TxId -> Spec Unit
78+
requestTransactionSpec txDescription requestedHash =
79+
it ("should respond to GET /tx/" <> requestedHash <> " for " <> txDescription <> " correctly") do
80+
res <- Stbx.requestTransaction endpointUrl requestedHash
81+
res # evalTransactionResponse
82+
(\(ResponseFormatError e obj) -> fail $ "ResponseFormatError: " <> show e) -- TODO spy obj?
83+
(\(DecodingError e) -> fail $ "DecodingError: " <> e)
84+
(\{id, tx} -> succeed) -- TODO more checks
85+
86+
-- TODO for now
87+
todo = succeed

0 commit comments

Comments
 (0)