Skip to content

Commit

Permalink
feat: Create Payment Option test
Browse files Browse the repository at this point in the history
  • Loading branch information
x1m3 committed Nov 15, 2024
1 parent a5655a0 commit ee7833e
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 5 deletions.
2 changes: 1 addition & 1 deletion internal/api/connections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ func TestServer_CreateConnection(t *testing.T) {
reqBytes, err := json.Marshal(tc.body)
require.NoError(t, err)
req, err := http.NewRequest(http.MethodPost, parsedURL.String(), bytes.NewBuffer(reqBytes))
req.SetBasicAuth(tc.auth())
require.NoError(t, err)
req.SetBasicAuth(tc.auth())

handler.ServeHTTP(rr, req)

Expand Down
5 changes: 5 additions & 0 deletions internal/api/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package api
import (
"context"
"encoding/json"
"errors"
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/iden3/go-iden3-core/v2/w3c"
comm "github.com/iden3/iden3comm/v2"

"github.com/polygonid/sh-id-platform/internal/log"
"github.com/polygonid/sh-id-platform/internal/repositories"
)

// GetPaymentOptions is the controller to get payment options
Expand Down Expand Up @@ -50,6 +52,9 @@ func (s *Server) CreatePaymentOption(ctx context.Context, request CreatePaymentO
id, err := s.paymentService.CreatePaymentOption(ctx, issuerDID, request.Body.Name, request.Body.Description, request.Body.Config)
if err != nil {
log.Error(ctx, "creating payment option", "err", err, "issuer", issuerDID, "request", request.Body)
if errors.Is(err, repositories.ErrIdentityNotFound) {
return CreatePaymentOption400JSONResponse{N400JSONResponse{Message: "invalid issuer did"}}, nil
}
return CreatePaymentOption500JSONResponse{N500JSONResponse{Message: fmt.Sprintf("can't create payment-option: <%s>", err.Error())}}, nil
}
return CreatePaymentOption201JSONResponse{Id: id.String()}, nil
Expand Down
140 changes: 140 additions & 0 deletions internal/api/payment_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,54 @@
package api

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/iden3/go-iden3-core/v2/w3c"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/polygonid/sh-id-platform/internal/core/ports"
)

const paymentOptionConfigurationTesting = `
{
"Chains": [
{
"ChainId": 137,
"Recipient": "0x..",
"SigningKeyId": "<key id>",
"Iden3PaymentRailsRequestV1": {
"Amount": "0.01",
"Currency": "POL"
},
"Iden3PaymentRailsERC20RequestV1": {
"USDT": {
"Amount": "3"
},
"USDC": {
"Amount": "3"
}
}
},
{
"ChainId": 1101,
"Recipient": "0x..",
"SigningKeyId": "<key id>",
"Iden3PaymentRailsRequestV1": {
"Amount": "0.5",
"Currency": "ETH"
}
}
]
}
`

func TestServer_GetPaymentSettings(t *testing.T) {
ctx := context.Background()

Expand All @@ -27,3 +65,105 @@ func TestServer_GetPaymentSettings(t *testing.T) {
var response GetPaymentSettings200JSONResponse
require.NoError(t, json.Unmarshal(rr.Body.Bytes(), &response))
}

func TestServer_CreatePaymentOption(t *testing.T) {
const (
method = "polygonid"
blockchain = "polygon"
network = "amoy"
BJJ = "BJJ"
)

var config map[string]interface{}
ctx := context.Background()

server := newTestServer(t, nil)
handler := getHandler(ctx, server)

iden, err := server.Services.identity.Create(ctx, "polygon-test", &ports.DIDCreationOptions{Method: method, Blockchain: blockchain, Network: network, KeyType: BJJ})
require.NoError(t, err)
issuerDID, err := w3c.ParseDID(iden.Identifier)
require.NoError(t, err)

otherDID, err := w3c.ParseDID("did:polygonid:polygon:amoy:2qRYvPBNBTkPaHk1mKBkcLTequfAdsHzXv549ktnL5")
require.NoError(t, err)

require.NoError(t, json.Unmarshal([]byte(paymentOptionConfigurationTesting), &config))

type expected struct {
httpCode int
msg string
}

for _, tc := range []struct {
name string
issuerDID w3c.DID
auth func() (string, string)
body CreatePaymentOptionJSONRequestBody
expected expected
}{
{
name: "no auth header",
auth: authWrong,
issuerDID: *issuerDID,
body: CreatePaymentOptionJSONRequestBody{
Config: config,
Description: "Payment Option explanation",
Name: "1 POL Payment",
},
expected: expected{
httpCode: http.StatusUnauthorized,
msg: "Unauthorized",
},
},
{
name: "Happy Path",
auth: authOk,
issuerDID: *issuerDID,
body: CreatePaymentOptionJSONRequestBody{
Config: config,
Description: "Payment Option explanation",
Name: "1 POL Payment",
},
expected: expected{
httpCode: http.StatusCreated,
},
},
{
name: "Not existing issuerDID",
auth: authOk,
issuerDID: *otherDID,
body: CreatePaymentOptionJSONRequestBody{
Config: config,
Description: "Payment Option explanation",
Name: "1 POL Payment",
},
expected: expected{
httpCode: http.StatusBadRequest,
msg: "invalid issuer did",
},
},
} {
t.Run(tc.name, func(t *testing.T) {
rr := httptest.NewRecorder()
payload, err := json.Marshal(tc.body)
require.NoError(t, err)
url := fmt.Sprintf("/v2/identities/%s/payment/options", tc.issuerDID.String())
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(payload))
assert.NoError(t, err)
req.SetBasicAuth(tc.auth())

handler.ServeHTTP(rr, req)
require.Equal(t, tc.expected.httpCode, rr.Code)
switch tc.expected.httpCode {
case http.StatusCreated:
var response CreatePaymentOption201JSONResponse
require.NoError(t, json.Unmarshal(rr.Body.Bytes(), &response))
case http.StatusBadRequest:
var response CreatePaymentOption400JSONResponse
require.NoError(t, json.Unmarshal(rr.Body.Bytes(), &response))
assert.Equal(t, tc.expected.msg, response.Message)
}
})
}
}
10 changes: 6 additions & 4 deletions internal/repositories/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/polygonid/sh-id-platform/internal/db"
)

