1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-24 13:48:49 +08:00
Mainflux.mainflux/ws/adapter_test.go
Sammy Kerata Oina 9b4c2b24c8
UV-267 - Update WS service to mproxy (#1917)
* update WS service to mproxy

Signed-off-by: SammyOina <sammyoina@gmail.com>

* Fix brokerstracing initialization in main.go

The commit fixes the initialization of the brokerstracing package in the main.go file. The target server configuration is now correctly passed to the NewPubSub function, ensuring that the correct host and port are used for the PubSub service. This resolves an issue where the wrong server configuration was being used, leading to incorrect behavior.

Signed-off-by: SammyOina <sammyoina@gmail.com>

* Fix goroutine issue in main.go

The commit fixes a goroutine issue in the main.go file. Previously, the `hs.Start()` function was not being executed in a goroutine, which caused the program to block. This commit wraps the `hs.Start()` function call in a goroutine to ensure it runs concurrently. This resolves the issue and allows the program to continue execution without blocking.

Signed-off-by: SammyOina <sammyoina@gmail.com>

* update to current mproxy

Signed-off-by: SammyOina <sammyoina@gmail.com>

* update dependencies

Signed-off-by: SammyOina <sammyoina@gmail.com>

* Fix targetWSHost value in proxyWS function

The targetWSHost value in the proxyWS function was empty. This commit fixes the issue by setting the targetWSHost value to "localhost". Additionally, the target variable in the proxyWS function is updated to include the "ws://" protocol prefix.

Signed-off-by: SammyOina <sammyoina@gmail.com>

* update deps

Signed-off-by: SammyOina <sammyoina@gmail.com>

* remove authorize from unsubscribe

Signed-off-by: SammyOina <sammyoina@gmail.com>

---------

Signed-off-by: SammyOina <sammyoina@gmail.com>
2023-10-23 16:22:09 +02:00

124 lines
3.0 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package ws_test
import (
"context"
"fmt"
"testing"
"github.com/mainflux/mainflux"
authmocks "github.com/mainflux/mainflux/auth/mocks"
"github.com/mainflux/mainflux/internal/testsutil"
"github.com/mainflux/mainflux/pkg/errors"
"github.com/mainflux/mainflux/pkg/messaging"
"github.com/mainflux/mainflux/ws"
"github.com/mainflux/mainflux/ws/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
const (
chanID = "1"
id = "1"
thingKey = "thing_key"
subTopic = "subtopic"
protocol = "ws"
)
var msg = messaging.Message{
Channel: chanID,
Publisher: id,
Subtopic: "",
Protocol: protocol,
Payload: []byte(`[{"n":"current","t":-5,"v":1.2}]`),
}
func newService() (ws.Service, mocks.MockPubSub, *authmocks.Service) {
pubsub := mocks.NewPubSub()
auth := new(authmocks.Service)
return ws.New(auth, pubsub), pubsub, auth
}
func TestSubscribe(t *testing.T) {
svc, pubsub, auth := newService()
c := ws.NewClient(nil)
cases := []struct {
desc string
thingKey string
chanID string
subtopic string
fail bool
err error
}{
{
desc: "subscribe to channel with valid thingKey, chanID, subtopic",
thingKey: thingKey,
chanID: chanID,
subtopic: subTopic,
fail: false,
err: nil,
},
{
desc: "subscribe again to channel with valid thingKey, chanID, subtopic",
thingKey: thingKey,
chanID: chanID,
subtopic: subTopic,
fail: false,
err: nil,
},
{
desc: "subscribe to channel with subscribe set to fail",
thingKey: thingKey,
chanID: chanID,
subtopic: subTopic,
fail: true,
err: errors.Wrap(ws.ErrFailedSubscription, ws.ErrFailedSubscription),
},
{
desc: "subscribe to channel with invalid chanID and invalid thingKey",
thingKey: authmocks.InvalidValue,
chanID: authmocks.InvalidValue,
subtopic: subTopic,
fail: false,
err: ws.ErrUnauthorizedAccess,
},
{
desc: "subscribe to channel with empty channel",
thingKey: thingKey,
chanID: "",
subtopic: subTopic,
fail: false,
err: ws.ErrUnauthorizedAccess,
},
{
desc: "subscribe to channel with empty thingKey",
thingKey: "",
chanID: chanID,
subtopic: subTopic,
fail: false,
err: ws.ErrUnauthorizedAccess,
},
{
desc: "subscribe to channel with empty thingKey and empty channel",
thingKey: "",
chanID: "",
subtopic: subTopic,
fail: false,
err: ws.ErrUnauthorizedAccess,
},
}
for _, tc := range cases {
pubsub.SetFail(tc.fail)
repocall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&mainflux.AuthorizeRes{Authorized: true, Id: testsutil.GenerateUUID(t)}, nil)
err := svc.Subscribe(context.Background(), tc.thingKey, tc.chanID, tc.subtopic, c)
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
repocall.Unset()
}
}