2021-09-22 21:32:40 +00:00
|
|
|
package memory_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-05 06:54:36 +00:00
|
|
|
"path"
|
2021-09-22 21:32:40 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-09-29 20:35:39 +00:00
|
|
|
|
2022-03-13 10:58:34 +00:00
|
|
|
"source.toby3d.me/toby3d/auth/internal/domain"
|
|
|
|
"source.toby3d.me/toby3d/auth/internal/token"
|
|
|
|
repository "source.toby3d.me/toby3d/auth/internal/token/repository/memory"
|
2021-09-22 21:32:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreate(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
store := new(sync.Map)
|
2021-10-17 23:12:00 +00:00
|
|
|
accessToken := domain.TestToken(t)
|
2021-10-13 21:53:31 +00:00
|
|
|
|
2021-10-17 22:51:10 +00:00
|
|
|
repo := repository.NewMemoryTokenRepository(store)
|
2022-02-01 19:17:22 +00:00
|
|
|
if err := repo.Create(context.TODO(), accessToken); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-09-22 21:32:40 +00:00
|
|
|
|
2021-10-17 23:12:00 +00:00
|
|
|
result, ok := store.Load(path.Join(repository.DefaultPathPrefix, accessToken.AccessToken))
|
2021-09-22 21:32:40 +00:00
|
|
|
assert.True(t, ok)
|
2021-10-17 23:12:00 +00:00
|
|
|
assert.Equal(t, accessToken, result)
|
2021-09-22 21:32:40 +00:00
|
|
|
|
2021-10-17 23:12:00 +00:00
|
|
|
assert.ErrorIs(t, repo.Create(context.TODO(), accessToken), token.ErrExist)
|
2021-09-22 21:32:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 21:53:31 +00:00
|
|
|
func TestGet(t *testing.T) {
|
2021-09-22 21:32:40 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
store := new(sync.Map)
|
2021-10-17 23:12:00 +00:00
|
|
|
accessToken := domain.TestToken(t)
|
2021-09-22 21:32:40 +00:00
|
|
|
|
2021-10-17 23:12:00 +00:00
|
|
|
store.Store(path.Join(repository.DefaultPathPrefix, accessToken.AccessToken), accessToken)
|
2021-09-22 21:32:40 +00:00
|
|
|
|
2021-10-17 23:12:00 +00:00
|
|
|
result, err := repository.NewMemoryTokenRepository(store).Get(context.TODO(), accessToken.AccessToken)
|
2021-10-13 21:53:31 +00:00
|
|
|
assert.NoError(t, err)
|
2021-10-17 23:12:00 +00:00
|
|
|
assert.Equal(t, accessToken, result)
|
2021-09-22 21:32:40 +00:00
|
|
|
}
|