From 8d8901308a19d4970ce67516a6c2f8962f65a688 Mon Sep 17 00:00:00 2001 From: Ahmed Radwan Date: Sat, 9 Dec 2023 20:06:54 +0200 Subject: [PATCH] test validator --- api/server.go | 2 +- api/validator.go | 2 +- api/validator_test.go | 32 +++++++++++++++++++++++++ db/migrations/000001_init_schema.up.sql | 2 +- db/migrations/000002_add_users.down.sql | 2 +- db/migrations/000002_add_users.up.sql | 4 ++-- token/paseto_maker_test.go | 8 +++++++ 7 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 api/validator_test.go diff --git a/api/server.go b/api/server.go index c29c951..f104645 100644 --- a/api/server.go +++ b/api/server.go @@ -30,7 +30,7 @@ func NewServer(config util.Config, store db.Store) (*Server, error) { server := &Server{store: store, tokenMaker: tokenMaker, config: config} if v, ok := binding.Validator.Engine().(*validator.Validate); ok { - v.RegisterValidation("currency", validCurrency) + v.RegisterValidation("unit", validUnit) } server.setupRouter() diff --git a/api/validator.go b/api/validator.go index a8680bb..19b9170 100644 --- a/api/validator.go +++ b/api/validator.go @@ -6,7 +6,7 @@ import ( "github.com/go-playground/validator/v10" ) -var validCurrency validator.Func = func(fl validator.FieldLevel) bool { +var validUnit validator.Func = func(fl validator.FieldLevel) bool { if unit, ok := fl.Field().Interface().(string); ok { // check if unit is supported return util.IsSupportedUnit(unit) diff --git a/api/validator_test.go b/api/validator_test.go new file mode 100644 index 0000000..a7da2bc --- /dev/null +++ b/api/validator_test.go @@ -0,0 +1,32 @@ +package api + +import ( + "testing" + + "github.com/aradwann/eenergy/util" + "github.com/go-playground/validator/v10" + "github.com/stretchr/testify/require" +) + +var unitItems = []struct { + have string + want bool +}{ + {util.KWH, true}, + {"1234567890QWERTYUIOP", false}, +} + +// TestValidateUnit +func TestValidateUnit(t *testing.T) { + validate := validator.New() + validate.RegisterValidation("unit", validUnit) + + for _, item := range unitItems { + err := validate.Var(item.have, "unit") + if item.want { + require.Nil(t, err) + } else { + require.Error(t, err) + } + } +} diff --git a/db/migrations/000001_init_schema.up.sql b/db/migrations/000001_init_schema.up.sql index 10b789b..00115da 100644 --- a/db/migrations/000001_init_schema.up.sql +++ b/db/migrations/000001_init_schema.up.sql @@ -2,7 +2,7 @@ CREATE TABLE "accounts" ( "id" bigserial PRIMARY KEY, "owner" varchar NOT NULL, "balance" bigint NOT NULL, - "currency" varchar NOT NULL, + "unit" varchar NOT NULL, "created_at" timestamptz NOT NULL DEFAULT (now()) ); diff --git a/db/migrations/000002_add_users.down.sql b/db/migrations/000002_add_users.down.sql index 0ea80ec..55b1af3 100644 --- a/db/migrations/000002_add_users.down.sql +++ b/db/migrations/000002_add_users.down.sql @@ -1,7 +1,7 @@ ALTER TABLE IF EXISTS "accounts" DROP CONSTRAINT -IF EXISTS "owner_currency_key"; +IF EXISTS "owner_unit_key"; ALTER TABLE IF EXISTS "accounts" diff --git a/db/migrations/000002_add_users.up.sql b/db/migrations/000002_add_users.up.sql index c21f26c..ad7d125 100644 --- a/db/migrations/000002_add_users.up.sql +++ b/db/migrations/000002_add_users.up.sql @@ -9,5 +9,5 @@ CREATE TABLE "users" ( ALTER TABLE "accounts" ADD FOREIGN KEY ("owner") REFERENCES "users" ("username"); --- CREATE UNIQUE INDEX ON "accounts" ("owner", "currency"); -ALTER TABLE "accounts" ADD CONSTRAINT "owner_currency_key" UNIQUE ("owner", "currency"); \ No newline at end of file +-- CREATE UNIQUE INDEX ON "accounts" ("owner", "unit"); +ALTER TABLE "accounts" ADD CONSTRAINT "owner_unit_key" UNIQUE ("owner", "unit"); \ No newline at end of file diff --git a/token/paseto_maker_test.go b/token/paseto_maker_test.go index 1bba234..4efe63a 100644 --- a/token/paseto_maker_test.go +++ b/token/paseto_maker_test.go @@ -50,3 +50,11 @@ func TestExpiredPASETOToken(t *testing.T) { require.Nil(t, payload) } + +func TestPASETOMakerInvalidKeySize(t *testing.T) { + symmetricKey := "shortKey" // Invalid key size + + _, err := NewPASETOMaker(symmetricKey) + require.Error(t, err) + require.Contains(t, err.Error(), "invalid key size") +}