certichain

Blockchain built from scratch for verifying document's legitimacy.
Log | Files | Refs | README | LICENSE

blocks_test.go (4644B)


      1 // Copyright 2026 Shivakumar (Certichain)
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package handlers
     16 
     17 import (
     18 	"encoding/hex"
     19 	"net/http"
     20 	"net/http/httptest"
     21 	"strconv"
     22 	"testing"
     23 
     24 	"certichain/api/dto"
     25 	"certichain/internal/crypto"
     26 	"certichain/internal/transaction"
     27 )
     28 
     29 func TestGetBlock(t *testing.T) {
     30 	t.Parallel()
     31 
     32 	deps := newTestDeps(t)
     33 	handler := GetBlock(deps.Chain)
     34 
     35 	did, _, priv := mustRegisterInstitution(t, deps.Registry)
     36 	root := crypto.HashBytes([]byte("TEST"))
     37 
     38 	tx, err := transaction.New(did, root[:])
     39 	if err != nil {
     40 		t.Fatalf("api: failed to generate transaction: %v", err)
     41 	}
     42 	if err := tx.Sign(priv); err != nil {
     43 		t.Fatalf("api: failed to sign transaction: %v", err)
     44 	}
     45 
     46 	block, err := deps.Chain.AddBlock([]*transaction.Transaction{tx})
     47 	if err != nil {
     48 		t.Fatalf("api: failed to add block: %v", err)
     49 	}
     50 
     51 	t.Run("valid transaction", func(t *testing.T) {
     52 		t.Parallel()
     53 
     54 		req := httptest.NewRequest(http.MethodGet, "/v1/blocks", nil)
     55 		req.SetPathValue("hash", block.Hash.String())
     56 		rec := httptest.NewRecorder()
     57 
     58 		handler(rec, req)
     59 
     60 		var resp dto.BlockResponse
     61 		decodeBody(t, rec, &resp)
     62 		if rec.Code != http.StatusOK {
     63 			t.Errorf("status mismatch: want = %d, got = %d", http.StatusOK, rec.Code)
     64 		}
     65 	})
     66 
     67 	t.Run("tampered transaction", func(t *testing.T) {
     68 		t.Parallel()
     69 
     70 		fakeHash := crypto.HashBytes([]byte("FAKE_HASH"))
     71 
     72 		req := httptest.NewRequest(http.MethodGet, "/v1/blocks", nil)
     73 		req.SetPathValue("hash", hex.EncodeToString(fakeHash[:]))
     74 		rec := httptest.NewRecorder()
     75 
     76 		handler(rec, req)
     77 
     78 		var resp dto.BlockResponse
     79 		decodeBody(t, rec, &resp)
     80 		if rec.Code != http.StatusNotFound {
     81 			t.Errorf("status mismatch: want = %d, got = %d", http.StatusNotFound, rec.Code)
     82 		}
     83 
     84 	})
     85 }
     86 
     87 func TestGetBlockByHeight(t *testing.T) {
     88 	t.Parallel()
     89 
     90 	deps := newTestDeps(t)
     91 	handler := GetBlockByHeight(deps.Chain)
     92 
     93 	did, _, priv := mustRegisterInstitution(t, deps.Registry)
     94 	root := crypto.HashBytes([]byte("TEST"))
     95 
     96 	tx, err := transaction.New(did, root[:])
     97 	if err != nil {
     98 		t.Fatalf("api: failed to generate transaction: %v", err)
     99 	}
    100 	if err := tx.Sign(priv); err != nil {
    101 		t.Fatalf("api: failed to sign transaction: %v", err)
    102 	}
    103 
    104 	_, err = deps.Chain.AddBlock([]*transaction.Transaction{tx})
    105 	if err != nil {
    106 		t.Fatalf("api: failed to add block: %v", err)
    107 	}
    108 
    109 	tests := []struct {
    110 		name       string
    111 		height     int
    112 		wantStatus int
    113 	}{
    114 		{
    115 			name:       "valid height",
    116 			height:     1,
    117 			wantStatus: http.StatusOK,
    118 		},
    119 		{
    120 			name:       "height not found",
    121 			height:     42,
    122 			wantStatus: http.StatusNotFound,
    123 		},
    124 		{
    125 			name:       "negative height",
    126 			height:     -1,
    127 			wantStatus: http.StatusBadRequest,
    128 		},
    129 	}
    130 	for _, tt := range tests {
    131 		t.Run(tt.name, func(t *testing.T) {
    132 			t.Parallel()
    133 
    134 			req := httptest.NewRequest(http.MethodGet, "/v1/blocks/height", nil)
    135 			req.SetPathValue("height", strconv.Itoa(tt.height))
    136 
    137 			rec := httptest.NewRecorder()
    138 
    139 			handler(rec, req)
    140 
    141 			var resp dto.BlockResponse
    142 			decodeBody(t, rec, &resp)
    143 
    144 			if rec.Code != tt.wantStatus {
    145 				t.Errorf("status mismatch: want = %d, got = %d", tt.wantStatus, rec.Code)
    146 			}
    147 
    148 		})
    149 
    150 	}
    151 }
    152 
    153 func TestGetBlockLatest(t *testing.T) {
    154 	t.Parallel()
    155 
    156 	deps := newTestDeps(t)
    157 	handler := GetLatestBlock(deps.Chain)
    158 
    159 	did, _, priv := mustRegisterInstitution(t, deps.Registry)
    160 	root := crypto.HashBytes([]byte("TEST"))
    161 
    162 	tx, err := transaction.New(did, root[:])
    163 	if err != nil {
    164 		t.Fatalf("api: failed to generate transaction: %v", err)
    165 	}
    166 	if err := tx.Sign(priv); err != nil {
    167 		t.Fatalf("api: failed to sign transaction: %v", err)
    168 	}
    169 
    170 	_, err = deps.Chain.AddBlock([]*transaction.Transaction{tx})
    171 	if err != nil {
    172 		t.Fatalf("api: failed to add block: %v", err)
    173 	}
    174 
    175 	req := httptest.NewRequest(http.MethodGet, "/v1/blocks/latest", nil)
    176 	rec := httptest.NewRecorder()
    177 
    178 	handler(rec, req)
    179 
    180 	var resp dto.BlockResponse
    181 	decodeBody(t, rec, &resp)
    182 
    183 	if rec.Code != http.StatusOK {
    184 		t.Errorf("status mismatch: want = %d, got = %d", http.StatusOK, rec.Code)
    185 	}
    186 
    187 	if resp.Height != deps.Chain.Height() {
    188 		t.Errorf("height mismatch: want = %d, got = %d", deps.Chain.Height(), resp.Height)
    189 	}
    190 }