@@ -19,9 +19,9 @@ def app
1919 end
2020 end
2121
22- describe 'POST /chain' do
22+ describe 'POST /api/v1/ chain' do
2323 it 'creates a new blockchain' do
24- post '/chain'
24+ post '/api/v1/ chain'
2525 expect ( last_response ) . to be_ok
2626 expect ( last_response . content_type ) . to include ( 'application/json' )
2727
@@ -31,12 +31,12 @@ def app
3131 end
3232 end
3333
34- describe 'POST /chain/:id/block' do
34+ describe 'POST /api/v1/ chain/:id/block' do
3535 let ( :blockchain ) { Blockchain . create! }
3636 let ( :block_data ) { { data : 'Test Block Data' } }
3737
3838 it 'adds a new block to the blockchain' do
39- post "/chain/#{ blockchain . id } /block" , block_data . to_json , { 'CONTENT_TYPE' => 'application/json' }
39+ post "/api/v1/ chain/#{ blockchain . id } /block" , block_data . to_json , { 'CONTENT_TYPE' => 'application/json' }
4040
4141 expect ( last_response ) . to be_ok
4242 expect ( last_response . content_type ) . to include ( 'application/json' )
@@ -45,21 +45,23 @@ def app
4545 expect ( json [ 'chain_id' ] ) . to eq ( blockchain . id . to_s )
4646 expect ( json [ 'block_id' ] ) . not_to be_nil
4747 expect ( json [ 'block_hash' ] ) . not_to be_nil
48+ expect ( json [ 'nonce' ] ) . not_to be_nil
49+ expect ( json [ 'difficulty' ] ) . to eq ( 2 )
4850 end
4951
5052 it 'returns error when chain not found' do
51- post '/chain/invalid_id/block' , block_data . to_json , { 'CONTENT_TYPE' => 'application/json' }
53+ post '/api/v1/ chain/invalid_id/block' , block_data . to_json , { 'CONTENT_TYPE' => 'application/json' }
5254 expect ( last_response . status ) . to eq ( 500 ) # rubocop:disable RSpecRails/HaveHttpStatus
5355 end
5456 end
5557
56- describe 'POST /chain/:id/block/:block_id/valid' do
58+ describe 'POST /api/v1/ chain/:id/block/:block_id/valid' do
5759 let ( :blockchain ) { Blockchain . create! }
5860 let ( :block_data ) { { data : 'Validation Test Data' } }
5961 let! ( :block ) { blockchain . add_block ( block_data [ :data ] ) }
6062
6163 it 'validates block with correct data' do
62- post "/chain/#{ blockchain . id } /block/#{ block . id } /valid" ,
64+ post "/api/v1/ chain/#{ blockchain . id } /block/#{ block . id } /valid" ,
6365 block_data . to_json ,
6466 { 'CONTENT_TYPE' => 'application/json' }
6567
@@ -70,7 +72,7 @@ def app
7072
7173 it 'invalidates block with incorrect data' do
7274 invalid_data = { data : 'Wrong Data' }
73- post "/chain/#{ blockchain . id } /block/#{ block . id } /valid" ,
75+ post "/api/v1/ chain/#{ blockchain . id } /block/#{ block . id } /valid" ,
7476 invalid_data . to_json ,
7577 { 'CONTENT_TYPE' => 'application/json' }
7678
@@ -79,4 +81,24 @@ def app
7981 expect ( json [ 'valid' ] ) . to be false
8082 end
8183 end
84+
85+ describe 'GET /api/v1/chain/:id/block/:block_id' do
86+ let ( :blockchain ) { Blockchain . create! }
87+ let ( :block_data ) { 'GET Block Test Data' }
88+ let! ( :block ) { blockchain . add_block ( block_data ) }
89+
90+ it 'retrieves block details with mining information' do
91+ get "/api/v1/chain/#{ blockchain . id } /block/#{ block . id } "
92+
93+ expect ( last_response ) . to be_ok
94+ json = JSON . parse ( last_response . body )
95+
96+ expect ( json [ 'chain_id' ] ) . to eq ( blockchain . id . to_s )
97+ expect ( json [ 'block' ] [ 'id' ] ) . to eq ( block . id . to_s )
98+ expect ( json [ 'block' ] [ 'data' ] ) . to eq ( block_data )
99+ expect ( json [ 'block' ] [ 'nonce' ] ) . not_to be_nil
100+ expect ( json [ 'block' ] [ 'difficulty' ] ) . to eq ( 2 )
101+ expect ( json [ 'block' ] [ 'valid_hash' ] ) . to be true
102+ end
103+ end
82104end
0 commit comments