commit 6c6f4ea6943adb16edabfd6b55b06cbae13d2856
parent f7a0a787e0d2b17235baaabba2c58bf8ce4cf82d
Author: Shivakumar <[email protected]>
Date: Sun, 9 Aug 2026 03:37:18 +0530
test(api): add helper and instituion api tests
Signed-off-by: Shivakumar <[email protected]>
Diffstat:
2 files changed, 249 insertions(+), 0 deletions(-)
diff --git a/api/handlers/helpers_test.go b/api/handlers/helpers_test.go
@@ -0,0 +1,111 @@
+// 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 (
+ "crypto/ed25519"
+ "encoding/hex"
+ "encoding/json"
+ "net/http/httptest"
+ "testing"
+
+ "certichain/api/dto"
+ "certichain/internal/blockchain"
+ "certichain/internal/crypto"
+ "certichain/internal/mempool"
+ "certichain/internal/registry"
+ "certichain/internal/storage"
+ "certichain/internal/transaction"
+)
+
+type testDeps struct {
+ Store storage.Store
+ Registry *registry.Registry
+ Mempool *mempool.Mempool
+ Chain *blockchain.Blockchain
+}
+
+func newTestDeps(t *testing.T) *testDeps {
+ t.Helper()
+
+ store := storage.NewMemoryStore()
+ reg := registry.New(store)
+ mem := mempool.New()
+
+ bc, err := blockchain.New(store, blockchain.WithKeyResolver(reg))
+ if err != nil {
+ t.Fatalf("api: blockchain creation: %v", err)
+ }
+
+ return &testDeps{
+ Store: store,
+ Registry: reg,
+ Mempool: mem,
+ Chain: bc,
+ }
+}
+
+func mustRegisterInstitution(t *testing.T, reg *registry.Registry) (
+ string,
+ ed25519.PublicKey,
+ ed25519.PrivateKey,
+) {
+ t.Helper()
+
+ pub, priv, err := crypto.GenerateKeyPair()
+ if err != nil {
+ t.Fatalf("api: failed to generate keypair: %v", err)
+ }
+
+ did, err := reg.Register(pub)
+ if err != nil {
+ t.Fatalf("api: failed to Register pub: %v", err)
+ }
+
+ return did, pub, priv
+}
+
+func mustSubmitTransaction(
+ t *testing.T,
+ did string,
+ priv ed25519.PrivateKey,
+ root []byte,
+) dto.SubmitTransactionRequest {
+ t.Helper()
+
+ tx, err := transaction.New(did, root)
+ if err != nil {
+ t.Fatalf("api: failed to create transaction: %v", err)
+ }
+ if err := tx.Sign(priv); err != nil {
+ t.Fatalf("api: failed to sign the transaction: %v", err)
+ }
+
+ return dto.SubmitTransactionRequest{
+ TxID: hex.EncodeToString(tx.TxID),
+ IssuerDID: tx.IssuerDID,
+ MerkleRoot: hex.EncodeToString(tx.MerkleRoot),
+ Nonce: hex.EncodeToString(tx.Nonce),
+ Timestamp: tx.Timestamp,
+ Signature: hex.EncodeToString(tx.Signature),
+ }
+}
+
+func decodeBody(t *testing.T, rec *httptest.ResponseRecorder, v any) {
+ t.Helper()
+ if err := json.NewDecoder(rec.Body).Decode(v); err != nil {
+ t.Fatalf("decoding response body: %v (raw body: %s)", err, rec.Body.String())
+ }
+}
diff --git a/api/handlers/institution_test.go b/api/handlers/institution_test.go
@@ -0,0 +1,138 @@
+// 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"
+)
+
+func TestRegisterInstitution(t *testing.T) {
+ t.Parallel()
+
+ deps := newTestDeps(t)
+ handler := RegisterInstitution(deps.Registry)
+
+ pub, _, err := crypto.GenerateKeyPair()
+ if err != nil {
+ t.Fatalf("api: failed to generate keypair: %v", err)
+ }
+
+ tests := []struct {
+ name string
+ pubKey []byte
+ statusCode int
+ }{
+ {
+ name: "ideal case",
+ pubKey: pub,
+ statusCode: http.StatusCreated,
+ },
+ {
+ name: "malformed JSON body",
+ pubKey: []byte(`"{ not: valid"`),
+ statusCode: http.StatusBadRequest,
+ },
+ {
+ name: "invalid hex encoding",
+ pubKey: []byte("NOT VALID"),
+ statusCode: http.StatusBadRequest,
+ },
+ {
+ name: "duplicate case",
+ pubKey: pub,
+ statusCode: http.StatusConflict,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ t.Parallel()
+
+ body, _ := json.Marshal(dto.RegisterInstitutionRequest{
+ PublicKey: hex.EncodeToString(tt.pubKey),
+ })
+ if tt.name == "malformed JSON body" {
+ body, _ = json.Marshal(tt.pubKey)
+ }
+
+ req := httptest.NewRequest(http.MethodPost, "/v1/institutions", bytes.NewReader(body))
+ rec := httptest.NewRecorder()
+
+ handler(rec, req)
+
+ if rec.Code != tt.statusCode {
+ t.Errorf("want = %d, got status = %d", tt.statusCode, rec.Code)
+ }
+ })
+ }
+}
+
+func TestGetInstitution(t *testing.T) {
+ t.Parallel()
+
+ deps := newTestDeps(t)
+ handler := GetInstitution(deps.Registry)
+
+ did, _, _ := mustRegisterInstitution(t, deps.Registry)
+ fakeDID := "did:certchain:0x" + strings.Repeat("a", 64)
+
+ tests := []struct {
+ name string
+ did string
+ registered bool
+ }{
+ {
+ name: "registered",
+ did: did,
+ registered: true,
+ },
+ {
+ name: "unregistered",
+ did: fakeDID,
+ registered: false,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ t.Parallel()
+
+ req := httptest.NewRequest(http.MethodGet, "/v1/institutions", nil)
+ req.SetPathValue("did", tt.did)
+ rec := httptest.NewRecorder()
+
+ handler(rec, req)
+
+ if rec.Code != http.StatusOK {
+ t.Errorf("want = %d, status got = %d", http.StatusOK, rec.Code)
+ }
+
+ var resp dto.GetInstitutionResponse
+ decodeBody(t, rec, &resp)
+
+ if resp.Registered != tt.registered {
+ t.Errorf("registered: want = %v, got = %v", tt.registered, resp.Registered)
+ }
+ })
+ }
+}