diff --git a/ocm/README.md b/ocm/README.md index 68b44457a28e90fd875138c79e0f8b646f4e5682..06775a0b0c0c4c37e1416fa2f7b20381592b6625 100644 --- a/ocm/README.md +++ b/ocm/README.md @@ -1,14 +1,20 @@ -#Golang Client package for the OCM Service +# Go client for the OCM service This go package contains client for communication with the OCM service. +### Installation + +```shell +go get code.vereign.com/gaiax/tsa/golib/ocm@latest +``` + ###Usage In order to use this package you must import it in your application and instantiate the client given the OCM service address like this: ``` -import "gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib/ocm" +import "code.vereign.com/gaiax/tsa/golib/ocm" func main() { client := ocm.New(ocmAddress) diff --git a/ocm/client.go b/ocm/client.go index 9fded9bf695d427f993a5045eb8d3dd476c2d849..abe0bacb2113bf4523507f009b08b7095de039c2 100644 --- a/ocm/client.go +++ b/ocm/client.go @@ -30,7 +30,7 @@ func New(addr string, opts ...Option) *Client { } // GetLoginProofInvitation calls the "invitation" endpoint on -// the "out-of-band" protocol in the OCM service +// the "out-of-band" protocol in the OCM service. func (c *Client) GetLoginProofInvitation(ctx context.Context, r *LoginProofInvitationRequest) (*LoginProofInvitationResponse, error) { b, err := json.Marshal(r) if err != nil { @@ -57,9 +57,8 @@ func (c *Client) GetLoginProofInvitation(ctx context.Context, r *LoginProofInvit return nil, err } - response := &LoginProofInvitationResponse{} - err = json.Unmarshal(bytes, response) - if err != nil { + var response *LoginProofInvitationResponse + if err := json.Unmarshal(bytes, &response); err != nil { return nil, err } diff --git a/ocm/client_test.go b/ocm/client_test.go new file mode 100644 index 0000000000000000000000000000000000000000..4a58c1ffd46b0a6a3f3698ddd1094c7caf2db6b2 --- /dev/null +++ b/ocm/client_test.go @@ -0,0 +1,75 @@ +package ocm_test + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + + "code.vereign.com/gaiax/tsa/golib/ocm" +) + +func Test_GetLoginProofInvitationSuccess(t *testing.T) { + expected := &ocm.LoginProofInvitationResponse{ + StatusCode: 200, + Message: "success", + } + + ocmServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + expectedRequestBody := `{"comment":"test","attributes":null,"schemaId":"schema:1.0","participantId":"12345"}` + bodyBytes, err := io.ReadAll(r.Body) + assert.NoError(t, err) + + bodyString := string(bodyBytes) + assert.Equal(t, expectedRequestBody, bodyString) + + w.WriteHeader(http.StatusOK) + _ = json.NewEncoder(w).Encode(expected) + })) + + req := &ocm.LoginProofInvitationRequest{ + Comment: "test", + Attributes: nil, + SchemaID: "schema:1.0", + ParticipantID: "12345", + } + + client := ocm.New(ocmServer.URL) + res, err := client.GetLoginProofInvitation(context.Background(), req) + + assert.NoError(t, err) + assert.Equal(t, expected.StatusCode, res.StatusCode) + assert.Equal(t, expected.Message, res.Message) + assert.Equal(t, expected.Data, res.Data) +} + +func Test_GetLoginProofInvitationErr(t *testing.T) { + ocmServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + expectedRequestBody := `{"comment":"test","attributes":null,"schemaId":"schema:1.0","participantId":"12345"}` + bodyBytes, err := io.ReadAll(r.Body) + assert.NoError(t, err) + + bodyString := string(bodyBytes) + assert.Equal(t, expectedRequestBody, bodyString) + + w.WriteHeader(http.StatusInternalServerError) + })) + + req := &ocm.LoginProofInvitationRequest{ + Comment: "test", + Attributes: nil, + SchemaID: "schema:1.0", + ParticipantID: "12345", + } + + client := ocm.New(ocmServer.URL) + res, err := client.GetLoginProofInvitation(context.Background(), req) + + assert.Nil(t, res) + assert.Error(t, err) + assert.Contains(t, err.Error(), "unexpected response code") +} diff --git a/ocm/types.go b/ocm/types.go index 3c1ad449e9957945a3fba50d5efc186a97dbe779..fa12f15a01869872b1328283928592a65aa75a2b 100644 --- a/ocm/types.go +++ b/ocm/types.go @@ -4,11 +4,11 @@ type LoginProofInvitationRequest struct { Comment string `json:"comment"` Attributes []Attribute `json:"attributes"` SchemaID string `json:"schemaId"` - ParticipantID string `json:"participant_id"` + ParticipantID string `json:"participantId"` } type Attribute struct { - AttributeName string `json:"attribute_name"` + AttributeName string `json:"attributeName"` Value string `json:"value"` Condition string `json:"condition"` } @@ -20,6 +20,6 @@ type LoginProofInvitationResponse struct { } type LoginProofInvitationResponseData struct { - PresentationID string `json:"presentation_id"` + PresentationID string `json:"presentationId"` PresentationMessage string `json:"presentationMessage"` }