Dawid Wysokiński
8e8e7e1f94
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #8
104 lines
2.2 KiB
Go
104 lines
2.2 KiB
Go
package domain_test
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"testing"
|
|
|
|
"gitea.dwysokinski.me/twhelp/sessions/internal/domain"
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestNewCreateSessionParams(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
serverKey string
|
|
sid string
|
|
userID int64
|
|
expectedErr error
|
|
}{
|
|
{
|
|
name: "OK",
|
|
serverKey: "pl151",
|
|
sid: base64.StdEncoding.EncodeToString([]byte(uuid.NewString())),
|
|
userID: 1,
|
|
expectedErr: nil,
|
|
},
|
|
{
|
|
name: "ERR: serverKey is required",
|
|
serverKey: "",
|
|
sid: base64.StdEncoding.EncodeToString([]byte(uuid.NewString())),
|
|
userID: 1,
|
|
expectedErr: domain.ValidationError{
|
|
Field: "ServerKey",
|
|
Err: domain.ErrRequired,
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: len(serverKey) > 10",
|
|
serverKey: randString(11),
|
|
sid: base64.StdEncoding.EncodeToString([]byte(uuid.NewString())),
|
|
userID: 1,
|
|
expectedErr: domain.ValidationError{
|
|
Field: "ServerKey",
|
|
Err: domain.MaxLengthError{
|
|
Max: 10,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: SID is required",
|
|
serverKey: "pl151",
|
|
sid: "",
|
|
userID: 1,
|
|
expectedErr: domain.ValidationError{
|
|
Field: "SID",
|
|
Err: domain.ErrRequired,
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: SID is not a valid base64",
|
|
serverKey: "pl151",
|
|
sid: uuid.NewString(),
|
|
userID: 1,
|
|
expectedErr: domain.ValidationError{
|
|
Field: "SID",
|
|
Err: domain.ErrBase64,
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: userID < 1",
|
|
serverKey: "pl151",
|
|
sid: base64.StdEncoding.EncodeToString([]byte(uuid.NewString())),
|
|
userID: 0,
|
|
expectedErr: domain.ValidationError{
|
|
Field: "UserID",
|
|
Err: domain.MinError{
|
|
Min: 1,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
params, err := domain.NewCreateSessionParams(tt.serverKey, tt.sid, tt.userID)
|
|
if tt.expectedErr != nil {
|
|
assert.Equal(t, tt.expectedErr, err)
|
|
assert.Zero(t, params)
|
|
return
|
|
}
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tt.serverKey, params.ServerKey())
|
|
assert.Equal(t, tt.sid, params.SID())
|
|
assert.Equal(t, tt.userID, params.UserID())
|
|
})
|
|
}
|
|
}
|