mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-02 22:17:10 +08:00

* remove owner id Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add users endpoint for retrieving users from group Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove groups from things and users Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move groups into auth Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * separate endpoints for users and things Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix problems with retrieving members Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add groups test Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove groups from users Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove groups from things Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * rename constant Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add new errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove unnecessary constants Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix validation Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * create groups db mock Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding tests Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * revert changes to docker related files Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove groups endpoints from users openapi Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove groups endpoints from users openapi Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move constant from postgres to groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move constant from postgres to groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move constant from postgres to groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove testing group Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renam typ to groupType Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add error for max level Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove print Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove groups.Member interface Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix query building and add test cases Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * uncomment tests Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move groups package Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove group type, add bulk assign and unassign Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * update openapi, remove parentID from create request, reorder endpoints Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * update openapi Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * update openapi for users and things Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix groups test Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix linter errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * resolve comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * rename assignReq structure Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * refactor mocks, response, remove type from endpoint Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * some refactor, renaming, errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * simplify check Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove package alias Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix naming and comment Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * additional comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add members grpc endpoint test Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix retrieving members for different types Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix retrieving members for different types Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove unecessary structure Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix api grpc Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * rename const Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * refactore retrieve parents and children with common function Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * small changes for errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix compile error Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix sorting in mock Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove regexp for groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * revert as change is made by mistake Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * revert as change is made by mistake Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * refactor groups and keys package Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix naming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix naming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix test for timestamp compare Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix error handling Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove errors not being used Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * var renaming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * resolve comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * minor changes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix test Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add endpoints for groups into nginx Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * reorganize endpoints, remove some errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * reorganize endpoints, remove some errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * small fix Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix linter errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * minor changes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * resolve comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix group save path problem Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * description constant Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * rename variables Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix validation Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * get back return Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix compile Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
263 lines
7.5 KiB
Go
263 lines
7.5 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package grpc_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/mainflux/mainflux"
|
|
"github.com/mainflux/mainflux/auth"
|
|
grpcapi "github.com/mainflux/mainflux/auth/api/grpc"
|
|
"github.com/mainflux/mainflux/auth/jwt"
|
|
"github.com/mainflux/mainflux/auth/mocks"
|
|
"github.com/mainflux/mainflux/pkg/uuid"
|
|
"github.com/opentracing/opentracing-go/mocktracer"
|
|
"github.com/stretchr/testify/assert"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
const (
|
|
port = 8081
|
|
secret = "secret"
|
|
email = "test@example.com"
|
|
id = "testID"
|
|
thingsType = "things"
|
|
usersType = "users"
|
|
description = "Description"
|
|
|
|
numOfThings = 5
|
|
numOfUsers = 5
|
|
)
|
|
|
|
var svc auth.Service
|
|
|
|
func newService() auth.Service {
|
|
repo := mocks.NewKeyRepository()
|
|
groupRepo := mocks.NewGroupRepository()
|
|
idProvider := uuid.NewMock()
|
|
t := jwt.New(secret)
|
|
|
|
return auth.New(repo, groupRepo, idProvider, t)
|
|
}
|
|
|
|
func startGRPCServer(svc auth.Service, port int) {
|
|
listener, _ := net.Listen("tcp", fmt.Sprintf(":%d", port))
|
|
server := grpc.NewServer()
|
|
mainflux.RegisterAuthServiceServer(server, grpcapi.NewServer(mocktracer.New(), svc))
|
|
go server.Serve(listener)
|
|
}
|
|
|
|
func TestIssue(t *testing.T) {
|
|
authAddr := fmt.Sprintf("localhost:%d", port)
|
|
conn, _ := grpc.Dial(authAddr, grpc.WithInsecure())
|
|
client := grpcapi.NewClient(mocktracer.New(), conn, time.Second)
|
|
|
|
cases := []struct {
|
|
desc string
|
|
id string
|
|
email string
|
|
kind uint32
|
|
err error
|
|
code codes.Code
|
|
}{
|
|
{
|
|
desc: "issue for user with valid token",
|
|
id: id,
|
|
email: email,
|
|
kind: auth.UserKey,
|
|
err: nil,
|
|
code: codes.OK,
|
|
},
|
|
{
|
|
desc: "issue recovery key",
|
|
id: id,
|
|
email: email,
|
|
kind: auth.RecoveryKey,
|
|
err: nil,
|
|
code: codes.OK,
|
|
},
|
|
{
|
|
desc: "issue API key unauthenticated",
|
|
id: id,
|
|
email: email,
|
|
kind: auth.APIKey,
|
|
err: nil,
|
|
code: codes.Unauthenticated,
|
|
},
|
|
{
|
|
desc: "issue for invalid key type",
|
|
id: id,
|
|
email: email,
|
|
kind: 32,
|
|
err: status.Error(codes.InvalidArgument, "received invalid token request"),
|
|
code: codes.InvalidArgument,
|
|
},
|
|
{
|
|
desc: "issue for user that exist",
|
|
id: "",
|
|
kind: auth.APIKey,
|
|
err: status.Error(codes.Unauthenticated, "unauthorized access"),
|
|
code: codes.Unauthenticated,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
_, err := client.Issue(context.Background(), &mainflux.IssueReq{Id: tc.id, Email: tc.email, Type: tc.kind})
|
|
e, ok := status.FromError(err)
|
|
assert.True(t, ok, "gRPC status can't be extracted from the error")
|
|
assert.Equal(t, tc.code, e.Code(), fmt.Sprintf("%s: expected %s got %s", tc.desc, tc.code, e.Code()))
|
|
}
|
|
}
|
|
|
|
func TestIdentify(t *testing.T) {
|
|
_, loginSecret, err := svc.Issue(context.Background(), "", auth.Key{Type: auth.UserKey, IssuedAt: time.Now(), IssuerID: id, Subject: email})
|
|
assert.Nil(t, err, fmt.Sprintf("Issuing user key expected to succeed: %s", err))
|
|
|
|
_, recoverySecret, err := svc.Issue(context.Background(), "", auth.Key{Type: auth.RecoveryKey, IssuedAt: time.Now(), IssuerID: id, Subject: email})
|
|
assert.Nil(t, err, fmt.Sprintf("Issuing recovery key expected to succeed: %s", err))
|
|
|
|
_, apiSecret, err := svc.Issue(context.Background(), loginSecret, auth.Key{Type: auth.APIKey, IssuedAt: time.Now(), ExpiresAt: time.Now().Add(time.Minute), IssuerID: id, Subject: email})
|
|
assert.Nil(t, err, fmt.Sprintf("Issuing API key expected to succeed: %s", err))
|
|
|
|
authAddr := fmt.Sprintf("localhost:%d", port)
|
|
conn, _ := grpc.Dial(authAddr, grpc.WithInsecure())
|
|
client := grpcapi.NewClient(mocktracer.New(), conn, time.Second)
|
|
|
|
cases := []struct {
|
|
desc string
|
|
token string
|
|
idt mainflux.UserIdentity
|
|
err error
|
|
code codes.Code
|
|
}{
|
|
{
|
|
desc: "identify user with user token",
|
|
token: loginSecret,
|
|
idt: mainflux.UserIdentity{Email: email, Id: id},
|
|
err: nil,
|
|
code: codes.OK,
|
|
},
|
|
{
|
|
desc: "identify user with recovery token",
|
|
token: recoverySecret,
|
|
idt: mainflux.UserIdentity{Email: email, Id: id},
|
|
err: nil,
|
|
code: codes.OK,
|
|
},
|
|
{
|
|
desc: "identify user with API token",
|
|
token: apiSecret,
|
|
idt: mainflux.UserIdentity{Email: email, Id: id},
|
|
err: nil,
|
|
code: codes.OK,
|
|
},
|
|
{
|
|
desc: "identify user with invalid user token",
|
|
token: "invalid",
|
|
idt: mainflux.UserIdentity{},
|
|
err: status.Error(codes.Unauthenticated, "unauthorized access"),
|
|
code: codes.Unauthenticated,
|
|
},
|
|
{
|
|
desc: "identify user that doesn't exist",
|
|
token: "",
|
|
idt: mainflux.UserIdentity{},
|
|
err: status.Error(codes.InvalidArgument, "received invalid token request"),
|
|
code: codes.InvalidArgument,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
idt, err := client.Identify(context.Background(), &mainflux.Token{Value: tc.token})
|
|
if idt != nil {
|
|
assert.Equal(t, tc.idt, *idt, fmt.Sprintf("%s: expected %v got %v", tc.desc, tc.idt, *idt))
|
|
}
|
|
e, ok := status.FromError(err)
|
|
assert.True(t, ok, "gRPC status can't be extracted from the error")
|
|
assert.Equal(t, tc.code, e.Code(), fmt.Sprintf("%s: expected %s got %s", tc.desc, tc.code, e.Code()))
|
|
}
|
|
}
|
|
|
|
func TestMembers(t *testing.T) {
|
|
_, token, err := svc.Issue(context.Background(), "", auth.Key{Type: auth.UserKey, IssuedAt: time.Now(), IssuerID: id, Subject: email})
|
|
assert.Nil(t, err, fmt.Sprintf("Issuing user key expected to succeed: %s", err))
|
|
|
|
group := auth.Group{
|
|
Name: "Mainflux",
|
|
Description: description,
|
|
}
|
|
|
|
var things []string
|
|
for i := 0; i < numOfThings; i++ {
|
|
id, err := uuid.New().ID()
|
|
assert.Nil(t, err, fmt.Sprintf("Generate thing id expected to succeed: %s", err))
|
|
|
|
things = append(things, id)
|
|
}
|
|
|
|
var users []string
|
|
for i := 0; i < numOfUsers; i++ {
|
|
id, err := uuid.New().ID()
|
|
assert.Nil(t, err, fmt.Sprintf("Generate thing id expected to succeed: %s", err))
|
|
|
|
users = append(users, id)
|
|
}
|
|
|
|
group, err = svc.CreateGroup(context.Background(), token, group)
|
|
assert.Nil(t, err, fmt.Sprintf("Creating group expected to succeed: %s", err))
|
|
|
|
err = svc.Assign(context.Background(), token, group.ID, thingsType, things...)
|
|
assert.Nil(t, err, fmt.Sprintf("Assign members to expected to succeed: %s", err))
|
|
|
|
err = svc.Assign(context.Background(), token, group.ID, usersType, users...)
|
|
assert.Nil(t, err, fmt.Sprintf("Assign members to group expected to succeed: %s", err))
|
|
|
|
cases := []struct {
|
|
desc string
|
|
token string
|
|
groupID string
|
|
groupType string
|
|
size int
|
|
err error
|
|
code codes.Code
|
|
}{
|
|
{
|
|
desc: "get all things with user token",
|
|
groupID: group.ID,
|
|
token: token,
|
|
groupType: thingsType,
|
|
size: numOfThings,
|
|
err: nil,
|
|
code: codes.OK,
|
|
},
|
|
{
|
|
desc: "get all users with user token",
|
|
groupID: group.ID,
|
|
token: token,
|
|
groupType: usersType,
|
|
size: numOfUsers,
|
|
err: nil,
|
|
code: codes.OK,
|
|
},
|
|
}
|
|
|
|
authAddr := fmt.Sprintf("localhost:%d", port)
|
|
conn, _ := grpc.Dial(authAddr, grpc.WithInsecure())
|
|
client := grpcapi.NewClient(mocktracer.New(), conn, time.Second)
|
|
|
|
for _, tc := range cases {
|
|
m, err := client.Members(context.Background(), &mainflux.MembersReq{Token: tc.token, GroupID: tc.groupID, Type: tc.groupType, Offset: 0, Limit: 10})
|
|
e, ok := status.FromError(err)
|
|
assert.Equal(t, tc.size, len(m.Members), fmt.Sprintf("%s: expected %d got %d", tc.desc, tc.size, len(m.Members)))
|
|
assert.Equal(t, tc.code, e.Code(), fmt.Sprintf("%s: expected %s got %s", tc.desc, tc.code, e.Code()))
|
|
assert.True(t, ok, "OK expected to be true")
|
|
}
|
|
}
|