2019-10-07 22:14:47 +08:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 19:15:48 +08:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-08-06 23:06:55 +08:00
|
|
|
package mocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-07-16 00:28:15 +08:00
|
|
|
"github.com/golang/protobuf/ptypes/empty"
|
|
|
|
|
2018-08-06 23:06:55 +08:00
|
|
|
"github.com/mainflux/mainflux"
|
2022-02-02 00:33:23 +08:00
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
2018-08-06 23:06:55 +08:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ mainflux.ThingsServiceClient = (*thingsServiceMock)(nil)
|
|
|
|
|
2022-02-10 04:25:34 +08:00
|
|
|
type thingsServiceMock struct {
|
|
|
|
channels map[string]string
|
|
|
|
}
|
2018-08-06 23:06:55 +08:00
|
|
|
|
|
|
|
// NewThingsService returns mock implementation of things service
|
2022-02-10 04:25:34 +08:00
|
|
|
func NewThingsService(channels map[string]string) mainflux.ThingsServiceClient {
|
|
|
|
return &thingsServiceMock{channels}
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|
|
|
|
|
2019-10-22 05:24:45 +08:00
|
|
|
func (svc thingsServiceMock) CanAccessByKey(ctx context.Context, in *mainflux.AccessByKeyReq, opts ...grpc.CallOption) (*mainflux.ThingID, error) {
|
2018-08-06 23:06:55 +08:00
|
|
|
token := in.GetToken()
|
|
|
|
if token == "invalid" {
|
2022-02-02 00:33:23 +08:00
|
|
|
return nil, errors.ErrAuthentication
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|
|
|
|
|
2018-12-05 20:09:25 +08:00
|
|
|
if token == "" {
|
2022-02-02 00:33:23 +08:00
|
|
|
return nil, errors.ErrAuthentication
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|
|
|
|
|
2022-02-10 04:25:34 +08:00
|
|
|
if token == "token" {
|
|
|
|
return nil, errors.ErrAuthorization
|
|
|
|
}
|
|
|
|
|
2018-12-05 20:09:25 +08:00
|
|
|
return &mainflux.ThingID{Value: token}, nil
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|
|
|
|
|
2019-07-16 00:28:15 +08:00
|
|
|
func (svc thingsServiceMock) CanAccessByID(context.Context, *mainflux.AccessByIDReq, ...grpc.CallOption) (*empty.Empty, error) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
2022-02-10 04:25:34 +08:00
|
|
|
func (svc thingsServiceMock) IsChannelOwner(ctx context.Context, in *mainflux.ChannelOwnerReq, opts ...grpc.CallOption) (*empty.Empty, error) {
|
|
|
|
if id, ok := svc.channels[in.GetOwner()]; ok {
|
|
|
|
if id == in.ChanID {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errors.ErrAuthorization
|
2021-02-23 02:41:59 +08:00
|
|
|
}
|
|
|
|
|
2019-07-16 00:28:15 +08:00
|
|
|
func (svc thingsServiceMock) Identify(context.Context, *mainflux.Token, ...grpc.CallOption) (*mainflux.ThingID, error) {
|
|
|
|
panic("not implemented")
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|