♻️ Refactored client memory repository
This commit is contained in:
parent
488cfa6b83
commit
e552351bd5
|
@ -0,0 +1,33 @@
|
|||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"source.toby3d.me/website/oauth/internal/client"
|
||||
"source.toby3d.me/website/oauth/internal/model"
|
||||
)
|
||||
|
||||
type memoryClientRepository struct {
|
||||
clients *sync.Map
|
||||
}
|
||||
|
||||
func NewMemoryClientRepository(clients *sync.Map) client.Repository {
|
||||
return &memoryClientRepository{
|
||||
clients: clients,
|
||||
}
|
||||
}
|
||||
|
||||
func (repo *memoryClientRepository) Get(ctx context.Context, id string) (*model.Client, error) {
|
||||
src, ok := repo.clients.Load(id)
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
c, ok := src.(*model.Client)
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package memory_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"source.toby3d.me/website/oauth/internal/client/repository/memory"
|
||||
"source.toby3d.me/website/oauth/internal/model"
|
||||
)
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
store := new(sync.Map)
|
||||
client := &model.Client{
|
||||
ID: "http://127.0.0.1:2368/",
|
||||
Name: "Example App",
|
||||
Logo: "http://127.0.0.1:2368/logo.png",
|
||||
URL: "http://127.0.0.1:2368/",
|
||||
RedirectURI: []model.URL{
|
||||
"https://app.example.com/redirect",
|
||||
"http://127.0.0.1:2368/redirect",
|
||||
},
|
||||
}
|
||||
|
||||
store.Store(string(client.ID), client)
|
||||
|
||||
result, err := memory.NewMemoryClientRepository(store).Get(context.TODO(), string(client.ID))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, client, result)
|
||||
}
|
Loading…
Reference in New Issue