1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-26 13:48:53 +08:00
Mainflux.mainflux/pkg/sdk/go/message_test.go
Aryan Godara 5e8555444a
MF-1723 - Fix lack of logging for invalid query params (#1724)
* fix error package errors

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix bootstap and bootstrap api

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix certs

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix consumers

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix http

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix provision

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix readers

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix twins

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix things

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix users

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix sdk excpet channel policies users things

Signed-off-by: aryan <aryangodara03@gmail.com>

* tests passing, but logging not working for things and users

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix sdk tests, and other failing tests

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix comment

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix errors acc to pr review

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix errror wrapping in consumers api

Signed-off-by: aryan <aryangodara03@gmail.com>

* all tests running

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix encodeError

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix minor issues

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix failing sdk policy tests

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix errors in things test sdk

Signed-off-by: aryan <aryangodara03@gmail.com>

* update things service

Signed-off-by: aryan <aryangodara03@gmail.com>

* update usrs service

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix things and users sdk

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix sdk for channels groups policies things users

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix remaining services and sdk

Signed-off-by: aryan <aryangodara03@gmail.com>

* fix bootstrap twins

Signed-off-by: aryan <aryangodara03@gmail.com>

* resolve conflicts

Signed-off-by: aryan <aryangodara03@gmail.com>

* Shift errmalformedentity to pkg/errors

Signed-off-by: aryan <aryangodara03@gmail.com>

* Fix bootstrap service

Signed-off-by: aryan <aryangodara03@gmail.com>

* Add errors.Unwrap and use in encodeError

Signed-off-by: aryan <aryangodara03@gmail.com>

* Fix type in print statement for policies_test

Signed-off-by: aryan <aryangodara03@gmail.com>

* Fix ordering of errvalidation wrapping and encodeError

Signed-off-by: aryan <aryangodara03@gmail.com>

* Fix failing tests

Signed-off-by: aryan <aryangodara03@gmail.com>

---------

Signed-off-by: aryan <aryangodara03@gmail.com>
2023-08-09 23:02:44 +02:00

141 lines
3.5 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package sdk_test
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
adapter "github.com/mainflux/mainflux/http"
"github.com/mainflux/mainflux/http/api"
"github.com/mainflux/mainflux/http/mocks"
"github.com/mainflux/mainflux/internal/apiutil"
"github.com/mainflux/mainflux/pkg/errors"
sdk "github.com/mainflux/mainflux/pkg/sdk/go"
"github.com/mainflux/mainflux/things/policies"
"github.com/stretchr/testify/assert"
)
func newMessageService(cc policies.AuthServiceClient) adapter.Service {
pub := mocks.NewPublisher()
return adapter.New(pub, cc)
}
func newMessageServer(svc adapter.Service) *httptest.Server {
mux := api.MakeHandler(svc, instanceID)
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,
}
mfsdk := sdk.NewSDK(sdkConf)
cases := map[string]struct {
chanID string
msg string
auth string
err errors.SDKError
}{
"publish message": {
chanID: chanID,
msg: msg,
auth: atoken,
err: nil,
},
"publish message without authorization token": {
chanID: chanID,
msg: msg,
auth: "",
err: errors.NewSDKErrorWithStatus(errors.Wrap(apiutil.ErrValidation, apiutil.ErrBearerKey), http.StatusUnauthorized),
},
"publish message with invalid authorization token": {
chanID: chanID,
msg: msg,
auth: invalidToken,
err: errors.NewSDKErrorWithStatus(errors.New(""), 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: errors.NewSDKErrorWithStatus(errors.Wrap(apiutil.ErrValidation, errors.ErrMalformedEntity), http.StatusBadRequest),
},
"publish message unable to authorize": {
chanID: chanID,
msg: msg,
auth: "invalid-token",
err: errors.NewSDKErrorWithStatus(errors.New(""), http.StatusUnauthorized),
},
}
for desc, tc := range cases {
err := mfsdk.SendMessage(tc.chanID, tc.msg, tc.auth)
if tc.err == nil {
assert.Nil(t, err, fmt.Sprintf("%s: got unexpected error: %s", desc, err))
} else {
assert.Equal(t, tc.err.Error(), err.Error(), fmt.Sprintf("%s: expected error %s, got %s", desc, err, tc.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,
}
mfsdk := sdk.NewSDK(sdkConf)
cases := []struct {
desc string
cType sdk.ContentType
err errors.SDKError
}{
{
desc: "set senml+json content type",
cType: "application/senml+json",
err: nil,
},
{
desc: "set invalid content type",
cType: "invalid",
err: errors.NewSDKError(apiutil.ErrUnsupportedContentType),
},
}
for _, tc := range cases {
err := mfsdk.SetContentType(tc.cType)
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", tc.desc, tc.err, err))
}
}