Skip to content
Snippets Groups Projects
client_test.go 1.99 KiB
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")
}