certichain

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

transactions_test.go (3882B)


      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 	"bytes"
     19 	"encoding/hex"
     20 	"encoding/json"
     21 	"net/http"
     22 	"net/http/httptest"
     23 	"strings"
     24 	"testing"
     25 
     26 	"certichain/api/dto"
     27 	"certichain/internal/crypto"
     28 	"certichain/internal/transaction"
     29 	// "certichain/internal/crypto"
     30 )
     31 
     32 func TestSubmitTransaction(t *testing.T) {
     33 	t.Parallel()
     34 
     35 	deps := newTestDeps(t)
     36 	handler := SubmitTransaction(deps.Mempool, deps.Registry)
     37 	did, _, priv := mustRegisterInstitution(t, deps.Registry)
     38 	resp := mustSubmitTransaction(t, did, priv, make([]byte, 32))
     39 
     40 	unregResp := resp
     41 	unregResp.IssuerDID = "did:certchain:0x" + strings.Repeat("a", 64)
     42 
     43 	tests := []struct {
     44 		name       string
     45 		tx         dto.SubmitTransactionRequest
     46 		statusCode int
     47 	}{
     48 		{
     49 			name:       "registered",
     50 			tx:         resp,
     51 			statusCode: http.StatusCreated,
     52 		},
     53 		{
     54 			name:       "tampered",
     55 			tx:         unregResp,
     56 			statusCode: http.StatusBadRequest,
     57 		},
     58 		{
     59 			name:       "duplicate",
     60 			tx:         resp,
     61 			statusCode: http.StatusConflict,
     62 		},
     63 	}
     64 	for _, tt := range tests {
     65 		t.Run(tt.name, func(t *testing.T) {
     66 			t.Parallel()
     67 
     68 			body, _ := json.Marshal(tt.tx)
     69 
     70 			req := httptest.NewRequest(http.MethodPost, "/v1/transactions", bytes.NewReader(body))
     71 			rec := httptest.NewRecorder()
     72 
     73 			handler(rec, req)
     74 
     75 			if rec.Code != tt.statusCode {
     76 				t.Errorf("status mismatch: want = %d, got = %d", tt.statusCode, rec.Code)
     77 			}
     78 
     79 			var resp dto.SubmitTransactionResponse
     80 			decodeBody(t, rec, &resp)
     81 
     82 			if tt.statusCode == http.StatusAccepted && resp.Status != "pending" {
     83 				t.Errorf("status mismatch: want = pending, got = %s", resp.Status)
     84 			}
     85 		})
     86 	}
     87 }
     88 
     89 func TestGetTransaction(t *testing.T) {
     90 	t.Parallel()
     91 
     92 	deps := newTestDeps(t)
     93 	handler := GetTransaction(deps.Mempool, deps.Chain)
     94 
     95 	did, _, priv := mustRegisterInstitution(t, deps.Registry)
     96 	root := crypto.HashBytes([]byte("TEST"))
     97 
     98 	tests := []struct {
     99 		name       string
    100 		call       func(tx *transaction.Transaction)
    101 		wantStatus string
    102 	}{
    103 		{
    104 			name: "mempool add",
    105 			call: func(tx *transaction.Transaction) {
    106 				if err := deps.Mempool.Add(tx); err != nil {
    107 					t.Fatalf("api: failed to add tx to mempool: %v", err)
    108 				}
    109 
    110 			},
    111 			wantStatus: "pending",
    112 		},
    113 		{
    114 			name: "blockchian add",
    115 			call: func(tx *transaction.Transaction) {
    116 				if _, err := deps.Chain.AddBlock([]*transaction.Transaction{tx}); err != nil {
    117 					t.Fatalf("api: failed to add tx to blockchain: %v", err)
    118 				}
    119 			},
    120 			wantStatus: "confirmed",
    121 		},
    122 		{
    123 			name:       "not found",
    124 			call:       func(tx *transaction.Transaction) { tx = nil },
    125 			wantStatus: "",
    126 		},
    127 	}
    128 	for _, tt := range tests {
    129 		t.Run(tt.name, func(t *testing.T) {
    130 			t.Parallel()
    131 
    132 			tx, err := transaction.New(did, root[:])
    133 			if err != nil {
    134 				t.Fatalf("api: failed to generate transaction: %v", err)
    135 			}
    136 
    137 			if err := tx.Sign(priv); err != nil {
    138 				t.Fatalf("api: failed to sign tx: %v", err)
    139 			}
    140 
    141 			tt.call(tx)
    142 
    143 			req := httptest.NewRequest(http.MethodGet, "/v1/transactions", nil)
    144 			req.SetPathValue("tx_id", hex.EncodeToString(tx.TxID))
    145 			rec := httptest.NewRecorder()
    146 
    147 			handler(rec, req)
    148 
    149 			var resp dto.GetTransactionResponse
    150 			decodeBody(t, rec, &resp)
    151 
    152 			if resp.Status != tt.wantStatus {
    153 				t.Errorf("status mismatch: want = %s, got = %s", tt.wantStatus, resp.Status)
    154 			}
    155 		})
    156 	}
    157 
    158 }