2023-02-21 09:05:16 +00:00
|
|
|
package opensea
|
2021-08-20 19:53:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-03-20 15:53:39 +00:00
|
|
|
"fmt"
|
2023-02-28 03:10:38 +00:00
|
|
|
"math/big"
|
2021-08-20 19:53:24 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2023-02-28 03:10:38 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/bigint"
|
2023-07-18 15:01:53 +00:00
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
|
|
|
"github.com/status-im/status-go/services/wallet/connection"
|
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty"
|
2023-02-28 03:10:38 +00:00
|
|
|
|
2021-08-20 19:53:24 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
2023-03-20 15:53:39 +00:00
|
|
|
const (
|
|
|
|
ExpiredKeyError = "Expired API key"
|
|
|
|
ExpectedExpiredKeyError = "invalid json: Expired API key"
|
|
|
|
)
|
|
|
|
|
2023-07-18 15:01:53 +00:00
|
|
|
func initTestClient(srv *httptest.Server) *Client {
|
|
|
|
urlGetter := func(chainID walletCommon.ChainID, path string) (string, error) {
|
|
|
|
return srv.URL, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
status := connection.NewStatus("", nil)
|
|
|
|
|
|
|
|
client := &HTTPClient{
|
|
|
|
client: srv.Client(),
|
|
|
|
}
|
|
|
|
opensea := &Client{
|
|
|
|
client: client,
|
|
|
|
connectionStatus: status,
|
|
|
|
urlGetter: urlGetter,
|
|
|
|
}
|
|
|
|
|
|
|
|
return opensea
|
|
|
|
}
|
|
|
|
|
2021-08-20 19:53:24 +00:00
|
|
|
func TestFetchAllCollectionsByOwner(t *testing.T) {
|
2023-07-18 15:01:53 +00:00
|
|
|
expectedOS := []OwnedCollection{{
|
2023-02-28 03:10:38 +00:00
|
|
|
Collection: Collection{
|
|
|
|
Name: "Rocky",
|
|
|
|
Slug: "rocky",
|
|
|
|
ImageURL: "ImageUrl",
|
|
|
|
},
|
|
|
|
OwnedAssetCount: &bigint.BigInt{Int: big.NewInt(1)},
|
|
|
|
}}
|
2023-07-18 15:01:53 +00:00
|
|
|
response, _ := json.Marshal(expectedOS)
|
2021-08-20 19:53:24 +00:00
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(200)
|
|
|
|
_, err := w.Write(response)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
2023-07-18 15:01:53 +00:00
|
|
|
opensea := initTestClient(srv)
|
|
|
|
res, err := opensea.FetchAllCollectionsByOwner(walletCommon.ChainID(1), common.Address{1})
|
|
|
|
assert.Equal(t, expectedOS, res)
|
2021-08-20 19:53:24 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
2023-03-20 15:53:39 +00:00
|
|
|
func TestFetchAllCollectionsByOwnerWithInValidJson(t *testing.T) {
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(200)
|
|
|
|
_, err := w.Write([]byte(ExpiredKeyError))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
2023-07-18 15:01:53 +00:00
|
|
|
opensea := initTestClient(srv)
|
|
|
|
res, err := opensea.FetchAllCollectionsByOwner(walletCommon.ChainID(1), common.Address{1})
|
2023-03-20 15:53:39 +00:00
|
|
|
assert.Nil(t, res)
|
|
|
|
assert.Equal(t, err, fmt.Errorf(ExpectedExpiredKeyError))
|
|
|
|
}
|
|
|
|
|
2021-08-20 19:53:24 +00:00
|
|
|
func TestFetchAllAssetsByOwnerAndCollection(t *testing.T) {
|
2023-07-18 15:01:53 +00:00
|
|
|
expectedOS := AssetContainer{
|
2023-03-06 16:15:48 +00:00
|
|
|
Assets: []Asset{{
|
|
|
|
ID: 1,
|
2023-07-18 15:01:53 +00:00
|
|
|
TokenID: &bigint.BigInt{Int: big.NewInt(1)},
|
2023-03-06 16:15:48 +00:00
|
|
|
Name: "Rocky",
|
|
|
|
Description: "Rocky Balboa",
|
|
|
|
Permalink: "permalink",
|
|
|
|
ImageThumbnailURL: "ImageThumbnailURL",
|
|
|
|
ImageURL: "ImageUrl",
|
2023-07-18 15:01:53 +00:00
|
|
|
Contract: Contract{
|
|
|
|
Address: "1",
|
|
|
|
ChainIdentifier: "ethereum",
|
|
|
|
},
|
|
|
|
Collection: Collection{
|
|
|
|
Name: "Rocky",
|
|
|
|
Traits: map[string]CollectionTrait{},
|
|
|
|
},
|
|
|
|
Traits: []Trait{},
|
2023-03-06 16:15:48 +00:00
|
|
|
}},
|
|
|
|
NextCursor: "",
|
|
|
|
PreviousCursor: "",
|
|
|
|
}
|
2023-07-18 15:01:53 +00:00
|
|
|
expectedCommon := thirdparty.CollectibleDataContainer{
|
|
|
|
Collectibles: []thirdparty.CollectibleData{{
|
|
|
|
ID: thirdparty.CollectibleUniqueID{
|
|
|
|
ChainID: 1,
|
|
|
|
ContractAddress: common.HexToAddress("0x1"),
|
|
|
|
TokenID: &bigint.BigInt{Int: big.NewInt(1)},
|
|
|
|
},
|
|
|
|
Name: "Rocky",
|
|
|
|
Description: "Rocky Balboa",
|
|
|
|
Permalink: "permalink",
|
|
|
|
ImageURL: "ImageUrl",
|
|
|
|
Traits: []thirdparty.CollectibleTrait{},
|
|
|
|
CollectionData: thirdparty.CollectionData{
|
|
|
|
Name: "Rocky",
|
|
|
|
Traits: map[string]thirdparty.CollectionTrait{},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
NextCursor: "",
|
|
|
|
PreviousCursor: "",
|
|
|
|
}
|
|
|
|
response, _ := json.Marshal(expectedOS)
|
2021-08-20 19:53:24 +00:00
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(200)
|
|
|
|
_, err := w.Write(response)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
2023-07-18 15:01:53 +00:00
|
|
|
opensea := initTestClient(srv)
|
|
|
|
res, err := opensea.FetchAllAssetsByOwnerAndCollection(walletCommon.ChainID(1), common.Address{1}, "rocky", "", 200)
|
2021-08-20 19:53:24 +00:00
|
|
|
assert.Nil(t, err)
|
2023-07-18 15:01:53 +00:00
|
|
|
assert.Equal(t, expectedCommon, *res)
|
2021-08-20 19:53:24 +00:00
|
|
|
}
|
2023-03-20 15:53:39 +00:00
|
|
|
|
|
|
|
func TestFetchAllAssetsByOwnerAndCollectionInvalidJson(t *testing.T) {
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(200)
|
|
|
|
_, err := w.Write([]byte(ExpiredKeyError))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
2023-07-18 15:01:53 +00:00
|
|
|
opensea := initTestClient(srv)
|
|
|
|
res, err := opensea.FetchAllAssetsByOwnerAndCollection(walletCommon.ChainID(1), common.Address{1}, "rocky", "", 200)
|
2023-03-20 15:53:39 +00:00
|
|
|
assert.Nil(t, res)
|
|
|
|
assert.Equal(t, fmt.Errorf(ExpectedExpiredKeyError), err)
|
|
|
|
}
|