certichain

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

institution_test.go (3143B)


      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 )
     29 
     30 func TestRegisterInstitution(t *testing.T) {
     31 	t.Parallel()
     32 
     33 	deps := newTestDeps(t)
     34 	handler := RegisterInstitution(deps.Registry)
     35 
     36 	pub, _, err := crypto.GenerateKeyPair()
     37 	if err != nil {
     38 		t.Fatalf("api: failed to generate keypair: %v", err)
     39 	}
     40 
     41 	tests := []struct {
     42 		name       string
     43 		pubKey     []byte
     44 		statusCode int
     45 	}{
     46 		{
     47 			name:       "ideal case",
     48 			pubKey:     pub,
     49 			statusCode: http.StatusCreated,
     50 		},
     51 		{
     52 			name:       "malformed JSON body",
     53 			pubKey:     []byte(`"{ not: valid"`),
     54 			statusCode: http.StatusBadRequest,
     55 		},
     56 		{
     57 			name:       "invalid hex encoding",
     58 			pubKey:     []byte("NOT VALID"),
     59 			statusCode: http.StatusBadRequest,
     60 		},
     61 		{
     62 			name:       "duplicate case",
     63 			pubKey:     pub,
     64 			statusCode: http.StatusConflict,
     65 		},
     66 	}
     67 
     68 	for _, tt := range tests {
     69 		t.Run(tt.name, func(t *testing.T) {
     70 			t.Parallel()
     71 
     72 			body, _ := json.Marshal(dto.RegisterInstitutionRequest{
     73 				PublicKey: hex.EncodeToString(tt.pubKey),
     74 			})
     75 			if tt.name == "malformed JSON body" {
     76 				body, _ = json.Marshal(tt.pubKey)
     77 			}
     78 
     79 			req := httptest.NewRequest(http.MethodPost, "/v1/institutions", bytes.NewReader(body))
     80 			rec := httptest.NewRecorder()
     81 
     82 			handler(rec, req)
     83 
     84 			if rec.Code != tt.statusCode {
     85 				t.Errorf("want = %d, got status = %d", tt.statusCode, rec.Code)
     86 			}
     87 		})
     88 	}
     89 }
     90 
     91 func TestGetInstitution(t *testing.T) {
     92 	t.Parallel()
     93 
     94 	deps := newTestDeps(t)
     95 	handler := GetInstitution(deps.Registry)
     96 
     97 	did, _, _ := mustRegisterInstitution(t, deps.Registry)
     98 	fakeDID := "did:certchain:0x" + strings.Repeat("a", 64)
     99 
    100 	tests := []struct {
    101 		name       string
    102 		did        string
    103 		registered bool
    104 	}{
    105 		{
    106 			name:       "registered",
    107 			did:        did,
    108 			registered: true,
    109 		},
    110 		{
    111 			name:       "unregistered",
    112 			did:        fakeDID,
    113 			registered: false,
    114 		},
    115 	}
    116 	for _, tt := range tests {
    117 		t.Run(tt.name, func(t *testing.T) {
    118 			t.Parallel()
    119 
    120 			req := httptest.NewRequest(http.MethodGet, "/v1/institutions", nil)
    121 			req.SetPathValue("did", tt.did)
    122 			rec := httptest.NewRecorder()
    123 
    124 			handler(rec, req)
    125 
    126 			if rec.Code != http.StatusOK {
    127 				t.Errorf("want = %d, status got = %d", http.StatusOK, rec.Code)
    128 			}
    129 
    130 			var resp dto.GetInstitutionResponse
    131 			decodeBody(t, rec, &resp)
    132 
    133 			if resp.Registered != tt.registered {
    134 				t.Errorf("registered: want = %v, got = %v", tt.registered, resp.Registered)
    135 			}
    136 		})
    137 	}
    138 }