From 194a137c4a4af5b6621d989a075a15ba1e9886f1 Mon Sep 17 00:00:00 2001 From: Adam Shannon Date: Tue, 2 Aug 2022 08:53:37 -0500 Subject: [PATCH] fix: remove achParticipants or wireParticipants from json responses Fixes: https://github.com/moov-io/fed/issues/182 --- cmd/server/search.go | 4 +- cmd/server/search_handlers_test.go | 262 +++++++++++++---------------- 2 files changed, 121 insertions(+), 145 deletions(-) diff --git a/cmd/server/search.go b/cmd/server/search.go index 4ba0a453..12688913 100644 --- a/cmd/server/search.go +++ b/cmd/server/search.go @@ -30,8 +30,8 @@ type searcher struct { // searchResponse defines a FEDACH search response type searchResponse struct { - ACHParticipants []*fed.ACHParticipant `json:"achParticipants"` - WIREParticipants []*fed.WIREParticipant `json:"wireParticipants"` + ACHParticipants []*fed.ACHParticipant `json:"achParticipants,omitempty"` + WIREParticipants []*fed.WIREParticipant `json:"wireParticipants,omitempty"` } // ACHFindNameOnly finds ACH Participants by name only diff --git a/cmd/server/search_handlers_test.go b/cmd/server/search_handlers_test.go index 420f2705..6ca102ec 100644 --- a/cmd/server/search_handlers_test.go +++ b/cmd/server/search_handlers_test.go @@ -14,6 +14,7 @@ import ( "github.com/moov-io/base/log" "github.com/moov-io/fed" "github.com/moov-io/fed/pkg/logos" + "github.com/stretchr/testify/require" "github.com/gorilla/mux" ) @@ -23,28 +24,24 @@ func TestSearch__ACHName(t *testing.T) { req := httptest.NewRequest("GET", "/fed/ach/search?name=Farmers", nil) s := searcher{} - if err := s.helperLoadFEDACHFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDACHFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) router.ServeHTTP(w, req) w.Flush() - if w.Code != http.StatusOK { - t.Errorf("incorrect status code: %d", w.Code) - } - - //fmt.Printf("%s", w.Body.String()) + require.Equal(t, http.StatusOK, w.Code) var wrapper struct { ACHParticipants []*fed.ACHParticipant `json:"achParticipants"` } + require.Contains(t, w.Body.String(), "achParticipants") + require.NotContains(t, w.Body.String(), "wireParticipants") - if err := json.NewDecoder(w.Body).Decode(&wrapper); err != nil { - t.Fatal(err) - } + err = json.NewDecoder(w.Body).Decode(&wrapper) + require.NoError(t, err) for _, p := range wrapper.ACHParticipants { if !strings.Contains(p.CustomerName, strings.ToUpper("FARM")) { @@ -58,26 +55,25 @@ func TestSearch__ACHRoutingNumber(t *testing.T) { req := httptest.NewRequest("GET", "/fed/ach/search?routingNumber=044112187&limit=3", nil) s := searcher{} - if err := s.helperLoadFEDACHFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDACHFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) router.ServeHTTP(w, req) w.Flush() - if w.Code != http.StatusOK { - t.Errorf("incorrect status code: %d", w.Code) - } + require.Equal(t, http.StatusOK, w.Code) var wrapper struct { ACHParticipants []*fed.ACHParticipant `json:"achParticipants"` } - if err := json.NewDecoder(w.Body).Decode(&wrapper); err != nil { - t.Fatal(err) - } + require.Contains(t, w.Body.String(), "achParticipants") + require.NotContains(t, w.Body.String(), "wireParticipants") + + err = json.NewDecoder(w.Body).Decode(&wrapper) + require.NoError(t, err) for _, p := range wrapper.ACHParticipants { if strings.HasPrefix(p.RoutingNumber, "044") { @@ -92,26 +88,24 @@ func TestSearch__ACHCity(t *testing.T) { req := httptest.NewRequest("GET", "/fed/ach/search?city=CALDWELL", nil) s := searcher{} - if err := s.helperLoadFEDACHFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDACHFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) router.ServeHTTP(w, req) w.Flush() - if w.Code != http.StatusOK { - t.Errorf("incorrect status code: %d", w.Code) - } + require.Equal(t, http.StatusOK, w.Code) var wrapper struct { ACHParticipants []*fed.ACHParticipant `json:"achParticipants"` } + require.Contains(t, w.Body.String(), "achParticipants") + require.NotContains(t, w.Body.String(), "wireParticipants") - if err := json.NewDecoder(w.Body).Decode(&wrapper); err != nil { - t.Fatal(err) - } + err = json.NewDecoder(w.Body).Decode(&wrapper) + require.NoError(t, err) for _, p := range wrapper.ACHParticipants { if !strings.Contains(p.City, strings.ToUpper("CALDWELL")) { @@ -125,26 +119,25 @@ func TestSearch__ACHState(t *testing.T) { req := httptest.NewRequest("GET", "/fed/ach/search?state=OH", nil) s := searcher{} - if err := s.helperLoadFEDACHFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDACHFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) router.ServeHTTP(w, req) w.Flush() - if w.Code != http.StatusOK { - t.Errorf("incorrect status code: %d", w.Code) - } + require.Equal(t, http.StatusOK, w.Code) var wrapper struct { ACHParticipants []*fed.ACHParticipant `json:"achParticipants"` } - if err := json.NewDecoder(w.Body).Decode(&wrapper); err != nil { - t.Fatal(err) - } + require.Contains(t, w.Body.String(), "achParticipants") + require.NotContains(t, w.Body.String(), "wireParticipants") + + err = json.NewDecoder(w.Body).Decode(&wrapper) + require.NoError(t, err) for _, p := range wrapper.ACHParticipants { if !strings.Contains(p.State, strings.ToUpper("OH")) { @@ -158,26 +151,25 @@ func TestSearch__ACHPostalCode(t *testing.T) { req := httptest.NewRequest("GET", "/fed/ach/search?postalCode=43724", nil) s := searcher{} - if err := s.helperLoadFEDACHFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDACHFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) router.ServeHTTP(w, req) w.Flush() - if w.Code != http.StatusOK { - t.Errorf("incorrect status code: %d", w.Code) - } + require.Equal(t, http.StatusOK, w.Code) var wrapper struct { ACHParticipants []*fed.ACHParticipant `json:"achParticipants"` } - if err := json.NewDecoder(w.Body).Decode(&wrapper); err != nil { - t.Fatal(err) - } + require.Contains(t, w.Body.String(), "achParticipants") + require.NotContains(t, w.Body.String(), "wireParticipants") + + err = json.NewDecoder(w.Body).Decode(&wrapper) + require.NoError(t, err) for _, p := range wrapper.ACHParticipants { if !strings.Contains(p.PostalCode, "43724") { @@ -191,26 +183,22 @@ func TestSearch__ACH(t *testing.T) { req := httptest.NewRequest("GET", "/fed/ach/search?name=Farmers&routingNumber=044112187&city=CALDWELL&state=OH&postalCode=43724", nil) s := searcher{} - if err := s.helperLoadFEDACHFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDACHFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) router.ServeHTTP(w, req) w.Flush() - if w.Code != http.StatusOK { - t.Errorf("incorrect status code: %d", w.Code) - } + require.Equal(t, http.StatusOK, w.Code) var wrapper struct { ACHParticipants []*fed.ACHParticipant `json:"achParticipants"` } - if err := json.NewDecoder(w.Body).Decode(&wrapper); err != nil { - t.Fatal(err) - } + err = json.NewDecoder(w.Body).Decode(&wrapper) + require.NoError(t, err) for _, p := range wrapper.ACHParticipants { if !strings.Contains(p.CustomerName, strings.ToUpper("Farmer")) { @@ -238,18 +226,15 @@ func TestSearch__ACHEmpty(t *testing.T) { req := httptest.NewRequest("GET", "/fed/ach/search", nil) s := searcher{} - if err := s.helperLoadFEDACHFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDACHFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) router.ServeHTTP(w, req) w.Flush() - if w.Code != http.StatusBadRequest { - t.Errorf("incorrect status code: %d", w.Code) - } + require.Equal(t, http.StatusBadRequest, w.Code) } // WIRES @@ -259,26 +244,25 @@ func TestSearch__WIREName(t *testing.T) { req := httptest.NewRequest("GET", "/fed/wire/search?name=MIDWEST", nil) s := searcher{} - if err := s.helperLoadFEDWIREFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDWIREFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) router.ServeHTTP(w, req) w.Flush() - if w.Code != http.StatusOK { - t.Errorf("incorrect status code: %d", w.Code) - } + require.Equal(t, http.StatusOK, w.Code) var wrapper struct { WIREParticipants []*fed.WIREParticipant `json:"wireParticipants"` } - if err := json.NewDecoder(w.Body).Decode(&wrapper); err != nil { - t.Fatal(err) - } + require.NotContains(t, w.Body.String(), "achParticipants") + require.Contains(t, w.Body.String(), "wireParticipants") + + err = json.NewDecoder(w.Body).Decode(&wrapper) + require.NoError(t, err) for _, p := range wrapper.WIREParticipants { if !strings.Contains(p.CustomerName, strings.ToUpper("Midwest")) { @@ -292,9 +276,8 @@ func TestSearch__WIRERoutingNumber(t *testing.T) { req := httptest.NewRequest("GET", "/fed/wire/search?routingNumber=091905114", nil) s := searcher{} - if err := s.helperLoadFEDWIREFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDWIREFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) @@ -309,9 +292,11 @@ func TestSearch__WIRERoutingNumber(t *testing.T) { WIREParticipants []*fed.WIREParticipant `json:"wireParticipants"` } - if err := json.NewDecoder(w.Body).Decode(&wrapper); err != nil { - t.Fatal(err) - } + require.NotContains(t, w.Body.String(), "achParticipants") + require.Contains(t, w.Body.String(), "wireParticipants") + + err = json.NewDecoder(w.Body).Decode(&wrapper) + require.NoError(t, err) for _, p := range wrapper.WIREParticipants { if !strings.Contains(p.RoutingNumber, "091905114") { @@ -325,9 +310,8 @@ func TestSearch__WIREState(t *testing.T) { req := httptest.NewRequest("GET", "/fed/wire/search?state=IA", nil) s := searcher{} - if err := s.helperLoadFEDWIREFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDWIREFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) @@ -342,9 +326,11 @@ func TestSearch__WIREState(t *testing.T) { WIREParticipants []*fed.WIREParticipant `json:"wireParticipants"` } - if err := json.NewDecoder(w.Body).Decode(&wrapper); err != nil { - t.Fatal(err) - } + require.NotContains(t, w.Body.String(), "achParticipants") + require.Contains(t, w.Body.String(), "wireParticipants") + + err = json.NewDecoder(w.Body).Decode(&wrapper) + require.NoError(t, err) for _, p := range wrapper.WIREParticipants { if !strings.Contains(p.State, strings.ToUpper("IA")) { @@ -358,9 +344,8 @@ func TestSearch__WIRECity(t *testing.T) { req := httptest.NewRequest("GET", "/fed/wire/search?city=IOWA+CITY", nil) s := searcher{} - if err := s.helperLoadFEDWIREFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDWIREFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) @@ -375,9 +360,11 @@ func TestSearch__WIRECity(t *testing.T) { WIREParticipants []*fed.WIREParticipant `json:"wireParticipants"` } - if err := json.NewDecoder(w.Body).Decode(&wrapper); err != nil { - t.Fatal(err) - } + require.NotContains(t, w.Body.String(), "achParticipants") + require.Contains(t, w.Body.String(), "wireParticipants") + + err = json.NewDecoder(w.Body).Decode(&wrapper) + require.NoError(t, err) for _, p := range wrapper.WIREParticipants { if !strings.Contains(p.City, strings.ToUpper("IOWA CITY")) { @@ -391,9 +378,8 @@ func TestSearch__WIRE(t *testing.T) { req := httptest.NewRequest("GET", "/fed/wire/search?name=MIDWEST&routingNumber=091905114&state=IA&city=IOWA+CITY", nil) s := searcher{} - if err := s.helperLoadFEDWIREFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDWIREFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) @@ -408,9 +394,11 @@ func TestSearch__WIRE(t *testing.T) { WIREParticipants []*fed.WIREParticipant `json:"wireParticipants"` } - if err := json.NewDecoder(w.Body).Decode(&wrapper); err != nil { - t.Fatal(err) - } + require.NotContains(t, w.Body.String(), "achParticipants") + require.Contains(t, w.Body.String(), "wireParticipants") + + err = json.NewDecoder(w.Body).Decode(&wrapper) + require.NoError(t, err) for _, p := range wrapper.WIREParticipants { if !strings.Contains(p.CustomerName, strings.ToUpper("Midwest")) { @@ -435,18 +423,15 @@ func TestSearch__ACHRoutingNumber1Digit(t *testing.T) { req := httptest.NewRequest("GET", "/fed/ach/search?name=Farmers&routingNumber=0&city=CALDWELL&state=OH&postalCode=43724", nil) s := searcher{} - if err := s.helperLoadFEDACHFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDACHFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) router.ServeHTTP(w, req) w.Flush() - if w.Code != http.StatusBadRequest { - t.Errorf("incorrect status code: %d", w.Code) - } + require.Equal(t, http.StatusBadRequest, w.Code) } func TestSearch__ACHRoutingNumberOnly1Digit(t *testing.T) { @@ -454,18 +439,15 @@ func TestSearch__ACHRoutingNumberOnly1Digit(t *testing.T) { req := httptest.NewRequest("GET", "/fed/ach/search?routingNumber=0", nil) s := searcher{} - if err := s.helperLoadFEDACHFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDACHFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) router.ServeHTTP(w, req) w.Flush() - if w.Code != http.StatusBadRequest { - t.Errorf("incorrect status code: %d", w.Code) - } + require.Equal(t, http.StatusBadRequest, w.Code) } func TestSearch__WIRERoutingNumber1Digit(t *testing.T) { @@ -473,18 +455,15 @@ func TestSearch__WIRERoutingNumber1Digit(t *testing.T) { req := httptest.NewRequest("GET", "/fed/wire/search?name=MIDWEST&routingNumber=0&state=IA&city=IOWA+CITY", nil) s := searcher{} - if err := s.helperLoadFEDWIREFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDWIREFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) router.ServeHTTP(w, req) w.Flush() - if w.Code != http.StatusBadRequest { - t.Errorf("incorrect status code: %d", w.Code) - } + require.Equal(t, http.StatusBadRequest, w.Code) } func TestSearch__WIRERoutingNumberOnly1Digit(t *testing.T) { @@ -492,18 +471,15 @@ func TestSearch__WIRERoutingNumberOnly1Digit(t *testing.T) { req := httptest.NewRequest("GET", "/fed/wire/search?routingNumber=0", nil) s := searcher{} - if err := s.helperLoadFEDWIREFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDWIREFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) router.ServeHTTP(w, req) w.Flush() - if w.Code != http.StatusBadRequest { - t.Errorf("incorrect status code: %d", w.Code) - } + require.Equal(t, http.StatusBadRequest, w.Code) } func TestSearch__WIREStateSoftLimit(t *testing.T) { @@ -511,9 +487,8 @@ func TestSearch__WIREStateSoftLimit(t *testing.T) { req := httptest.NewRequest("GET", "/fed/wire/search?state=IA", nil) s := searcher{} - if err := s.helperLoadFEDWIREFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDWIREFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) @@ -528,9 +503,11 @@ func TestSearch__WIREStateSoftLimit(t *testing.T) { WIREParticipants []*fed.WIREParticipant `json:"wireParticipants"` } - if err := json.NewDecoder(w.Body).Decode(&wrapper); err != nil { - t.Fatal(err) - } + require.NotContains(t, w.Body.String(), "achParticipants") + require.Contains(t, w.Body.String(), "wireParticipants") + + err = json.NewDecoder(w.Body).Decode(&wrapper) + require.NoError(t, err) if len(wrapper.WIREParticipants) != 100 { t.Errorf("exceeded the limit: %d", len(wrapper.WIREParticipants)) @@ -542,9 +519,8 @@ func TestSearch__WIREStateLimit(t *testing.T) { req := httptest.NewRequest("GET", "/fed/wire/search?state=PA&limit=110", nil) s := searcher{} - if err := s.helperLoadFEDWIREFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDWIREFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) @@ -559,9 +535,11 @@ func TestSearch__WIREStateLimit(t *testing.T) { WIREParticipants []*fed.WIREParticipant `json:"wireParticipants"` } - if err := json.NewDecoder(w.Body).Decode(&wrapper); err != nil { - t.Fatal(err) - } + require.NotContains(t, w.Body.String(), "achParticipants") + require.Contains(t, w.Body.String(), "wireParticipants") + + err = json.NewDecoder(w.Body).Decode(&wrapper) + require.NoError(t, err) if len(wrapper.WIREParticipants) != 110 { t.Errorf("exceeded the limit: %d", len(wrapper.WIREParticipants)) @@ -573,9 +551,8 @@ func TestSearch__WIREStateHardLimit(t *testing.T) { req := httptest.NewRequest("GET", "/fed/wire/search?state=CA&limit=550", nil) s := searcher{} - if err := s.helperLoadFEDWIREFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDWIREFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) @@ -590,9 +567,11 @@ func TestSearch__WIREStateHardLimit(t *testing.T) { WIREParticipants []*fed.WIREParticipant `json:"wireParticipants"` } - if err := json.NewDecoder(w.Body).Decode(&wrapper); err != nil { - t.Fatal(err) - } + require.NotContains(t, w.Body.String(), "achParticipants") + require.Contains(t, w.Body.String(), "wireParticipants") + + err = json.NewDecoder(w.Body).Decode(&wrapper) + require.NoError(t, err) if len(wrapper.WIREParticipants) != 500 { t.Errorf("exceeded the limit: %d", len(wrapper.WIREParticipants)) @@ -604,16 +583,13 @@ func TestSearch__WireEmpty(t *testing.T) { req := httptest.NewRequest("GET", "/fed/wire/search", nil) s := searcher{} - if err := s.helperLoadFEDWIREFile(t); err != nil { - t.Fatal(err) - } + err := s.helperLoadFEDWIREFile(t) + require.NoError(t, err) router := mux.NewRouter() addSearchRoutes(log.NewNopLogger(), router, &s, logos.NewNopGrabber()) router.ServeHTTP(w, req) w.Flush() - if w.Code != http.StatusBadRequest { - t.Errorf("incorrect status code: %d", w.Code) - } + require.Equal(t, http.StatusBadRequest, w.Code) }