Skip to content

Commit

Permalink
test validator
Browse files Browse the repository at this point in the history
  • Loading branch information
aradwann committed Dec 9, 2023
1 parent 89e9143 commit 8d89013
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion api/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions api/validator_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
2 changes: 1 addition & 1 deletion db/migrations/000001_init_schema.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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())
);

Expand Down
2 changes: 1 addition & 1 deletion db/migrations/000002_add_users.down.sql
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 2 additions & 2 deletions db/migrations/000002_add_users.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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");
-- CREATE UNIQUE INDEX ON "accounts" ("owner", "unit");
ALTER TABLE "accounts" ADD CONSTRAINT "owner_unit_key" UNIQUE ("owner", "unit");
8 changes: 8 additions & 0 deletions token/paseto_maker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit 8d89013

Please sign in to comment.