github.com/prebid/prebid-server/v2@v2.18.0/gdpr/signal_test.go (about) 1 package gdpr 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestSignalParse(t *testing.T) { 10 tests := []struct { 11 description string 12 rawSignal string 13 wantSignal Signal 14 wantError bool 15 }{ 16 { 17 description: "valid raw signal is 0", 18 rawSignal: "0", 19 wantSignal: SignalNo, 20 wantError: false, 21 }, 22 { 23 description: "Valid signal - raw signal is 1", 24 rawSignal: "1", 25 wantSignal: SignalYes, 26 wantError: false, 27 }, 28 { 29 description: "Valid signal - raw signal is empty", 30 rawSignal: "", 31 wantSignal: SignalAmbiguous, 32 wantError: false, 33 }, 34 { 35 description: "Invalid signal - raw signal is -1", 36 rawSignal: "-1", 37 wantSignal: SignalAmbiguous, 38 wantError: true, 39 }, 40 { 41 description: "Invalid signal - raw signal is abc", 42 rawSignal: "abc", 43 wantSignal: SignalAmbiguous, 44 wantError: true, 45 }, 46 { 47 description: "Out of bounds signal - raw signal is 5", 48 rawSignal: "5", 49 wantSignal: SignalAmbiguous, 50 wantError: true, 51 }, 52 } 53 54 for _, test := range tests { 55 t.Run(test.description, func(t *testing.T) { 56 signal, err := StrSignalParse(test.rawSignal) 57 58 assert.Equal(t, test.wantSignal, signal, test.description) 59 60 if test.wantError { 61 assert.NotNil(t, err, test.description) 62 } else { 63 assert.Nil(t, err, test.description) 64 } 65 }) 66 } 67 } 68 69 func TestIntSignalParse(t *testing.T) { 70 type testOutput struct { 71 signal Signal 72 err error 73 } 74 testCases := []struct { 75 desc string 76 input int 77 expected testOutput 78 }{ 79 { 80 desc: "input out of bounds, return SgnalAmbituous and gdprSignalError", 81 input: -1, 82 expected: testOutput{ 83 signal: SignalAmbiguous, 84 err: gdprSignalError, 85 }, 86 }, 87 { 88 desc: "input in bounds equals signalNo, return signalNo and nil error", 89 input: 0, 90 expected: testOutput{ 91 signal: SignalNo, 92 err: nil, 93 }, 94 }, 95 { 96 desc: "input in bounds equals signalYes, return signalYes and nil error", 97 input: 1, 98 expected: testOutput{ 99 signal: SignalYes, 100 err: nil, 101 }, 102 }, 103 } 104 for _, tc := range testCases { 105 t.Run(tc.desc, func(t *testing.T) { 106 outSignal, outErr := IntSignalParse(tc.input) 107 108 assert.Equal(t, tc.expected.signal, outSignal, tc.desc) 109 assert.Equal(t, tc.expected.err, outErr, tc.desc) 110 }) 111 } 112 } 113 114 func TestSignalNormalize(t *testing.T) { 115 tests := []struct { 116 description string 117 defaultValue string 118 giveSignal Signal 119 wantSignal Signal 120 }{ 121 { 122 description: "Don't normalize - Signal No and Default Value 1", 123 defaultValue: "1", 124 giveSignal: SignalNo, 125 wantSignal: SignalNo, 126 }, 127 { 128 description: "Don't normalize - Signal No and Default Value 0", 129 defaultValue: "0", 130 giveSignal: SignalNo, 131 wantSignal: SignalNo, 132 }, 133 { 134 description: "Don't normalize - Signal Yes and Default Value 1", 135 defaultValue: "1", 136 giveSignal: SignalYes, 137 wantSignal: SignalYes, 138 }, 139 { 140 description: "Don't normalize - Signal Yes and Default Value 0", 141 defaultValue: "0", 142 giveSignal: SignalYes, 143 wantSignal: SignalYes, 144 }, 145 { 146 description: "Normalize - Signal Ambiguous and Default Value 1", 147 defaultValue: "1", 148 giveSignal: SignalAmbiguous, 149 wantSignal: SignalYes, 150 }, 151 { 152 description: "Normalize - Signal Ambiguous and Default Value 0", 153 defaultValue: "0", 154 giveSignal: SignalAmbiguous, 155 wantSignal: SignalNo, 156 }, 157 } 158 159 for _, test := range tests { 160 normalizedSignal := SignalNormalize(test.giveSignal, test.defaultValue) 161 162 assert.Equal(t, test.wantSignal, normalizedSignal, test.description) 163 } 164 }