Skip to content

Commit

Permalink
NOISSUE - Fix WS Tests (#1927)
Browse files Browse the repository at this point in the history
* feat(testsutil): Add function to generate random UUID

This commit adds a new function to the `common.go` file in the `testsutil` package. The function `GenerateRandomUUID` generates a random UUID using the `github.com/mainflux/mainflux/pkg/uuid` package. This function will be used in tests to generate unique identifiers for testing purposes.

The function is added to the existing `common.go` file in the `testsutil` package, which is used for common utilities in tests.

This change improves the testability of the codebase by providing a convenient way to generate random UUIDs for testing purposes.

Signed-off-by: Rodney Osodo <[email protected]>

* feat(auth): add error constants

This commit adds two error constants, InvalidID and InvalidToken, to the auth package. These constants will be used to represent invalid ID and token values in the authentication service.

Signed-off-by: Rodney Osodo <[email protected]>

* feat(ws): add validation for empty message payload

This commit adds a validation check for an empty message payload in the Publish method of the adapterService struct in the ws/adapter.go file. If the payload is empty, the method will now return an ErrFailedMessagePublish error. This validation ensures that only non-empty payloads are published.

Signed-off-by: Rodney Osodo <[email protected]>

* feat(auth): add constant for invalid value

Added a constant named InvalidValue to the auth/mocks/service.go file. This constant is set to "invalid" and will be used to represent an invalid value in the codebase.

Signed-off-by: Rodney Osodo <[email protected]>

---------

Signed-off-by: Rodney Osodo <[email protected]>
  • Loading branch information
rodneyosodo authored Oct 18, 2023
1 parent 67276c1 commit e6f34e1
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 74 deletions.
21 changes: 21 additions & 0 deletions auth/mocks/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import (
context "context"

"github.com/mainflux/mainflux"
"github.com/mainflux/mainflux/pkg/errors"
"github.com/stretchr/testify/mock"
"google.golang.org/grpc"
)

const InvalidValue = "invalid"

var _ mainflux.AuthServiceClient = (*Service)(nil)

type Service struct {
Expand All @@ -19,30 +22,48 @@ type Service struct {

func (m *Service) Issue(ctx context.Context, in *mainflux.IssueReq, opts ...grpc.CallOption) (*mainflux.Token, error) {
ret := m.Called(ctx, in)
if in.GetId() == InvalidValue || in.GetId() == "" {
return &mainflux.Token{}, errors.ErrAuthentication
}

return ret.Get(0).(*mainflux.Token), ret.Error(1)
}

func (m *Service) Login(ctx context.Context, in *mainflux.LoginReq, opts ...grpc.CallOption) (*mainflux.Token, error) {
ret := m.Called(ctx, in)
if in.GetId() == InvalidValue || in.GetId() == "" {
return &mainflux.Token{}, errors.ErrAuthentication
}

return ret.Get(0).(*mainflux.Token), ret.Error(1)
}

func (m *Service) Refresh(ctx context.Context, in *mainflux.RefreshReq, opts ...grpc.CallOption) (*mainflux.Token, error) {
ret := m.Called(ctx, in)
if in.GetValue() == InvalidValue || in.GetValue() == "" {
return &mainflux.Token{}, errors.ErrAuthentication
}

return ret.Get(0).(*mainflux.Token), ret.Error(1)
}

func (m *Service) Identify(ctx context.Context, in *mainflux.IdentityReq, opts ...grpc.CallOption) (*mainflux.IdentityRes, error) {
ret := m.Called(ctx, in)
if in.GetToken() == InvalidValue || in.GetToken() == "" {
return &mainflux.IdentityRes{}, errors.ErrAuthentication
}

return ret.Get(0).(*mainflux.IdentityRes), ret.Error(1)
}

func (m *Service) Authorize(ctx context.Context, in *mainflux.AuthorizeReq, opts ...grpc.CallOption) (*mainflux.AuthorizeRes, error) {
ret := m.Called(ctx, in)
if in.GetSubject() == InvalidValue || in.GetSubject() == "" {
return &mainflux.AuthorizeRes{Authorized: false}, errors.ErrAuthorization
}
if in.GetObject() == InvalidValue || in.GetObject() == "" {
return &mainflux.AuthorizeRes{Authorized: false}, errors.ErrAuthorization
}

return ret.Get(0).(*mainflux.AuthorizeRes), ret.Error(1)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/testsutil/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import (
"testing"

"github.com/jmoiron/sqlx"
"github.com/mainflux/mainflux"
mfclients "github.com/mainflux/mainflux/pkg/clients"
"github.com/mainflux/mainflux/pkg/errors"
"github.com/mainflux/mainflux/pkg/uuid"
"github.com/mainflux/mainflux/users"
cmocks "github.com/mainflux/mainflux/users/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func GenerateUUID(t *testing.T, idProvider mainflux.IDProvider) string {
func GenerateUUID(t *testing.T) string {
idProvider := uuid.New()
ulid, err := idProvider.ID()
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
return ulid
Expand Down
34 changes: 16 additions & 18 deletions things/postgres/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/mainflux/mainflux/internal/testsutil"
"github.com/mainflux/mainflux/pkg/clients"
"github.com/mainflux/mainflux/pkg/errors"
"github.com/mainflux/mainflux/pkg/uuid"
"github.com/mainflux/mainflux/things/postgres"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -21,7 +20,6 @@ import (
const maxNameSize = 1024

var (
idProvider = uuid.New()
invalidName = strings.Repeat("m", maxNameSize+10)
clientIdentity = "[email protected]"
clientName = "client name"
Expand All @@ -34,7 +32,7 @@ func TestClientsSave(t *testing.T) {
})
repo := postgres.NewRepository(database)

uid := testsutil.GenerateUUID(t, idProvider)
uid := testsutil.GenerateUUID(t)

cases := []struct {
desc string
Expand All @@ -48,7 +46,7 @@ func TestClientsSave(t *testing.T) {
Name: clientName,
Credentials: clients.Credentials{
Identity: clientIdentity,
Secret: testsutil.GenerateUUID(t, idProvider),
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
Expand All @@ -58,12 +56,12 @@ func TestClientsSave(t *testing.T) {
{
desc: "add new client with an owner",
client: clients.Client{
ID: testsutil.GenerateUUID(t, idProvider),
ID: testsutil.GenerateUUID(t),
Owner: uid,
Name: clientName,
Credentials: clients.Credentials{
Identity: "[email protected]",
Secret: testsutil.GenerateUUID(t, idProvider),
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
Expand All @@ -77,7 +75,7 @@ func TestClientsSave(t *testing.T) {
Name: clientName,
Credentials: clients.Credentials{
Identity: "[email protected]",
Secret: testsutil.GenerateUUID(t, idProvider),
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
Expand All @@ -87,11 +85,11 @@ func TestClientsSave(t *testing.T) {
{
desc: "add client with invalid client name",
client: clients.Client{
ID: testsutil.GenerateUUID(t, idProvider),
ID: testsutil.GenerateUUID(t),
Name: invalidName,
Credentials: clients.Credentials{
Identity: "[email protected]",
Secret: testsutil.GenerateUUID(t, idProvider),
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
Expand All @@ -101,11 +99,11 @@ func TestClientsSave(t *testing.T) {
{
desc: "add client with invalid client owner",
client: clients.Client{
ID: testsutil.GenerateUUID(t, idProvider),
ID: testsutil.GenerateUUID(t),
Owner: invalidName,
Credentials: clients.Credentials{
Identity: "[email protected]",
Secret: testsutil.GenerateUUID(t, idProvider),
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
Expand All @@ -115,11 +113,11 @@ func TestClientsSave(t *testing.T) {
{
desc: "add client with invalid client identity",
client: clients.Client{
ID: testsutil.GenerateUUID(t, idProvider),
ID: testsutil.GenerateUUID(t),
Name: clientName,
Credentials: clients.Credentials{
Identity: invalidName,
Secret: testsutil.GenerateUUID(t, idProvider),
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
Expand All @@ -129,10 +127,10 @@ func TestClientsSave(t *testing.T) {
{
desc: "add client with a missing client identity",
client: clients.Client{
ID: testsutil.GenerateUUID(t, idProvider),
ID: testsutil.GenerateUUID(t),
Credentials: clients.Credentials{
Identity: "",
Secret: testsutil.GenerateUUID(t, idProvider),
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
},
Expand All @@ -141,7 +139,7 @@ func TestClientsSave(t *testing.T) {
{
desc: "add client with a missing client secret",
client: clients.Client{
ID: testsutil.GenerateUUID(t, idProvider),
ID: testsutil.GenerateUUID(t),
Credentials: clients.Credentials{
Identity: "[email protected]",
Secret: "",
Expand Down Expand Up @@ -169,11 +167,11 @@ func TestClientsRetrieveBySecret(t *testing.T) {
repo := postgres.NewRepository(database)

client := clients.Client{
ID: testsutil.GenerateUUID(t, idProvider),
ID: testsutil.GenerateUUID(t),
Name: clientName,
Credentials: clients.Credentials{
Identity: clientIdentity,
Secret: testsutil.GenerateUUID(t, idProvider),
Secret: testsutil.GenerateUUID(t),
},
Status: clients.EnabledStatus,
}
Expand Down
20 changes: 10 additions & 10 deletions things/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
idProvider = uuid.New()
secret = "strongsecret"
validCMetadata = mfclients.Metadata{"role": "client"}
ID = testsutil.GenerateUUID(&testing.T{}, idProvider)
ID = testsutil.GenerateUUID(&testing.T{})
client = mfclients.Client{
ID: ID,
Name: "clientname",
Expand Down Expand Up @@ -218,7 +218,7 @@ func TestRegisterClient(t *testing.T) {
{
desc: "register a new client with empty secret",
client: mfclients.Client{
Owner: testsutil.GenerateUUID(t, idProvider),
Owner: testsutil.GenerateUUID(t),
Credentials: mfclients.Credentials{
Identity: "[email protected]",
},
Expand Down Expand Up @@ -327,7 +327,7 @@ func TestListClients(t *testing.T) {

nClients := uint64(200)
aClients := []mfclients.Client{}
OwnerID := testsutil.GenerateUUID(t, idProvider)
OwnerID := testsutil.GenerateUUID(t)
for i := uint64(1); i < nClients; i++ {
identity := fmt.Sprintf("TestListClients_%[email protected]", i)
client := mfclients.Client{
Expand All @@ -341,7 +341,7 @@ func TestListClients(t *testing.T) {
}
if i%50 == 0 {
client.Owner = OwnerID
client.Owner = testsutil.GenerateUUID(t, idProvider)
client.Owner = testsutil.GenerateUUID(t)
}
aClients = append(aClients, client)
}
Expand Down Expand Up @@ -1089,11 +1089,11 @@ func TestListMembers(t *testing.T) {

nClients := uint64(10)
aClients := []mfclients.Client{}
owner := testsutil.GenerateUUID(t, idProvider)
owner := testsutil.GenerateUUID(t)
for i := uint64(0); i < nClients; i++ {
identity := fmt.Sprintf("member_%[email protected]", i)
client := mfclients.Client{
ID: testsutil.GenerateUUID(t, idProvider),
ID: testsutil.GenerateUUID(t),
Name: identity,
Credentials: mfclients.Credentials{
Identity: identity,
Expand All @@ -1119,7 +1119,7 @@ func TestListMembers(t *testing.T) {
{
desc: "list clients with authorized token",
token: validToken,
groupID: testsutil.GenerateUUID(t, idProvider),
groupID: testsutil.GenerateUUID(t),
page: mfclients.Page{
Owner: adminEmail,
},
Expand All @@ -1136,7 +1136,7 @@ func TestListMembers(t *testing.T) {
{
desc: "list clients with offset and limit",
token: validToken,
groupID: testsutil.GenerateUUID(t, idProvider),
groupID: testsutil.GenerateUUID(t),
page: mfclients.Page{
Offset: 6,
Limit: nClients,
Expand All @@ -1153,7 +1153,7 @@ func TestListMembers(t *testing.T) {
{
desc: "list clients with an invalid token",
token: inValidToken,
groupID: testsutil.GenerateUUID(t, idProvider),
groupID: testsutil.GenerateUUID(t),
page: mfclients.Page{
Owner: adminEmail,
},
Expand Down Expand Up @@ -1185,7 +1185,7 @@ func TestListMembers(t *testing.T) {
{
desc: "list clients for an owner",
token: validToken,
groupID: testsutil.GenerateUUID(t, idProvider),
groupID: testsutil.GenerateUUID(t),
page: mfclients.Page{
Owner: adminEmail,
},
Expand Down
Loading

0 comments on commit e6f34e1

Please sign in to comment.