github.com/prebid/prebid-server/v2@v2.18.0/config/experiment_test.go (about)

     1  package config
     2  
     3  import (
     4  	"errors"
     5  	"github.com/stretchr/testify/assert"
     6  	"testing"
     7  )
     8  
     9  func TestExperimentValidate(t *testing.T) {
    10  	testCases := []struct {
    11  		desc           string
    12  		data           Experiment
    13  		expectErrors   bool
    14  		expectedErrors []error
    15  	}{
    16  		{
    17  			desc: "Remote signer config: invalid remote url passed to config",
    18  			data: Experiment{
    19  				AdCerts: ExperimentAdsCert{Mode: AdCertsSignerModeRemote, Remote: AdsCertRemote{Url: "test@com", SigningTimeoutMs: 5}},
    20  			},
    21  			expectErrors:   true,
    22  			expectedErrors: []error{errors.New("invalid url for remote signer: test@com")},
    23  		},
    24  		{
    25  			desc: "Remote signer config: invalid SigningTimeoutMs passed to config",
    26  			data: Experiment{
    27  				AdCerts: ExperimentAdsCert{Mode: AdCertsSignerModeRemote, Remote: AdsCertRemote{Url: "http://test.com", SigningTimeoutMs: 0}},
    28  			},
    29  			expectErrors:   true,
    30  			expectedErrors: []error{errors.New("invalid signing timeout for remote signer: 0")},
    31  		},
    32  		{
    33  			desc: "Remote signer config: invalid URL and SigningTimeoutMs passed to config",
    34  			data: Experiment{
    35  				AdCerts: ExperimentAdsCert{Mode: AdCertsSignerModeRemote, Remote: AdsCertRemote{Url: "test@com", SigningTimeoutMs: 0}},
    36  			},
    37  			expectErrors: true,
    38  			expectedErrors: []error{errors.New("invalid url for remote signer: test@com"),
    39  				errors.New("invalid signing timeout for remote signer: 0")},
    40  		},
    41  		{
    42  			desc: "Remote signer config: valid URL and SigningTimeoutMs passed to config",
    43  			data: Experiment{
    44  				AdCerts: ExperimentAdsCert{Mode: AdCertsSignerModeRemote, Remote: AdsCertRemote{Url: "http://test.com", SigningTimeoutMs: 5}},
    45  			},
    46  			expectErrors:   false,
    47  			expectedErrors: []error{},
    48  		},
    49  		{
    50  			desc: "Experiment config: experiment config is empty",
    51  			data: Experiment{
    52  				AdCerts: ExperimentAdsCert{Mode: ""},
    53  			},
    54  			expectErrors:   false,
    55  			expectedErrors: []error{},
    56  		},
    57  		{
    58  			desc: "Experiment config: experiment config is off",
    59  			data: Experiment{
    60  				AdCerts: ExperimentAdsCert{Mode: AdCertsSignerModeOff},
    61  			},
    62  			expectErrors:   false,
    63  			expectedErrors: []error{},
    64  		},
    65  		{
    66  			desc: "Experiment config: experiment config is init with a wrong value",
    67  			data: Experiment{
    68  				AdCerts: ExperimentAdsCert{Mode: "test"},
    69  			},
    70  			expectErrors:   true,
    71  			expectedErrors: []error{ErrSignerModeIncorrect},
    72  		},
    73  		{
    74  			desc: "Inprocess signer config: valid config",
    75  			data: Experiment{
    76  				AdCerts: ExperimentAdsCert{Mode: AdCertsSignerModeInprocess, InProcess: AdsCertInProcess{Origin: "http://test.com", PrivateKey: "pk", DNSCheckIntervalInSeconds: 10, DNSRenewalIntervalInSeconds: 10}},
    77  			},
    78  			expectErrors:   false,
    79  			expectedErrors: []error{},
    80  		},
    81  		{
    82  			desc: "Inprocess signer config: invaild origin url passed to config",
    83  			data: Experiment{
    84  				AdCerts: ExperimentAdsCert{Mode: AdCertsSignerModeInprocess, InProcess: AdsCertInProcess{Origin: "test@com", PrivateKey: "pk", DNSCheckIntervalInSeconds: 10, DNSRenewalIntervalInSeconds: 10}},
    85  			},
    86  			expectErrors:   true,
    87  			expectedErrors: []error{errors.New("invalid url for inprocess signer: test@com")},
    88  		},
    89  		{
    90  			desc: "Inprocess signer config: empty PK passed to config",
    91  			data: Experiment{
    92  				AdCerts: ExperimentAdsCert{Mode: AdCertsSignerModeInprocess, InProcess: AdsCertInProcess{Origin: "http://test.com", PrivateKey: "", DNSCheckIntervalInSeconds: 10, DNSRenewalIntervalInSeconds: 10}},
    93  			},
    94  			expectErrors:   true,
    95  			expectedErrors: []error{ErrInProcessSignerInvalidPrivateKey},
    96  		},
    97  		{
    98  			desc: "Inprocess signer config: negative dns check interval passed to config",
    99  			data: Experiment{
   100  				AdCerts: ExperimentAdsCert{Mode: AdCertsSignerModeInprocess, InProcess: AdsCertInProcess{Origin: "http://test.com", PrivateKey: "pk", DNSCheckIntervalInSeconds: -10, DNSRenewalIntervalInSeconds: 10}},
   101  			},
   102  			expectErrors:   true,
   103  			expectedErrors: []error{errors.New("invalid dns check interval for inprocess signer: -10")},
   104  		},
   105  		{
   106  			desc: "Inprocess signer config: zero dns check interval passed to config",
   107  			data: Experiment{
   108  				AdCerts: ExperimentAdsCert{Mode: AdCertsSignerModeInprocess, InProcess: AdsCertInProcess{Origin: "http://test.com", PrivateKey: "pk", DNSCheckIntervalInSeconds: 10, DNSRenewalIntervalInSeconds: 0}},
   109  			},
   110  			expectErrors:   true,
   111  			expectedErrors: []error{errors.New("invalid dns renewal interval for inprocess signer: 0")},
   112  		},
   113  		{
   114  			desc: "Inprocess signer config: all config parameters are invalid",
   115  			data: Experiment{
   116  				AdCerts: ExperimentAdsCert{Mode: AdCertsSignerModeInprocess, InProcess: AdsCertInProcess{Origin: "test@com", PrivateKey: "", DNSCheckIntervalInSeconds: -10, DNSRenewalIntervalInSeconds: 0}},
   117  			},
   118  			expectErrors: true,
   119  			expectedErrors: []error{
   120  				errors.New("invalid url for inprocess signer: test@com"),
   121  				ErrInProcessSignerInvalidPrivateKey,
   122  				errors.New("invalid dns check interval for inprocess signer: -10"),
   123  				errors.New("invalid dns renewal interval for inprocess signer: 0")},
   124  		},
   125  	}
   126  	for _, test := range testCases {
   127  		errs := test.data.validate([]error{})
   128  		if test.expectErrors {
   129  			assert.ElementsMatch(t, test.expectedErrors, errs, "Test case threw unexpected errors. Desc: %s  \n", test.desc)
   130  		} else {
   131  			assert.Empty(t, test.expectedErrors, "Test case should not return errors. Desc: %s  \n", test.desc)
   132  		}
   133  	}
   134  }