Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 9 additions & 5 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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) {
Expand Down
73 changes: 72 additions & 1 deletion internal/api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand Down