Skip to content

Commit

Permalink
cmd/server: add test for /crypto endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Jan 3, 2024
1 parent 2685d05 commit 32aa6e0
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions cmd/server/search_crypto_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2022 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.

package main

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"

"github.com/moov-io/base/log"
"github.com/moov-io/watchman/pkg/ofac"

"github.com/gorilla/mux"
"github.com/stretchr/testify/require"
)

var (
cryptoSearcher = newSearcher(log.NewNopLogger(), noLogPipeliner, 1)
)

func init() {
// Set SDN Comments
ofacResults, err := ofac.Read(filepath.Join("..", "..", "test", "testdata", "sdn_comments.csv"))
if err != nil {
panic(fmt.Sprintf("ERROR reading sdn_comments.csv: %v", err))
}

cryptoSearcher.SDNComments = ofacResults.SDNComments
cryptoSearcher.SDNs = precomputeSDNs([]*ofac.SDN{
{
EntityID: "39796", // matches TestSearchCrypto
SDNName: "Person A",
SDNType: "individual",
Title: "Guy or Girl doing crypto stuff",
},
}, nil, noLogPipeliner)
}

func TestSearchCryptoSetup(t *testing.T) {
require.Len(t, cryptoSearcher.SDNComments, 13)
require.Len(t, cryptoSearcher.SDNs, 1)
}

type expectedCryptoAddressSearchResult struct {
OFAC []SDNWithDigitalCurrencyAddress `json:"ofac"`
}

func TestSearchCrypto(t *testing.T) {
router := mux.NewRouter()
addSearchRoutes(log.NewNopLogger(), router, cryptoSearcher)

w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/crypto?address=0x242654336ca2205714071898f67E254EB49ACdCe", nil)
router.ServeHTTP(w, req)
w.Flush()
require.Equal(t, http.StatusOK, w.Code)

var response expectedCryptoAddressSearchResult
err := json.NewDecoder(w.Body).Decode(&response)
require.NoError(t, err)

require.Len(t, response.OFAC, 1)
require.Equal(t, "39796", response.OFAC[0].SDN.EntityID)

// Now with cryptocurrency name specified
req = httptest.NewRequest("GET", "/crypto?name=ETH&address=0x242654336ca2205714071898f67E254EB49ACdCe", nil)
router.ServeHTTP(w, req)
w.Flush()
require.Equal(t, http.StatusOK, w.Code)

err = json.NewDecoder(w.Body).Decode(&response)
require.NoError(t, err)

require.Len(t, response.OFAC, 1)
require.Equal(t, "39796", response.OFAC[0].SDN.EntityID)

// With wrong cryptocurrency name
req = httptest.NewRequest("GET", "/crypto?name=QRR&address=0x242654336ca2205714071898f67E254EB49ACdCe", nil)
router.ServeHTTP(w, req)
w.Flush()
require.Equal(t, http.StatusOK, w.Code)

err = json.NewDecoder(w.Body).Decode(&response)
require.NoError(t, err)

require.Len(t, response.OFAC, 0)
}

0 comments on commit 32aa6e0

Please sign in to comment.