2019-09-11 09:36:44 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package authorization
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/testcontext"
|
2019-09-11 09:36:44 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestService_GetOrCreate(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
authorizationDB := newTestAuthDB(t, ctx)
|
2019-11-13 15:32:44 +00:00
|
|
|
defer ctx.Check(authorizationDB.Close)
|
|
|
|
|
2019-09-11 09:36:44 +01:00
|
|
|
service := NewService(zaptest.NewLogger(t), authorizationDB)
|
|
|
|
require.NotNil(t, service)
|
|
|
|
|
|
|
|
{ // new user, no existing authorization tokens (create)
|
|
|
|
userID := "new@mail.test"
|
|
|
|
group, err := authorizationDB.Get(ctx, userID)
|
2019-11-13 10:07:21 +00:00
|
|
|
require.Error(t, err, ErrNotFound)
|
2019-09-11 09:36:44 +01:00
|
|
|
require.Empty(t, group)
|
|
|
|
|
|
|
|
token, err := service.GetOrCreate(ctx, userID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, token)
|
|
|
|
|
|
|
|
require.Equal(t, userID, token.UserID)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // existing user with unclaimed authorization token (get)
|
|
|
|
userID := "old@mail.test"
|
|
|
|
group, err := authorizationDB.Create(ctx, userID, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, group)
|
|
|
|
require.Len(t, group, 1)
|
|
|
|
|
|
|
|
existingAuth := group[0]
|
|
|
|
|
|
|
|
token, err := service.GetOrCreate(ctx, userID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, token)
|
|
|
|
|
|
|
|
require.Equal(t, userID, token.UserID)
|
|
|
|
require.Equal(t, existingAuth.Token, *token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_GetOrCreate_error(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
authorizationDB := newTestAuthDB(t, ctx)
|
2019-11-13 15:32:44 +00:00
|
|
|
defer ctx.Check(authorizationDB.Close)
|
|
|
|
|
2019-09-11 09:36:44 +01:00
|
|
|
service := NewService(zaptest.NewLogger(t), authorizationDB)
|
|
|
|
require.NotNil(t, service)
|
|
|
|
|
|
|
|
{ // empty user ID
|
|
|
|
token, err := service.GetOrCreate(ctx, "")
|
|
|
|
require.Error(t, errs.Unwrap(err), ErrEmptyUserID.Error())
|
|
|
|
require.Nil(t, token)
|
|
|
|
}
|
|
|
|
}
|