Skip to content

Commit

Permalink
test cita sync api
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Jun 19, 2018
1 parent 5793798 commit f8fece7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ group :test do
gem 'selenium-webdriver'
# Easy installation and use of chromedriver to run system tests with Chrome
gem 'chromedriver-helper'

# mock http request
gem 'webmock', '~> 3.4', '>= 3.4.2'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ GEM
execjs
coffee-script-source (1.12.2)
concurrent-ruby (1.0.5)
crack (0.4.3)
safe_yaml (~> 1.0.0)
crass (1.0.4)
dotenv (2.4.0)
dotenv-rails (2.4.0)
Expand All @@ -86,6 +88,7 @@ GEM
ffi (1.9.25)
globalid (0.4.1)
activesupport (>= 4.2.0)
hashdiff (0.3.7)
i18n (1.0.1)
concurrent-ruby (~> 1.0)
io-like (0.3.0)
Expand Down Expand Up @@ -156,6 +159,7 @@ GEM
ffi (>= 0.5.0, < 2)
ruby_dep (1.5.0)
rubyzip (1.2.1)
safe_yaml (1.0.4)
sass (3.5.6)
sass-listen (~> 4.0.0)
sass-listen (4.0.0)
Expand Down Expand Up @@ -197,6 +201,10 @@ GEM
activemodel (>= 5.0)
bindex (>= 0.4.0)
railties (>= 5.0)
webmock (3.4.2)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff
websocket-driver (0.7.0)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
Expand Down Expand Up @@ -231,6 +239,7 @@ DEPENDENCIES
tzinfo-data
uglifier (>= 1.3.0)
web-console (>= 3.3.0)
webmock (~> 3.4, >= 3.4.2)

RUBY VERSION
ruby 2.4.4p296
Expand Down
44 changes: 41 additions & 3 deletions test/models/cita_sync/api_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
require 'test_helper'

class CitaSync::ApiTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
setup do
ENV["CITA_URL"] = "http://www.cita.com"

stub_request(:post, "www.cita.com").
with(body: hash_including({ method: "cita_blockNumber" }), headers: { "Content-Type": "application/json" }).
to_return(status: 200, body: { jsonrpc: "2.0", id: 83, result: "0x7781" }.to_json)


result = { "version" => 0,
"hash" => "0xa18f9c384107d9a4fcd2fae656415928bd921047519fea5650cba394f6b6142b",
"header" =>
{ "timestamp" => 1528702183591,
"prevHash" => "0xda8991b9cbc7f7bc56e94abbd7056dffc501603a4ab6bcaa7e2ed08b3e58e554",
"number" => "0x1",
"stateRoot" => "0x048523e8326427968d05673210cc77a8f76e60d0b9170d1bdc1d49c131da9c85",
"transactionsRoot" => "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"receiptsRoot" => "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"gasUsed" => "0x0",
"proof" => nil,
"proposer" => "0x91827976af27e1fd405469b00dc8d3b0ea2203f6" },
"body" => { "transactions" => [] } }
stub_request(:post, "www.cita.com").
with(body: hash_including({ method: "cita_getBlockByNumber", params: ["0x1", true] }), headers: { "Content-Type": "application/json" }).
to_return(status: 200, body: { jsonrpc: "2.0", id: 83, result: result }.to_json)

end

test "cita_blockNumber" do
resp = CitaSync::Api.cita_block_number

assert resp["jsonrpc"], "2.0"
assert resp["id"], 83
assert resp["result"], "0x7781"
end

test "cita_getBlockByNumber" do
resp = CitaSync::Api.cita_get_block_by_number(["0x1", true])

assert resp.dig("result", "hash"), "0xa18f9c384107d9a4fcd2fae656415928bd921047519fea5650cba394f6b6142b"
end

end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
require 'webmock/minitest'

class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
Expand Down

0 comments on commit f8fece7

Please sign in to comment.