|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package service |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/go-chi/chi/v5" |
| 13 | + "github.com/stretchr/testify/mock" |
| 14 | + "go.uber.org/zap" |
| 15 | + |
| 16 | + apperrors "github.com/chainsafe/canton-middleware/pkg/app/errors" |
| 17 | + "github.com/chainsafe/canton-middleware/pkg/auth" |
| 18 | + "github.com/chainsafe/canton-middleware/pkg/auth/jwt" |
| 19 | + "github.com/chainsafe/canton-middleware/pkg/auth/service/mocks" |
| 20 | +) |
| 21 | + |
| 22 | +func newLoginTestServer(svc Service) http.Handler { |
| 23 | + r := chi.NewRouter() |
| 24 | + RegisterRoutes(r, svc, zap.NewNop()) |
| 25 | + return r |
| 26 | +} |
| 27 | + |
| 28 | +func TestNonceHTTP_ReturnsNonce(t *testing.T) { |
| 29 | + svc := mocks.NewService(t) |
| 30 | + svc.EXPECT().Nonce(mock.Anything).Return("the-nonce", nil) |
| 31 | + |
| 32 | + rec := httptest.NewRecorder() |
| 33 | + req := httptest.NewRequest(http.MethodGet, "/auth/nonce?address=0x0000000000000000000000000000000000000001", nil) |
| 34 | + newLoginTestServer(svc).ServeHTTP(rec, req) |
| 35 | + |
| 36 | + if rec.Code != http.StatusOK { |
| 37 | + t.Fatalf("status = %d, want 200", rec.Code) |
| 38 | + } |
| 39 | + var got auth.NonceResponse |
| 40 | + if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil { |
| 41 | + t.Fatalf("decode: %v", err) |
| 42 | + } |
| 43 | + if got.Nonce != "the-nonce" { |
| 44 | + t.Fatalf("nonce = %q, want the-nonce", got.Nonce) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestNonceHTTP_MissingOrInvalidAddress_Returns400(t *testing.T) { |
| 49 | + svc := mocks.NewService(t) // Nonce must not be called |
| 50 | + |
| 51 | + for _, q := range []string{"/auth/nonce", "/auth/nonce?address=not-an-address"} { |
| 52 | + rec := httptest.NewRecorder() |
| 53 | + newLoginTestServer(svc).ServeHTTP(rec, httptest.NewRequest(http.MethodGet, q, nil)) |
| 54 | + if rec.Code != http.StatusBadRequest { |
| 55 | + t.Fatalf("%s: status = %d, want 400", q, rec.Code) |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestLoginHTTP_Success(t *testing.T) { |
| 61 | + svc := mocks.NewService(t) |
| 62 | + svc.EXPECT().Login(mock.Anything, "msg", "sig"). |
| 63 | + Return(&auth.LoginResponse{Token: "tok", ExpiresAt: 12345}, nil) |
| 64 | + |
| 65 | + body, _ := json.Marshal(auth.LoginRequest{Message: "msg", Signature: "sig"}) |
| 66 | + rec := httptest.NewRecorder() |
| 67 | + newLoginTestServer(svc).ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "/auth/login", strings.NewReader(string(body)))) |
| 68 | + |
| 69 | + if rec.Code != http.StatusOK { |
| 70 | + t.Fatalf("status = %d, want 200, body = %s", rec.Code, rec.Body.String()) |
| 71 | + } |
| 72 | + var got auth.LoginResponse |
| 73 | + if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil { |
| 74 | + t.Fatalf("decode: %v", err) |
| 75 | + } |
| 76 | + if got.Token != "tok" || got.ExpiresAt != 12345 { |
| 77 | + t.Fatalf("response = %+v, want {tok 12345}", got) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestLoginHTTP_InvalidJSON_Returns400(t *testing.T) { |
| 82 | + svc := mocks.NewService(t) // Login must not be called |
| 83 | + rec := httptest.NewRecorder() |
| 84 | + newLoginTestServer(svc).ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "/auth/login", strings.NewReader("{not json"))) |
| 85 | + |
| 86 | + if rec.Code != http.StatusBadRequest { |
| 87 | + t.Fatalf("status = %d, want 400", rec.Code) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestLoginHTTP_MissingFields_Returns400(t *testing.T) { |
| 92 | + svc := mocks.NewService(t) // Login must not be called |
| 93 | + body, _ := json.Marshal(auth.LoginRequest{Message: "msg"}) // signature missing |
| 94 | + rec := httptest.NewRecorder() |
| 95 | + newLoginTestServer(svc).ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "/auth/login", strings.NewReader(string(body)))) |
| 96 | + |
| 97 | + if rec.Code != http.StatusBadRequest { |
| 98 | + t.Fatalf("status = %d, want 400", rec.Code) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestLoginHTTP_ServiceUnauthorized_Returns401(t *testing.T) { |
| 103 | + svc := mocks.NewService(t) |
| 104 | + svc.EXPECT().Login(mock.Anything, "msg", "sig"). |
| 105 | + Return(nil, apperrors.UnAuthorizedError(nil, "invalid sign-in message or signature")) |
| 106 | + |
| 107 | + body, _ := json.Marshal(auth.LoginRequest{Message: "msg", Signature: "sig"}) |
| 108 | + rec := httptest.NewRecorder() |
| 109 | + newLoginTestServer(svc).ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "/auth/login", strings.NewReader(string(body)))) |
| 110 | + |
| 111 | + if rec.Code != http.StatusUnauthorized { |
| 112 | + t.Fatalf("status = %d, want 401", rec.Code) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestJWKSHTTP_ServesKeySet(t *testing.T) { |
| 117 | + svc := mocks.NewService(t) |
| 118 | + svc.EXPECT().JWKS().Return(jwt.JWKS{Keys: []jwt.JWK{{Kid: "kid-1", Kty: "RSA"}}}) |
| 119 | + |
| 120 | + rec := httptest.NewRecorder() |
| 121 | + newLoginTestServer(svc).ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/.well-known/jwks.json", nil)) |
| 122 | + |
| 123 | + if rec.Code != http.StatusOK { |
| 124 | + t.Fatalf("status = %d, want 200", rec.Code) |
| 125 | + } |
| 126 | + var got jwt.JWKS |
| 127 | + if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil { |
| 128 | + t.Fatalf("decode: %v", err) |
| 129 | + } |
| 130 | + if len(got.Keys) != 1 || got.Keys[0].Kid != "kid-1" { |
| 131 | + t.Fatalf("jwks = %+v, want one key kid-1", got) |
| 132 | + } |
| 133 | +} |
0 commit comments