Skip to content
This repository has been archived by the owner on Dec 22, 2022. It is now read-only.

Commit

Permalink
New bid adapter for Smaato (prebid#1413)
Browse files Browse the repository at this point in the history
Co-authored-by: vikram <vikram.chandel@smaato.com>
Co-authored-by: Stephan <s@brosinski.com>
  • Loading branch information
3 people authored Aug 6, 2020
1 parent 3e2a8d9 commit 25c66c4
Show file tree
Hide file tree
Showing 25 changed files with 1,570 additions and 0 deletions.
53 changes: 53 additions & 0 deletions adapters/smaato/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package smaato

import (
"encoding/json"
"fmt"
"net/url"
"strings"
)

type imageAd struct {
Image image `json:"image"`
}
type image struct {
Img img `json:"img"`
Impressiontrackers []string `json:"impressiontrackers"`
Clicktrackers []string `json:"clicktrackers"`
}
type img struct {
URL string `json:"url"`
W int `json:"w"`
H int `json:"h"`
Ctaurl string `json:"ctaurl"`
}

func extractAdmImage(adapterResponseAdm string) (string, error) {
var imgMarkup string
var err error

var imageAd imageAd
err = json.Unmarshal([]byte(adapterResponseAdm), &imageAd)
var image = imageAd.Image

if err == nil {
var clickEvent strings.Builder
var impressionTracker strings.Builder

for _, clicktracker := range image.Clicktrackers {
clickEvent.WriteString("fetch(decodeURIComponent('" + url.QueryEscape(clicktracker) + "'.replace(/\\+/g, ' ')), " +
"{cache: 'no-cache'});")
}

for _, impression := range image.Impressiontrackers {

impressionTracker.WriteString(fmt.Sprintf(`<img src="%s" alt="" width="0" height="0"/>`, impression))
}

imgMarkup = fmt.Sprintf(`<div style="cursor:pointer" onclick="%s;window.open(decodeURIComponent('%s'.replace(/\+/g, ' ')));"><img src="%s" width="%d" height="%d"/>%s</div>`,
&clickEvent, url.QueryEscape(image.Img.Ctaurl), image.
Img.URL, image.Img.W, image.Img.
H, &impressionTracker)
}
return imgMarkup, err
}
44 changes: 44 additions & 0 deletions adapters/smaato/image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package smaato

import (
"testing"
)

func TestRenderAdMarkup(t *testing.T) {
type args struct {
adType adMarkupType
adapterResponseAdm string
}
expectedResult := `<div style="cursor:pointer"` +
` onclick="fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F1'.replace(/\+/g, ' ')),` +
` {cache: 'no-cache'});fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F2'.replace(/\+/g, ' ')),` +
` {cache: 'no-cache'});;window.open(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fctaurl%2F1'.replace(/\+/g, ' ')));">` +
`<img src="//prebid-test.smaatolabs.net/img/320x50.jpg" width="350" height="50"/>` +
`<img src="//prebid-test.smaatolabs.net/track/imp/1" alt="" width="0" height="0"/>` +
`<img src="//prebid-test.smaatolabs.net/track/imp/2" alt="" width="0" height="0"/></div>`

tests := []struct {
testName string
args args
result string
}{
{"imageTest", args{"Img",
"{\"image\":{\"img\":{\"url\":\"//prebid-test.smaatolabs.net/img/320x50.jpg\"," +
"\"w\":350,\"h\":50,\"ctaurl\":\"//prebid-test.smaatolabs.net/track/ctaurl/1\"}," +
"\"impressiontrackers\":[\"//prebid-test.smaatolabs.net/track/imp/1\",\"//prebid-test.smaatolabs.net/track/imp/2\"]," +
"\"clicktrackers\":[\"//prebid-test.smaatolabs.net/track/click/1\",\"//prebid-test.smaatolabs.net/track/click/2\"]}}"},
expectedResult,
},
}
for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
got, err := renderAdMarkup(tt.args.adType, tt.args.adapterResponseAdm)
if err != nil {
t.Errorf("error rendering ad markup: %v", err)
}
if got != tt.result {
t.Errorf("renderAdMarkup() got = %v, result %v", got, tt.result)
}
})
}
}
65 changes: 65 additions & 0 deletions adapters/smaato/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package smaato

import (
"encoding/json"
"testing"

"github.com/prebid/prebid-server/openrtb_ext"
)

// This file intends to test static/bidder-params/smaato.json

// These also validate the format of the external API: request.imp[i].bidRequestExt.smaato

