diff --git a/database/integration_test.go b/database/integration_test.go index f478fdd8e..7284d1177 100644 --- a/database/integration_test.go +++ b/database/integration_test.go @@ -910,7 +910,7 @@ func testJWKs(t *testing.T, db Interface, resources *Resources) { t.Errorf("unable to get jwk %s: %v", jkPub.KeyID(), err) } - if !cmp.Equal(jkPub, got) { + if !cmp.Equal(jkPub, got, testutils.JwkOpts) { t.Errorf("GetJWK() is %v, want %v", got, jkPub) } } diff --git a/database/jwk/get.go b/database/jwk/get.go index 49494a222..187753df8 100644 --- a/database/jwk/get.go +++ b/database/jwk/get.go @@ -5,9 +5,10 @@ package jwk import ( "context" + "github.com/lestrrat-go/jwx/jwk" + "github.com/go-vela/server/constants" "github.com/go-vela/server/database/types" - "github.com/lestrrat-go/jwx/jwk" ) // GetActiveJWK gets a JWK by UUID (kid) from the database if active. diff --git a/database/jwk/get_test.go b/database/jwk/get_test.go index ab7568573..819cc1ee9 100644 --- a/database/jwk/get_test.go +++ b/database/jwk/get_test.go @@ -79,7 +79,7 @@ func TestJWK_Engine_GetJWK(t *testing.T) { t.Errorf("GetActiveJWK for %s returned err: %v", test.name, err) } - if diff := cmp.Diff(test.want, got, jwkOpts); diff != "" { + if diff := cmp.Diff(test.want, got, testutils.JwkOpts); diff != "" { t.Errorf("GetActiveJWK mismatch (-want +got):\n%s", diff) } }) diff --git a/database/jwk/jwk_test.go b/database/jwk/jwk_test.go index d0a958218..95ff9a463 100644 --- a/database/jwk/jwk_test.go +++ b/database/jwk/jwk_test.go @@ -7,8 +7,6 @@ import ( "testing" "github.com/DATA-DOG/go-sqlmock" - "github.com/google/go-cmp/cmp" - "github.com/lestrrat-go/jwx/jwk" "github.com/sirupsen/logrus" "gorm.io/driver/postgres" "gorm.io/driver/sqlite" @@ -163,26 +161,3 @@ func testSqlite(t *testing.T) *engine { return _engine } - -var jwkOpts = cmp.Options{ - cmp.FilterValues(func(x, y interface{}) bool { - _, xOk := x.(jwk.RSAPublicKey) - _, yOk := y.(jwk.RSAPublicKey) - return xOk && yOk - }, cmp.Comparer(func(x, y interface{}) bool { - xJWK := x.(jwk.RSAPublicKey) - yJWK := y.(jwk.RSAPublicKey) - - var rawXKey, rawYKey interface{} - - if err := xJWK.Raw(&rawXKey); err != nil { - return false - } - - if err := yJWK.Raw(&rawYKey); err != nil { - return false - } - - return reflect.DeepEqual(rawXKey, rawYKey) && xJWK.KeyID() == yJWK.KeyID() - })), -} diff --git a/database/jwk/list.go b/database/jwk/list.go index 890d04376..fde45e660 100644 --- a/database/jwk/list.go +++ b/database/jwk/list.go @@ -5,9 +5,10 @@ package jwk import ( "context" + "github.com/lestrrat-go/jwx/jwk" + "github.com/go-vela/server/constants" "github.com/go-vela/server/database/types" - "github.com/lestrrat-go/jwx/jwk" ) // ListJWKs gets a list of all configured JWKs from the database. diff --git a/database/testutils/api_resources.go b/database/testutils/api_resources.go index 2db83998b..9e97491fc 100644 --- a/database/testutils/api_resources.go +++ b/database/testutils/api_resources.go @@ -6,12 +6,13 @@ import ( "crypto/rand" "crypto/rsa" + "github.com/google/uuid" + "github.com/lestrrat-go/jwx/jwk" + api "github.com/go-vela/server/api/types" "github.com/go-vela/server/api/types/actions" "github.com/go-vela/types/library" "github.com/go-vela/types/raw" - "github.com/google/uuid" - "github.com/lestrrat-go/jwx/jwk" ) // API TEST RESOURCES diff --git a/database/testutils/mock_args.go b/database/testutils/mock_args.go index c2d8c562f..8cff1b813 100644 --- a/database/testutils/mock_args.go +++ b/database/testutils/mock_args.go @@ -4,7 +4,11 @@ package testutils import ( "database/sql/driver" + "reflect" "time" + + "github.com/google/go-cmp/cmp" + "github.com/lestrrat-go/jwx/jwk" ) // This will be used with the github.com/DATA-DOG/go-sqlmock library to compare values @@ -33,3 +37,26 @@ func (t NowTimestamp) Match(v driver.Value) bool { return now-ts < 10 } + +var JwkOpts = cmp.Options{ + cmp.FilterValues(func(x, y interface{}) bool { + _, xOk := x.(jwk.RSAPublicKey) + _, yOk := y.(jwk.RSAPublicKey) + return xOk && yOk + }, cmp.Comparer(func(x, y interface{}) bool { + xJWK := x.(jwk.RSAPublicKey) + yJWK := y.(jwk.RSAPublicKey) + + var rawXKey, rawYKey interface{} + + if err := xJWK.Raw(&rawXKey); err != nil { + return false + } + + if err := yJWK.Raw(&rawYKey); err != nil { + return false + } + + return reflect.DeepEqual(rawXKey, rawYKey) && xJWK.KeyID() == yJWK.KeyID() + })), +}