diff --git a/cmd/main.go b/cmd/main.go index f66c2ab..7cb5efd 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -17,6 +17,7 @@ import ( "github.com/iden3/go-iden3-auth/v2/pubsignals" "github.com/iden3/go-iden3-auth/v2/state" core "github.com/iden3/go-iden3-core/v2" + "github.com/patrickmn/go-cache" log "github.com/sirupsen/logrus" "github.com/0xPolygonID/verifier-backend/internal/api" @@ -56,7 +57,8 @@ func main() { return } - apiServer := api.New(*cfg, verifier, senderDIDs) + c := cache.New(cfg.CacheExpiration.AsDuration(), cfg.CacheExpiration.AsDuration()) + apiServer := api.New(*cfg, verifier, senderDIDs, c) api.HandlerFromMux(api.NewStrictHandlerWithOptions(apiServer, nil, api.StrictHTTPServerOptions{RequestErrorHandlerFunc: errors.RequestErrorHandlerFunc}), mux) api.RegisterStatic(mux) diff --git a/internal/api/server.go b/internal/api/server.go index 66fd15f..438d387 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -48,12 +48,11 @@ type Server struct { } // New creates a new API server -func New(cfg config.Config, verifier *auth.Verifier, senderDIDs map[string]string) *Server { - c := cache.New(cfg.CacheExpiration.AsDuration(), cfg.CacheExpiration.AsDuration()) +func New(cfg config.Config, verifier *auth.Verifier, senderDIDs map[string]string, cache *cache.Cache) *Server { return &Server{ cfg: cfg, - qrStore: NewQRCodeStore(c), - cache: c, + qrStore: NewQRCodeStore(cache), + cache: cache, verifier: verifier, senderDIDs: senderDIDs, } @@ -223,8 +222,13 @@ func (s *Server) Status(_ context.Context, request StatusRequestObject) (StatusR }, nil } return getStatusVerificationResponse(value, vps), nil + default: + return Status404JSONResponse{ + N404JSONResponse{ + Message: "unexpected object in the session storage", + }, + }, nil } - return nil, nil } func getVerifiablePresentations(jwzToken string) (VerifiablePresentations, error) { diff --git a/internal/api/server_test.go b/internal/api/server_test.go index 4015332..bd6eda2 100644 --- a/internal/api/server_test.go +++ b/internal/api/server_test.go @@ -11,6 +11,7 @@ import ( "github.com/iden3/go-circuits/v2" "github.com/iden3/iden3comm/v2/packers" "github.com/iden3/iden3comm/v2/protocol" + "github.com/patrickmn/go-cache" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -22,9 +23,79 @@ const ( mumbaiNetwork = "80001" ) +func TestStatus(t *testing.T) { + ctx := context.Background() + c := cache.New(cfg.CacheExpiration.AsDuration(), cfg.CacheExpiration.AsDuration()) + server := New(cfg, nil, map[string]string{"80001": mumbaiSenderDID}, c) + + sessionID := uuid.New() + sessionID2 := uuid.New() + + c.Set(sessionID2.String(), struct { + WrongType string + }{ + WrongType: "a wrong type", + }, cache.DefaultExpiration) + + type expected struct { + httpCode int + ErrorMessage string + } + + type testConfig struct { + name string + request StatusRequestObject + expected expected + } + + for _, tc := range []testConfig{ + { + name: "should return error - session ID not found", + request: StatusRequestObject{ + Params: StatusParams{ + SessionID: sessionID, + }, + }, + expected: expected{ + httpCode: http.StatusNotFound, + ErrorMessage: "sessionID not found", + }, + }, + { + name: "should return an error wrong object in the cache", + request: StatusRequestObject{ + Params: StatusParams{ + SessionID: sessionID2, + }, + }, + expected: expected{ + httpCode: http.StatusNotFound, + ErrorMessage: "unexpected object in the session storage", + }, + }, + } { + t.Run(tc.name, func(t *testing.T) { + rr, err := server.Status(ctx, tc.request) + require.NoError(t, err) + switch tc.expected.httpCode { + case http.StatusOK: + _, ok := rr.(Status200JSONResponse) + require.True(t, ok) + case http.StatusNotFound: + response, ok := rr.(Status404JSONResponse) + require.True(t, ok) + assert.Equal(t, tc.expected.ErrorMessage, response.Message) + default: + t.Errorf("unexpected http code: %d", tc.expected.httpCode) + } + }) + } +} + func TestSignIn(t *testing.T) { ctx := context.Background() - server := New(cfg, nil, map[string]string{"80001": mumbaiSenderDID}) + c := cache.New(cfg.CacheExpiration.AsDuration(), cfg.CacheExpiration.AsDuration()) + server := New(cfg, nil, map[string]string{"80001": mumbaiSenderDID}, c) type expected struct { httpCode int