github.com/prebid/prebid-server/v2@v2.18.0/adapters/adgeneration/adgeneration_test.go (about)

     1  package adgeneration
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/prebid/openrtb/v20/openrtb2"
     8  	"github.com/prebid/prebid-server/v2/adapters"
     9  	"github.com/prebid/prebid-server/v2/adapters/adapterstest"
    10  	"github.com/prebid/prebid-server/v2/config"
    11  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestJsonSamples(t *testing.T) {
    16  	bidder, buildErr := Builder(openrtb_ext.BidderAdgeneration, config.Adapter{
    17  		Endpoint: "https://d.socdm.com/adsv/v1"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})
    18  
    19  	if buildErr != nil {
    20  		t.Fatalf("Builder returned unexpected error %v", buildErr)
    21  	}
    22  
    23  	adapterstest.RunJSONBidderTest(t, "adgenerationtest", bidder)
    24  }
    25  
    26  func TestGetRequestUri(t *testing.T) {
    27  	bidder, buildErr := Builder(openrtb_ext.BidderAdgeneration, config.Adapter{
    28  		Endpoint: "https://d.socdm.com/adsv/v1"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})
    29  
    30  	if buildErr != nil {
    31  		t.Fatalf("Builder returned unexpected error %v", buildErr)
    32  	}
    33  
    34  	bidderAdgeneration, _ := bidder.(*AdgenerationAdapter)
    35  
    36  	// Test items
    37  	failedRequest := &openrtb2.BidRequest{
    38  		ID: "test-failed-bid-request",
    39  		Imp: []openrtb2.Imp{
    40  			{ID: "extImpBidder-failed-test", Banner: &openrtb2.Banner{Format: []openrtb2.Format{{W: 300, H: 250}}}, Ext: json.RawMessage(`{{ "id": "58278" }}`)},
    41  			{ID: "extImpBidder-failed-test", Banner: &openrtb2.Banner{Format: []openrtb2.Format{{W: 300, H: 250}}}, Ext: json.RawMessage(`{"_bidder": { "id": "58278" }}`)},
    42  			{ID: "extImpAdgeneration-failed-test", Banner: &openrtb2.Banner{Format: []openrtb2.Format{{W: 300, H: 250}}}, Ext: json.RawMessage(`{"bidder": { "_id": "58278" }}`)},
    43  		},
    44  		Source: &openrtb2.Source{TID: "SourceTID"},
    45  		Device: &openrtb2.Device{UA: "testUA", IP: "testIP"},
    46  		Site:   &openrtb2.Site{Page: "https://supership.com"},
    47  		User:   &openrtb2.User{BuyerUID: "buyerID"},
    48  	}
    49  	successRequest := &openrtb2.BidRequest{
    50  		ID: "test-success-bid-request",
    51  		Imp: []openrtb2.Imp{
    52  			{ID: "bidRequest-success-test", Banner: &openrtb2.Banner{Format: []openrtb2.Format{{W: 300, H: 250}}}, Ext: json.RawMessage(`{"bidder": { "id": "58278" }}`)},
    53  		},
    54  		Source: &openrtb2.Source{TID: "SourceTID"},
    55  		Device: &openrtb2.Device{UA: "testUA", IP: "testIP"},
    56  		Site:   &openrtb2.Site{Page: "https://supership.com"},
    57  		User:   &openrtb2.User{BuyerUID: "buyerID"},
    58  	}
    59  
    60  	numRequests := len(failedRequest.Imp)
    61  	for index := 0; index < numRequests; index++ {
    62  		httpRequests, err := bidderAdgeneration.getRequestUri(failedRequest, index)
    63  		if err == nil {
    64  			t.Errorf("getRequestUri: %v did not throw an error", failedRequest.Imp[index])
    65  		}
    66  		if httpRequests != "" {
    67  			t.Errorf("getRequestUri: %v did return Request: %s", failedRequest.Imp[index], httpRequests)
    68  		}
    69  	}
    70  	numRequests = len(successRequest.Imp)
    71  	for index := 0; index < numRequests; index++ {
    72  		// getRawQuery Test.
    73  		adgExt, err := unmarshalExtImpAdgeneration(&successRequest.Imp[index])
    74  		if err != nil {
    75  			t.Errorf("unmarshalExtImpAdgeneration: %v did throw an error: %v", successRequest.Imp[index], err)
    76  		}
    77  		rawQuery := bidderAdgeneration.getRawQuery(adgExt.Id, successRequest, &successRequest.Imp[index])
    78  		expectQueries := map[string]string{
    79  			"posall":        "SSPLOC",
    80  			"id":            adgExt.Id,
    81  			"sdktype":       "0",
    82  			"hb":            "true",
    83  			"currency":      bidderAdgeneration.getCurrency(successRequest),
    84  			"sdkname":       "prebidserver",
    85  			"adapterver":    bidderAdgeneration.version,
    86  			"sizes":         getSizes(&successRequest.Imp[index]),
    87  			"tp":            successRequest.Site.Page,
    88  			"transactionid": successRequest.Source.TID,
    89  		}
    90  		for key, expectedValue := range expectQueries {
    91  			actualValue := rawQuery.Get(key)
    92  			if actualValue != expectedValue {
    93  				t.Errorf("getRawQuery: %s value does not match expected %s, actual %s", key, expectedValue, actualValue)
    94  			}
    95  		}
    96  
    97  		// RequestUri Test.
    98  		actualUri, err := bidderAdgeneration.getRequestUri(successRequest, index)
    99  		if err != nil {
   100  			t.Errorf("getRequestUri: %v did throw an error: %v", successRequest.Imp[index], err)
   101  		}
   102  		expectedUri := "https://d.socdm.com/adsv/v1?adapterver=" + bidderAdgeneration.version + "&currency=JPY&hb=true&id=58278&posall=SSPLOC&sdkname=prebidserver&sdktype=0&sizes=300x250&t=json3&tp=https%3A%2F%2Fsupership.com&transactionid=SourceTID"
   103  		if actualUri != expectedUri {
   104  			t.Errorf("getRequestUri: does not match expected %s, actual %s", expectedUri, actualUri)
   105  		}
   106  	}
   107  }
   108  
   109  func TestGetSizes(t *testing.T) {
   110  	// Test items
   111  	var request *openrtb2.Imp
   112  	var size string
   113  	multiFormatBanner := &openrtb2.Banner{Format: []openrtb2.Format{{W: 300, H: 250}, {W: 320, H: 50}}}
   114  	noFormatBanner := &openrtb2.Banner{Format: []openrtb2.Format{}}
   115  	nativeFormat := &openrtb2.Native{}
   116  
   117  	request = &openrtb2.Imp{Banner: multiFormatBanner}
   118  	size = getSizes(request)
   119  	if size != "300x250,320x50" {
   120  		t.Errorf("%v does not match size.", multiFormatBanner)
   121  	}
   122  	request = &openrtb2.Imp{Banner: noFormatBanner}
   123  	size = getSizes(request)
   124  	if size != "" {
   125  		t.Errorf("%v does not match size.", noFormatBanner)
   126  	}
   127  	request = &openrtb2.Imp{Native: nativeFormat}
   128  	size = getSizes(request)
   129  	if size != "" {
   130  		t.Errorf("%v does not match size.", nativeFormat)
   131  	}
   132  }
   133  
   134  func TestGetCurrency(t *testing.T) {
   135  	bidder, buildErr := Builder(openrtb_ext.BidderAdgeneration, config.Adapter{
   136  		Endpoint: "https://d.socdm.com/adsv/v1"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})
   137  
   138  	if buildErr != nil {
   139  		t.Fatalf("Builder returned unexpected error %v", buildErr)
   140  	}
   141  
   142  	bidderAdgeneration, _ := bidder.(*AdgenerationAdapter)
   143  
   144  	// Test items
   145  	var request *openrtb2.BidRequest
   146  	var currency string
   147  	innerDefaultCur := []string{"USD", "JPY"}
   148  	usdCur := []string{"USD", "EUR"}
   149  
   150  	request = &openrtb2.BidRequest{Cur: innerDefaultCur}
   151  	currency = bidderAdgeneration.getCurrency(request)
   152  	if currency != "JPY" {
   153  		t.Errorf("%v does not match currency.", innerDefaultCur)
   154  	}
   155  	request = &openrtb2.BidRequest{Cur: usdCur}
   156  	currency = bidderAdgeneration.getCurrency(request)
   157  	if currency != "USD" {
   158  		t.Errorf("%v does not match currency.", usdCur)
   159  	}
   160  }
   161  
   162  func TestCreateAd(t *testing.T) {
   163  	// Test items
   164  	adgBannerImpId := "test-banner-imp"
   165  	adgBannerResponse := adgServerResponse{
   166  		Ad:         "<!DOCTYPE html>\n<head>\n<meta charset=\"UTF-8\">\n<script src=\"test.com\"></script>\n<body>\n<div id=\"medibasspContainer\">\n<iframe src=\"https://dummy-iframe.com></iframe>\n</div>\n</body>\n",
   167  		Beacon:     "<img src=\"https://dummy-beacon.com\">",
   168  		Beaconurl:  "https://dummy-beacon.com",
   169  		Cpm:        50,
   170  		Creativeid: "DummyDsp_SdkTeam_supership.jp",
   171  		H:          300,
   172  		W:          250,
   173  		Ttl:        10,
   174  		LandingUrl: "",
   175  		Scheduleid: "111111",
   176  	}
   177  	matchBannerTag := "<div id=\"medibasspContainer\">\n<iframe src=\"https://dummy-iframe.com></iframe>\n</div>\n<img src=\"https://dummy-beacon.com\">"
   178  
   179  	adgVastImpId := "test-vast-imp"
   180  	adgVastResponse := adgServerResponse{
   181  		Ad:         "<!DOCTYPE html>\n<head>\n<meta charset=\"UTF-8\">\n<script src=\"test.com\"></script>\n<body>\n<div id=\"medibasspContainer\">\n<iframe src=\"https://dummy-iframe.com></iframe>\n</div>\n</body>\n",
   182  		Beacon:     "<img src=\"https://dummy-beacon.com\">",
   183  		Beaconurl:  "https://dummy-beacon.com",
   184  		Cpm:        50,
   185  		Creativeid: "DummyDsp_SdkTeam_supership.jp",
   186  		H:          300,
   187  		W:          250,
   188  		Ttl:        10,
   189  		LandingUrl: "",
   190  		Vastxml:    "<?xml version=\"1.0\"><VAST version=\"3.0\"</VAST>",
   191  		Scheduleid: "111111",
   192  	}
   193  	matchVastTag := "<div id=\"apvad-test-vast-imp\"></div><script type=\"text/javascript\" id=\"apv\" src=\"https://cdn.apvdr.com/js/VideoAd.min.js\"></script><script type=\"text/javascript\"> (function(){ new APV.VideoAd({s:\"test-vast-imp\"}).load('<?xml version=\"1.0\"><VAST version=\"3.0\"</VAST>'); })(); </script><img src=\"https://dummy-beacon.com\">"
   194  
   195  	bannerAd := createAd(&adgBannerResponse, adgBannerImpId)
   196  	if bannerAd != matchBannerTag {
   197  		t.Errorf("%v does not match createAd.", adgBannerResponse)
   198  	}
   199  	vastAd := createAd(&adgVastResponse, adgVastImpId)
   200  	if vastAd != matchVastTag {
   201  		t.Errorf("%v does not match createAd.", adgVastResponse)
   202  	}
   203  }
   204  
   205  func TestMakeBids(t *testing.T) {
   206  	bidder, buildErr := Builder(openrtb_ext.BidderAdgeneration, config.Adapter{
   207  		Endpoint: "https://d.socdm.com/adsv/v1"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})
   208  
   209  	if buildErr != nil {
   210  		t.Fatalf("Builder returned unexpected error %v", buildErr)
   211  	}
   212  
   213  	bidderAdgeneration, _ := bidder.(*AdgenerationAdapter)
   214  
   215  	internalRequest := &openrtb2.BidRequest{
   216  		ID: "test-success-bid-request",
   217  		Imp: []openrtb2.Imp{
   218  			{ID: "bidRequest-success-test", Banner: &openrtb2.Banner{Format: []openrtb2.Format{{W: 300, H: 250}}}, Ext: json.RawMessage(`{"bidder": { "id": "58278" }}`)},
   219  		},
   220  		Device: &openrtb2.Device{UA: "testUA", IP: "testIP"},
   221  		Site:   &openrtb2.Site{Page: "https://supership.com"},
   222  		User:   &openrtb2.User{BuyerUID: "buyerID"},
   223  	}
   224  	externalRequest := adapters.RequestData{}
   225  	response := adapters.ResponseData{
   226  		StatusCode: 200,
   227  		Body:       ([]byte)("{\n \"ad\": \"testAd\",\n \"cpm\": 30,\n \"creativeid\": \"Dummy_supership.jp\",\n \"h\": 250,\n \"locationid\": \"58278\",\n \"results\": [{}],\n \"dealid\": \"test-deal-id\",\n \"w\": 300\n }"),
   228  	}
   229  	// default Currency InternalRequest
   230  	defaultCurBidderResponse, errs := bidder.MakeBids(internalRequest, &externalRequest, &response)
   231  	if len(errs) > 0 {
   232  		t.Errorf("MakeBids return errors. errors: %v", errs)
   233  	}
   234  	checkBidResponse(t, defaultCurBidderResponse, bidderAdgeneration.defaultCurrency)
   235  
   236  	// Specified Currency InternalRequest
   237  	usdCur := "USD"
   238  	internalRequest.Cur = []string{usdCur}
   239  	specifiedCurBidderResponse, errs := bidder.MakeBids(internalRequest, &externalRequest, &response)
   240  	if len(errs) > 0 {
   241  		t.Errorf("MakeBids return errors. errors: %v", errs)
   242  	}
   243  	checkBidResponse(t, specifiedCurBidderResponse, usdCur)
   244  
   245  }
   246  
   247  func checkBidResponse(t *testing.T, bidderResponse *adapters.BidderResponse, expectedCurrency string) {
   248  	if bidderResponse == nil {
   249  		t.Errorf("actual bidResponse is nil.")
   250  	}
   251  
   252  	// AdM is assured by TestCreateAd and JSON tests
   253  	var expectedAdM string = "testAd"
   254  	var expectedID string = "58278"
   255  	var expectedImpID = "bidRequest-success-test"
   256  	var expectedPrice float64 = 30.0
   257  	var expectedW int64 = 300
   258  	var expectedH int64 = 250
   259  	var expectedCrID string = "Dummy_supership.jp"
   260  	var extectedDealID string = "test-deal-id"
   261  
   262  	assert.Equal(t, expectedCurrency, bidderResponse.Currency)
   263  	assert.Equal(t, 1, len(bidderResponse.Bids))
   264  	assert.Equal(t, expectedID, bidderResponse.Bids[0].Bid.ID)
   265  	assert.Equal(t, expectedImpID, bidderResponse.Bids[0].Bid.ImpID)
   266  	assert.Equal(t, expectedAdM, bidderResponse.Bids[0].Bid.AdM)
   267  	assert.Equal(t, expectedPrice, bidderResponse.Bids[0].Bid.Price)
   268  	assert.Equal(t, expectedW, bidderResponse.Bids[0].Bid.W)
   269  	assert.Equal(t, expectedH, bidderResponse.Bids[0].Bid.H)
   270  	assert.Equal(t, expectedCrID, bidderResponse.Bids[0].Bid.CrID)
   271  	assert.Equal(t, extectedDealID, bidderResponse.Bids[0].Bid.DealID)
   272  }