github.com/prebid/prebid-server/v2@v2.18.0/adapters/seedingAlliance/seedingAlliance_test.go (about)

     1  package seedingAlliance
     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.BidderSeedingAlliance, config.Adapter{
    17  		Endpoint: "https://mockup.seeding-alliance.de/?ssp={{.AccountID}}",
    18  	}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})
    19  	if buildErr != nil {
    20  		t.Fatalf("Builder returned unexpected error %v", buildErr)
    21  	}
    22  
    23  	adapterstest.RunJSONBidderTest(t, "seedingAlliancetest", bidder)
    24  }
    25  
    26  func TestResolvePriceMacro(t *testing.T) {
    27  	adm := `{"link":{"url":"https://some_url.com/abc123?wp=${AUCTION_PRICE}"}`
    28  	want := `{"link":{"url":"https://some_url.com/abc123?wp=12.34"}`
    29  
    30  	bid := openrtb2.Bid{AdM: adm, Price: 12.34}
    31  	resolvePriceMacro(&bid)
    32  
    33  	if bid.AdM != want {
    34  		t.Fatalf("want: %v, got: %v", want, bid.AdM)
    35  	}
    36  }
    37  
    38  func TestGetMediaTypeForBid(t *testing.T) {
    39  	tests := []struct {
    40  		name           string
    41  		want           openrtb_ext.BidType
    42  		invalidJSON    bool
    43  		wantErr        bool
    44  		wantErrContain string
    45  		bidType        openrtb_ext.BidType
    46  	}{
    47  		{
    48  			name:           "native",
    49  			want:           openrtb_ext.BidTypeNative,
    50  			invalidJSON:    false,
    51  			wantErr:        false,
    52  			wantErrContain: "",
    53  			bidType:        "native",
    54  		},
    55  		{
    56  			name:           "banner",
    57  			want:           openrtb_ext.BidTypeBanner,
    58  			invalidJSON:    false,
    59  			wantErr:        false,
    60  			wantErrContain: "",
    61  			bidType:        "banner",
    62  		},
    63  		{
    64  			name:           "video",
    65  			want:           openrtb_ext.BidTypeVideo,
    66  			invalidJSON:    false,
    67  			wantErr:        false,
    68  			wantErrContain: "",
    69  			bidType:        "video",
    70  		},
    71  		{
    72  			name:           "audio",
    73  			want:           openrtb_ext.BidTypeAudio,
    74  			invalidJSON:    false,
    75  			wantErr:        false,
    76  			wantErrContain: "",
    77  			bidType:        "audio",
    78  		},
    79  		{
    80  			name:           "empty type",
    81  			want:           "",
    82  			invalidJSON:    false,
    83  			wantErr:        true,
    84  			wantErrContain: "invalid BidType",
    85  			bidType:        "",
    86  		},
    87  		{
    88  			name:           "invalid type",
    89  			want:           "",
    90  			invalidJSON:    false,
    91  			wantErr:        true,
    92  			wantErrContain: "invalid BidType",
    93  			bidType:        "invalid",
    94  		},
    95  		{
    96  			name:           "invalid json",
    97  			want:           "",
    98  			invalidJSON:    true,
    99  			wantErr:        true,
   100  			wantErrContain: "bid.Ext.Prebid is empty",
   101  			bidType:        "",
   102  		},
   103  	}
   104  
   105  	for _, test := range tests {
   106  		var bid openrtb2.SeatBid
   107  		var extBid openrtb_ext.ExtBid
   108  
   109  		var bidExtJsonString string
   110  		if test.invalidJSON {
   111  			bidExtJsonString = `{"x_prebid": {"type":""}}`
   112  		} else {
   113  			bidExtJsonString = `{"prebid": {"type":"` + string(test.bidType) + `"}}`
   114  		}
   115  
   116  		if err := bid.Ext.UnmarshalJSON([]byte(bidExtJsonString)); err != nil {
   117  			t.Fatalf("unexpected error %v", err)
   118  		}
   119  
   120  		if err := json.Unmarshal(bid.Ext, &extBid); err != nil {
   121  			t.Fatalf("could not unmarshal extBid: %v", err)
   122  		}
   123  
   124  		got, gotErr := getMediaTypeForBid(bid.Ext)
   125  		assert.Equal(t, test.want, got)
   126  
   127  		if test.wantErr {
   128  			if gotErr != nil {
   129  				assert.Contains(t, gotErr.Error(), test.wantErrContain)
   130  				continue
   131  			}
   132  			t.Fatalf("wantErr: %v, gotErr: %v", test.wantErr, gotErr)
   133  		}
   134  	}
   135  }
   136  
   137  func TestGetExtInfo(t *testing.T) {
   138  	type args struct {
   139  		adUnitId  string
   140  		seatId    string
   141  		accountId string
   142  	}
   143  	tests := []struct {
   144  		name              string
   145  		expectedAdUnitID  string
   146  		expectedAccountId string
   147  		data              args
   148  		wantErr           bool
   149  	}{
   150  		{"regular case", "abc123", "pbs", args{adUnitId: "abc123"}, false},
   151  		{"nil case", "", "pbs", args{adUnitId: ""}, false},
   152  		{"unmarshal err case", "", "pbs", args{adUnitId: ""}, true},
   153  		{"seatId case", "abc123", "seat1", args{adUnitId: "abc123", seatId: "seat1"}, false},
   154  		{"accountId case", "abc123", "account1", args{adUnitId: "abc123", accountId: "account1"}, false},
   155  		{"accountId and seatId case", "abc123", "account1", args{adUnitId: "abc123", accountId: "account1", seatId: "seat1"}, false},
   156  	}
   157  
   158  	for _, test := range tests {
   159  		extSA, err := json.Marshal(openrtb_ext.ImpExtSeedingAlliance{AdUnitID: test.data.adUnitId, SeatID: test.data.seatId, AccountID: test.data.accountId})
   160  		if err != nil {
   161  			t.Fatalf("unexpected error %v", err)
   162  		}
   163  
   164  		extBidder, err := json.Marshal(adapters.ExtImpBidder{Bidder: extSA})
   165  		if err != nil {
   166  			t.Fatalf("unexpected error %v", err)
   167  		}
   168  
   169  		if test.wantErr {
   170  			extBidder = []byte{}
   171  		}
   172  
   173  		ortbImp := openrtb2.Imp{Ext: extBidder}
   174  		accountId, err := getExtInfo(&ortbImp)
   175  		if err != nil {
   176  			if test.wantErr {
   177  				continue
   178  			}
   179  			t.Fatalf("unexpected error %v", err)
   180  		}
   181  
   182  		if test.expectedAdUnitID != ortbImp.TagID {
   183  			t.Fatalf("want: %v, got: %v", test.expectedAdUnitID, ortbImp.TagID)
   184  		}
   185  
   186  		if test.expectedAccountId != accountId {
   187  			t.Fatalf("want: %v, got: %v", test.expectedAccountId, accountId)
   188  		}
   189  	}
   190  }
   191  
   192  func TestCurExists(t *testing.T) {
   193  	tests := []struct {
   194  		name string
   195  		cur  string
   196  		data []string
   197  		want bool
   198  	}{
   199  		{"no eur", "EUR", []string{"USD"}, false},
   200  		{"eur exists", "EUR", []string{"USD", "EUR"}, true},
   201  	}
   202  
   203  	for _, test := range tests {
   204  		got := curExists(test.data, test.cur)
   205  		assert.Equal(t, test.want, got)
   206  	}
   207  }