Skip to content

Commit

Permalink
refactor: generated UUID variant & version test (#2793)
Browse files Browse the repository at this point in the history
Closes #2792
  • Loading branch information
grantzvolsky committed May 18, 2022
1 parent d6e9412 commit 560ed15
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
7 changes: 6 additions & 1 deletion client/manager_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

"github.com/ory/fosite"

"github.com/ory/hydra/internal/testhelpers/uuid"
"github.com/ory/hydra/x"
)

Expand All @@ -45,7 +46,11 @@ func TestHelperClientAutoGenerateKey(k string, m Storage) func(t *testing.T) {
TermsOfServiceURI: "foo",
}
assert.NoError(t, m.CreateClient(ctx, c))
// assert.NotEmpty(t, c.ID)
dbClient, err := m.GetClient(ctx, c.GetID())
assert.NoError(t, err)
dbClientConcrete, ok := dbClient.(*Client)
assert.True(t, ok)
uuid.AssertUUID(t, &dbClientConcrete.ID)
assert.NoError(t, m.DeleteClient(ctx, c.GetID()))
}
}
Expand Down
13 changes: 13 additions & 0 deletions internal/testhelpers/uuid/uuid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package uuid

import (
"testing"

"github.com/gofrs/uuid"
"github.com/stretchr/testify/require"
)

func AssertUUID(t *testing.T, id *uuid.UUID) {
require.Equal(t, id.Version(), uuid.V4)
require.Equal(t, id.Variant(), uuid.VariantRFC4122)
}
17 changes: 2 additions & 15 deletions persistence/sql/migratest/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"testing"

"github.com/gobuffalo/pop/v6"
"github.com/gofrs/uuid"

"github.com/ory/x/configx"

"github.com/ory/hydra/internal/testhelpers/uuid"
"github.com/ory/hydra/persistence/sql"

"github.com/ory/x/popx"
Expand All @@ -29,11 +29,6 @@ import (
"github.com/ory/hydra/x"
)

func assertUUID(t *testing.T, id *uuid.UUID) {
require.Equal(t, id.Version(), uuid.V4)
require.Equal(t, id.Variant(), uuid.VariantRFC4122)
}

func TestMigrations(t *testing.T) {
if testing.Short() {
t.SkipNow()
Expand Down Expand Up @@ -83,7 +78,7 @@ func TestMigrations(t *testing.T) {
actual := &client.Client{}
outfacingID := fmt.Sprintf("client-%04d", i)
require.NoError(t, c.Where("id = ?", outfacingID).First(actual))
assertUUID(t, &actual.ID)
uuid.AssertUUID(t, &actual.ID)
expected := expectedClient(actual.ID, i)
assertEqualClients(t, expected, actual)
lastClient = actual
Expand Down Expand Up @@ -146,14 +141,6 @@ func TestMigrations(t *testing.T) {
})
}

t.Run("case=client migration 20211004/description=new client ID should be valid UUIDv4 variant 1", func(t *testing.T) {
outfacingID := "2021100400"
require.NoError(t, d.Persister().CreateClient(context.Background(), &client.Client{OutfacingID: outfacingID}))
actual := &client.Client{}
require.NoError(t, c.Where("id = ?", outfacingID).First(actual))
assertUUID(t, &actual.ID)
})

// TODO https://github.com/ory/hydra/issues/1815
// this is very stupid and should be replaced as soon the manager uses pop
// necessary because the manager does not provide any way to access the data
Expand Down

0 comments on commit 560ed15

Please sign in to comment.