github.com/prebid/prebid-server@v0.275.0/analytics/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"net/http"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/prebid/openrtb/v19/openrtb2"
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"github.com/prebid/prebid-server/analytics"
    12  	"github.com/prebid/prebid-server/config"
    13  )
    14  
    15  const TEST_DIR string = "testFiles"
    16  
    17  func TestSampleModule(t *testing.T) {
    18  	var count int
    19  	am := initAnalytics(&count)
    20  	am.LogAuctionObject(&analytics.AuctionObject{
    21  		Status:   http.StatusOK,
    22  		Errors:   nil,
    23  		Response: &openrtb2.BidResponse{},
    24  	})
    25  	if count != 1 {
    26  		t.Errorf("PBSAnalyticsModule failed at LogAuctionObject")
    27  	}
    28  
    29  	am.LogSetUIDObject(&analytics.SetUIDObject{
    30  		Status:  http.StatusOK,
    31  		Bidder:  "bidders string",
    32  		UID:     "uid",
    33  		Errors:  nil,
    34  		Success: true,
    35  	})
    36  	if count != 2 {
    37  		t.Errorf("PBSAnalyticsModule failed at LogSetUIDObject")
    38  	}
    39  
    40  	am.LogCookieSyncObject(&analytics.CookieSyncObject{})
    41  	if count != 3 {
    42  		t.Errorf("PBSAnalyticsModule failed at LogCookieSyncObject")
    43  	}
    44  
    45  	am.LogAmpObject(&analytics.AmpObject{})
    46  	if count != 4 {
    47  		t.Errorf("PBSAnalyticsModule failed at LogAmpObject")
    48  	}
    49  
    50  	am.LogVideoObject(&analytics.VideoObject{})
    51  	if count != 5 {
    52  		t.Errorf("PBSAnalyticsModule failed at LogVideoObject")
    53  	}
    54  
    55  	am.LogNotificationEventObject(&analytics.NotificationEvent{})
    56  	if count != 6 {
    57  		t.Errorf("PBSAnalyticsModule failed at LogNotificationEventObject")
    58  	}
    59  }
    60  
    61  type sampleModule struct {
    62  	count *int
    63  }
    64  
    65  func (m *sampleModule) LogAuctionObject(ao *analytics.AuctionObject) { *m.count++ }
    66  
    67  func (m *sampleModule) LogVideoObject(vo *analytics.VideoObject) { *m.count++ }
    68  
    69  func (m *sampleModule) LogCookieSyncObject(cso *analytics.CookieSyncObject) { *m.count++ }
    70  
    71  func (m *sampleModule) LogSetUIDObject(so *analytics.SetUIDObject) { *m.count++ }
    72  
    73  func (m *sampleModule) LogAmpObject(ao *analytics.AmpObject) { *m.count++ }
    74  
    75  func (m *sampleModule) LogNotificationEventObject(ne *analytics.NotificationEvent) { *m.count++ }
    76  
    77  func initAnalytics(count *int) analytics.PBSAnalyticsModule {
    78  	modules := make(enabledAnalytics, 0)
    79  	modules = append(modules, &sampleModule{count})
    80  	return &modules
    81  }
    82  
    83  func TestNewPBSAnalytics(t *testing.T) {
    84  	pbsAnalytics := NewPBSAnalytics(&config.Analytics{})
    85  	instance := pbsAnalytics.(enabledAnalytics)
    86  
    87  	assert.Equal(t, len(instance), 0)
    88  }
    89  
    90  func TestNewPBSAnalytics_FileLogger(t *testing.T) {
    91  	if _, err := os.Stat(TEST_DIR); os.IsNotExist(err) {
    92  		if err = os.MkdirAll(TEST_DIR, 0755); err != nil {
    93  			t.Fatalf("Could not create test directory for FileLogger")
    94  		}
    95  	}
    96  	defer os.RemoveAll(TEST_DIR)
    97  	mod := NewPBSAnalytics(&config.Analytics{File: config.FileLogs{Filename: TEST_DIR + "/test"}})
    98  	switch modType := mod.(type) {
    99  	case enabledAnalytics:
   100  		if len(enabledAnalytics(modType)) != 1 {
   101  			t.Fatalf("Failed to add analytics module")
   102  		}
   103  	default:
   104  		t.Fatalf("Failed to initialize analytics module")
   105  	}
   106  
   107  	pbsAnalytics := NewPBSAnalytics(&config.Analytics{File: config.FileLogs{Filename: TEST_DIR + "/test"}})
   108  	instance := pbsAnalytics.(enabledAnalytics)
   109  
   110  	assert.Equal(t, len(instance), 1)
   111  }
   112  
   113  func TestNewPBSAnalytics_Pubstack(t *testing.T) {
   114  
   115  	pbsAnalyticsWithoutError := NewPBSAnalytics(&config.Analytics{
   116  		Pubstack: config.Pubstack{
   117  			Enabled:   true,
   118  			ScopeId:   "scopeId",
   119  			IntakeUrl: "https://pubstack.io/intake",
   120  			Buffers: config.PubstackBuffer{
   121  				BufferSize: "100KB",
   122  				EventCount: 0,
   123  				Timeout:    "30s",
   124  			},
   125  			ConfRefresh: "2h",
   126  		},
   127  	})
   128  	instanceWithoutError := pbsAnalyticsWithoutError.(enabledAnalytics)
   129  
   130  	assert.Equal(t, len(instanceWithoutError), 1)
   131  
   132  	pbsAnalyticsWithError := NewPBSAnalytics(&config.Analytics{
   133  		Pubstack: config.Pubstack{
   134  			Enabled: true,
   135  		},
   136  	})
   137  	instanceWithError := pbsAnalyticsWithError.(enabledAnalytics)
   138  	assert.Equal(t, len(instanceWithError), 0)
   139  }