github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/model/api_key_test.go (about) 1 package model_test 2 3 import ( 4 "strings" 5 6 "github.com/hashicorp/go-multierror" 7 . "github.com/onsi/ginkgo/v2" 8 . "github.com/onsi/gomega" 9 10 "github.com/pyroscope-io/pyroscope/pkg/model" 11 ) 12 13 var _ = Describe("API key validation", func() { 14 type testCase struct { 15 params model.CreateAPIKeyParams 16 err error 17 } 18 19 validateParams := func(c testCase) { 20 expectErrOrNil(c.params.Validate(), c.err) 21 } 22 23 DescribeTable("API key cases", 24 validateParams, 25 26 Entry("valid params", testCase{ 27 params: model.CreateAPIKeyParams{ 28 Name: "johndoe", 29 Role: model.ReadOnlyRole, 30 }, 31 }), 32 33 Entry("name is too long", testCase{ 34 params: model.CreateAPIKeyParams{ 35 Name: strings.Repeat("X", 256), 36 }, 37 err: &multierror.Error{Errors: []error{ 38 model.ErrAPIKeyNameTooLong, 39 model.ErrRoleUnknown, 40 }}, 41 }), 42 43 Entry("invalid params", testCase{ 44 params: model.CreateAPIKeyParams{}, 45 err: &multierror.Error{Errors: []error{ 46 model.ErrAPIKeyNameEmpty, 47 model.ErrRoleUnknown, 48 }}, 49 }), 50 ) 51 }) 52 53 var _ = Describe("API key encoding", func() { 54 defer GinkgoRecover() 55 56 Describe("psx", func() { 57 Context("generated API key can be decoded", func() { 58 apiKey := model.APIKey{ID: 13} 59 key, hash, err := model.GenerateAPIKey(apiKey.ID) 60 Expect(err).ToNot(HaveOccurred()) 61 apiKey.Hash = hash 62 63 id, secret, err := model.DecodeAPIKey(key) 64 Expect(err).ToNot(HaveOccurred()) 65 Expect(id).To(Equal(apiKey.ID)) 66 Expect(apiKey.Verify(secret)).ToNot(HaveOccurred()) 67 }) 68 }) 69 })