From 41cb9d7bea08e1b051e93ba1779e5a19c3006fbf Mon Sep 17 00:00:00 2001 From: Samuel Hawksby-Robinson Date: Tue, 9 Aug 2022 23:54:44 +0100 Subject: [PATCH] Added httptest.NewServer to DecodeFromURL and CryptoOnRamp tests --- _assets/tests/ramps.json | 47 +++++++++++++++++++++++++++++++++ images/decode_test.go | 25 ++++++------------ protocol/urls/urls.go | 1 - services/wallet/on_ramp.go | 2 -- services/wallet/on_ramp_test.go | 16 +++++++++-- 5 files changed, 69 insertions(+), 22 deletions(-) create mode 100644 _assets/tests/ramps.json diff --git a/_assets/tests/ramps.json b/_assets/tests/ramps.json new file mode 100644 index 000000000..3cdeeda42 --- /dev/null +++ b/_assets/tests/ramps.json @@ -0,0 +1,47 @@ +[ + { + "name": "Wyre", + "description": "A secure bridge for fiat and crypto", + "fees": "from 2.9%", + "region": "US & Europe", + "logoUrl":"https://www.sendwyre.com/favicon.ico", + "siteUrl": "https://pay.sendwyre.com/purchase", + "hostname": "sendwyre.com" + }, + { + "name": "MoonPay", + "description": "The new standard for fiat to crypto", + "fees": "1%-4.5%", + "region": "US & Europe", + "logoUrl":"https://buy.moonpay.com/favicon-32x32.png", + "siteUrl": "https://buy.moonpay.com", + "hostname": "moonpay.com" + }, + { + "name": "Transak", + "description": "Global fiat <-> crypto payment gateway", + "fees": "1%-4.5%", + "region": "Global", + "logoUrl":"https://global.transak.com/favicon.png", + "siteUrl": "https://global.transak.com", + "hostname": "transak.com" + }, + { + "name": "Ramp", + "description": "Global crypto to fiat flow", + "fees": "1.5%", + "region": "Global", + "logoUrl":"https://ramp.network/assets/favicons/favicon-32x32.png", + "siteUrl": "https://ramp.network/buy/", + "hostname": "ramp.network" + }, + { + "name": "LocalCryptos", + "description": "Non-custodial crypto marketplace", + "fees": "1.5%", + "region": "Global", + "logoUrl":"https://localcryptos.com/images/favicon.png", + "siteUrl": "https://localcryptos.com", + "hostname": "localcryptos.com" + } +] \ No newline at end of file diff --git a/images/decode_test.go b/images/decode_test.go index 86ad23bba..5cccf5cd8 100644 --- a/images/decode_test.go +++ b/images/decode_test.go @@ -4,9 +4,8 @@ import ( "errors" "image" "net/http" - "net/url" + "net/http/httptest" "testing" - "time" "github.com/stretchr/testify/require" ) @@ -88,16 +87,8 @@ func TestDecode(t *testing.T) { } func TestDecodeFromURL(t *testing.T) { - u := url.URL{ - Scheme: "http", - Host: "localhost:8089", - } - - go func() { - err := http.ListenAndServe(u.Host, http.FileServer(http.Dir("../_assets/tests"))) - require.NoError(t, err) - }() - time.Sleep(100 * time.Millisecond) + s := httptest.NewServer(http.FileServer(http.Dir(path))) + defer s.Close() cs := []struct { Filepath string @@ -105,7 +96,7 @@ func TestDecodeFromURL(t *testing.T) { Bounds image.Rectangle }{ { - u.String() + "/2x1.png", + s.URL + "/2x1.png", false, image.Rectangle{ Min: image.Point{X: 0, Y: 0}, @@ -113,7 +104,7 @@ func TestDecodeFromURL(t *testing.T) { }, }, { - u.String() + "/1.jpg", + s.URL + "/1.jpg", false, image.Rectangle{ Min: image.Point{X: 0, Y: 0}, @@ -121,7 +112,7 @@ func TestDecodeFromURL(t *testing.T) { }, }, { - u.String() + "/1.gif", + s.URL + "/1.gif", false, image.Rectangle{ Min: image.Point{X: 0, Y: 0}, @@ -129,7 +120,7 @@ func TestDecodeFromURL(t *testing.T) { }, }, { - u.String() + "/1.webp", + s.URL + "/1.webp", false, image.Rectangle{ Min: image.Point{X: 0, Y: 0}, @@ -137,7 +128,7 @@ func TestDecodeFromURL(t *testing.T) { }, }, { - u.String() + "/1.webp", + s.URL + "/1.webp", true, image.Rectangle{ Min: image.Point{X: 0, Y: 0}, diff --git a/protocol/urls/urls.go b/protocol/urls/urls.go index 4db17c970..c39561244 100644 --- a/protocol/urls/urls.go +++ b/protocol/urls/urls.go @@ -119,7 +119,6 @@ func LinkPreviewWhitelist() []Site { } func GetURLContent(url string) (data []byte, err error) { - // nolint: gosec response, err := httpClient.Get(url) if err != nil { return data, fmt.Errorf("can't get content from link %s", url) diff --git a/services/wallet/on_ramp.go b/services/wallet/on_ramp.go index 57a1a16f8..de74ebf42 100644 --- a/services/wallet/on_ramp.go +++ b/services/wallet/on_ramp.go @@ -10,8 +10,6 @@ import ( ) const ( - cryptoOnRampsData = "https://raw.githubusercontent.com/status-im/crypto-on-ramps/master/ramps.json" - DataSourceHTTP DataSourceType = iota + 1 DataSourceStatic ) diff --git a/services/wallet/on_ramp_test.go b/services/wallet/on_ramp_test.go index 095acb229..5fa93f15f 100644 --- a/services/wallet/on_ramp_test.go +++ b/services/wallet/on_ramp_test.go @@ -1,18 +1,27 @@ package wallet import ( + "net/http" + "net/http/httptest" "testing" "time" "github.com/stretchr/testify/require" ) +const ( + path = "../../_assets/tests/" +) + func TestCryptoOnRamps_Get(t *testing.T) { + s := httptest.NewServer(http.FileServer(http.Dir(path))) + defer s.Close() + cs := []*CryptoOnRampManager{ {options: &CryptoOnRampOptions{dataSourceType: DataSourceStatic}}, {options: &CryptoOnRampOptions{ dataSourceType: DataSourceHTTP, - dataSource: cryptoOnRampsData, + dataSource: s.URL + "/ramps.json", }}, } @@ -26,9 +35,12 @@ func TestCryptoOnRamps_Get(t *testing.T) { } func TestCryptoOnRampManager_hasCacheExpired(t *testing.T) { + s := httptest.NewServer(http.FileServer(http.Dir(path))) + defer s.Close() + corm := NewCryptoOnRampManager(&CryptoOnRampOptions{ dataSourceType: DataSourceHTTP, - dataSource: cryptoOnRampsData, + dataSource: s.URL + "/ramps.json", }) nt := time.Time{}.Add(30 * time.Minute)