github.com/prebid/prebid-server/v2@v2.18.0/analytics/filesystem/file_module_test.go (about)

     1  package filesystem
     2  
     3  import (
     4  	"net/http"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/prebid/prebid-server/v2/analytics"
    10  	"github.com/prebid/prebid-server/v2/config"
    11  
    12  	"github.com/prebid/openrtb/v20/openrtb2"
    13  )
    14  
    15  const TEST_DIR string = "testFiles"
    16  
    17  func TestAmpObject_ToJson(t *testing.T) {
    18  	ao := &analytics.AmpObject{
    19  		Status:             http.StatusOK,
    20  		Errors:             make([]error, 0),
    21  		AuctionResponse:    &openrtb2.BidResponse{},
    22  		AmpTargetingValues: map[string]string{},
    23  	}
    24  	if aoJson := jsonifyAmpObject(ao); strings.Contains(aoJson, "Transactional Logs Error") {
    25  		t.Fatalf("AmpObject failed to convert to json")
    26  	}
    27  }
    28  
    29  func TestAuctionObject_ToJson(t *testing.T) {
    30  	ao := &analytics.AuctionObject{
    31  		Status: http.StatusOK,
    32  	}
    33  	if aoJson := jsonifyAuctionObject(ao); strings.Contains(aoJson, "Transactional Logs Error") {
    34  		t.Fatalf("AuctionObject failed to convert to json")
    35  	}
    36  }
    37  
    38  func TestVideoObject_ToJson(t *testing.T) {
    39  	vo := &analytics.VideoObject{
    40  		Status: http.StatusOK,
    41  	}
    42  	if voJson := jsonifyVideoObject(vo); strings.Contains(voJson, "Transactional Logs Error") {
    43  		t.Fatalf("AuctionObject failed to convert to json")
    44  	}
    45  }
    46  
    47  func TestSetUIDObject_ToJson(t *testing.T) {
    48  	so := &analytics.SetUIDObject{
    49  		Status: http.StatusOK,
    50  		Bidder: "any-bidder",
    51  		UID:    "uid string",
    52  	}
    53  	if soJson := jsonifySetUIDObject(so); strings.Contains(soJson, "Transactional Logs Error") {
    54  		t.Fatalf("SetUIDObject failed to convert to json")
    55  	}
    56  }
    57  
    58  func TestCookieSyncObject_ToJson(t *testing.T) {
    59  	cso := &analytics.CookieSyncObject{
    60  		Status:       http.StatusOK,
    61  		BidderStatus: []*analytics.CookieSyncBidder{},
    62  	}
    63  	if csoJson := jsonifyCookieSync(cso); strings.Contains(csoJson, "Transactional Logs Error") {
    64  		t.Fatalf("CookieSyncObject failed to convert to json")
    65  	}
    66  }
    67  
    68  func TestLogNotificationEventObject_ToJson(t *testing.T) {
    69  	neo := &analytics.NotificationEvent{
    70  		Request: &analytics.EventRequest{
    71  			Bidder: "bidder",
    72  		},
    73  		Account: &config.Account{
    74  			ID: "id",
    75  		},
    76  	}
    77  	if neoJson := jsonifyNotificationEventObject(neo); strings.Contains(neoJson, "Transactional Logs Error") {
    78  		t.Fatalf("NotificationEventObject failed to convert to json")
    79  	}
    80  }
    81  
    82  func TestFileLogger_LogObjects(t *testing.T) {
    83  	if _, err := os.Stat(TEST_DIR); os.IsNotExist(err) {
    84  		if err = os.MkdirAll(TEST_DIR, 0755); err != nil {
    85  			t.Fatalf("Could not create test directory for FileLogger")
    86  		}
    87  	}
    88  	defer os.RemoveAll(TEST_DIR)
    89  	if fl, err := NewFileLogger(TEST_DIR + "//test"); err == nil {
    90  		fl.LogAuctionObject(&analytics.AuctionObject{})
    91  		fl.LogVideoObject(&analytics.VideoObject{})
    92  		fl.LogAmpObject(&analytics.AmpObject{})
    93  		fl.LogSetUIDObject(&analytics.SetUIDObject{})
    94  		fl.LogCookieSyncObject(&analytics.CookieSyncObject{})
    95  		fl.LogNotificationEventObject(&analytics.NotificationEvent{})
    96  	} else {
    97  		t.Fatalf("Couldn't initialize file logger: %v", err)
    98  	}
    99  }