// TestValidParams makes sure that the Smaato schema accepts all imp.bidRequestExt fields which Smaato supports.
func TestValidParams(t *testing.T) {
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
if err != nil {
t.Fatalf("Failed to fetch the json-schemas. %v", err)
}

for _, validParam := range validParams {
if err := validator.Validate(openrtb_ext.BidderSmaato, json.RawMessage(validParam)); err != nil {
t.Errorf("Schema rejected smaato params: %s \n Error: %s", validParam, err)
}
}
}

// TestInvalidParams makes sure that the Smaato schema rejects all the imp.bidRequestExt fields which are not support.
func TestInvalidParams(t *testing.T) {
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
if err != nil {
t.Fatalf("Failed to fetch the json-schemas. %v", err)
}

for _, invalidParam := range invalidParams {
if err := validator.Validate(openrtb_ext.BidderSmaato, json.RawMessage(invalidParam)); err == nil {
t.Errorf("Schema allowed unexpected params: %s", invalidParam)
}
}
}

var validParams = []string{
`{"publisherId":"test-id-1234-smaato","adspaceId": "1123581321"}`,
}

var invalidParams = []string{
``,
`null`,
`true`,
`5`,
`4.2`,
`[]`,
`{}`,
`{"publisherId":"test-id-1234-smaato"}`,
`{"adspaceId": "1123581321"}`,
`{"publisherId":false}`,
`{"adspaceId":false}`,
`{"publisherId":0,"adspaceId": 1123581321}`,
`{"publisherId":false,"adspaceId": true}`,
`{"instl": 0}`,
`{"secure": 0}`,
`{"adspaceId": "1123581321","instl": 0,"secure": 0}`,
`{"instl": 0,"secure": 0}`,
`{"publisherId":"test-id-1234-smaato","instl": 0,"secure": 0}`,
}
52 changes: 52 additions & 0 deletions adapters/smaato/richmedia.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package smaato

import (
"encoding/json"
"fmt"
"net/url"
"strings"
)

type richMediaAd struct {
RichMedia richmedia `json:"richmedia"`
}
type mediadata struct {
Content string `json:"content"`
W int `json:"w"`
H int `json:"h"`
}

type richmedia struct {
MediaData mediadata `json:"mediadata"`
Impressiontrackers []string `json:"impressiontrackers"`
Clicktrackers []string `json:"clicktrackers"`
}

func extractAdmRichMedia(adapterResponseAdm string) (string, error) {
var richMediaMarkup string
var err error

var richMediaAd richMediaAd
err = json.Unmarshal([]byte(adapterResponseAdm), &richMediaAd)
var richMedia = richMediaAd.RichMedia

if err == nil {
var clickEvent strings.Builder
var impressionTracker strings.Builder

for _, clicktracker := range richMedia.Clicktrackers {
clickEvent.WriteString("fetch(decodeURIComponent('" + url.QueryEscape(clicktracker) + "'), " +
"{cache: 'no-cache'});")
}
for _, impression := range richMedia.Impressiontrackers {

impressionTracker.WriteString(fmt.Sprintf(`<img src="%s" alt="" width="0" height="0"/>`, impression))
}

richMediaMarkup = fmt.Sprintf(`<div onclick="%s">%s%s</div>`,
&clickEvent,
richMedia.MediaData.Content,
&impressionTracker)
}
return richMediaMarkup, err
}
39 changes: 39 additions & 0 deletions adapters/smaato/richmedia_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package smaato

import (
"testing"
)

func TestExtractAdmRichMedia(t *testing.T) {
type args struct {
adType adMarkupType
adapterResponseAdm string
}
expectedResult := `<div onclick="fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F1'),` +
` {cache: 'no-cache'});fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F2'),` +
` {cache: 'no-cache'});"><div>hello</div><img src="//prebid-test.smaatolabs.net/track/imp/1" alt="" width="0" height="0"/>` +
`<img src="//prebid-test.smaatolabs.net/track/imp/2" alt="" width="0" height="0"/></div>`
tests := []struct {
testName string
args args
result string
}{
{"richmediaTest", args{"Richmedia", "{\"richmedia\":{\"mediadata\":{\"content\":\"<div>hello</div>\"," +
"" + "\"w\":350," +
"\"h\":50},\"impressiontrackers\":[\"//prebid-test.smaatolabs.net/track/imp/1\",\"//prebid-test.smaatolabs.net/track/imp/2\"]," +
"\"clicktrackers\":[\"//prebid-test.smaatolabs.net/track/click/1\",\"//prebid-test.smaatolabs.net/track/click/2\"]}}"},
expectedResult,
},
}
for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
got, err := renderAdMarkup(tt.args.adType, tt.args.adapterResponseAdm)
if err != nil {
t.Errorf("error rendering ad markup: %v", err)
}
if got != tt.result {
t.Errorf("renderAdMarkup() got = %v, result %v", got, tt.result)
}
})
}
}
Loading

0 comments on commit 25c66c4

Please sign in to comment.