Skip to content

Commit

Permalink
Reject request if stored auction or stored bid response not found (#2691
Browse files Browse the repository at this point in the history
)
  • Loading branch information
VeronikaSolovei9 authored Apr 18, 2023
1 parent 8a48df0 commit 1ed4ebf
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 20 deletions.
22 changes: 8 additions & 14 deletions endpoints/openrtb2/auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4305,13 +4305,10 @@ func TestParseRequestStoredResponses(t *testing.T) {
expectedErrorCount: 0,
},
{
name: "req has two imps with missing stored responses",
givenRequestBody: validRequest(t, "req-two-imps-missing-stored-response.json"),
expectedStoredResponses: map[string]json.RawMessage{
"imp-id1": json.RawMessage(`[{"bid": [{"id": "bid_id1"],"seat": "appnexus"}]`),
"imp-id2": json.RawMessage(nil),
},
expectedErrorCount: 0,
name: "req has two imps with missing stored responses",
givenRequestBody: validRequest(t, "req-two-imps-missing-stored-response.json"),
expectedStoredResponses: nil,
expectedErrorCount: 2,
},
{
name: "req has two imps: one with stored response and another imp without stored resp",
Expand Down Expand Up @@ -4404,13 +4401,10 @@ func TestParseRequestStoredBidResponses(t *testing.T) {
expectedErrorCount: 0,
},
{
name: "req has two imps with missing stored bid responses",
givenRequestBody: validRequest(t, "req-two-imps-missing-stored-bid-response.json"),
expectedStoredBidResponses: map[string]map[string]json.RawMessage{
"imp-id1": {"testBidder1": nil},
"imp-id2": {"testBidder2": nil},
},
expectedErrorCount: 0,
name: "req has two imps with missing stored bid responses",
givenRequestBody: validRequest(t, "req-two-imps-missing-stored-bid-response.json"),
expectedStoredBidResponses: nil,
expectedErrorCount: 2,
},
}
for _, test := range tests {
Expand Down
22 changes: 16 additions & 6 deletions stored_responses/stored_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ func ProcessStoredResponses(ctx context.Context, requestJson []byte, storedRespF
}
bidderImpIdReplaceImp := flipMap(impBidderReplaceImp)

impIdToStoredResp, impBidderToStoredBidResponse := buildStoredResponsesMaps(storedResponses, impBidderToStoredBidResponseId, impIdToRespId)
return impIdToStoredResp, impBidderToStoredBidResponse, bidderImpIdReplaceImp, nil
impIdToStoredResp, impBidderToStoredBidResponse, errs := buildStoredResponsesMaps(storedResponses, impBidderToStoredBidResponseId, impIdToRespId)

return impIdToStoredResp, impBidderToStoredBidResponse, bidderImpIdReplaceImp, errs
}
return nil, nil, nil, nil
}
Expand All @@ -166,24 +167,33 @@ func flipMap(impBidderReplaceImpId ImpBidderReplaceImpID) BidderImpReplaceImpID
return flippedMap
}

