Skip to content

Commit

Permalink
Refactor the oauthsessions sql tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Peltoche committed Mar 16, 2024
1 parent b6cff23 commit b5a7370
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 15 deletions.
66 changes: 66 additions & 0 deletions internal/service/oauthsessions/model_helper.go
Original file line number Diff line number Diff line change
@@ -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
// }
62 changes: 47 additions & 15 deletions internal/service/oauthsessions/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,97 +6,129 @@ 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"
)

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)
})
}

0 comments on commit b5a7370

Please sign in to comment.