|
| 1 | +package handlers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "github.com/rsksmart/liquidity-provider-server/internal/adapters/entrypoints/rest/handlers" |
| 6 | + "github.com/rsksmart/liquidity-provider-server/internal/entities" |
| 7 | + lpEntity "github.com/rsksmart/liquidity-provider-server/internal/entities/liquidity_provider" |
| 8 | + "github.com/rsksmart/liquidity-provider-server/internal/entities/utils" |
| 9 | + "github.com/rsksmart/liquidity-provider-server/internal/usecases/liquidity_provider" |
| 10 | + "github.com/rsksmart/liquidity-provider-server/pkg" |
| 11 | + "github.com/rsksmart/liquidity-provider-server/test/mocks" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/mock" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + "net/http" |
| 16 | + "net/http/httptest" |
| 17 | + "testing" |
| 18 | +) |
| 19 | + |
| 20 | +func TestNewProviderDetailsHandler(t *testing.T) { |
| 21 | + const ( |
| 22 | + path = "/providers/details" |
| 23 | + verb = "GET" |
| 24 | + captchaKey = "captchaKey" |
| 25 | + captchaDisabled = true |
| 26 | + ) |
| 27 | + |
| 28 | + providerMock := &mocks.ProviderMock{} |
| 29 | + providerMock.On("GeneralConfiguration", mock.Anything).Return(lpEntity.GeneralConfiguration{ |
| 30 | + RskConfirmations: map[int]uint16{1: 10, 2: 20, 3: 50, 4: 15}, |
| 31 | + BtcConfirmations: map[int]uint16{1: 15, 2: 11, 3: 14, 4: 11}, |
| 32 | + PublicLiquidityCheck: true, |
| 33 | + }).Times(5) |
| 34 | + providerMock.On("PeginConfiguration", mock.Anything).Return(lpEntity.PeginConfiguration{ |
| 35 | + TimeForDeposit: 300, |
| 36 | + CallTime: 400, |
| 37 | + PenaltyFee: entities.NewWei(500), |
| 38 | + FixedFee: entities.NewWei(700), |
| 39 | + FeePercentage: utils.NewBigFloat64(15.77), |
| 40 | + MaxValue: entities.NewWei(800), |
| 41 | + MinValue: entities.NewWei(100), |
| 42 | + }).Times(5) |
| 43 | + providerMock.On("PegoutConfiguration", mock.Anything).Return(lpEntity.PegoutConfiguration{ |
| 44 | + TimeForDeposit: 111, |
| 45 | + ExpireTime: 222, |
| 46 | + PenaltyFee: entities.NewWei(333), |
| 47 | + FixedFee: entities.NewWei(444), |
| 48 | + FeePercentage: utils.NewBigFloat64(0.33), |
| 49 | + MaxValue: entities.NewWei(1000), |
| 50 | + MinValue: entities.NewWei(10), |
| 51 | + ExpireBlocks: 500, |
| 52 | + BridgeTransactionMin: entities.NewWei(1500), |
| 53 | + }).Times(5) |
| 54 | + |
| 55 | + t.Run("should return 200 on success", func(t *testing.T) { |
| 56 | + useCase := liquidity_provider.NewGetDetailUseCase(captchaKey, captchaDisabled, providerMock, providerMock, providerMock) |
| 57 | + handler := handlers.NewProviderDetailsHandler(useCase) |
| 58 | + assert.HTTPSuccess(t, handler, verb, path, nil) |
| 59 | + assert.HTTPBodyContains(t, handler, verb, path, nil, `{"siteKey":"captchaKey","liquidityCheckEnabled":true,"pegin":{"fee":700,"fixedFee":700,"feePercentage":15.77,"minTransactionValue":100,"maxTransactionValue":800,"requiredConfirmations":15},"pegout":{"fee":444,"fixedFee":444,"feePercentage":0.33,"minTransactionValue":10,"maxTransactionValue":1000,"requiredConfirmations":50}}`) |
| 60 | + }) |
| 61 | + t.Run("should handle internal error", func(t *testing.T) { |
| 62 | + useCase := liquidity_provider.NewGetDetailUseCase("", false, providerMock, providerMock, providerMock) |
| 63 | + handler := handlers.NewProviderDetailsHandler(useCase) |
| 64 | + assert.HTTPStatusCode(t, handler, verb, path, nil, http.StatusInternalServerError) |
| 65 | + assert.HTTPBodyContains(t, handler, verb, path, nil, `"details":{"error":"ProviderDetail: missing captcha key"}`) |
| 66 | + }) |
| 67 | + t.Run("should return deprecated fee field", func(t *testing.T) { |
| 68 | + var result pkg.ProviderDetailResponse |
| 69 | + useCase := liquidity_provider.NewGetDetailUseCase(captchaKey, captchaDisabled, providerMock, providerMock, providerMock) |
| 70 | + handler := handlers.NewProviderDetailsHandler(useCase) |
| 71 | + recorder := httptest.NewRecorder() |
| 72 | + handler.ServeHTTP(recorder, httptest.NewRequest(verb, path, nil)) |
| 73 | + require.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &result)) |
| 74 | + // disable linter to be able to check over the deprecated field |
| 75 | + // nolint:staticcheck |
| 76 | + assert.Equal(t, result.Pegout.FixedFee, result.Pegout.Fee) |
| 77 | + // nolint:staticcheck |
| 78 | + assert.Equal(t, result.Pegin.FixedFee, result.Pegin.Fee) |
| 79 | + }) |
| 80 | + providerMock.AssertExpectations(t) |
| 81 | +} |
0 commit comments