certichain

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

helpers_test.go (2699B)


      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 	"crypto/ed25519"
     19 	"encoding/hex"
     20 	"encoding/json"
     21 	"net/http/httptest"
     22 	"testing"
     23 
     24 	"certichain/api/dto"
     25 	"certichain/internal/blockchain"
     26 	"certichain/internal/crypto"
     27 	"certichain/internal/mempool"
     28 	"certichain/internal/registry"
     29 	"certichain/internal/storage"
     30 	"certichain/internal/transaction"
     31 )
     32 
     33 type testDeps struct {
     34 	Store    storage.Store
     35 	Registry *registry.Registry
     36 	Mempool  *mempool.Mempool
     37 	Chain    *blockchain.Blockchain
     38 }
     39 
     40 func newTestDeps(t *testing.T) *testDeps {
     41 	t.Helper()
     42 
     43 	store := storage.NewMemoryStore()
     44 	reg := registry.New(store)
     45 	mem := mempool.New()
     46 
     47 	bc, err := blockchain.New(store, blockchain.WithKeyResolver(reg))
     48 	if err != nil {
     49 		t.Fatalf("api: blockchain creation: %v", err)
     50 	}
     51 
     52 	return &testDeps{
     53 		Store:    store,
     54 		Registry: reg,
     55 		Mempool:  mem,
     56 		Chain:    bc,
     57 	}
     58 }
     59 
     60 func mustRegisterInstitution(t *testing.T, reg *registry.Registry) (
     61 	string,
     62 	ed25519.PublicKey,
     63 	ed25519.PrivateKey,
     64 ) {
     65 	t.Helper()
     66 
     67 	pub, priv, err := crypto.GenerateKeyPair()
     68 	if err != nil {
     69 		t.Fatalf("api: failed to generate keypair: %v", err)
     70 	}
     71 
     72 	did, err := reg.Register(pub)
     73 	if err != nil {
     74 		t.Fatalf("api: failed to Register pub: %v", err)
     75 	}
     76 
     77 	return did, pub, priv
     78 }
     79 
     80 func mustSubmitTransaction(
     81 	t *testing.T,
     82 	did string,
     83 	priv ed25519.PrivateKey,
     84 	root []byte,
     85 ) dto.SubmitTransactionRequest {
     86 	t.Helper()
     87 
     88 	tx, err := transaction.New(did, root)
     89 	if err != nil {
     90 		t.Fatalf("api: failed to create transaction: %v", err)
     91 	}
     92 	if err := tx.Sign(priv); err != nil {
     93 		t.Fatalf("api: failed to sign the transaction: %v", err)
     94 	}
     95 
     96 	return dto.SubmitTransactionRequest{
     97 		TxID:       hex.EncodeToString(tx.TxID),
     98 		IssuerDID:  tx.IssuerDID,
     99 		MerkleRoot: hex.EncodeToString(tx.MerkleRoot),
    100 		Nonce:      hex.EncodeToString(tx.Nonce),
    101 		Timestamp:  tx.Timestamp,
    102 		Signature:  hex.EncodeToString(tx.Signature),
    103 	}
    104 }
    105 
    106 func decodeBody(t *testing.T, rec *httptest.ResponseRecorder, v any) {
    107 	t.Helper()
    108 	if err := json.NewDecoder(rec.Body).Decode(v); err != nil {
    109 		t.Fatalf("decoding response body: %v (raw body: %s)", err, rec.Body.String())
    110 	}
    111 }