1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00

Fix HTTP Tests (#1940)

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
This commit is contained in:
b1ackd0t 2023-10-22 00:49:26 +03:00 committed by GitHub
parent 6da9a5da21
commit 02158d4701
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,20 +11,23 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/mainflux/mainflux"
authmocks "github.com/mainflux/mainflux/auth/mocks" authmocks "github.com/mainflux/mainflux/auth/mocks"
server "github.com/mainflux/mainflux/http" server "github.com/mainflux/mainflux/http"
"github.com/mainflux/mainflux/http/api" "github.com/mainflux/mainflux/http/api"
"github.com/mainflux/mainflux/http/mocks" "github.com/mainflux/mainflux/http/mocks"
"github.com/mainflux/mainflux/internal/apiutil" "github.com/mainflux/mainflux/internal/apiutil"
"github.com/mainflux/mainflux/internal/testsutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
) )
const instanceID = "5de9b29a-feb9-11ed-be56-0242ac120002" const instanceID = "5de9b29a-feb9-11ed-be56-0242ac120002"
func newService() server.Service { func newService() (server.Service, *authmocks.Service) {
auth := new(authmocks.Service) auth := new(authmocks.Service)
pub := mocks.NewPublisher() pub := mocks.NewPublisher()
return server.New(pub, auth) return server.New(pub, auth), auth
} }
func newHTTPServer(svc server.Service) *httptest.Server { func newHTTPServer(svc server.Service) *httptest.Server {
@ -66,11 +69,11 @@ func TestPublish(t *testing.T) {
ctSenmlCBOR := "application/senml+cbor" ctSenmlCBOR := "application/senml+cbor"
ctJSON := "application/json" ctJSON := "application/json"
thingKey := "thing_key" thingKey := "thing_key"
invalidKey := "invalid_key" invalidKey := authmocks.InvalidValue
msg := `[{"n":"current","t":-1,"v":1.6}]` msg := `[{"n":"current","t":-1,"v":1.6}]`
msgJSON := `{"field1":"val1","field2":"val2"}` msgJSON := `{"field1":"val1","field2":"val2"}`
msgCBOR := `81A3616E6763757272656E746174206176FB3FF999999999999A` msgCBOR := `81A3616E6763757272656E746174206176FB3FF999999999999A`
svc := newService() svc, auth := newService()
ts := newHTTPServer(svc) ts := newHTTPServer(svc)
defer ts.Close() defer ts.Close()
@ -123,7 +126,7 @@ func TestPublish(t *testing.T) {
msg: msg, msg: msg,
contentType: ctSenmlJSON, contentType: ctSenmlJSON,
key: invalidKey, key: invalidKey,
status: http.StatusUnauthorized, status: http.StatusForbidden,
}, },
"publish message with invalid basic auth": { "publish message with invalid basic auth": {
chanID: chanID, chanID: chanID,
@ -131,7 +134,7 @@ func TestPublish(t *testing.T) {
contentType: ctSenmlJSON, contentType: ctSenmlJSON,
key: invalidKey, key: invalidKey,
basicAuth: true, basicAuth: true,
status: http.StatusUnauthorized, status: http.StatusForbidden,
}, },
"publish message without content type": { "publish message without content type": {
chanID: chanID, chanID: chanID,
@ -151,12 +154,13 @@ func TestPublish(t *testing.T) {
chanID: chanID, chanID: chanID,
msg: msg, msg: msg,
contentType: ctSenmlJSON, contentType: ctSenmlJSON,
// key: mocks.ServiceErrToken, key: authmocks.InvalidValue,
status: http.StatusInternalServerError, status: http.StatusForbidden,
}, },
} }
for desc, tc := range cases { for desc, tc := range cases {
repocall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&mainflux.AuthorizeRes{Authorized: true, Id: testsutil.GenerateUUID(t)}, nil)
req := testRequest{ req := testRequest{
client: ts.Client(), client: ts.Client(),
method: http.MethodPost, method: http.MethodPost,
@ -169,5 +173,6 @@ func TestPublish(t *testing.T) {
res, err := req.make() res, err := req.make()
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", desc, err)) assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", desc, err))
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", desc, tc.status, res.StatusCode)) assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", desc, tc.status, res.StatusCode))
repocall.Unset()
} }
} }