certichain

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

verify_test.go (2164B)


      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 	"testing"
     22 
     23 	"certichain/api/dto"
     24 	"certichain/internal/crypto"
     25 	"certichain/internal/transaction"
     26 )
     27 
     28 func TestVerify(t *testing.T) {
     29 	t.Parallel()
     30 
     31 	deps := newTestDeps(t)
     32 	handler := Verify(deps.Chain, deps.Registry)
     33 
     34 	did, _, priv := mustRegisterInstitution(t, deps.Registry)
     35 	root := crypto.HashBytes([]byte("TEST"))
     36 
     37 	tx, err := transaction.New(did, root[:])
     38 	if err != nil {
     39 		t.Fatalf("api: failed to generate transaction: %v", err)
     40 	}
     41 	if err := tx.Sign(priv); err != nil {
     42 		t.Fatalf("api: failed to sign transaction: %v", err)
     43 	}
     44 
     45 	_, err = deps.Chain.AddBlock([]*transaction.Transaction{tx})
     46 	if err != nil {
     47 		t.Fatalf("api: failed to add block: %v", err)
     48 	}
     49 
     50 	fakeTx := tx.Clone()
     51 	fakeTx.TxID = make([]byte, 32)
     52 
     53 	tests := []struct {
     54 		name       string
     55 		tx         *transaction.Transaction
     56 		wantStatus int
     57 	}{
     58 		{
     59 			name:       "valid transaction",
     60 			tx:         tx,
     61 			wantStatus: http.StatusOK,
     62 		},
     63 		{
     64 			name:       "tampered transaction",
     65 			tx:         fakeTx,
     66 			wantStatus: http.StatusNotFound,
     67 		},
     68 	}
     69 
     70 	for _, tt := range tests {
     71 		t.Run(tt.name, func(t *testing.T) {
     72 			t.Parallel()
     73 
     74 			req := httptest.NewRequest(http.MethodGet, "/v1/verify/", nil)
     75 			req.SetPathValue("tx_id", hex.EncodeToString(tt.tx.TxID))
     76 			rec := httptest.NewRecorder()
     77 
     78 			handler(rec, req)
     79 
     80 			var resp dto.VerifyResponse
     81 			decodeBody(t, rec, &resp)
     82 
     83 			if rec.Code != tt.wantStatus {
     84 				t.Errorf("status mismatch: want = %d, got = %d", tt.wantStatus, rec.Code)
     85 			}
     86 
     87 		})
     88 	}
     89 }