MG-31 - Fix Consumers tests (#101)

Signed-off-by: felix.gateru <felix.gateru@gmail.com>
This commit is contained in:
Felix Gateru
2023-12-01 15:47:07 +03:00
committed by GitHub
parent 94cfadc745
commit 3d1ce7c3c8
3 changed files with 27 additions and 18 deletions
+3 -3
View File
@@ -20,7 +20,7 @@ import (
"github.com/absmach/magistrala/consumers/notifiers/mocks"
"github.com/absmach/magistrala/internal/apiutil"
"github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/errors"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
"github.com/absmach/magistrala/pkg/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
@@ -38,8 +38,8 @@ const (
)
var (
notFoundRes = toJSON(apiutil.ErrorRes{Msg: errors.ErrNotFound.Error()})
unauthRes = toJSON(apiutil.ErrorRes{Msg: errors.ErrAuthentication.Error()})
notFoundRes = toJSON(apiutil.ErrorRes{Msg: svcerr.ErrNotFound.Error()})
unauthRes = toJSON(apiutil.ErrorRes{Msg: svcerr.ErrAuthentication.Error()})
invalidRes = toJSON(apiutil.ErrorRes{Err: apiutil.ErrInvalidQueryParams.Error(), Msg: apiutil.ErrValidation.Error()})
missingTokRes = toJSON(apiutil.ErrorRes{Err: apiutil.ErrBearerToken.Error(), Msg: apiutil.ErrValidation.Error()})
)
+15 -7
View File
@@ -14,6 +14,7 @@ import (
"github.com/absmach/magistrala/internal/apiutil"
"github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/errors"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
"github.com/go-chi/chi/v5"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -53,6 +54,13 @@ func MakeHandler(svc notifiers.Service, logger logger.Logger, instanceID string)
opts...,
), "list").ServeHTTP)
r.Delete("/", otelhttp.NewHandler(kithttp.NewServer(
deleteSubscriptionEndpint(svc),
decodeSubscription,
encodeResponse,
opts...,
), "delete").ServeHTTP)
r.Get("/{subID}", otelhttp.NewHandler(kithttp.NewServer(
viewSubscriptionEndpint(svc),
decodeSubscription,
@@ -146,25 +154,25 @@ func encodeError(_ context.Context, err error, w http.ResponseWriter) {
}
switch {
case errors.Contains(err, errors.ErrMalformedEntity),
case errors.Contains(err, svcerr.ErrMalformedEntity),
errors.Contains(err, apiutil.ErrInvalidContact),
errors.Contains(err, apiutil.ErrInvalidTopic),
errors.Contains(err, apiutil.ErrMissingID),
errors.Contains(err, apiutil.ErrInvalidQueryParams):
w.WriteHeader(http.StatusBadRequest)
case errors.Contains(err, errors.ErrNotFound):
case errors.Contains(err, svcerr.ErrNotFound):
w.WriteHeader(http.StatusNotFound)
case errors.Contains(err, errors.ErrAuthentication),
case errors.Contains(err, svcerr.ErrAuthentication),
errors.Contains(err, apiutil.ErrBearerToken):
w.WriteHeader(http.StatusUnauthorized)
case errors.Contains(err, errors.ErrConflict):
case errors.Contains(err, svcerr.ErrConflict):
w.WriteHeader(http.StatusConflict)
case errors.Contains(err, apiutil.ErrUnsupportedContentType):
w.WriteHeader(http.StatusUnsupportedMediaType)
case errors.Contains(err, errors.ErrCreateEntity),
errors.Contains(err, errors.ErrViewEntity),
errors.Contains(err, errors.ErrRemoveEntity):
case errors.Contains(err, svcerr.ErrCreateEntity),
errors.Contains(err, svcerr.ErrViewEntity),
errors.Contains(err, svcerr.ErrRemoveEntity):
w.WriteHeader(http.StatusInternalServerError)
default:
+9 -8
View File
@@ -14,6 +14,7 @@ import (
"github.com/absmach/magistrala/consumers/notifiers/mocks"
"github.com/absmach/magistrala/internal/testsutil"
"github.com/absmach/magistrala/pkg/errors"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
"github.com/absmach/magistrala/pkg/messaging"
"github.com/absmach/magistrala/pkg/uuid"
"github.com/stretchr/testify/assert"
@@ -59,14 +60,14 @@ func TestCreateSubscription(t *testing.T) {
token: exampleUser1,
sub: notifiers.Subscription{Contact: exampleUser1, Topic: "valid.topic"},
id: "",
err: errors.ErrConflict,
err: svcerr.ErrConflict,
},
{
desc: "test with empty token",
token: "",
sub: notifiers.Subscription{Contact: exampleUser1, Topic: "valid.topic"},
id: "",
err: errors.ErrAuthentication,
err: svcerr.ErrAuthentication,
},
}
@@ -108,14 +109,14 @@ func TestViewSubscription(t *testing.T) {
token: exampleUser1,
id: "not_exist",
sub: notifiers.Subscription{},
err: errors.ErrNotFound,
err: svcerr.ErrNotFound,
},
{
desc: "test with empty token",
token: "",
id: id,
sub: notifiers.Subscription{},
err: errors.ErrAuthentication,
err: svcerr.ErrAuthentication,
},
}
@@ -188,7 +189,7 @@ func TestListSubscriptions(t *testing.T) {
Contact: "empty@example.com",
},
page: notifiers.Page{},
err: errors.ErrNotFound,
err: svcerr.ErrNotFound,
},
{
desc: "test with empty token",
@@ -199,7 +200,7 @@ func TestListSubscriptions(t *testing.T) {
Topic: "topic.subtopic.13",
},
page: notifiers.Page{},
err: errors.ErrAuthentication,
err: svcerr.ErrAuthentication,
},
{
desc: "test with topic",
@@ -274,13 +275,13 @@ func TestRemoveSubscription(t *testing.T) {
desc: "test not existing",
token: exampleUser1,
id: "not_exist",
err: errors.ErrNotFound,
err: svcerr.ErrNotFound,
},
{
desc: "test with empty token",
token: "",
id: id,
err: errors.ErrAuthentication,
err: svcerr.ErrAuthentication,
},
}