Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

NOISSUE - Make Username Unique #75

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 51 additions & 22 deletions users/postgres/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"testing"

"github.com/0x6flab/namegenerator"
"github.com/absmach/magistrala/internal/testsutil"
mgclients "github.com/absmach/magistrala/pkg/clients"
"github.com/absmach/magistrala/pkg/errors"
Expand All @@ -22,10 +23,9 @@ const (
)

var (
invalidName = strings.Repeat("m", maxNameSize+10)
password = "$tr0ngPassw0rd"
clientIdentity = "[email protected]"
clientName = "client name"
invalidName = strings.Repeat("m", maxNameSize+10)
password = "$tr0ngPassw0rd"
namesgen = namegenerator.NewNameGenerator()
)

func TestClientsSave(t *testing.T) {
Expand All @@ -37,6 +37,9 @@ func TestClientsSave(t *testing.T) {

uid := testsutil.GenerateUUID(t)

name := namesgen.Generate()
clientIdentity := name + "@example.com"

cases := []struct {
desc string
client mgclients.Client
Expand All @@ -46,7 +49,7 @@ func TestClientsSave(t *testing.T) {
desc: "add new client successfully",
client: mgclients.Client{
ID: uid,
Name: clientName,
Name: name,
Credentials: mgclients.Credentials{
Identity: clientIdentity,
Secret: password,
Expand All @@ -61,9 +64,9 @@ func TestClientsSave(t *testing.T) {
client: mgclients.Client{
ID: testsutil.GenerateUUID(t),
Owner: uid,
Name: clientName,
Name: namesgen.Generate(),
Credentials: mgclients.Credentials{
Identity: "withowner-client@example.com",
Identity: fmt.Sprintf("%s@example.com", namesgen.Generate()),
Secret: password,
},
Metadata: mgclients.Metadata{},
Expand All @@ -75,7 +78,21 @@ func TestClientsSave(t *testing.T) {
desc: "add client with duplicate client identity",
client: mgclients.Client{
ID: testsutil.GenerateUUID(t),
Name: clientName,
Name: namesgen.Generate(),
Credentials: mgclients.Credentials{
Identity: clientIdentity,
Secret: password,
},
Metadata: mgclients.Metadata{},
Status: mgclients.EnabledStatus,
},
err: errors.ErrConflict,
},
{
desc: "add client with duplicate client name",
client: mgclients.Client{
ID: testsutil.GenerateUUID(t),
Name: name,
Credentials: mgclients.Credentials{
Identity: clientIdentity,
Secret: password,
Expand All @@ -89,9 +106,9 @@ func TestClientsSave(t *testing.T) {
desc: "add client with invalid client id",
client: mgclients.Client{
ID: invalidName,
Name: clientName,
Name: namesgen.Generate(),
Credentials: mgclients.Credentials{
Identity: "invalidid-client@example.com",
Identity: fmt.Sprintf("%s@example.com", namesgen.Generate()),
Secret: password,
},
Metadata: mgclients.Metadata{},
Expand All @@ -105,7 +122,7 @@ func TestClientsSave(t *testing.T) {
ID: testsutil.GenerateUUID(t),
Name: invalidName,
Credentials: mgclients.Credentials{
Identity: "invalidname-client@example.com",
Identity: fmt.Sprintf("%s@example.com", namesgen.Generate()),
Secret: password,
},
Metadata: mgclients.Metadata{},
Expand All @@ -119,7 +136,7 @@ func TestClientsSave(t *testing.T) {
ID: testsutil.GenerateUUID(t),
Owner: invalidName,
Credentials: mgclients.Credentials{
Identity: "invalidowner-client@example.com",
Identity: fmt.Sprintf("%s@example.com", namesgen.Generate()),
Secret: password,
},
Metadata: mgclients.Metadata{},
Expand All @@ -131,7 +148,7 @@ func TestClientsSave(t *testing.T) {
desc: "add client with invalid client identity",
client: mgclients.Client{
ID: testsutil.GenerateUUID(t),
Name: clientName,
Name: namesgen.Generate(),
Credentials: mgclients.Credentials{
Identity: invalidName,
Secret: password,
Expand All @@ -142,24 +159,36 @@ func TestClientsSave(t *testing.T) {
err: errors.ErrMalformedEntity,
},
{
desc: "add client with a missing client identity",
desc: "add client with a missing client name",
client: mgclients.Client{
ID: testsutil.GenerateUUID(t),
Credentials: mgclients.Credentials{
Identity: "",
Identity: fmt.Sprintf("%[email protected]", namesgen.Generate()),
Secret: password,
},
Metadata: mgclients.Metadata{},
},
err: nil,
},
{
desc: "add client with a missing client identity",
client: mgclients.Client{
ID: testsutil.GenerateUUID(t),
Name: namesgen.Generate(),
Credentials: mgclients.Credentials{
Secret: password,
},
Metadata: mgclients.Metadata{},
},
err: nil,
},
{
desc: "add client with a missing client secret",
client: mgclients.Client{
ID: testsutil.GenerateUUID(t),
ID: testsutil.GenerateUUID(t),
Name: namesgen.Generate(),
Credentials: mgclients.Credentials{
Identity: "[email protected]",
Secret: "",
Identity: fmt.Sprintf("%[email protected]", namesgen.Generate()),
},
Metadata: mgclients.Metadata{},
},
Expand Down Expand Up @@ -192,9 +221,9 @@ func TestIsPlatformAdmin(t *testing.T) {
desc: "authorize check for super user",
client: mgclients.Client{
ID: testsutil.GenerateUUID(t),
Name: clientName,
Name: namesgen.Generate(),
Credentials: mgclients.Credentials{
Identity: "admin@example.com",
Identity: fmt.Sprintf("%s@example.com", namesgen.Generate()),
Secret: password,
},
Metadata: mgclients.Metadata{},
Expand All @@ -207,9 +236,9 @@ func TestIsPlatformAdmin(t *testing.T) {
desc: "unauthorize user",
client: mgclients.Client{
ID: testsutil.GenerateUUID(t),
Name: clientName,
Name: namesgen.Generate(),
Credentials: mgclients.Credentials{
Identity: clientIdentity,
Identity: fmt.Sprintf("%[email protected]", namesgen.Generate()),
Secret: password,
},
Metadata: mgclients.Metadata{},
Expand Down
2 changes: 1 addition & 1 deletion users/postgres/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Migration() *migrate.MemoryMigrationSource {
Up: []string{
`CREATE TABLE IF NOT EXISTS clients (
id VARCHAR(36) PRIMARY KEY,
name VARCHAR(254),
name VARCHAR(254) NOT NULL UNIQUE,
owner_id VARCHAR(36),
identity VARCHAR(254) NOT NULL UNIQUE,
secret TEXT NOT NULL,
Expand Down
Loading