mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-28 13:48:49 +08:00

* MF-1443 - add policies Signed-off-by: Burak Sekili <buraksekili@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * fix users create Signed-off-by: Burak Sekili <buraksekili@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * MF-1454 - Add Policies for sharing a Thing (#1463) * MF-1454 - Add policies for sharing a Thing Signed-off-by: Burak Sekili <buraksekili@gmail.com> * Add a test case for sharing thing and update mock of AddPolicy Signed-off-by: Burak Sekili <buraksekili@gmail.com> * Update ShareThing parameter naming Signed-off-by: Burak Sekili <buraksekili@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * MF-1454 - Policy Removal (#1466) * Add DeletePolicy gRPC endpoint in auth package Signed-off-by: Burak Sekili <buraksekili@gmail.com> * Update default admin creation Signed-off-by: Burak Sekili <buraksekili@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Add policy addition endpoint (#1479) * NOISSUE - Add policy addition endpoint Signed-off-by: Burak Sekili <buraksekili@gmail.com> * Update name of the method Signed-off-by: Burak Sekili <buraksekili@gmail.com> remove build tag Signed-off-by: Burak Sekili <buraksekili@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Add tests for AddPolicies (#1480) * NOISSUE - Add tests for adding policy and update authz check Signed-off-by: Burak Sekili <buraksekili@gmail.com> * Add more tests and update request body validation Signed-off-by: Burak Sekili <buraksekili@gmail.com> * Update test case structure and utilize mock prefix for test ids Signed-off-by: Burak Sekili <buraksekili@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * MF-1454 - Add initial policies for Group access control (#1467) Signed-off-by: Burak Sekili <buraksekili@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Resolve PR comments Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Co-authored-by: Author: Burak Sekili <buraksekili@gmail.com>
182 lines
5.0 KiB
Go
182 lines
5.0 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package sdk_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"regexp"
|
|
"testing"
|
|
|
|
"github.com/mainflux/mainflux"
|
|
sdk "github.com/mainflux/mainflux/pkg/sdk/go"
|
|
"github.com/mainflux/mainflux/pkg/uuid"
|
|
"github.com/mainflux/mainflux/users"
|
|
"github.com/mainflux/mainflux/users/api"
|
|
"github.com/mainflux/mainflux/users/mocks"
|
|
"github.com/opentracing/opentracing-go/mocktracer"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const (
|
|
invalidEmail = "userexample.com"
|
|
)
|
|
|
|
var (
|
|
passRegex = regexp.MustCompile("^.{8,}$")
|
|
)
|
|
|
|
func newUserService() users.Service {
|
|
usersRepo := mocks.NewUserRepository()
|
|
hasher := mocks.NewHasher()
|
|
userEmail := "user@example.com"
|
|
|
|
mockAuthzDB := map[string][]mocks.SubjectSet{}
|
|
mockAuthzDB[userEmail] = append(mockAuthzDB[userEmail], mocks.SubjectSet{Object: "authorities", Relation: "member"})
|
|
auth := mocks.NewAuthService(map[string]string{userEmail: userEmail}, mockAuthzDB)
|
|
|
|
emailer := mocks.NewEmailer()
|
|
idProvider := uuid.New()
|
|
|
|
return users.New(usersRepo, hasher, auth, emailer, idProvider, passRegex)
|
|
}
|
|
|
|
func newUserServer(svc users.Service) *httptest.Server {
|
|
mux := api.MakeHandler(svc, mocktracer.New())
|
|
return httptest.NewServer(mux)
|
|
}
|
|
|
|
func TestCreateUser(t *testing.T) {
|
|
svc := newUserService()
|
|
ts := newUserServer(svc)
|
|
defer ts.Close()
|
|
sdkConf := sdk.Config{
|
|
UsersURL: ts.URL,
|
|
MsgContentType: contentType,
|
|
TLSVerification: false,
|
|
}
|
|
|
|
user := sdk.User{Email: "user@example.com", Password: "password"}
|
|
|
|
mockAuthzDB := map[string][]mocks.SubjectSet{}
|
|
mockAuthzDB[user.Email] = append(mockAuthzDB[user.Email], mocks.SubjectSet{Object: "authorities", Relation: "member"})
|
|
auth := mocks.NewAuthService(map[string]string{user.Email: user.Email}, mockAuthzDB)
|
|
|
|
tkn, _ := auth.Issue(context.Background(), &mainflux.IssueReq{Id: user.ID, Email: user.Email, Type: 0})
|
|
token := tkn.GetValue()
|
|
|
|
mainfluxSDK := sdk.NewSDK(sdkConf)
|
|
cases := []struct {
|
|
desc string
|
|
user sdk.User
|
|
token string
|
|
err error
|
|
}{
|
|
{
|
|
desc: "register new user",
|
|
user: user,
|
|
token: token,
|
|
err: nil,
|
|
},
|
|
{
|
|
desc: "register existing user",
|
|
user: user,
|
|
token: token,
|
|
err: createError(sdk.ErrFailedCreation, http.StatusConflict),
|
|
},
|
|
{
|
|
desc: "register user with invalid email address",
|
|
user: sdk.User{Email: invalidEmail, Password: "password"},
|
|
token: token,
|
|
err: createError(sdk.ErrFailedCreation, http.StatusBadRequest),
|
|
},
|
|
{
|
|
desc: "register user with empty password",
|
|
user: sdk.User{Email: "user2@example.com", Password: ""},
|
|
token: token,
|
|
err: createError(sdk.ErrFailedCreation, http.StatusBadRequest),
|
|
},
|
|
{
|
|
desc: "register user without password",
|
|
user: sdk.User{Email: "user2@example.com"},
|
|
token: token,
|
|
err: createError(sdk.ErrFailedCreation, http.StatusBadRequest),
|
|
},
|
|
{
|
|
desc: "register user without email",
|
|
user: sdk.User{Password: "password"},
|
|
token: token,
|
|
err: createError(sdk.ErrFailedCreation, http.StatusBadRequest),
|
|
},
|
|
{
|
|
desc: "register empty user",
|
|
user: sdk.User{},
|
|
token: token,
|
|
err: createError(sdk.ErrFailedCreation, http.StatusBadRequest),
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
_, err := mainfluxSDK.CreateUser(tc.token, tc.user)
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err))
|
|
}
|
|
}
|
|
|
|
func TestCreateToken(t *testing.T) {
|
|
svc := newUserService()
|
|
ts := newUserServer(svc)
|
|
defer ts.Close()
|
|
sdkConf := sdk.Config{
|
|
UsersURL: ts.URL,
|
|
MsgContentType: contentType,
|
|
TLSVerification: false,
|
|
}
|
|
|
|
mainfluxSDK := sdk.NewSDK(sdkConf)
|
|
user := sdk.User{Email: "user@example.com", Password: "password"}
|
|
|
|
mockAuthzDB := map[string][]mocks.SubjectSet{}
|
|
mockAuthzDB[user.Email] = append(mockAuthzDB[user.Email], mocks.SubjectSet{Object: "authorities", Relation: "member"})
|
|
auth := mocks.NewAuthService(map[string]string{user.Email: user.Email}, mockAuthzDB)
|
|
|
|
tkn, _ := auth.Issue(context.Background(), &mainflux.IssueReq{Id: user.ID, Email: user.Email, Type: 0})
|
|
token := tkn.GetValue()
|
|
_, err := mainfluxSDK.CreateUser(token, user)
|
|
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
|
|
|
|
cases := []struct {
|
|
desc string
|
|
user sdk.User
|
|
token string
|
|
err error
|
|
}{
|
|
{
|
|
desc: "create token for user",
|
|
user: user,
|
|
token: token,
|
|
err: nil,
|
|
},
|
|
{
|
|
desc: "create token for non existing user",
|
|
user: sdk.User{Email: "user2@example.com", Password: "password"},
|
|
token: "",
|
|
err: createError(sdk.ErrFailedCreation, http.StatusForbidden),
|
|
},
|
|
{
|
|
desc: "create user with empty email",
|
|
user: sdk.User{Email: "", Password: "password"},
|
|
token: "",
|
|
err: createError(sdk.ErrFailedCreation, http.StatusBadRequest),
|
|
},
|
|
}
|
|
for _, tc := range cases {
|
|
token, err := mainfluxSDK.CreateToken(tc.user)
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", tc.desc, tc.err, err))
|
|
assert.Equal(t, tc.token, token, fmt.Sprintf("%s: expected response: %s, got: %s", tc.desc, token, tc.token))
|
|
}
|
|
}
|