// PaymentOptionNotFound error
var PaymentOptionNotFound = errors.New("payment option not found")
// ErrPaymentOptionNotFound error
var ErrPaymentOptionNotFound = errors.New("payment option not found")

// Payment repository
type Payment struct {
Expand All @@ -35,7 +35,9 @@ VALUES ($1, $2, $3, $4, $5, $6, $7);

_, err := p.conn.Pgx.Exec(ctx, query, opt.ID, opt.IssuerDID.String(), opt.Name, opt.Description, opt.Config, opt.CreatedAt, opt.UpdatedAt)
if err != nil {
return uuid.Nil, err
if strings.Contains(err.Error(), "violates foreign key constraint") {
return uuid.Nil, ErrIdentityNotFound
}
}
return opt.ID, nil
}
Expand Down Expand Up @@ -85,7 +87,7 @@ AND issuer_did = $2;`
err := p.conn.Pgx.QueryRow(ctx, query, id, issuerDID.String()).Scan(&opt.ID, &strIssuerDID, &opt.Name, &opt.Description, &opt.Config, &opt.CreatedAt, &opt.UpdatedAt)
if err != nil {
if strings.Contains(err.Error(), "no rows in result set") {
return nil, PaymentOptionNotFound
return nil, ErrPaymentOptionNotFound
}
return nil, err
}
Expand Down

0 comments on commit ee7833e

Please sign in to comment.