1package oidc 2 3import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/require" 8) 9 10func TestNewID(t *testing.T) { 11 t.Parallel() 12 tests := []struct { 13 name string 14 opt []Option 15 wantErr bool 16 wantPrefix string 17 wantLen int 18 }{ 19 { 20 name: "no-prefix", 21 wantLen: DefaultIDLength, 22 }, 23 { 24 name: "with-prefix", 25 opt: []Option{WithPrefix("alice")}, 26 wantPrefix: "alice", 27 wantLen: DefaultIDLength + len("alice_"), 28 }, 29 } 30 for _, tt := range tests { 31 t.Run(tt.name, func(t *testing.T) { 32 assert, require := assert.New(t), require.New(t) 33 got, err := NewID(tt.opt...) 34 if tt.wantErr { 35 require.Error(err) 36 return 37 } 38 require.NoError(err) 39 if tt.wantPrefix != "" { 40 assert.Containsf(got, tt.wantPrefix, "NewID() = %v and wanted prefix %s", got, tt.wantPrefix) 41 } 42 assert.Equalf(tt.wantLen, len(got), "NewID() = %v, with len of %d and wanted len of %v", got, len(got), tt.wantLen) 43 }) 44 } 45} 46 47func Test_WithPrefix(t *testing.T) { 48 t.Parallel() 49 assert := assert.New(t) 50 opts := getIDOpts(WithPrefix("alice")) 51 testOpts := idDefaults() 52 testOpts.withPrefix = "alice" 53 assert.Equal(opts, testOpts) 54} 55