1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00
Mainflux.mainflux/pkg/sdk/go/message_test.go
Manuel Imperiale e5278c463f
MF-1348 - Add transport errors logging (#1544)
* MF-1348 - Add go-kit transport level logging

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix reviews

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix reviews

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix merge

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix remark

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix go test flags

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Use httputil errors in things and http service

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix SDK tests

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Use httputil errors in certs and provision service

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Use httputil errors in consumers service

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* General renaming and add ErrMissingToken

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Rename httputil -> apiutil and use errors in users servive

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Use apiutil errors in auth, bootstrap, readers, things and twins

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Replace errors.Contain by comparison

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix remarks

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Simplify validateID

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Simplify validateID

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Simplify and rename ExtractAuthToken -> ExtractBearerToken

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix readers

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix auth key test and remarks

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Improve comment

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Simplify validateUUID check

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix typo

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
2022-03-03 17:13:46 +01:00

136 lines
3.3 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package sdk_test
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/mainflux/mainflux"
adapter "github.com/mainflux/mainflux/http"
"github.com/mainflux/mainflux/http/api"
"github.com/mainflux/mainflux/http/mocks"
"github.com/mainflux/mainflux/logger"
sdk "github.com/mainflux/mainflux/pkg/sdk/go"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/stretchr/testify/assert"
)
func newMessageService(cc mainflux.ThingsServiceClient) adapter.Service {
pub := mocks.NewPublisher()
return adapter.New(pub, cc)
}
func newMessageServer(svc adapter.Service) *httptest.Server {
logger := logger.NewMock()
mux := api.MakeHandler(svc, mocktracer.New(), logger)
return httptest.NewServer(mux)
}
func TestSendMessage(t *testing.T) {
chanID := "1"
atoken := "auth_token"
invalidToken := "invalid_token"
msg := `[{"n":"current","t":-1,"v":1.6}]`
thingsClient := mocks.NewThingsClient(map[string]string{atoken: chanID})
pub := newMessageService(thingsClient)
ts := newMessageServer(pub)
defer ts.Close()
sdkConf := sdk.Config{
HTTPAdapterURL: ts.URL,
MsgContentType: contentType,
TLSVerification: false,
}
mainfluxSDK := sdk.NewSDK(sdkConf)
cases := map[string]struct {
chanID string
msg string
auth string
err error
}{
"publish message": {
chanID: chanID,
msg: msg,
auth: atoken,
err: nil,
},
"publish message without authorization token": {
chanID: chanID,
msg: msg,
auth: "",
err: createError(sdk.ErrFailedPublish, http.StatusUnauthorized),
},
"publish message with invalid authorization token": {
chanID: chanID,
msg: msg,
auth: invalidToken,
err: createError(sdk.ErrFailedPublish, http.StatusUnauthorized),
},
"publish message with wrong content type": {
chanID: chanID,
msg: "text",
auth: atoken,
err: nil,
},
"publish message to wrong channel": {
chanID: "",
msg: msg,
auth: atoken,
err: createError(sdk.ErrFailedPublish, http.StatusBadRequest),
},
"publish message unable to authorize": {
chanID: chanID,
msg: msg,
auth: "invalid-token",
err: createError(sdk.ErrFailedPublish, http.StatusUnauthorized),
},
}
for desc, tc := range cases {
err := mainfluxSDK.SendMessage(tc.chanID, tc.msg, tc.auth)
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", desc, tc.err, err))
}
}
func TestSetContentType(t *testing.T) {
chanID := "1"
atoken := "auth_token"
thingsClient := mocks.NewThingsClient(map[string]string{atoken: chanID})
pub := newMessageService(thingsClient)
ts := newMessageServer(pub)
defer ts.Close()
sdkConf := sdk.Config{
HTTPAdapterURL: ts.URL,
MsgContentType: contentType,
TLSVerification: false,
}
mainfluxSDK := sdk.NewSDK(sdkConf)
cases := []struct {
desc string
cType sdk.ContentType
err error
}{
{
desc: "set senml+json content type",
cType: "application/senml+json",
err: nil,
},
{
desc: "set invalid content type",
cType: "invalid",
err: sdk.ErrInvalidContentType,
},
}
for _, tc := range cases {
err := mainfluxSDK.SetContentType(tc.cType)
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", tc.desc, tc.err, err))
}
}