From b5a73703be54aed5c9030b59685cab75af69c597 Mon Sep 17 00:00:00 2001 From: Peltoche Date: Sat, 16 Mar 2024 18:08:52 +0100 Subject: [PATCH] Refactor the oauthsessions sql tests --- .../service/oauthsessions/model_helper.go | 66 +++++++++++++++++++ .../service/oauthsessions/storage_test.go | 62 ++++++++++++----- 2 files changed, 113 insertions(+), 15 deletions(-) create mode 100644 internal/service/oauthsessions/model_helper.go diff --git a/internal/service/oauthsessions/model_helper.go b/internal/service/oauthsessions/model_helper.go new file mode 100644 index 00000000..dcae17af --- /dev/null +++ b/internal/service/oauthsessions/model_helper.go @@ -0,0 +1,66 @@ +package oauthsessions + +import ( + "testing" + "time" + + "github.com/brianvoe/gofakeit/v7" + "github.com/theduckcompany/duckcloud/internal/service/oauthclients" + "github.com/theduckcompany/duckcloud/internal/service/users" + "github.com/theduckcompany/duckcloud/internal/tools/secret" + "github.com/theduckcompany/duckcloud/internal/tools/uuid" +) + +type FakeSessionBuilder struct { + t testing.TB + session *Session +} + +func NewFakeSession(t testing.TB) *FakeSessionBuilder { + t.Helper() + + uuidProvider := uuid.NewProvider() + createdAt := gofakeit.DateRange(time.Now().Add(-time.Hour*1000), time.Now()) + + return &FakeSessionBuilder{ + t: t, + session: &Session{ + accessToken: secret.NewText(gofakeit.Password(true, true, true, false, false, 8)), + accessCreatedAt: createdAt, + accessExpiresAt: createdAt.Add(time.Hour), + refreshToken: secret.NewText(gofakeit.Password(true, true, true, false, false, 8)), + refreshCreatedAt: createdAt, + refreshExpiresAt: createdAt.Add(time.Hour), + clientID: gofakeit.Name(), + userID: uuidProvider.New(), + scope: "scope-a,scope-b", + }, + } +} + +func (f *FakeSessionBuilder) WithClient(client *oauthclients.Client) *FakeSessionBuilder { + f.session.clientID = client.GetID() + + return f +} + +func (f *FakeSessionBuilder) CreatedBy(user *users.User) *FakeSessionBuilder { + f.session.userID = user.ID() + + return f +} + +func (f *FakeSessionBuilder) Build() *Session { + return f.session +} + +// func (f *FakeSessionBuilder) BuildAndStore(ctx context.Context, db *sql.DB) *Session { +// f.t.Helper() +// +// storage := newSqlStorage(db) +// +// err := storage.Save(ctx, f.session) +// require.NoError(f.t, err) +// +// return f.session +// } diff --git a/internal/service/oauthsessions/storage_test.go b/internal/service/oauthsessions/storage_test.go index a8cc330d..65fade60 100644 --- a/internal/service/oauthsessions/storage_test.go +++ b/internal/service/oauthsessions/storage_test.go @@ -6,6 +6,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/theduckcompany/duckcloud/internal/service/oauthclients" + "github.com/theduckcompany/duckcloud/internal/service/users" "github.com/theduckcompany/duckcloud/internal/tools/secret" "github.com/theduckcompany/duckcloud/internal/tools/sqlstorage" "github.com/theduckcompany/duckcloud/internal/tools/uuid" @@ -13,90 +15,120 @@ import ( func TestSessionStorageStorage(t *testing.T) { ctx := context.Background() + db := sqlstorage.NewTestStorage(t) store := newSqlStorage(db) + user := users.NewFakeUser(t).BuildAndStore(ctx, db) + client := oauthclients.NewFakeClient(t).CreatedBy(user).BuildAndStore(ctx, db) + + session := NewFakeSession(t).CreatedBy(user).WithClient(client).Build() + session2 := NewFakeSession(t).CreatedBy(user).WithClient(client).Build() + t.Run("Save success", func(t *testing.T) { - err := store.Save(ctx, &ExampleAliceSession) + // Run + err := store.Save(ctx, session) + + // Asserts require.NoError(t, err) }) t.Run("GetAllForUser success", func(t *testing.T) { - res, err := store.GetAllForUser(ctx, ExampleAliceSession.UserID(), nil) + // Run + res, err := store.GetAllForUser(ctx, session.UserID(), nil) + // Asserts require.NoError(t, err) - assert.Equal(t, []Session{ExampleAliceSession}, res) + assert.Equal(t, []Session{*session}, res) }) t.Run("GetAllForUser with an unknown user", func(t *testing.T) { + // Run res, err := store.GetAllForUser(ctx, uuid.UUID("some-invalid-id"), nil) + // Asserts require.NoError(t, err) assert.Equal(t, []Session{}, res) }) t.Run("GetByAccessToken success", func(t *testing.T) { - res, err := store.GetByAccessToken(ctx, ExampleAliceSession.AccessToken()) + // Run + res, err := store.GetByAccessToken(ctx, session.AccessToken()) + // Asserts require.NoError(t, err) - assert.Equal(t, &ExampleAliceSession, res) + assert.Equal(t, session, res) }) t.Run("GetByAccessToken with an invalid access", func(t *testing.T) { + // Run res, err := store.GetByAccessToken(ctx, secret.NewText("some-invalid-token")) + // Asserts assert.Nil(t, res) require.ErrorIs(t, err, errNotFound) }) t.Run("GetByRefreshToken success", func(t *testing.T) { - res, err := store.GetByRefreshToken(ctx, ExampleAliceSession.RefreshToken()) + // Run + res, err := store.GetByRefreshToken(ctx, session.RefreshToken()) + + // Asserts require.NoError(t, err) - assert.Equal(t, &ExampleAliceSession, res) + assert.Equal(t, session, res) }) t.Run("GetByRefreshToken with an invalid access", func(t *testing.T) { + // Run res, err := store.GetByRefreshToken(ctx, secret.NewText("some-invalid-token")) + // Asserts assert.Nil(t, res) require.ErrorIs(t, err, errNotFound) }) t.Run("RemoveByAccessToken success", func(t *testing.T) { - err := store.RemoveByAccessToken(ctx, ExampleAliceSession.AccessToken()) - require.NoError(t, err) + // Run + err := store.RemoveByAccessToken(ctx, session.AccessToken()) - res, err := store.GetByAccessToken(ctx, ExampleAliceSession.AccessToken()) + // Asserts + require.NoError(t, err) + res, err := store.GetByAccessToken(ctx, session.AccessToken()) assert.Nil(t, res) require.ErrorIs(t, err, errNotFound) }) t.Run("RemoveByAccessToken with an invalid token", func(t *testing.T) { + // Run err := store.RemoveByAccessToken(ctx, secret.NewText("some-invalid-token")) + // Asserts require.NoError(t, err) }) t.Run("Save success 2", func(t *testing.T) { - err := store.Save(ctx, &ExampleAliceSession) + err := store.Save(ctx, session2) + // Asserts require.NoError(t, err) }) t.Run("RemoveByRefreshToken success", func(t *testing.T) { - err := store.RemoveByRefreshToken(ctx, ExampleAliceSession.RefreshToken()) + // Run + err := store.RemoveByRefreshToken(ctx, session2.RefreshToken()) + // Asserts require.NoError(t, err) - - res, err := store.GetByRefreshToken(ctx, ExampleAliceSession.RefreshToken()) - + res, err := store.GetByRefreshToken(ctx, session2.RefreshToken()) assert.Nil(t, res) require.ErrorIs(t, err, errNotFound) }) t.Run("RemoveByRefreshToken with an invalid token", func(t *testing.T) { + // Run err := store.RemoveByRefreshToken(ctx, secret.NewText("some-invalid-token")) + // Asserts require.NoError(t, err) }) }