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

* MF-739 - Add ID to the User entity Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Resolve remarks Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Move idp to project root Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Use RetrieveByEmail func and UUIDProvider Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Rm idp.go Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix comment Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Rename UserInfo into ViewUser Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix ViewUser naming Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
90 lines
2.0 KiB
Go
90 lines
2.0 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package postgres_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
|
"github.com/mainflux/mainflux/pkg/uuid"
|
|
"github.com/mainflux/mainflux/users"
|
|
"github.com/mainflux/mainflux/users/postgres"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestUserSave(t *testing.T) {
|
|
email := "user-save@example.com"
|
|
|
|
uid, err := uuid.New().ID()
|
|
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
|
|
|
|
cases := []struct {
|
|
desc string
|
|
user users.User
|
|
err error
|
|
}{
|
|
{
|
|
desc: "new user",
|
|
user: users.User{
|
|
ID: uid,
|
|
Email: email,
|
|
Password: "pass",
|
|
},
|
|
err: nil,
|
|
},
|
|
{
|
|
desc: "duplicate user",
|
|
user: users.User{
|
|
ID: uid,
|
|
Email: email,
|
|
Password: "pass",
|
|
},
|
|
err: users.ErrConflict,
|
|
},
|
|
}
|
|
|
|
dbMiddleware := postgres.NewDatabase(db)
|
|
repo := postgres.New(dbMiddleware)
|
|
|
|
for _, tc := range cases {
|
|
err := repo.Save(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 TestSingleUserRetrieval(t *testing.T) {
|
|
dbMiddleware := postgres.NewDatabase(db)
|
|
repo := postgres.New(dbMiddleware)
|
|
|
|
email := "user-retrieval@example.com"
|
|
|
|
uid, err := uuid.New().ID()
|
|
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
|
|
|
|
user := users.User{
|
|
ID: uid,
|
|
Email: email,
|
|
Password: "pass",
|
|
}
|
|
|
|
err = repo.Save(context.Background(), user)
|
|
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
|
|
|
|
cases := map[string]struct {
|
|
email string
|
|
err error
|
|
}{
|
|
"existing user": {email, nil},
|
|
"non-existing user": {"unknown@example.com", users.ErrNotFound},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
_, err := repo.RetrieveByEmail(context.Background(), tc.email)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|