Skip to content

Commit

Permalink
status codes for discovery client (#3513)
Browse files Browse the repository at this point in the history
* status codes for discovery client

* pr feedback

* pr feedback
  • Loading branch information
gerardsn authored Oct 25, 2024
1 parent 1e5c672 commit 9296c03
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 195 deletions.
4 changes: 2 additions & 2 deletions auth/api/iam/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions discovery/api/server/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package client
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/nuts-foundation/go-did/vc"
Expand All @@ -35,9 +34,9 @@ import (
)

// New creates a new DefaultHTTPClient.
func New(strictMode bool, timeout time.Duration, tlsConfig *tls.Config) *DefaultHTTPClient {
func New(timeout time.Duration) *DefaultHTTPClient {
return &DefaultHTTPClient{
client: client.NewWithTLSConfig(timeout, tlsConfig),
client: client.New(timeout),
}
}

Expand Down
16 changes: 8 additions & 8 deletions discovery/api/server/client/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestHTTPInvoker_Register(t *testing.T) {
t.Run("ok", func(t *testing.T) {
handler := &testHTTP.Handler{StatusCode: http.StatusCreated}
server := httptest.NewServer(handler)
client := New(false, time.Minute, server.TLS)
client := New(time.Minute)

err := client.Register(context.Background(), server.URL, vp)

Expand All @@ -51,7 +51,7 @@ func TestHTTPInvoker_Register(t *testing.T) {
})
t.Run("non-ok with problem details", func(t *testing.T) {
server := httptest.NewServer(&testHTTP.Handler{StatusCode: http.StatusBadRequest, ResponseData: `{"title":"missing credentials", "status":400, "detail":"could not resolve DID"}`})
client := New(false, time.Minute, server.TLS)
client := New(time.Minute)

err := client.Register(context.Background(), server.URL, vp)

Expand All @@ -61,7 +61,7 @@ func TestHTTPInvoker_Register(t *testing.T) {
})
t.Run("non-ok other", func(t *testing.T) {
server := httptest.NewServer(&testHTTP.Handler{StatusCode: http.StatusNotFound, ResponseData: `not found`})
client := New(false, time.Minute, server.TLS)
client := New(time.Minute)

err := client.Register(context.Background(), server.URL, vp)

Expand All @@ -84,7 +84,7 @@ func TestHTTPInvoker_Get(t *testing.T) {
"timestamp": 1,
}
server := httptest.NewServer(handler)
client := New(false, time.Minute, server.TLS)
client := New(time.Minute)

presentations, seed, timestamp, err := client.Get(context.Background(), server.URL, 0)

Expand All @@ -102,7 +102,7 @@ func TestHTTPInvoker_Get(t *testing.T) {
"timestamp": 1,
}
server := httptest.NewServer(handler)
client := New(false, time.Minute, server.TLS)
client := New(time.Minute)

presentations, _, timestamp, err := client.Get(context.Background(), server.URL, 1)

Expand All @@ -120,7 +120,7 @@ func TestHTTPInvoker_Get(t *testing.T) {
writer.Write([]byte("{}"))
}
server := httptest.NewServer(http.HandlerFunc(handler))
client := New(false, time.Minute, server.TLS)
client := New(time.Minute)

_, _, _, err := client.Get(context.Background(), server.URL, 0)

Expand All @@ -130,7 +130,7 @@ func TestHTTPInvoker_Get(t *testing.T) {
t.Run("server returns invalid status code", func(t *testing.T) {
handler := &testHTTP.Handler{StatusCode: http.StatusInternalServerError, ResponseData: `{"title":"internal server error", "status":500, "detail":"db not found"}`}
server := httptest.NewServer(handler)
client := New(false, time.Minute, server.TLS)
client := New(time.Minute)

_, _, _, err := client.Get(context.Background(), server.URL, 0)

Expand All @@ -142,7 +142,7 @@ func TestHTTPInvoker_Get(t *testing.T) {
handler := &testHTTP.Handler{StatusCode: http.StatusOK}
handler.ResponseData = "not json"
server := httptest.NewServer(handler)
client := New(false, time.Minute, server.TLS)
client := New(time.Minute)

_, _, _, err := client.Get(context.Background(), server.URL, 0)

Expand Down
18 changes: 13 additions & 5 deletions discovery/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ func (w *Wrapper) ActivateServiceForSubject(ctx context.Context, request Activat
func (w *Wrapper) DeactivateServiceForSubject(ctx context.Context, request DeactivateServiceForSubjectRequestObject) (DeactivateServiceForSubjectResponseObject, error) {
err := w.Client.DeactivateServiceForSubject(ctx, request.ServiceID, request.SubjectID)
if err != nil {
if errors.Is(err, discovery.ErrPresentationRegistrationFailed) {
return DeactivateServiceForSubject202JSONResponse{Reason: err.Error()}, nil
}
return nil, err
}
return DeactivateServiceForSubject200Response{}, nil
Expand All @@ -132,19 +135,24 @@ func (w *Wrapper) GetServices(_ context.Context, _ GetServicesRequestObject) (Ge
}

func (w *Wrapper) GetServiceActivation(ctx context.Context, request GetServiceActivationRequestObject) (GetServiceActivationResponseObject, error) {
response := GetServiceActivation200JSONResponse{
Status: ServiceStatusActive,
}
response := GetServiceActivation200JSONResponse{}
activated, presentations, err := w.Client.GetServiceActivation(ctx, request.ServiceID, request.SubjectID)
if err != nil {
if !errors.As(err, &discovery.RegistrationRefreshError{}) {
return nil, err
}
response.Status = ServiceStatusError
response.Status = to.Ptr(ServiceStatusError)
response.Error = to.Ptr(err.Error())
}
response.Activated = activated
response.Vp = &presentations
if activated && response.Status == nil {
// only set if not already set to ServiceStatusError
response.Status = to.Ptr(ServiceStatusActive)
}
if presentations != nil {
// if presentations is nil this would add `"vp":null` to the response
response.Vp = &presentations
}

return response, nil
}
17 changes: 15 additions & 2 deletions discovery/api/v1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ func TestWrapper_DeactivateServiceForSubject(t *testing.T) {
assert.NoError(t, err)
assert.IsType(t, DeactivateServiceForSubject200Response{}, response)
})
t.Run("server error", func(t *testing.T) {
test := newMockContext(t)
expectedErr := errors.Join(discovery.ErrPresentationRegistrationFailed, errors.New("custom error"))
test.client.EXPECT().DeactivateServiceForSubject(gomock.Any(), serviceID, subjectID).Return(expectedErr)

response, err := test.wrapper.DeactivateServiceForSubject(nil, DeactivateServiceForSubjectRequestObject{
ServiceID: serviceID,
SubjectID: subjectID,
})

assert.NoError(t, err)
assert.IsType(t, DeactivateServiceForSubject202JSONResponse{Reason: expectedErr.Error()}, response)
})
t.Run("error", func(t *testing.T) {
test := newMockContext(t)
test.client.EXPECT().DeactivateServiceForSubject(gomock.Any(), serviceID, subjectID).Return(errors.New("foo"))
Expand Down Expand Up @@ -199,7 +212,7 @@ func TestWrapper_GetServiceActivation(t *testing.T) {
assert.NoError(t, err)
require.IsType(t, GetServiceActivation200JSONResponse{}, response)
assert.True(t, response.(GetServiceActivation200JSONResponse).Activated)
assert.Equal(t, ServiceStatusActive, string(response.(GetServiceActivation200JSONResponse).Status))
assert.Equal(t, ServiceStatusActive, *response.(GetServiceActivation200JSONResponse).Status)
assert.Nil(t, response.(GetServiceActivation200JSONResponse).Error)
assert.Empty(t, response.(GetServiceActivation200JSONResponse).Vp)
})
Expand All @@ -215,7 +228,7 @@ func TestWrapper_GetServiceActivation(t *testing.T) {
assert.NoError(t, err)
require.IsType(t, GetServiceActivation200JSONResponse{}, response)
assert.True(t, response.(GetServiceActivation200JSONResponse).Activated)
assert.Equal(t, ServiceStatusError, string(response.(GetServiceActivation200JSONResponse).Status))
assert.Equal(t, ServiceStatusError, *response.(GetServiceActivation200JSONResponse).Status)
assert.NotNil(t, response.(GetServiceActivation200JSONResponse).Error)
assert.Empty(t, response.(GetServiceActivation200JSONResponse).Vp)
})
Expand Down
66 changes: 6 additions & 60 deletions discovery/api/v1/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions discovery/api/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type GetServiceActivation200JSONResponseStatus string

const (
// ServiceStatusActive is the status for an active service.
ServiceStatusActive = "active"
ServiceStatusActive GetServiceActivation200JSONResponseStatus = "active"
// ServiceStatusError is the status for an inactive service.
ServiceStatusError = "error"
ServiceStatusError GetServiceActivation200JSONResponseStatus = "error"
)
Loading

0 comments on commit 9296c03

Please sign in to comment.