func buildStoredResponsesMaps(storedResponses StoredResponseIdToStoredResponse, impBidderToStoredBidResponseId ImpBiddersWithBidResponseIDs, impIdToRespId ImpsWithAuctionResponseIDs) (ImpsWithBidResponses, ImpBidderStoredResp) {
func buildStoredResponsesMaps(storedResponses StoredResponseIdToStoredResponse, impBidderToStoredBidResponseId ImpBiddersWithBidResponseIDs, impIdToRespId ImpsWithAuctionResponseIDs) (ImpsWithBidResponses, ImpBidderStoredResp, []error) {
var errs []error
//imp id to stored resp body
impIdToStoredResp := ImpsWithBidResponses{}
//stored bid responses: imp id to bidder to stored response body
impBidderToStoredBidResponse := ImpBidderStoredResp{}

for impId, respId := range impIdToRespId {
impIdToStoredResp[impId] = storedResponses[respId]
if len(storedResponses[respId]) == 0 {
errs = append(errs, fmt.Errorf("failed to fetch stored auction response for impId = %s and storedAuctionResponse id = %s", impId, respId))
} else {
impIdToStoredResp[impId] = storedResponses[respId]
}
}

for impId, bidderStoredResp := range impBidderToStoredBidResponseId {
bidderStoredResponses := StoredResponseIdToStoredResponse{}
for bidderName, id := range bidderStoredResp {
bidderStoredResponses[bidderName] = storedResponses[id]
if len(storedResponses[id]) == 0 {
errs = append(errs, fmt.Errorf("failed to fetch stored bid response for impId = %s, bidder = %s and storedBidResponse id = %s", impId, bidderName, id))
} else {
bidderStoredResponses[bidderName] = storedResponses[id]
}
}
impBidderToStoredBidResponse[impId] = bidderStoredResponses
}
return impIdToStoredResp, impBidderToStoredBidResponse
return impIdToStoredResp, impBidderToStoredBidResponse, errs
}

// parseImpInfo parses the request JSON and returns the impressions with their unmarshalled imp.ext.prebid
Expand Down
207 changes: 207 additions & 0 deletions stored_responses/stored_responses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,213 @@ func TestProcessStoredAuctionAndBidResponses(t *testing.T) {

}

func TestProcessStoredResponsesNotFoundResponse(t *testing.T) {
bidderMap := map[string]openrtb_ext.BidderName{"bidderA": "bidderA", "bidderB": "bidderB"}
bidStoredResp1 := json.RawMessage(`[{"bid": [{"id": "bid_id1"],"seat": "bidderA"}]`)
bidStoredResp2 := json.RawMessage(`[{"bid": [{"id": "bid_id2"],"seat": "bidderB"}]`)
mockStoredResponses := map[string]json.RawMessage{
"1": bidStoredResp1,
"2": bidStoredResp2,
"3": nil,
"4": nil,
}
fetcher := &mockStoredBidResponseFetcher{mockStoredResponses}

testCases := []struct {
description string
requestJson []byte
expectedErrors []error
}{
{
description: "Stored bid response with nil data, one bidder one imp",
requestJson: []byte(`{"imp": [
{
"id": "imp-id1",
"ext": {
"appnexus": {
"placementId": 123
},
"prebid": {
"storedbidresponse": [
{"bidder":"bidderB", "id": "3"}
]
}
}
}
]}`),
expectedErrors: []error{
errors.New("failed to fetch stored bid response for impId = imp-id1, bidder = bidderB and storedBidResponse id = 3"),
},
},
{
description: "Stored bid response with nil data, one bidder, two imps, one with correct stored response",
requestJson: []byte(`{"imp": [
{
"id": "imp-id1",
"ext": {
"appnexus": {
"placementId": 123
},
"prebid": {
"storedbidresponse": [
{"bidder":"bidderB", "id": "1"}
]
}
}
},
{
"id": "imp-id2",
"ext": {
"appnexus": {
"placementId": 123
},
"prebid": {
"storedbidresponse": [
{"bidder":"bidderB", "id": "3"}
]
}
}
}
]}`),
expectedErrors: []error{
errors.New("failed to fetch stored bid response for impId = imp-id2, bidder = bidderB and storedBidResponse id = 3"),
},
},
{
description: "Stored bid response with nil data, one bidder, two imps, both with correct stored response",
requestJson: []byte(`{"imp": [
{
"id": "imp-id1",
"ext": {
"appnexus": {
"placementId": 123
},
"prebid": {
"storedbidresponse": [
{"bidder":"bidderB", "id": "4"}
]
}
}
},
{
"id": "imp-id2",
"ext": {
"appnexus": {
"placementId": 123
},
"prebid": {
"storedbidresponse": [
{"bidder":"bidderB", "id": "3"}
]
}
}
}
]}`),
expectedErrors: []error{
errors.New("failed to fetch stored bid response for impId = imp-id1, bidder = bidderB and storedBidResponse id = 4"),
errors.New("failed to fetch stored bid response for impId = imp-id2, bidder = bidderB and storedBidResponse id = 3"),
},
},
{
description: "Stored auction response with nil data and one imp",
requestJson: []byte(`{"imp": [
{
"id": "imp-id1",
"ext": {
"appnexus": {
"placementId": 123
},
"prebid": {
"storedauctionresponse": {
"id": "4"
}
}
}
}
]}`),
expectedErrors: []error{
errors.New("failed to fetch stored auction response for impId = imp-id1 and storedAuctionResponse id = 4"),
},
},
{
description: "Stored auction response with nil data, and two imps with nil responses",
requestJson: []byte(`{"imp": [
{
"id": "imp-id1",
"ext": {
"appnexus": {
"placementId": 123
},
"prebid": {
"storedauctionresponse": {
"id": "4"
}
}
}
},
{
"id": "imp-id2",
"ext": {
"appnexus": {
"placementId": 123
},
"prebid": {
"storedauctionresponse": {
"id": "3"
}
}
}
}
]}`),
expectedErrors: []error{
errors.New("failed to fetch stored auction response for impId = imp-id1 and storedAuctionResponse id = 4"),
errors.New("failed to fetch stored auction response for impId = imp-id2 and storedAuctionResponse id = 3"),
},
},
{
description: "Stored auction response with nil data, two imps, one with nil responses",
requestJson: []byte(`{"imp": [
{
"id": "imp-id1",
"ext": {
"appnexus": {
"placementId": 123
},
"prebid": {
"storedauctionresponse": {
"id": "2"
}
}
}
},
{
"id": "imp-id2",
"ext": {
"appnexus": {
"placementId": 123
},
"prebid": {
"storedauctionresponse": {
"id": "3"
}
}
}
}
]}`),
expectedErrors: []error{
errors.New("failed to fetch stored auction response for impId = imp-id2 and storedAuctionResponse id = 3"),
},
},
}

for _, test := range testCases {
_, _, _, errorList := ProcessStoredResponses(nil, test.requestJson, fetcher, bidderMap)
for _, err := range test.expectedErrors {
assert.Contains(t, errorList, err, "incorrect errors returned: %s", test.description)
}
}
}

func TestFlipMap(t *testing.T) {
testCases := []struct {
description string
Expand Down

0 comments on commit 1ed4ebf

Please sign in to comment.