github.com/prebid/prebid-server/v2@v2.18.0/endpoints/events/account_test.go (about) 1 package events 2 3 import ( 4 "errors" 5 "io" 6 "net/http" 7 "net/http/httptest" 8 "strings" 9 "testing" 10 11 "github.com/julienschmidt/httprouter" 12 "github.com/prebid/prebid-server/v2/config" 13 "github.com/prebid/prebid-server/v2/errortypes" 14 "github.com/prebid/prebid-server/v2/metrics" 15 "github.com/prebid/prebid-server/v2/stored_requests" 16 "github.com/stretchr/testify/assert" 17 "github.com/stretchr/testify/require" 18 ) 19 20 func TestHandleAccountServiceErrors(t *testing.T) { 21 tests := map[string]struct { 22 fetcher *mockAccountsFetcher 23 cfg *config.Configuration 24 accountID string 25 wantCode int 26 wantResponse string 27 }{ 28 "bad-request": { 29 fetcher: &mockAccountsFetcher{ 30 Fail: true, 31 Error: errors.New("some error"), 32 }, 33 cfg: &config.Configuration{ 34 AccountDefaults: config.Account{Disabled: true}, 35 AccountRequired: true, 36 MaxRequestSize: maxSize, 37 VTrack: config.VTrack{ 38 TimeoutMS: int64(2000), AllowUnknownBidder: false, 39 }, 40 }, 41 accountID: "testacc", 42 wantCode: 400, 43 wantResponse: "Invalid request: some error\nInvalid request: Prebid-server could not verify the Account ID. Please reach out to the prebid server host.\n", 44 }, 45 "malformed-account-config": { 46 fetcher: &mockAccountsFetcher{ 47 Fail: true, 48 Error: &errortypes.MalformedAcct{}, 49 }, 50 cfg: &config.Configuration{ 51 MaxRequestSize: maxSize, 52 VTrack: config.VTrack{ 53 TimeoutMS: int64(2000), AllowUnknownBidder: false, 54 }, 55 }, 56 accountID: "malformed_acct", 57 wantCode: 500, 58 wantResponse: "Invalid request: The prebid-server account config for account id \"malformed_acct\" is malformed. Please reach out to the prebid server host.\n", 59 }, 60 "service-unavailable": { 61 fetcher: &mockAccountsFetcher{ 62 Fail: false, 63 }, 64 cfg: &config.Configuration{ 65 AccountDefaults: config.Account{}, 66 AccountRequired: true, 67 MaxRequestSize: maxSize, 68 VTrack: config.VTrack{ 69 TimeoutMS: int64(2000), AllowUnknownBidder: false, 70 }, 71 }, 72 accountID: "disabled_acct", 73 wantCode: 503, 74 wantResponse: "Invalid request: Prebid-server has disabled Account ID: disabled_acct, please reach out to the prebid server host.\n", 75 }, 76 "timeout": { 77 fetcher: &mockAccountsFetcher{ 78 Fail: false, 79 DurationMS: 50, 80 }, 81 cfg: &config.Configuration{ 82 AccountDefaults: config.Account{Disabled: true}, 83 AccountRequired: true, 84 Event: config.Event{ 85 TimeoutMS: 1, 86 }, 87 MaxRequestSize: maxSize, 88 VTrack: config.VTrack{ 89 TimeoutMS: int64(1), 90 AllowUnknownBidder: false, 91 }, 92 }, 93 accountID: "testacc", 94 wantCode: 504, 95 wantResponse: "Invalid request: context deadline exceeded\nInvalid request: Prebid-server could not verify the Account ID. Please reach out to the prebid server host.\n", 96 }, 97 } 98 99 for name, test := range tests { 100 handlers := []struct { 101 name string 102 h httprouter.Handle 103 r *http.Request 104 }{ 105 vast(t, test.cfg, test.fetcher, test.accountID), 106 event(test.cfg, test.fetcher, test.accountID), 107 } 108 109 for _, handler := range handlers { 110 t.Run(handler.name+"-"+name, func(t *testing.T) { 111 test.cfg.MarshalAccountDefaults() 112 113 recorder := httptest.NewRecorder() 114 115 // execute 116 handler.h(recorder, handler.r, nil) 117 d, err := io.ReadAll(recorder.Result().Body) 118 require.NoError(t, err) 119 120 // validate 121 assert.Equal(t, test.wantCode, recorder.Result().StatusCode) 122 assert.Equal(t, test.wantResponse, string(d)) 123 }) 124 } 125 } 126 } 127 128 func event(cfg *config.Configuration, fetcher stored_requests.AccountFetcher, accountID string) struct { 129 name string 130 h httprouter.Handle 131 r *http.Request 132 } { 133 return struct { 134 name string 135 h httprouter.Handle 136 r *http.Request 137 }{ 138 name: "event", 139 h: NewEventEndpoint(cfg, fetcher, nil, &metrics.MetricsEngineMock{}), 140 r: httptest.NewRequest("GET", "/event?t=win&b=test&ts=1234&f=b&x=1&a="+accountID, strings.NewReader("")), 141 } 142 } 143 144 func vast(t *testing.T, cfg *config.Configuration, fetcher stored_requests.AccountFetcher, accountID string) struct { 145 name string 146 h httprouter.Handle 147 r *http.Request 148 } { 149 vtrackBody, err := getValidVTrackRequestBody(true, true) 150 if err != nil { 151 t.Fatal(err) 152 } 153 154 return struct { 155 name string 156 h httprouter.Handle 157 r *http.Request 158 }{ 159 name: "vast", 160 h: NewVTrackEndpoint(cfg, fetcher, &vtrackMockCacheClient{}, config.BidderInfos{}, &metrics.MetricsEngineMock{}), 161 r: httptest.NewRequest("POST", "/vtrack?a="+accountID, strings.NewReader(vtrackBody)), 162 } 163 }