github.com/prebid/prebid-server@v0.275.0/endpoints/events/account_test.go (about)

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