NOISSUE - Update messaging SDK and CLI (#2815)

Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>
This commit is contained in:
Dušan Borovčanin
2025-04-11 12:31:50 +02:00
committed by GitHub
parent 5fd1d9b431
commit 01f20a29d2
6 changed files with 86 additions and 92 deletions
-6
View File
@@ -272,12 +272,6 @@ supermq-cli channels connections <channel_id> <user_token>
supermq-cli messages send <domain_id> <channel_id.subtopic> <secret> '[{"bn":"Dev1","n":"temp","v":20}, {"n":"hum","v":40}, {"bn":"Dev2", "n":"temp","v":20}, {"n":"hum","v":40}]'
```
#### Read messages over HTTP
```bash
supermq-cli messages read <channel_id> <user_token> -R <reader_url>
```
### Groups
#### Create Group
+2 -2
View File
@@ -7,7 +7,7 @@ import "github.com/spf13/cobra"
var cmdMessages = []cobra.Command{
{
Use: "send <domain_id> <channel_id.subtopic> <secret> <JSON_string>",
Use: "send <domain_id> <channel_id.subtopic> <JSON_string> <secret>",
Short: "Send messages",
Long: `Sends message on the channel`,
Run: func(cmd *cobra.Command, args []string) {
@@ -29,7 +29,7 @@ var cmdMessages = []cobra.Command{
// NewMessagesCmd returns messages command.
func NewMessagesCmd() *cobra.Command {
cmd := cobra.Command{
Use: "messages [send | read]",
Use: "messages [send]",
Short: "Send messages",
Long: `Send messages using the http-adapter`,
}
+1 -1
View File
@@ -15,7 +15,7 @@ import (
const channelParts = 2
func (sdk mgSDK) SendMessage(ctx context.Context, domainID, topic, secret, msg string) errors.SDKError {
func (sdk mgSDK) SendMessage(ctx context.Context, domainID, topic, msg, secret string) errors.SDKError {
chanNameParts := strings.SplitN(topic, ".", channelParts)
chanID := chanNameParts[0]
subtopicPart := ""
+73 -73
View File
@@ -74,92 +74,92 @@ func TestSendMessage(t *testing.T) {
mgsdk := sdk.NewSDK(sdkConf)
cases := []struct {
desc string
topic string
domainID string
msg string
clientKey string
authRes *grpcClientsV1.AuthnRes
authErr error
svcErr error
err errors.SDKError
desc string
topic string
domainID string
msg string
secret string
authRes *grpcClientsV1.AuthnRes
authErr error
svcErr error
err errors.SDKError
}{
{
desc: "publish message successfully",
topic: channelID,
domainID: domainID,
msg: msg,
clientKey: clientKey,
authRes: &grpcClientsV1.AuthnRes{Authenticated: true, Id: ""},
authErr: nil,
svcErr: nil,
err: nil,
desc: "publish message successfully",
topic: channelID,
domainID: domainID,
msg: msg,
secret: clientKey,
authRes: &grpcClientsV1.AuthnRes{Authenticated: true, Id: ""},
authErr: nil,
svcErr: nil,
err: nil,
},
{
desc: "publish message with empty client key",
topic: channelID,
domainID: domainID,
msg: msg,
clientKey: "",
authRes: &grpcClientsV1.AuthnRes{Authenticated: false, Id: ""},
authErr: svcerr.ErrAuthentication,
svcErr: nil,
err: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, http.StatusUnauthorized),
desc: "publish message with empty client key",
topic: channelID,
domainID: domainID,
msg: msg,
secret: "",
authRes: &grpcClientsV1.AuthnRes{Authenticated: false, Id: ""},
authErr: svcerr.ErrAuthentication,
svcErr: nil,
err: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, http.StatusUnauthorized),
},
{
desc: "publish message with invalid client key",
topic: channelID,
domainID: domainID,
msg: msg,
clientKey: "invalid",
authRes: &grpcClientsV1.AuthnRes{Authenticated: false, Id: ""},
authErr: svcerr.ErrAuthentication,
svcErr: svcerr.ErrAuthentication,
err: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, http.StatusUnauthorized),
desc: "publish message with invalid client key",
topic: channelID,
domainID: domainID,
msg: msg,
secret: "invalid",
authRes: &grpcClientsV1.AuthnRes{Authenticated: false, Id: ""},
authErr: svcerr.ErrAuthentication,
svcErr: svcerr.ErrAuthentication,
err: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, http.StatusUnauthorized),
},
{
desc: "publish message with invalid channel ID",
topic: wrongID,
domainID: domainID,
msg: msg,
clientKey: clientKey,
authRes: &grpcClientsV1.AuthnRes{Authenticated: false, Id: ""},
authErr: svcerr.ErrAuthentication,
svcErr: svcerr.ErrAuthentication,
err: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, http.StatusUnauthorized),
desc: "publish message with invalid channel ID",
topic: wrongID,
domainID: domainID,
msg: msg,
secret: clientKey,
authRes: &grpcClientsV1.AuthnRes{Authenticated: false, Id: ""},
authErr: svcerr.ErrAuthentication,
svcErr: svcerr.ErrAuthentication,
err: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, http.StatusUnauthorized),
},
{
desc: "publish message with empty message body",
topic: channelID,
domainID: domainID,
msg: "",
clientKey: clientKey,
authRes: &grpcClientsV1.AuthnRes{Authenticated: true, Id: ""},
authErr: nil,
svcErr: nil,
err: errors.NewSDKErrorWithStatus(errors.Wrap(apiutil.ErrValidation, apiutil.ErrEmptyMessage), http.StatusBadRequest),
desc: "publish message with empty message body",
topic: channelID,
domainID: domainID,
msg: "",
secret: clientKey,
authRes: &grpcClientsV1.AuthnRes{Authenticated: true, Id: ""},
authErr: nil,
svcErr: nil,
err: errors.NewSDKErrorWithStatus(errors.Wrap(apiutil.ErrValidation, apiutil.ErrEmptyMessage), http.StatusBadRequest),
},
{
desc: "publish message with channel subtopic",
topic: channelID + ".subtopic",
domainID: domainID,
msg: msg,
clientKey: clientKey,
authRes: &grpcClientsV1.AuthnRes{Authenticated: true, Id: ""},
authErr: nil,
svcErr: nil,
err: nil,
desc: "publish message with channel subtopic",
topic: channelID + ".subtopic",
domainID: domainID,
msg: msg,
secret: clientKey,
authRes: &grpcClientsV1.AuthnRes{Authenticated: true, Id: ""},
authErr: nil,
svcErr: nil,
err: nil,
},
{
desc: "publish message with invalid domain ID",
topic: channelID,
domainID: wrongID,
msg: msg,
clientKey: clientKey,
authRes: &grpcClientsV1.AuthnRes{Authenticated: false, Id: ""},
authErr: svcerr.ErrAuthentication,
svcErr: svcerr.ErrAuthentication,
err: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, http.StatusUnauthorized),
desc: "publish message with invalid domain ID",
topic: channelID,
domainID: wrongID,
msg: msg,
secret: clientKey,
authRes: &grpcClientsV1.AuthnRes{Authenticated: false, Id: ""},
authErr: svcerr.ErrAuthentication,
svcErr: svcerr.ErrAuthentication,
err: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, http.StatusUnauthorized),
},
}
for _, tc := range cases {
@@ -167,7 +167,7 @@ func TestSendMessage(t *testing.T) {
authzCall := clientsGRPCClient.On("Authenticate", mock.Anything, mock.Anything).Return(tc.authRes, tc.authErr)
authnCall := channelsGRPCClient.On("Authorize", mock.Anything, mock.Anything).Return(&grpcChannelsV1.AuthzRes{Authorized: true}, nil)
svcCall := pub.On("Publish", mock.Anything, channelID, mock.Anything).Return(tc.svcErr)
err := mgsdk.SendMessage(context.Background(), tc.domainID, tc.topic, tc.clientKey, tc.msg)
err := mgsdk.SendMessage(context.Background(), tc.domainID, tc.topic, tc.msg, tc.secret)
assert.Equal(t, tc.err, err)
if tc.err == nil {
ok := svcCall.Parent.AssertCalled(t, "Publish", mock.Anything, channelID, mock.Anything)
+8 -8
View File
@@ -5644,8 +5644,8 @@ func (_c *SDK_SendInvitation_Call) RunAndReturn(run func(ctx context.Context, in
}
// SendMessage provides a mock function for the type SDK
func (_mock *SDK) SendMessage(ctx context.Context, domainID string, topic string, secret string, msg string) errors.SDKError {
ret := _mock.Called(ctx, domainID, topic, secret, msg)
func (_mock *SDK) SendMessage(ctx context.Context, domainID string, topic string, msg string, secret string) errors.SDKError {
ret := _mock.Called(ctx, domainID, topic, msg, secret)
if len(ret) == 0 {
panic("no return value specified for SendMessage")
@@ -5653,7 +5653,7 @@ func (_mock *SDK) SendMessage(ctx context.Context, domainID string, topic string
var r0 errors.SDKError
if returnFunc, ok := ret.Get(0).(func(context.Context, string, string, string, string) errors.SDKError); ok {
r0 = returnFunc(ctx, domainID, topic, secret, msg)
r0 = returnFunc(ctx, domainID, topic, msg, secret)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(errors.SDKError)
@@ -5671,13 +5671,13 @@ type SDK_SendMessage_Call struct {
// - ctx
// - domainID
// - topic
// - secret
// - msg
func (_e *SDK_Expecter) SendMessage(ctx interface{}, domainID interface{}, topic interface{}, secret interface{}, msg interface{}) *SDK_SendMessage_Call {
return &SDK_SendMessage_Call{Call: _e.mock.On("SendMessage", ctx, domainID, topic, secret, msg)}
// - secret
func (_e *SDK_Expecter) SendMessage(ctx interface{}, domainID interface{}, topic interface{}, msg interface{}, secret interface{}) *SDK_SendMessage_Call {
return &SDK_SendMessage_Call{Call: _e.mock.On("SendMessage", ctx, domainID, topic, msg, secret)}
}
func (_c *SDK_SendMessage_Call) Run(run func(ctx context.Context, domainID string, topic string, secret string, msg string)) *SDK_SendMessage_Call {
func (_c *SDK_SendMessage_Call) Run(run func(ctx context.Context, domainID string, topic string, msg string, secret string)) *SDK_SendMessage_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(string), args[4].(string))
})
@@ -5689,7 +5689,7 @@ func (_c *SDK_SendMessage_Call) Return(sDKError errors.SDKError) *SDK_SendMessag
return _c
}
func (_c *SDK_SendMessage_Call) RunAndReturn(run func(ctx context.Context, domainID string, topic string, secret string, msg string) errors.SDKError) *SDK_SendMessage_Call {
func (_c *SDK_SendMessage_Call) RunAndReturn(run func(ctx context.Context, domainID string, topic string, msg string, secret string) errors.SDKError) *SDK_SendMessage_Call {
_c.Call.Return(run)
return _c
}
+2 -2
View File
@@ -1015,9 +1015,9 @@ type SDK interface {
//
// example:
// msg := '[{"bn":"some-base-name:","bt":1.276020076001e+09, "bu":"A","bver":5, "n":"voltage","u":"V","v":120.1}, {"n":"current","t":-5,"v":1.2}, {"n":"current","t":-4,"v":1.3}]'
// err := sdk.SendMessage("domainID", "topic", "clientSecret", msg)
// err := sdk.SendMessage("domainID", "topic", msg, "clientSecret")
// fmt.Println(err)
SendMessage(ctx context.Context, domainID, topic, secret string, msg string) errors.SDKError
SendMessage(ctx context.Context, domainID, topic, msg, secret string) errors.SDKError
// SetContentType sets message content type.
//