Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NOISSUE - Rename package aliases uuidProvider into uuid #1323

Merged
merged 13 commits into from
Jan 17, 2021
32 changes: 20 additions & 12 deletions auth/postgres/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@ import (
"github.com/mainflux/mainflux/auth"
"github.com/mainflux/mainflux/auth/postgres"
"github.com/mainflux/mainflux/pkg/errors"
uuidProvider "github.com/mainflux/mainflux/pkg/uuid"
"github.com/mainflux/mainflux/pkg/uuid"
"github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const email = "user-save@example.com"

var (
expTime = time.Now().Add(5 * time.Minute)
uuidProvider = uuid.New()
)

func TestKeySave(t *testing.T) {
dbMiddleware := postgres.NewDatabase(db)
repo := postgres.New(dbMiddleware)

email := "user-save@example.com"
expTime := time.Now().Add(5 * time.Minute)
id, _ := uuidProvider.New().ID()
id, err := uuidProvider.ID()
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

cases := []struct {
desc string
key auth.Key
Expand Down Expand Up @@ -63,17 +71,17 @@ func TestKeyRetrieve(t *testing.T) {
dbMiddleware := postgres.NewDatabase(db)
repo := postgres.New(dbMiddleware)

email := "user-save@example.com"
expTime := time.Now().Add(5 * time.Minute)
id, _ := uuidProvider.New().ID()
id, err := uuidProvider.ID()
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

key := auth.Key{
Subject: email,
IssuedAt: time.Now(),
ExpiresAt: expTime,
ID: id,
IssuerID: id,
}
_, err := repo.Save(context.Background(), key)
_, err = repo.Save(context.Background(), key)
assert.Nil(t, err, fmt.Sprintf("Storing Key expected to succeed: %s", err))
cases := []struct {
desc string
Expand Down Expand Up @@ -111,17 +119,17 @@ func TestKeyRemove(t *testing.T) {
dbMiddleware := postgres.NewDatabase(db)
repo := postgres.New(dbMiddleware)

email := "user-save@example.com"
expTime := time.Now().Add(5 * time.Minute)
id, _ := uuidProvider.New().ID()
id, err := uuidProvider.ID()
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

key := auth.Key{
Subject: email,
IssuedAt: time.Now(),
ExpiresAt: expTime,
ID: id,
IssuerID: id,
}
_, err := repo.Save(opentracing.ContextWithSpan(context.Background(), opentracing.StartSpan("")), key)
_, err = repo.Save(opentracing.ContextWithSpan(context.Background(), opentracing.StartSpan("")), key)
assert.Nil(t, err, fmt.Sprintf("Storing Key expected to succeed: %s", err))
cases := []struct {
desc string
Expand Down
6 changes: 3 additions & 3 deletions cmd/auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/mainflux/mainflux/auth/postgres"
"github.com/mainflux/mainflux/auth/tracing"
"github.com/mainflux/mainflux/logger"
uuidProvider "github.com/mainflux/mainflux/pkg/uuid"
"github.com/mainflux/mainflux/pkg/uuid"
"github.com/opentracing/opentracing-go"
stdprometheus "github.com/prometheus/client_golang/prometheus"
jconfig "github.com/uber/jaeger-client-go/config"
Expand Down Expand Up @@ -182,9 +182,9 @@ func newService(db *sqlx.DB, tracer opentracing.Tracer, secret string, logger lo
groupsRepo := postgres.NewGroupRepo(database)
groupsRepo = tracing.GroupRepositoryMiddleware(tracer, groupsRepo)

up := uuidProvider.New()
uuidProvider := uuid.New()
t := jwt.New(secret)
svc := auth.New(keysRepo, groupsRepo, up, t)
svc := auth.New(keysRepo, groupsRepo, uuidProvider, t)
svc = api.LoggingMiddleware(svc, logger)
svc = api.MetricsMiddleware(
svc,
Expand Down
30 changes: 15 additions & 15 deletions cmd/things/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/mainflux/mainflux"
authapi "github.com/mainflux/mainflux/auth/api/grpc"
"github.com/mainflux/mainflux/logger"
uuidProvider "github.com/mainflux/mainflux/pkg/uuid"
"github.com/mainflux/mainflux/pkg/uuid"
"github.com/mainflux/mainflux/things"
"github.com/mainflux/mainflux/things/api"
authgrpcapi "github.com/mainflux/mainflux/things/api/auth/grpc"
Expand Down Expand Up @@ -68,8 +68,8 @@ const (
defSingleUserEmail = ""
defSingleUserToken = ""
defJaegerURL = ""
defAuthnURL = "localhost:8181"
defAuthnTimeout = "1s"
defAuthURL = "localhost:8181"
defAuthTimeout = "1s"

envLogLevel = "MF_THINGS_LOG_LEVEL"
envDBHost = "MF_THINGS_DB_HOST"
Expand Down Expand Up @@ -97,8 +97,8 @@ const (
envSingleUserEmail = "MF_THINGS_SINGLE_USER_EMAIL"
envSingleUserToken = "MF_THINGS_SINGLE_USER_TOKEN"
envJaegerURL = "MF_JAEGER_URL"
envAuthnURL = "MF_AUTH_GRPC_URL"
envAuthnTimeout = "MF_AUTH_GRPC_TIMEOUT"
envAuthURL = "MF_AUTH_GRPC_URL"
envAuthTimeout = "MF_AUTH_GRPC_TIMEOUT"
)

type config struct {
Expand All @@ -120,8 +120,8 @@ type config struct {
singleUserEmail string
singleUserToken string
jaegerURL string
authnURL string
authnTimeout time.Duration
authURL string
authTimeout time.Duration
}

func main() {
Expand Down Expand Up @@ -179,9 +179,9 @@ func loadConfig() config {
log.Fatalf("Invalid value passed for %s\n", envClientTLS)
}

authnTimeout, err := time.ParseDuration(mainflux.Env(envAuthnTimeout, defAuthnTimeout))
authTimeout, err := time.ParseDuration(mainflux.Env(envAuthTimeout, defAuthTimeout))
if err != nil {
log.Fatalf("Invalid %s value: %s", envAuthnTimeout, err.Error())
log.Fatalf("Invalid %s value: %s", envAuthTimeout, err.Error())
}

dbConfig := postgres.Config{
Expand Down Expand Up @@ -215,8 +215,8 @@ func loadConfig() config {
singleUserEmail: mainflux.Env(envSingleUserEmail, defSingleUserEmail),
singleUserToken: mainflux.Env(envSingleUserToken, defSingleUserToken),
jaegerURL: mainflux.Env(envJaegerURL, defJaegerURL),
authnURL: mainflux.Env(envAuthnURL, defAuthnURL),
authnTimeout: authnTimeout,
authURL: mainflux.Env(envAuthURL, defAuthURL),
authTimeout: authTimeout,
}
}

Expand Down Expand Up @@ -273,7 +273,7 @@ func createAuthClient(cfg config, tracer opentracing.Tracer, logger logger.Logge
}

conn := connectToAuth(cfg, logger)
return authapi.NewClient(tracer, conn, cfg.authnTimeout), conn.Close
return authapi.NewClient(tracer, conn, cfg.authTimeout), conn.Close
}

func connectToAuth(cfg config, logger logger.Logger) *grpc.ClientConn {
Expand All @@ -292,7 +292,7 @@ func connectToAuth(cfg config, logger logger.Logger) *grpc.ClientConn {
logger.Info("gRPC communication is not encrypted")
}

conn, err := grpc.Dial(cfg.authnURL, opts...)
conn, err := grpc.Dial(cfg.authURL, opts...)
if err != nil {
logger.Error(fmt.Sprintf("Failed to connect to auth service: %s", err))
os.Exit(1)
Expand All @@ -318,9 +318,9 @@ func newService(auth mainflux.AuthServiceClient, dbTracer opentracing.Tracer, ca

thingCache := rediscache.NewThingCache(cacheClient)
thingCache = tracing.ThingCacheMiddleware(cacheTracer, thingCache)
up := uuidProvider.New()
uuidProvider := uuid.New()

svc := things.New(auth, thingsRepo, channelsRepo, groupsRepo, chanCache, thingCache, up)
svc := things.New(auth, thingsRepo, channelsRepo, groupsRepo, chanCache, thingCache, uuidProvider)
svc = rediscache.NewEventStoreMiddleware(svc, esClient)
svc = api.LoggingMiddleware(svc, logger)
svc = api.MetricsMiddleware(
Expand Down
6 changes: 3 additions & 3 deletions cmd/twins/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/mainflux/mainflux/logger"
"github.com/mainflux/mainflux/pkg/messaging"
"github.com/mainflux/mainflux/pkg/messaging/nats"
uuidProvider "github.com/mainflux/mainflux/pkg/uuid"
"github.com/mainflux/mainflux/pkg/uuid"
localusers "github.com/mainflux/mainflux/things/users"
"github.com/mainflux/mainflux/twins"
"github.com/mainflux/mainflux/twins/api"
Expand Down Expand Up @@ -269,11 +269,11 @@ func newService(ps messaging.PubSub, chanID string, users mainflux.AuthServiceCl
stateRepo := twmongodb.NewStateRepository(db)
stateRepo = tracing.StateRepositoryMiddleware(dbTracer, stateRepo)

up := uuidProvider.New()
uuidProvider := uuid.New()
twinCache := rediscache.NewTwinCache(cacheClient)
twinCache = tracing.TwinCacheMiddleware(cacheTracer, twinCache)

svc := twins.New(ps, users, twinRepo, twinCache, stateRepo, up, chanID, logger)
svc := twins.New(ps, users, twinRepo, twinCache, stateRepo, uuidProvider, chanID, logger)
svc = api.LoggingMiddleware(svc, logger)
svc = api.MetricsMiddleware(
svc,
Expand Down
5 changes: 4 additions & 1 deletion cmd/users/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"time"

"github.com/mainflux/mainflux/internal/email"
"github.com/mainflux/mainflux/pkg/uuid"
"github.com/mainflux/mainflux/users"
"github.com/mainflux/mainflux/users/bcrypt"
"github.com/mainflux/mainflux/users/emailer"
Expand Down Expand Up @@ -284,7 +285,9 @@ func newService(db *sqlx.DB, tracer opentracing.Tracer, auth mainflux.AuthServic
logger.Error(fmt.Sprintf("Failed to configure e-mailing util: %s", err.Error()))
}

svc := users.New(userRepo, groupRepo, hasher, auth, emailer)
uuidProvider := uuid.New()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are using hasher as an instance of the hasher (bcrypt), we should do the same for the ID provider. We don't really care what's the underlying implementation.


svc := users.New(userRepo, groupRepo, hasher, auth, emailer, uuidProvider)
svc = api.LoggingMiddleware(svc, logger)
svc = api.MetricsMiddleware(
svc,
Expand Down
4 changes: 3 additions & 1 deletion pkg/sdk/go/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"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"
Expand All @@ -29,8 +30,9 @@ func newUserService() users.Service {
hasher := mocks.NewHasher()
auth := mocks.NewAuthService(map[string]string{"user@example.com": "user@example.com"})
emailer := mocks.NewEmailer()
uuidProvider := uuid.New()

return users.New(usersRepo, groupsRepo, hasher, auth, emailer)
return users.New(usersRepo, groupsRepo, hasher, auth, emailer, uuidProvider)
}

func newUserServer(svc users.Service) *httptest.Server {
Expand Down
8 changes: 5 additions & 3 deletions readers/cassandra/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var (
vb = true
vd = "base64"
sum float64 = 42

dborovcanin marked this conversation as resolved.
Show resolved Hide resolved
uuidProvider = uuid.New()
)

func TestReadSenml(t *testing.T) {
Expand All @@ -47,11 +49,11 @@ func TestReadSenml(t *testing.T) {
defer session.Close()
writer := writer.New(session)

chanID, err := uuid.New().ID()
chanID, err := uuidProvider.ID()
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
pubID, err := uuid.New().ID()
pubID, err := uuidProvider.ID()
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
pub2ID, err := uuid.New().ID()
pub2ID, err := uuidProvider.ID()
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

m := senml.Message{
Expand Down
11 changes: 6 additions & 5 deletions readers/influxdb/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
influxdata "github.com/influxdata/influxdb/client/v2"
iwriter "github.com/mainflux/mainflux/consumers/writers/influxdb"
"github.com/mainflux/mainflux/pkg/transformers/senml"
uuidProvider "github.com/mainflux/mainflux/pkg/uuid"
"github.com/mainflux/mainflux/pkg/uuid"
"github.com/mainflux/mainflux/readers"
ireader "github.com/mainflux/mainflux/readers/influxdb"

Expand All @@ -34,17 +34,18 @@ var (
vd = "dataValue"
sum float64 = 42

client influxdata.Client
client influxdata.Client
uuidProvider = uuid.New()
)

func TestReadAll(t *testing.T) {
writer := iwriter.New(client, testDB)

chanID, err := uuidProvider.New().ID()
chanID, err := uuidProvider.ID()
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
pubID, err := uuidProvider.New().ID()
pubID, err := uuidProvider.ID()
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
pub2ID, err := uuidProvider.New().ID()
pub2ID, err := uuidProvider.ID()
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

m := senml.Message{
Expand Down
12 changes: 7 additions & 5 deletions readers/postgres/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

pwriter "github.com/mainflux/mainflux/consumers/writers/postgres"
"github.com/mainflux/mainflux/pkg/transformers/senml"
uuidProvider "github.com/mainflux/mainflux/pkg/uuid"
"github.com/mainflux/mainflux/pkg/uuid"
"github.com/mainflux/mainflux/readers"
preader "github.com/mainflux/mainflux/readers/postgres"
"github.com/stretchr/testify/assert"
Expand All @@ -33,18 +33,20 @@ var (
vb = true
vd = "dataValue"
sum float64 = 42

uuidProvider = uuid.New()
)

func TestReadSenml(t *testing.T) {
writer := pwriter.New(db)

chanID, err := uuidProvider.New().ID()
chanID, err := uuidProvider.ID()
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
pubID, err := uuidProvider.New().ID()
pubID, err := uuidProvider.ID()
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
pub2ID, err := uuidProvider.New().ID()
pub2ID, err := uuidProvider.ID()
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
wrongID, err := uuidProvider.New().ID()
wrongID, err := uuidProvider.ID()
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

m := senml.Message{
Expand Down
Loading