certichain

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

commit b97fb81addfd83843ebb4a3611c87e049cf6c2f0
parent 6c6f4ea6943adb16edabfd6b55b06cbae13d2856
Author: Shivakumar <[email protected]>
Date:   Sun,  9 Aug 2026 03:38:11 +0530

tests(api): add tests to ever other handlers

Signed-off-by: Shivakumar <[email protected]>

Diffstat:
Aapi/handlers/blocks_test.go | 190+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aapi/handlers/transactions_test.go | 158+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aapi/handlers/verify_test.go | 89+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 437 insertions(+), 0 deletions(-)

diff --git a/api/handlers/blocks_test.go b/api/handlers/blocks_test.go @@ -0,0 +1,190 @@ +// Copyright 2026 Shivakumar (Certichain) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package handlers + +import ( + "encoding/hex" + "net/http" + "net/http/httptest" + "strconv" + "testing" + + "certichain/api/dto" + "certichain/internal/crypto" + "certichain/internal/transaction" +) + +func TestGetBlock(t *testing.T) { + t.Parallel() + + deps := newTestDeps(t) + handler := GetBlock(deps.Chain) + + did, _, priv := mustRegisterInstitution(t, deps.Registry) + root := crypto.HashBytes([]byte("TEST")) + + tx, err := transaction.New(did, root[:]) + if err != nil { + t.Fatalf("api: failed to generate transaction: %v", err) + } + if err := tx.Sign(priv); err != nil { + t.Fatalf("api: failed to sign transaction: %v", err) + } + + block, err := deps.Chain.AddBlock([]*transaction.Transaction{tx}) + if err != nil { + t.Fatalf("api: failed to add block: %v", err) + } + + t.Run("valid transaction", func(t *testing.T) { + t.Parallel() + + req := httptest.NewRequest(http.MethodGet, "/v1/blocks", nil) + req.SetPathValue("hash", block.Hash.String()) + rec := httptest.NewRecorder() + + handler(rec, req) + + var resp dto.BlockResponse + decodeBody(t, rec, &resp) + if rec.Code != http.StatusOK { + t.Errorf("status mismatch: want = %d, got = %d", http.StatusOK, rec.Code) + } + }) + + t.Run("tampered transaction", func(t *testing.T) { + t.Parallel() + + fakeHash := crypto.HashBytes([]byte("FAKE_HASH")) + + req := httptest.NewRequest(http.MethodGet, "/v1/blocks", nil) + req.SetPathValue("hash", hex.EncodeToString(fakeHash[:])) + rec := httptest.NewRecorder() + + handler(rec, req) + + var resp dto.BlockResponse + decodeBody(t, rec, &resp) + if rec.Code != http.StatusNotFound { + t.Errorf("status mismatch: want = %d, got = %d", http.StatusNotFound, rec.Code) + } + + }) +} + +func TestGetBlockByHeight(t *testing.T) { + t.Parallel() + + deps := newTestDeps(t) + handler := GetBlockByHeight(deps.Chain) + + did, _, priv := mustRegisterInstitution(t, deps.Registry) + root := crypto.HashBytes([]byte("TEST")) + + tx, err := transaction.New(did, root[:]) + if err != nil { + t.Fatalf("api: failed to generate transaction: %v", err) + } + if err := tx.Sign(priv); err != nil { + t.Fatalf("api: failed to sign transaction: %v", err) + } + + _, err = deps.Chain.AddBlock([]*transaction.Transaction{tx}) + if err != nil { + t.Fatalf("api: failed to add block: %v", err) + } + + tests := []struct { + name string + height int + wantStatus int + }{ + { + name: "valid height", + height: 1, + wantStatus: http.StatusOK, + }, + { + name: "height not found", + height: 42, + wantStatus: http.StatusNotFound, + }, + { + name: "negative height", + height: -1, + wantStatus: http.StatusBadRequest, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + req := httptest.NewRequest(http.MethodGet, "/v1/blocks/height", nil) + req.SetPathValue("height", strconv.Itoa(tt.height)) + + rec := httptest.NewRecorder() + + handler(rec, req) + + var resp dto.BlockResponse + decodeBody(t, rec, &resp) + + if rec.Code != tt.wantStatus { + t.Errorf("status mismatch: want = %d, got = %d", tt.wantStatus, rec.Code) + } + + }) + + } +} + +func TestGetBlockLatest(t *testing.T) { + t.Parallel() + + deps := newTestDeps(t) + handler := GetLatestBlock(deps.Chain) + + did, _, priv := mustRegisterInstitution(t, deps.Registry) + root := crypto.HashBytes([]byte("TEST")) + + tx, err := transaction.New(did, root[:]) + if err != nil { + t.Fatalf("api: failed to generate transaction: %v", err) + } + if err := tx.Sign(priv); err != nil { + t.Fatalf("api: failed to sign transaction: %v", err) + } + + _, err = deps.Chain.AddBlock([]*transaction.Transaction{tx}) + if err != nil { + t.Fatalf("api: failed to add block: %v", err) + } + + req := httptest.NewRequest(http.MethodGet, "/v1/blocks/latest", nil) + rec := httptest.NewRecorder() + + handler(rec, req) + + var resp dto.BlockResponse + decodeBody(t, rec, &resp) + + if rec.Code != http.StatusOK { + t.Errorf("status mismatch: want = %d, got = %d", http.StatusOK, rec.Code) + } + + if resp.Height != deps.Chain.Height() { + t.Errorf("height mismatch: want = %d, got = %d", deps.Chain.Height(), resp.Height) + } +} diff --git a/api/handlers/transactions_test.go b/api/handlers/transactions_test.go @@ -0,0 +1,158 @@ +// Copyright 2026 Shivakumar (Certichain) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package handlers + +import ( + "bytes" + "encoding/hex" + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "certichain/api/dto" + "certichain/internal/crypto" + "certichain/internal/transaction" + // "certichain/internal/crypto" +) + +func TestSubmitTransaction(t *testing.T) { + t.Parallel() + + deps := newTestDeps(t) + handler := SubmitTransaction(deps.Mempool, deps.Registry) + did, _, priv := mustRegisterInstitution(t, deps.Registry) + resp := mustSubmitTransaction(t, did, priv, make([]byte, 32)) + + unregResp := resp + unregResp.IssuerDID = "did:certchain:0x" + strings.Repeat("a", 64) + + tests := []struct { + name string + tx dto.SubmitTransactionRequest + statusCode int + }{ + { + name: "registered", + tx: resp, + statusCode: http.StatusCreated, + }, + { + name: "tampered", + tx: unregResp, + statusCode: http.StatusBadRequest, + }, + { + name: "duplicate", + tx: resp, + statusCode: http.StatusConflict, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + body, _ := json.Marshal(tt.tx) + + req := httptest.NewRequest(http.MethodPost, "/v1/transactions", bytes.NewReader(body)) + rec := httptest.NewRecorder() + + handler(rec, req) + + if rec.Code != tt.statusCode { + t.Errorf("status mismatch: want = %d, got = %d", tt.statusCode, rec.Code) + } + + var resp dto.SubmitTransactionResponse + decodeBody(t, rec, &resp) + + if tt.statusCode == http.StatusAccepted && resp.Status != "pending" { + t.Errorf("status mismatch: want = pending, got = %s", resp.Status) + } + }) + } +} + +func TestGetTransaction(t *testing.T) { + t.Parallel() + + deps := newTestDeps(t) + handler := GetTransaction(deps.Mempool, deps.Chain) + + did, _, priv := mustRegisterInstitution(t, deps.Registry) + root := crypto.HashBytes([]byte("TEST")) + + tests := []struct { + name string + call func(tx *transaction.Transaction) + wantStatus string + }{ + { + name: "mempool add", + call: func(tx *transaction.Transaction) { + if err := deps.Mempool.Add(tx); err != nil { + t.Fatalf("api: failed to add tx to mempool: %v", err) + } + + }, + wantStatus: "pending", + }, + { + name: "blockchian add", + call: func(tx *transaction.Transaction) { + if _, err := deps.Chain.AddBlock([]*transaction.Transaction{tx}); err != nil { + t.Fatalf("api: failed to add tx to blockchain: %v", err) + } + }, + wantStatus: "confirmed", + }, + { + name: "not found", + call: func(tx *transaction.Transaction) { tx = nil }, + wantStatus: "", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + tx, err := transaction.New(did, root[:]) + if err != nil { + t.Fatalf("api: failed to generate transaction: %v", err) + } + + if err := tx.Sign(priv); err != nil { + t.Fatalf("api: failed to sign tx: %v", err) + } + + tt.call(tx) + + req := httptest.NewRequest(http.MethodGet, "/v1/transactions", nil) + req.SetPathValue("tx_id", hex.EncodeToString(tx.TxID)) + rec := httptest.NewRecorder() + + handler(rec, req) + + var resp dto.GetTransactionResponse + decodeBody(t, rec, &resp) + + if resp.Status != tt.wantStatus { + t.Errorf("status mismatch: want = %s, got = %s", tt.wantStatus, resp.Status) + } + }) + } + +} diff --git a/api/handlers/verify_test.go b/api/handlers/verify_test.go @@ -0,0 +1,89 @@ +// Copyright 2026 Shivakumar (Certichain) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package handlers + +import ( + "encoding/hex" + "net/http" + "net/http/httptest" + "testing" + + "certichain/api/dto" + "certichain/internal/crypto" + "certichain/internal/transaction" +) + +func TestVerify(t *testing.T) { + t.Parallel() + + deps := newTestDeps(t) + handler := Verify(deps.Chain, deps.Registry) + + did, _, priv := mustRegisterInstitution(t, deps.Registry) + root := crypto.HashBytes([]byte("TEST")) + + tx, err := transaction.New(did, root[:]) + if err != nil { + t.Fatalf("api: failed to generate transaction: %v", err) + } + if err := tx.Sign(priv); err != nil { + t.Fatalf("api: failed to sign transaction: %v", err) + } + + _, err = deps.Chain.AddBlock([]*transaction.Transaction{tx}) + if err != nil { + t.Fatalf("api: failed to add block: %v", err) + } + + fakeTx := tx.Clone() + fakeTx.TxID = make([]byte, 32) + + tests := []struct { + name string + tx *transaction.Transaction + wantStatus int + }{ + { + name: "valid transaction", + tx: tx, + wantStatus: http.StatusOK, + }, + { + name: "tampered transaction", + tx: fakeTx, + wantStatus: http.StatusNotFound, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + req := httptest.NewRequest(http.MethodGet, "/v1/verify/", nil) + req.SetPathValue("tx_id", hex.EncodeToString(tt.tx.TxID)) + rec := httptest.NewRecorder() + + handler(rec, req) + + var resp dto.VerifyResponse + decodeBody(t, rec, &resp) + + if rec.Code != tt.wantStatus { + t.Errorf("status mismatch: want = %d, got = %d", tt.wantStatus, rec.Code) + } + + }) + } +}