2018-05-10 23:53:25 +02:00
|
|
|
package mocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/mainflux/mainflux"
|
2018-05-15 17:13:09 +02:00
|
|
|
"github.com/mainflux/mainflux/things"
|
2018-05-10 23:53:25 +02:00
|
|
|
"google.golang.org/grpc"
|
2018-06-16 02:30:46 +02:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2018-05-10 23:53:25 +02:00
|
|
|
)
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
var _ mainflux.ThingsServiceClient = (*thingsClient)(nil)
|
2018-05-10 23:53:25 +02:00
|
|
|
|
2018-06-16 02:30:46 +02:00
|
|
|
// ServiceErrToken is used to simulate internal server error.
|
|
|
|
const ServiceErrToken = "unavailable"
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
type thingsClient struct {
|
2018-05-21 12:51:46 +02:00
|
|
|
things map[string]uint64
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
// NewThingsClient returns mock implementation of things service client.
|
2018-05-21 12:51:46 +02:00
|
|
|
func NewThingsClient(data map[string]uint64) mainflux.ThingsServiceClient {
|
2018-05-15 17:13:09 +02:00
|
|
|
return &thingsClient{data}
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-21 12:51:46 +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()
|
2018-06-16 02:30:46 +02:00
|
|
|
|
|
|
|
// 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 == "" {
|
2018-05-15 17:13:09 +02:00
|
|
|
return nil, things.ErrUnauthorizedAccess
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
id, ok := tc.things[key]
|
2018-05-10 23:53:25 +02:00
|
|
|
if !ok {
|
2018-06-16 02:30:46 +02:00
|
|
|
return nil, status.Error(codes.PermissionDenied, "invalid credentials provided")
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-21 12:51:46 +02:00
|
|
|
return &mainflux.ThingID{Value: id}, nil
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
2018-05-17 20:17:02 +02:00
|
|
|
|
2018-05-21 12:51:46 +02:00
|
|
|
func (tc thingsClient) Identify(ctx context.Context, req *mainflux.Token, opts ...grpc.CallOption) (*mainflux.ThingID, error) {
|
2018-05-17 20:17:02 +02:00
|
|
|
return nil, nil
|
|
|
|
}
|