1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-28 13:48:49 +08:00
Mainflux.mainflux/pkg/sdk/go/users_test.go
Dušan Borovčanin f6b1ae735c
MF-1244 - Return UserID alongside with user Email in Identify response (#1245)
* Add both an ID and an Email to API key requests

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Use return UserIdentity response

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Replace GetValue with GetEmail

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Refactor Mainflux Key

Add `Subject` field and reorganize Key manipulation.
**Remove backward compatibility**

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Fix service test

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Fix DB tests

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Fix API tests

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Fix JWT tests

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Uncomment and fix API tests

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Fix SQL statements alignment

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Fix Issue method docs

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Fix Retrieve API and API docs

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Update tests

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
2020-10-27 19:42:53 +01:00

156 lines
4.1 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package sdk_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/mainflux/mainflux"
sdk "github.com/mainflux/mainflux/pkg/sdk/go"
"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"
)
const (
invalidEmail = "userexample.com"
)
func newUserService() users.Service {
usersRepo := mocks.NewUserRepository()
groupsRepo := mocks.NewGroupRepository()
hasher := mocks.NewHasher()
auth := mocks.NewAuthService(map[string]string{"user@example.com": "user@example.com"})
emailer := mocks.NewEmailer()
return users.New(usersRepo, groupsRepo, hasher, auth, emailer)
}
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{
BaseURL: ts.URL,
UsersPrefix: "",
GroupsPrefix: "",
ThingsPrefix: "",
HTTPAdapterPrefix: "",
MsgContentType: contentType,
TLSVerification: false,
}
mainfluxSDK := sdk.NewSDK(sdkConf)
user := sdk.User{Email: "user@example.com", Password: "password"}
cases := []struct {
desc string
user sdk.User
err error
}{
{
desc: "register new user",
user: user,
err: nil,
},
{
desc: "register existing user",
user: user,
err: createError(sdk.ErrFailedCreation, http.StatusConflict),
},
{
desc: "register user with invalid email address",
user: sdk.User{Email: invalidEmail, Password: "password"},
err: createError(sdk.ErrFailedCreation, http.StatusBadRequest),
},
{
desc: "register user with empty password",
user: sdk.User{Email: "user2@example.com", Password: ""},
err: createError(sdk.ErrFailedCreation, http.StatusBadRequest),
},
{
desc: "register user without password",
user: sdk.User{Email: "user2@example.com"},
err: createError(sdk.ErrFailedCreation, http.StatusBadRequest),
},
{
desc: "register user without email",
user: sdk.User{Password: "password"},
err: createError(sdk.ErrFailedCreation, http.StatusBadRequest),
},
{
desc: "register empty user",
user: sdk.User{},
err: createError(sdk.ErrFailedCreation, http.StatusBadRequest),
},
}
for _, tc := range cases {
_, err := mainfluxSDK.CreateUser(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{
BaseURL: ts.URL,
UsersPrefix: "",
GroupsPrefix: "",
ThingsPrefix: "",
HTTPAdapterPrefix: "",
MsgContentType: contentType,
TLSVerification: false,
}
mainfluxSDK := sdk.NewSDK(sdkConf)
user := sdk.User{Email: "user@example.com", Password: "password"}
auth := mocks.NewAuthService(map[string]string{user.Email: user.Email})
tkn, _ := auth.Issue(context.Background(), &mainflux.IssueReq{Id: user.ID, Email: user.Email, Type: 0})
token := tkn.GetValue()
mainfluxSDK.CreateUser(user)
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))
}
}