NOISSUE - Fix Consumers Tests (#43)

Fix tests in consumers package by syncing with the new auth scheme
This commit is contained in:
b1ackd0t
2023-11-21 15:50:14 +03:00
committed by GitHub
parent b67d1d2501
commit 01b312a01b
3 changed files with 76 additions and 29 deletions
+38 -13
View File
@@ -13,6 +13,7 @@ import (
"strings"
"testing"
"github.com/absmach/magistrala"
authmocks "github.com/absmach/magistrala/auth/mocks"
"github.com/absmach/magistrala/consumers/notifiers"
httpapi "github.com/absmach/magistrala/consumers/notifiers/api"
@@ -22,6 +23,7 @@ import (
"github.com/absmach/magistrala/pkg/errors"
"github.com/absmach/magistrala/pkg/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
const (
@@ -30,9 +32,9 @@ const (
contact1 = "email1@example.com"
contact2 = "email2@example.com"
token = "token"
wrongValue = "wrong_value"
topic = "topic"
instanceID = "5de9b29a-feb9-11ed-be56-0242ac120002"
validID = "d4ebb847-5d0e-4e46-bdd9-b6aceaaa3a22"
)
var (
@@ -65,13 +67,13 @@ func (tr testRequest) make() (*http.Response, error) {
return tr.client.Do(req)
}
func newService(tokens map[string]string) notifiers.Service {
func newService() (notifiers.Service, *authmocks.Service) {
auth := new(authmocks.Service)
repo := mocks.NewRepo(make(map[string]notifiers.Subscription))
idp := uuid.NewMock()
notif := mocks.NewNotifier()
from := "exampleFrom"
return notifiers.New(auth, repo, idp, notif, from)
return notifiers.New(auth, repo, idp, notif, from), auth
}
func newServer(svc notifiers.Service) *httptest.Server {
@@ -89,7 +91,7 @@ func toJSON(data interface{}) string {
}
func TestCreate(t *testing.T) {
svc := newService(map[string]string{token: email})
svc, auth := newService()
ss := newServer(svc)
defer ss.Close()
@@ -147,7 +149,7 @@ func TestCreate(t *testing.T) {
desc: "add with invalid auth token",
req: data,
contentType: contentType,
auth: wrongValue,
auth: authmocks.InvalidValue,
status: http.StatusUnauthorized,
location: "",
},
@@ -178,6 +180,8 @@ func TestCreate(t *testing.T) {
}
for _, tc := range cases {
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.auth}).Return(&magistrala.IdentityRes{Id: validID}, nil)
req := testRequest{
client: ss.Client(),
method: http.MethodPost,
@@ -192,11 +196,13 @@ func TestCreate(t *testing.T) {
location := res.Header.Get("Location")
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode))
assert.Equal(t, tc.location, location, fmt.Sprintf("%s: expected location %s got %s", tc.desc, tc.location, location))
repoCall.Unset()
}
}
func TestView(t *testing.T) {
svc := newService(map[string]string{token: email})
svc, auth := newService()
ss := newServer(svc)
defer ss.Close()
@@ -204,11 +210,14 @@ func TestView(t *testing.T) {
Topic: topic,
Contact: contact1,
}
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: token}).Return(&magistrala.IdentityRes{Id: validID}, nil)
id, err := svc.CreateSubscription(context.Background(), token, sub)
assert.Nil(t, err, fmt.Sprintf("got an error creating id: %s", err))
repoCall.Unset()
sr := subRes{
ID: id,
OwnerID: email,
OwnerID: validID,
Contact: sub.Contact,
Topic: sub.Topic,
}
@@ -238,7 +247,7 @@ func TestView(t *testing.T) {
{
desc: "view with invalid auth token",
id: id,
auth: wrongValue,
auth: authmocks.InvalidValue,
status: http.StatusUnauthorized,
res: unauthRes,
},
@@ -252,6 +261,8 @@ func TestView(t *testing.T) {
}
for _, tc := range cases {
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.auth}).Return(&magistrala.IdentityRes{Id: validID}, nil)
req := testRequest{
client: ss.Client(),
method: http.MethodGet,
@@ -265,11 +276,13 @@ func TestView(t *testing.T) {
data := strings.Trim(string(body), "\n")
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode))
assert.Equal(t, tc.res, data, fmt.Sprintf("%s: expected body %s got %s", tc.desc, tc.res, data))
repoCall.Unset()
}
}
func TestList(t *testing.T) {
svc := newService(map[string]string{token: email})
svc, auth := newService()
ss := newServer(svc)
defer ss.Close()
@@ -284,14 +297,16 @@ func TestList(t *testing.T) {
if i%2 == 0 {
sub.Contact = contact2
}
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: token}).Return(&magistrala.IdentityRes{Id: validID}, nil)
id, err := svc.CreateSubscription(context.Background(), token, sub)
sr := subRes{
ID: id,
OwnerID: email,
OwnerID: validID,
Contact: sub.Contact,
Topic: sub.Topic,
}
assert.Nil(t, err, fmt.Sprintf("got an error creating id: %s", err))
repoCall.Unset()
subs = append(subs, sr)
}
noLimit := toJSON(page{Offset: 5, Limit: 20, Total: numSubs, Subscriptions: subs[5:25]})
@@ -359,7 +374,7 @@ func TestList(t *testing.T) {
},
{
desc: "list with invalid auth token",
auth: wrongValue,
auth: authmocks.InvalidValue,
status: http.StatusUnauthorized,
res: unauthRes,
},
@@ -372,6 +387,8 @@ func TestList(t *testing.T) {
}
for _, tc := range cases {
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.auth}).Return(&magistrala.IdentityRes{Id: validID}, nil)
req := testRequest{
client: ss.Client(),
method: http.MethodGet,
@@ -385,11 +402,13 @@ func TestList(t *testing.T) {
data := strings.Trim(string(body), "\n")
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode))
assert.Equal(t, tc.res, data, fmt.Sprintf("%s: got unexpected body\n", tc.desc))
repoCall.Unset()
}
}
func TestRemove(t *testing.T) {
svc := newService(map[string]string{token: email})
svc, auth := newService()
ss := newServer(svc)
defer ss.Close()
@@ -397,8 +416,10 @@ func TestRemove(t *testing.T) {
Topic: "topic",
Contact: contact1,
}
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: token}).Return(&magistrala.IdentityRes{Id: validID}, nil)
id, err := svc.CreateSubscription(context.Background(), token, sub)
assert.Nil(t, err, fmt.Sprintf("got an error creating id: %s", err))
repoCall.Unset()
cases := []struct {
desc string
@@ -428,7 +449,7 @@ func TestRemove(t *testing.T) {
{
desc: "view with invalid auth token",
id: id,
auth: wrongValue,
auth: authmocks.InvalidValue,
status: http.StatusUnauthorized,
res: unauthRes,
},
@@ -442,6 +463,8 @@ func TestRemove(t *testing.T) {
}
for _, tc := range cases {
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.auth}).Return(&magistrala.IdentityRes{Id: validID}, nil)
req := testRequest{
client: ss.Client(),
method: http.MethodDelete,
@@ -451,6 +474,8 @@ func TestRemove(t *testing.T) {
res, err := req.make()
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err))
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode))
repoCall.Unset()
}
}
+2 -2
View File
@@ -10,7 +10,7 @@ import (
var _ notifiers.Notifier = (*notifier)(nil)
const invalidSender = "invalid@example.com"
const InvalidSender = "invalid@example.com"
type notifier struct{}
@@ -21,7 +21,7 @@ func NewNotifier() notifiers.Notifier {
func (n notifier) Notify(from string, to []string, msg *messaging.Message) error {
for _, t := range to {
if t == invalidSender {
if t == InvalidSender {
return notifiers.ErrNotify
}
}
+36 -14
View File
@@ -8,34 +8,37 @@ import (
"fmt"
"testing"
"github.com/absmach/magistrala"
authmocks "github.com/absmach/magistrala/auth/mocks"
"github.com/absmach/magistrala/consumers/notifiers"
"github.com/absmach/magistrala/consumers/notifiers/mocks"
"github.com/absmach/magistrala/internal/testsutil"
"github.com/absmach/magistrala/pkg/errors"
"github.com/absmach/magistrala/pkg/messaging"
"github.com/absmach/magistrala/pkg/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
const (
total = 100
exampleUser1 = "email1@example.com"
exampleUser2 = "email2@example.com"
invalidUser = "invalid@example.com"
exampleUser1 = "token1"
exampleUser2 = "token2"
validID = "d4ebb847-5d0e-4e46-bdd9-b6aceaaa3a22"
)
func newService() notifiers.Service {
func newService() (notifiers.Service, *authmocks.Service) {
repo := mocks.NewRepo(make(map[string]notifiers.Subscription))
auth := new(authmocks.Service)
notifier := mocks.NewNotifier()
idp := uuid.NewMock()
from := "exampleFrom"
return notifiers.New(auth, repo, idp, notifier, from)
return notifiers.New(auth, repo, idp, notifier, from), auth
}
func TestCreateSubscription(t *testing.T) {
svc := newService()
svc, auth := newService()
cases := []struct {
desc string
@@ -68,19 +71,23 @@ func TestCreateSubscription(t *testing.T) {
}
for _, tc := range cases {
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.token}).Return(&magistrala.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil)
id, err := svc.CreateSubscription(context.Background(), tc.token, tc.sub)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
assert.Equal(t, tc.id, id, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.id, id))
repoCall.Unset()
}
}
func TestViewSubscription(t *testing.T) {
svc := newService()
svc, auth := newService()
sub := notifiers.Subscription{Contact: exampleUser1, Topic: "valid.topic"}
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: exampleUser1}).Return(&magistrala.IdentityRes{Id: validID}, nil)
id, err := svc.CreateSubscription(context.Background(), exampleUser1, sub)
require.Nil(t, err, "Saving a Subscription must succeed")
repoCall.Unset()
sub.ID = id
sub.OwnerID = exampleUser1
sub.OwnerID = validID
cases := []struct {
desc string
@@ -113,14 +120,16 @@ func TestViewSubscription(t *testing.T) {
}
for _, tc := range cases {
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.token}).Return(&magistrala.IdentityRes{Id: validID}, nil)
sub, err := svc.ViewSubscription(context.Background(), tc.token, tc.id)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
assert.Equal(t, tc.sub, sub, fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.sub, sub))
repoCall.Unset()
}
}
func TestListSubscriptions(t *testing.T) {
svc := newService()
svc, auth := newService()
sub := notifiers.Subscription{Contact: exampleUser1, OwnerID: exampleUser1}
topic := "topic.subtopic"
var subs []notifiers.Subscription
@@ -133,9 +142,12 @@ func TestListSubscriptions(t *testing.T) {
token = exampleUser2
}
tmp.Topic = fmt.Sprintf("%s.%d", topic, i)
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: token}).Return(&magistrala.IdentityRes{Id: validID}, nil)
id, err := svc.CreateSubscription(context.Background(), token, tmp)
require.Nil(t, err, "Saving a Subscription must succeed")
repoCall.Unset()
tmp.ID = id
tmp.OwnerID = validID
subs = append(subs, tmp)
}
@@ -228,19 +240,23 @@ func TestListSubscriptions(t *testing.T) {
}
for _, tc := range cases {
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.token}).Return(&magistrala.IdentityRes{Id: validID}, nil)
page, err := svc.ListSubscriptions(context.Background(), tc.token, tc.pageMeta)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
assert.Equal(t, tc.page, page, fmt.Sprintf("%s: got unexpected page\n", tc.desc))
repoCall.Unset()
}
}
func TestRemoveSubscription(t *testing.T) {
svc := newService()
svc, auth := newService()
sub := notifiers.Subscription{Contact: exampleUser1, Topic: "valid.topic"}
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: exampleUser1}).Return(&magistrala.IdentityRes{Id: validID}, nil)
id, err := svc.CreateSubscription(context.Background(), exampleUser1, sub)
require.Nil(t, err, "Saving a Subscription must succeed")
repoCall.Unset()
sub.ID = id
sub.OwnerID = exampleUser1
sub.OwnerID = validID
cases := []struct {
desc string
@@ -269,16 +285,18 @@ func TestRemoveSubscription(t *testing.T) {
}
for _, tc := range cases {
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.token}).Return(&magistrala.IdentityRes{Id: validID}, nil)
err := svc.RemoveSubscription(context.Background(), tc.token, tc.id)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
repoCall.Unset()
}
}
func TestConsume(t *testing.T) {
svc := newService()
svc, auth := newService()
sub := notifiers.Subscription{
Contact: exampleUser1,
OwnerID: exampleUser1,
OwnerID: validID,
Topic: "topic.subtopic",
}
for i := 0; i < total; i++ {
@@ -287,14 +305,18 @@ func TestConsume(t *testing.T) {
if i%2 == 0 {
tmp.Topic = fmt.Sprintf("%s-2", sub.Topic)
}
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: exampleUser1}).Return(&magistrala.IdentityRes{Id: validID}, nil)
_, err := svc.CreateSubscription(context.Background(), exampleUser1, tmp)
require.Nil(t, err, "Saving a Subscription must succeed")
repoCall.Unset()
}
sub.Contact = invalidUser
sub.Contact = mocks.InvalidSender
sub.Topic = fmt.Sprintf("%s-2", sub.Topic)
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: exampleUser1}).Return(&magistrala.IdentityRes{Id: validID}, nil)
_, err := svc.CreateSubscription(context.Background(), exampleUser1, sub)
require.Nil(t, err, "Saving a Subscription must succeed")
repoCall.Unset()
msg := messaging.Message{
Channel: "topic",