github.com/decred/politeia@v1.4.0/politeiad/backendv2/tstorebe/plugins/ticketvote/timestamp_test.go (about) 1 // Copyright (c) 2022 The Decred developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package ticketvote 6 7 import ( 8 "encoding/hex" 9 "testing" 10 ) 11 12 func TestGetVoteTimestampKey(t *testing.T) { 13 token := "45154fb45664714b" 14 15 // Setup tests 16 tests := []struct { 17 name string 18 token string 19 page uint32 20 index uint32 21 shouldError bool 22 cacheKey string 23 }{ 24 { 25 name: "success case 1", 26 token: token, 27 page: 1, 28 index: 0, 29 shouldError: false, 30 cacheKey: "timestamp-vote-45154fb-1-0", 31 }, 32 { 33 name: "success case 2", 34 token: token, 35 page: 3, 36 index: 9, 37 shouldError: false, 38 cacheKey: "timestamp-vote-45154fb-3-9", 39 }, 40 { 41 name: "invalid token", 42 token: "", 43 page: 1, 44 index: 9, 45 shouldError: true, 46 cacheKey: "", 47 }, 48 } 49 50 // Run tests 51 for _, tc := range tests { 52 t.Run(tc.name, func(t *testing.T) { 53 // Convert token to []byte if set 54 var ( 55 tokenb []byte 56 err error 57 ) 58 if tc.token != "" { 59 tokenb, err = hex.DecodeString(tc.token) 60 if err != nil { 61 t.Fatal(err) 62 } 63 } 64 cacheKey, err := getVoteTimestampKey(tokenb, tc.page, tc.index) 65 switch { 66 case tc.shouldError && err == nil: 67 // Wanted an error but didn't get one 68 t.Errorf("want error got nil") 69 return 70 71 case !tc.shouldError && err != nil: 72 // Wanted success but got an error 73 t.Errorf("want error nil, got '%v'", err) 74 return 75 76 case !tc.shouldError && err == nil: 77 // Verify result 78 if cacheKey != tc.cacheKey { 79 // Expected key was not found, error 80 t.Errorf("unexpected cache key; want: %v, got: %v", tc.cacheKey, 81 cacheKey) 82 } 83 return 84 } 85 }) 86 } 87 } 88 89 func TestGetAuthTimestampKey(t *testing.T) { 90 token := "45154fb45664714b" 91 92 // Setup tests 93 tests := []struct { 94 name string 95 token string 96 index uint32 97 shouldError bool 98 cacheKey string 99 }{ 100 { 101 name: "success case 1", 102 token: token, 103 index: 0, 104 shouldError: false, 105 cacheKey: "timestamp-auth-45154fb-0", 106 }, 107 { 108 name: "success case 2", 109 token: token, 110 index: 255, 111 shouldError: false, 112 cacheKey: "timestamp-auth-45154fb-255", 113 }, 114 { 115 name: "invalid token", 116 token: "", 117 index: 9, 118 shouldError: true, 119 cacheKey: "", 120 }, 121 } 122 123 // Run tests 124 for _, tc := range tests { 125 t.Run(tc.name, func(t *testing.T) { 126 // Convert token to []byte if set 127 var ( 128 tokenb []byte 129 err error 130 ) 131 if tc.token != "" { 132 tokenb, err = hex.DecodeString(tc.token) 133 if err != nil { 134 t.Fatal(err) 135 } 136 } 137 cacheKey, err := getAuthTimestampKey(tokenb, tc.index) 138 switch { 139 case tc.shouldError && err == nil: 140 // Wanted an error but didn't get one 141 t.Errorf("want error got nil") 142 return 143 144 case !tc.shouldError && err != nil: 145 // Wanted success but got an error 146 t.Errorf("want error nil, got '%v'", err) 147 return 148 149 case !tc.shouldError && err == nil: 150 // Verify result 151 if cacheKey != tc.cacheKey { 152 // Expected key was not found, error 153 t.Errorf("unexpected cache key; want: %v, got: %v", tc.cacheKey, 154 cacheKey) 155 } 156 return 157 } 158 }) 159 } 160 } 161 162 func TestGetDetailsTimestampKey(t *testing.T) { 163 token := "45154fb45664714b" 164 165 // Setup tests 166 tests := []struct { 167 name string 168 token string 169 shouldError bool 170 cacheKey string 171 }{ 172 { 173 name: "success case 1", 174 token: token, 175 shouldError: false, 176 cacheKey: "timestamp-details-45154fb", 177 }, 178 { 179 name: "invalid token", 180 token: "", 181 shouldError: true, 182 cacheKey: "", 183 }, 184 } 185 186 // Run tests 187 for _, tc := range tests { 188 t.Run(tc.name, func(t *testing.T) { 189 // Convert token to []byte if set 190 var ( 191 tokenb []byte 192 err error 193 ) 194 if tc.token != "" { 195 tokenb, err = hex.DecodeString(tc.token) 196 if err != nil { 197 t.Fatal(err) 198 } 199 } 200 cacheKey, err := getDetailsTimestampKey(tokenb) 201 switch { 202 case tc.shouldError && err == nil: 203 // Wanted an error but didn't get one 204 t.Errorf("want error got nil") 205 return 206 207 case !tc.shouldError && err != nil: 208 // Wanted success but got an error 209 t.Errorf("want error nil, got '%v'", err) 210 return 211 212 case !tc.shouldError && err == nil: 213 // Verify result 214 if cacheKey != tc.cacheKey { 215 // Expected key was not found, error 216 t.Errorf("unexpected cache key; want: %v, got: %v", tc.cacheKey, 217 cacheKey) 218 } 219 return 220 } 221 }) 222 } 223 } 224 225 func TestParseVoteTimestampKey(t *testing.T) { 226 // Setup tests 227 tests := []struct { 228 name string 229 cacheKey string 230 shouldError bool 231 index uint32 232 }{ 233 { 234 name: "success case", 235 cacheKey: "timestamp-vote-45154fb-1-8", 236 shouldError: false, 237 index: 8, 238 }, 239 { 240 name: "invalid key", 241 cacheKey: "--", 242 shouldError: true, 243 index: 0, 244 }, 245 } 246 247 // Run tests 248 for _, tc := range tests { 249 t.Run(tc.name, func(t *testing.T) { 250 index, err := parseVoteTimestampKey(tc.cacheKey) 251 switch { 252 case tc.shouldError && err == nil: 253 // Wanted an error but didn't get one 254 t.Errorf("want error got nil") 255 return 256 257 case !tc.shouldError && err != nil: 258 // Wanted success but got an error 259 t.Errorf("want error nil, got '%v'", err) 260 return 261 262 case !tc.shouldError && err == nil: 263 // Verify result 264 if index != tc.index { 265 // Expected key was not found, error 266 t.Errorf("unexpected index; want: %v, got: %v", tc.index, index) 267 } 268 return 269 } 270 }) 271 } 272 } 273 274 func TestParseAuthTimestampKey(t *testing.T) { 275 // Setup tests 276 tests := []struct { 277 name string 278 cacheKey string 279 shouldError bool 280 index uint32 281 }{ 282 { 283 name: "success case", 284 cacheKey: "timestamp-auth-45154fb-109", 285 shouldError: false, 286 index: 109, 287 }, 288 { 289 name: "invalid key", 290 cacheKey: "---", 291 shouldError: true, 292 index: 0, 293 }, 294 } 295 296 // Run tests 297 for _, tc := range tests { 298 t.Run(tc.name, func(t *testing.T) { 299 index, err := parseAuthTimestampKey(tc.cacheKey) 300 switch { 301 case tc.shouldError && err == nil: 302 // Wanted an error but didn't get one 303 t.Errorf("want error got nil") 304 return 305 306 case !tc.shouldError && err != nil: 307 // Wanted success but got an error 308 t.Errorf("want error nil, got '%v'", err) 309 return 310 311 case !tc.shouldError && err == nil: 312 // Verify result 313 if index != tc.index { 314 // Expected key was not found, error 315 t.Errorf("unexpected index; want: %v, got: %v", tc.index, index) 316 } 317 return 318 } 319 }) 320 } 321 }