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

* user service - wraping errors Signed-off-by: Ivan Milošević <iva@blokovi.com> * wrapping more errors unwrap only wrapped errors Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add internal database error Wrap internal database error Signed-off-by: Ivan Milošević <iva@blokovi.com> * Wrap user not found error Signed-off-by: Ivan Milošević <iva@blokovi.com> * Wrapping errors in idp and hasher Signed-off-by: Ivan Milošević <iva@blokovi.com> * Use error.Is for testing errors in Identify test Signed-off-by: Ivan Milošević <iva@blokovi.com> * Get wraper from wrapped errors Signed-off-by: Ivan Milošević <iva@blokovi.com> * Switch order of wrapping errors Remove dead code (comments) Signed-off-by: Ivan Milošević <iva@blokovi.com> * assert true in tests Signed-off-by: Ivan Milošević <iva@blokovi.com> * Change comparing errors in tests (assert.True) Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add errorRes structure to API responses in body in things service Signed-off-by: Ivan Milošević <iva@blokovi.com> * resolve conflicts after rebasing Signed-off-by: Ivan Milošević <iva@blokovi.com> * Create errors package Signed-off-by: Ivan Milošević <iva@blokovi.com> * implement new errors package Signed-off-by: Ivan Milošević <iva@blokovi.com> * Modify tests Signed-off-by: Ivan Milošević <iva@blokovi.com> * return copyright comments Signed-off-by: Ivan Milošević <iva@blokovi.com> * remove changes from .gitignore Signed-off-by: Ivan Milošević <iva@blokovi.com> * Move logging to encode errors Comment exported vars and methods Formatting Signed-off-by: Ivan Milošević <iva@blokovi.com> * Login function returns errors.Error Signed-off-by: Ivan Milošević <iva@blokovi.com> * Modify login tests to meet login returning errors.Error Signed-off-by: Ivan Milošević <iva@blokovi.com> * Error interface Signed-off-by: Ivan Milošević <iva@blokovi.com> * Change parameter in Wrapper to interface Error Signed-off-by: Ivan Milošević <iva@blokovi.com> * implement new error interface Signed-off-by: Ivan Milošević <iva@blokovi.com> * Modify tests to use new Error interface Signed-off-by: Ivan Milošević <iva@blokovi.com> * Fix Login Signed-off-by: Ivan Milošević <iva@blokovi.com> * Remove unnecessary errir casting Signed-off-by: Ivan Milošević <iva@blokovi.com> * new error interface implementation Signed-off-by: Ivan Milošević <iva@blokovi.com> * check if Error is empty in registrationEndpoint Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add Empty factory function Use new Empty factory function Use isEmpty method Signed-off-by: Ivan Milošević <iva@blokovi.com> * Lose else in encodeError Signed-off-by: Ivan Milošević <iva@blokovi.com> * Modify tests Signed-off-by: Ivan Milošević <iva@blokovi.com> * Remove *json.UnmarshalTypeError and *json.SyntaxError types from encodeError type switch Signed-off-by: Ivan Milošević <iva@blokovi.com> * Fix nil error value in jwtIdentityProvider Signed-off-by: Ivan Milošević <iva@blokovi.com> * Fix gprc to use new error package Signed-off-by: Ivan Milošević <iva@blokovi.com> * rename receiver in errors package grpc errors Signed-off-by: Ivan Milošević <iva@blokovi.com> * remove debugging code Signed-off-by: Ivan Milošević <iva@blokovi.com> * Resolving conflicts after rebase Signed-off-by: Ivan Milošević <iva@blokovi.com> * Remove comment Signed-off-by: Ivan Milošević <iva@blokovi.com> * Remove Empty from custom error Implement custom error on new methods Signed-off-by: Ivan Milošević <iva@blokovi.com> * WIP tests Signed-off-by: Ivan Milošević <iva@blokovi.com> * remove wrap from Error interface Signed-off-by: Ivan Milošević <iva@blokovi.com> * password-change related tests remove debug code Signed-off-by: Ivan Milošević <iva@blokovi.com> * remove dead code Signed-off-by: Ivan Milošević <iva@blokovi.com> * Move all errors casting to errors package Signed-off-by: Ivan Milošević <iva@blokovi.com> * Fix comment in error package Signed-off-by: Ivan Milošević <iva@blokovi.com> * Change struct pointer to interface in package methods Signed-off-by: Ivan Milošević <iva@blokovi.com> * resolving reviews Signed-off-by: Ivan Milošević <iva@blokovi.com> * fix return in database.go Signed-off-by: Ivan Milošević <iva@blokovi.com> * Fix copyright header Signed-off-by: Ivan Milošević <iva@blokovi.com> * Fix comment in hasher Signed-off-by: Ivan Milošević <iva@blokovi.com>
251 lines
6.5 KiB
Go
251 lines
6.5 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package users_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/mainflux/mainflux/errors"
|
|
"github.com/mainflux/mainflux/users"
|
|
|
|
"github.com/mainflux/mainflux/users/mocks"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
const wrong string = "wrong-value"
|
|
|
|
var (
|
|
user = users.User{Email: "user@example.com", Password: "password", Metadata: map[string]interface{}{"role": "user"}}
|
|
nonExistingUser = users.User{Email: "non-ex-user@example.com", Password: "password", Metadata: map[string]interface{}{"role": "user"}}
|
|
host = "example.com"
|
|
)
|
|
|
|
func newService() users.Service {
|
|
repo := mocks.NewUserRepository()
|
|
hasher := mocks.NewHasher()
|
|
idp := mocks.NewIdentityProvider()
|
|
token := mocks.NewTokenizer()
|
|
e := mocks.NewEmailer()
|
|
|
|
return users.New(repo, hasher, idp, e, token)
|
|
}
|
|
|
|
func TestRegister(t *testing.T) {
|
|
svc := newService()
|
|
|
|
cases := []struct {
|
|
desc string
|
|
user users.User
|
|
err errors.Error
|
|
}{
|
|
{
|
|
desc: "register new user",
|
|
user: user,
|
|
err: nil,
|
|
},
|
|
{
|
|
desc: "register existing user",
|
|
user: user,
|
|
err: users.ErrConflict,
|
|
},
|
|
{
|
|
desc: "register new user with empty password",
|
|
user: users.User{
|
|
Email: user.Email,
|
|
Password: "",
|
|
},
|
|
err: users.ErrMalformedEntity,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
err := svc.Register(context.Background(), tc.user)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestLogin(t *testing.T) {
|
|
svc := newService()
|
|
svc.Register(context.Background(), user)
|
|
|
|
cases := map[string]struct {
|
|
user users.User
|
|
err errors.Error
|
|
}{
|
|
"login with good credentials": {
|
|
user: user,
|
|
err: nil,
|
|
},
|
|
"login with wrong e-mail": {
|
|
user: users.User{
|
|
Email: wrong,
|
|
Password: user.Password,
|
|
},
|
|
err: users.ErrUnauthorizedAccess,
|
|
},
|
|
"login with wrong password": {
|
|
user: users.User{
|
|
Email: user.Email,
|
|
Password: wrong,
|
|
},
|
|
err: users.ErrUnauthorizedAccess,
|
|
},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
_, err := svc.Login(context.Background(), tc.user)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestIdentify(t *testing.T) {
|
|
svc := newService()
|
|
svc.Register(context.Background(), user)
|
|
key, _ := svc.Login(context.Background(), user)
|
|
|
|
cases := map[string]struct {
|
|
key string
|
|
err error
|
|
}{
|
|
"valid token's identity": {key, nil},
|
|
"invalid token's identity": {"", users.ErrUnauthorizedAccess},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
_, err := svc.Identify(tc.key)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestUserInfo(t *testing.T) {
|
|
svc := newService()
|
|
svc.Register(context.Background(), user)
|
|
key, _ := svc.Login(context.Background(), user)
|
|
u := user
|
|
u.Password = ""
|
|
|
|
cases := map[string]struct {
|
|
user users.User
|
|
key string
|
|
err error
|
|
}{
|
|
"valid token's user info": {u, key, nil},
|
|
"invalid token's user info": {users.User{}, "", users.ErrUnauthorizedAccess},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
u, err := svc.UserInfo(context.Background(), tc.key)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("expected %s got %s\n", tc.err, err))
|
|
assert.Equal(t, tc.user, u, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.user, u))
|
|
}
|
|
}
|
|
|
|
func TestUpdateUser(t *testing.T) {
|
|
svc := newService()
|
|
svc.Register(context.Background(), user)
|
|
key, _ := svc.Login(context.Background(), user)
|
|
|
|
user.Metadata = map[string]interface{}{"role": "test"}
|
|
|
|
cases := map[string]struct {
|
|
user users.User
|
|
token string
|
|
err error
|
|
}{
|
|
"update user with valid token": {user, key, nil},
|
|
"update user with invalid token": {user, "non-existent", users.ErrUnauthorizedAccess},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
err := svc.UpdateUser(context.Background(), tc.token, tc.user)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestGenerateResetToken(t *testing.T) {
|
|
svc := newService()
|
|
svc.Register(context.Background(), user)
|
|
|
|
cases := map[string]struct {
|
|
email string
|
|
err error
|
|
}{
|
|
"valid user reset token": {user.Email, nil},
|
|
"invalid user rest token": {nonExistingUser.Email, users.ErrUserNotFound},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
err := svc.GenerateResetToken(context.Background(), tc.email, host)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestChangePassword(t *testing.T) {
|
|
svc := newService()
|
|
svc.Register(context.Background(), user)
|
|
token, _ := svc.Login(context.Background(), user)
|
|
|
|
cases := map[string]struct {
|
|
token string
|
|
password string
|
|
oldPassword string
|
|
err error
|
|
}{
|
|
"valid user change password ": {token, "newpassword", user.Password, nil},
|
|
"valid user change password with wrong password": {token, "newpassword", "wrongpassword", users.ErrUnauthorizedAccess},
|
|
"valid user change password invalid token": {"", "newpassword", user.Password, users.ErrUnauthorizedAccess},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
err := svc.ChangePassword(context.Background(), tc.token, tc.password, tc.oldPassword)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
|
|
}
|
|
}
|
|
|
|
func TestResetPassword(t *testing.T) {
|
|
svc := newService()
|
|
svc.Register(context.Background(), user)
|
|
tokenizer := mocks.NewTokenizer()
|
|
resetToken, _ := tokenizer.Generate(user.Email, 0)
|
|
resetNonExisting, _ := tokenizer.Generate(nonExistingUser.Email, 0)
|
|
|
|
cases := map[string]struct {
|
|
token string
|
|
password string
|
|
err error
|
|
}{
|
|
"valid user reset password ": {resetToken, "newpassword", nil},
|
|
"invalid user reset password ": {resetNonExisting, "newpassword", users.ErrUserNotFound},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
err := svc.ResetPassword(context.Background(), tc.token, tc.password)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
|
|
}
|
|
}
|
|
|
|
func TestSendPasswordReset(t *testing.T) {
|
|
svc := newService()
|
|
svc.Register(context.Background(), user)
|
|
token, _ := svc.Login(context.Background(), user)
|
|
|
|
cases := map[string]struct {
|
|
token string
|
|
email string
|
|
err error
|
|
}{
|
|
"valid user reset password ": {token, user.Email, nil},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
err := svc.SendPasswordReset(context.Background(), host, tc.email, tc.token)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
|
|
}
|
|
}
|