1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-26 13:48:53 +08:00

59 lines
1.5 KiB
Go
Raw Normal View History

//
// Copyright (c) 2018
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
2018-05-10 23:53:25 +02:00
package mocks
import (
"context"
"github.com/mainflux/mainflux"
"github.com/mainflux/mainflux/things"
2018-05-10 23:53:25 +02:00
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
2018-05-10 23:53:25 +02:00
)
var _ mainflux.ThingsServiceClient = (*thingsClient)(nil)
2018-05-10 23:53:25 +02:00
// ServiceErrToken is used to simulate internal server error.
const ServiceErrToken = "unavailable"
type thingsClient struct {
things map[string]uint64
2018-05-10 23:53:25 +02:00
}
// NewThingsClient returns mock implementation of things service client.
func NewThingsClient(data map[string]uint64) mainflux.ThingsServiceClient {
return &thingsClient{data}
2018-05-10 23:53:25 +02:00
}
func (tc thingsClient) CanAccess(ctx context.Context, req *mainflux.AccessReq, opts ...grpc.CallOption) (*mainflux.ThingID, error) {
2018-05-10 23:53:25 +02:00
key := req.GetToken()
// Since there is no appropriate way to simulate internal server error,
// we had to use this obscure approach. ErrorToken simulates gRPC
// call which returns internal server error.
if key == ServiceErrToken {
return nil, status.Error(codes.Internal, "internal server error")
}
2018-05-10 23:53:25 +02:00
if key == "" {
return nil, things.ErrUnauthorizedAccess
2018-05-10 23:53:25 +02:00
}
id, ok := tc.things[key]
2018-05-10 23:53:25 +02:00
if !ok {
return nil, status.Error(codes.PermissionDenied, "invalid credentials provided")
2018-05-10 23:53:25 +02:00
}
return &mainflux.ThingID{Value: id}, nil
2018-05-10 23:53:25 +02:00
}
func (tc thingsClient) Identify(ctx context.Context, req *mainflux.Token, opts ...grpc.CallOption) (*mainflux.ThingID, error) {
return nil, nil
}