97 lines
3.0 KiB
Go
97 lines
3.0 KiB
Go
![]() |
package mock
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net/url"
|
||
|
|
||
|
"github.com/walkerus/go-wiremock"
|
||
|
"go.uber.org/zap"
|
||
|
"penahub.gitlab.yandexcloud.net/external/treasurer/internal/errors"
|
||
|
"penahub.gitlab.yandexcloud.net/external/treasurer/internal/models"
|
||
|
"penahub.gitlab.yandexcloud.net/external/treasurer/internal/models/yandex"
|
||
|
)
|
||
|
|
||
|
type YoomoneyMockServiceDeps struct {
|
||
|
Logger *zap.Logger
|
||
|
YoomoneyURL *models.YoomomeyURL
|
||
|
}
|
||
|
|
||
|
type YoomoneyMockService struct {
|
||
|
logger *zap.Logger
|
||
|
yoomoneyURL *models.YoomomeyURL
|
||
|
}
|
||
|
|
||
|
func NewYoomoneyMockService(deps YoomoneyMockServiceDeps) (*YoomoneyMockService, errors.Error) {
|
||
|
if deps.Logger == nil {
|
||
|
return nil, errors.NewWithMessage("logger is nil on <NewYoomoneyMockService>", errors.ErrInvalidArgs)
|
||
|
}
|
||
|
|
||
|
if deps.YoomoneyURL == nil {
|
||
|
return nil, errors.NewWithMessage("yoomoney urls is nil on <NewYoomoneyMockService>", errors.ErrInvalidArgs)
|
||
|
}
|
||
|
|
||
|
return &YoomoneyMockService{
|
||
|
logger: deps.Logger,
|
||
|
yoomoneyURL: deps.YoomoneyURL,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
func (receiver *YoomoneyMockService) Register(host string) errors.Error {
|
||
|
return receiver.RegisterPayments(host)
|
||
|
}
|
||
|
|
||
|
func (receiver *YoomoneyMockService) RegisterPayments(host string) errors.Error {
|
||
|
url, parseErr := url.Parse(receiver.yoomoneyURL.Payments)
|
||
|
if parseErr != nil {
|
||
|
receiver.logger.Error("failed to parse payment url on <RegisterPayment> of <YoomoneyMockService>",
|
||
|
zap.Error(parseErr),
|
||
|
zap.String("url", receiver.yoomoneyURL.Payments),
|
||
|
)
|
||
|
|
||
|
return errors.NewWithError(fmt.Errorf("failed to parse payment url: %w", parseErr), errors.ErrInternalError)
|
||
|
}
|
||
|
|
||
|
if url.Host != host {
|
||
|
return errors.NewWithMessage("yoomoney hosts not equal", errors.ErrInternalError)
|
||
|
}
|
||
|
|
||
|
mockClient := wiremock.NewClient(host)
|
||
|
|
||
|
rule := wiremock.
|
||
|
Post(wiremock.URLPathEqualTo(url.Path)).
|
||
|
WillReturnJSON(
|
||
|
&yandex.Payment{
|
||
|
ID: "f32d71f4-214b-464b-94bb-f7b4d5299af0",
|
||
|
Status: yandex.PaymentStatusPending,
|
||
|
Amount: yandex.Amount{
|
||
|
Value: "150.00",
|
||
|
Currency: "RUB",
|
||
|
},
|
||
|
Confirmation: &yandex.ConfirmationRedirect{
|
||
|
Type: yandex.ConfirmationTypeRedirect,
|
||
|
ConfirmationURL: "https://yandex-redirect-url.com",
|
||
|
},
|
||
|
},
|
||
|
map[string]string{"Content-Type": "application/json"},
|
||
|
200,
|
||
|
).
|
||
|
AtPriority(1)
|
||
|
|
||
|
if err := mockClient.StubFor(rule); err != nil {
|
||
|
receiver.logger.Error("failed to init payment mock handler on <RegisterPayment> of <YoomoneyMockService>", zap.Error(err))
|
||
|
return errors.NewWithError(fmt.Errorf("failed to init payment mock handler: %w", err), errors.ErrInternalError)
|
||
|
}
|
||
|
|
||
|
isVerified, err := mockClient.Verify(wiremock.NewRequest("post", wiremock.URLPathEqualTo(url.Path)), 0)
|
||
|
if err != nil {
|
||
|
receiver.logger.Error("failed to verify payment mock handler on <RegisterPayment> of <YoomoneyMockService>", zap.Error(err))
|
||
|
return errors.NewWithError(fmt.Errorf("failed to verify payment mock handler: %w", err), errors.ErrInternalError)
|
||
|
}
|
||
|
|
||
|
if !isVerified {
|
||
|
return errors.NewWithMessage("payment mock handler not verified", errors.ErrInternalError)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|