add tests

This commit is contained in:
Pavel 2023-12-03 20:46:10 +03:00
parent 7833d427f6
commit 81431e0d17
4 changed files with 199 additions and 9 deletions

4
go.mod

@ -7,18 +7,22 @@ toolchain go1.21.4
require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/gofiber/fiber/v2 v2.51.0
github.com/stretchr/testify v1.8.4
)
require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.50.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/sys v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

10
go.sum

@ -1,5 +1,7 @@
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/gofiber/fiber/v2 v2.51.0 h1:JNACcZy5e2tGApWB2QrRpenTWn0fq0hkFm6k0C86gKQ=
@ -15,8 +17,12 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.50.0 h1:H7fweIlBm0rXLs2q0XbalvJ6r0CUPFWK3/bB4N13e9M=
@ -27,3 +33,7 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

@ -1,6 +1,7 @@
package privilege
import (
"encoding/json"
"fmt"
"github.com/gofiber/fiber/v2"
"time"
@ -70,18 +71,24 @@ func (c *Controller) PublishPrivileges() error {
// getActualPrivileges возвращает текущие привилегии из сервиса.
func (c *Controller) getActualPrivileges() ([]Privilege, error) {
res := []Privilege{}
_, _, err := c.fiberClient.Get(c.client.URL + "/privilege/service/" + c.client.ServiceName).Struct(&res)
if err != nil {
return res, err[0]
_, body, errs := c.fiberClient.Get(c.client.URL + "/privilege/service/" + c.client.ServiceName).Bytes()
if errs != nil {
return res, errs[0]
}
fmt.Println("str", res)
fmt.Println("Response Body:", string(body))
err := json.Unmarshal(body, &res)
if err != nil {
return res, err
}
return res, nil
}
// updatePrivileges обновляет привилегии в сервисе.
func (c *Controller) updatePrivileges() error {
_, _, err := c.fiberClient.Put(c.client.URL + "/privilege/many").JSON(map[string][]Privilege{
"privilege": c.client.Privileges,
"privileges": c.client.Privileges,
}).Bytes()
if err != nil {
return err[0]
@ -93,12 +100,16 @@ func (c *Controller) updatePrivileges() error {
// setupActualPrivileges настраивает привилегии в сервисе.
func (c *Controller) setupActualPrivileges() error {
res := []Privilege{}
_, _, err := c.fiberClient.Post(c.client.URL + "/privilege/many").JSON(map[string][]Privilege{
_, body, errs := c.fiberClient.Post(c.client.URL + "/privilege/many").JSON(map[string][]Privilege{
"privileges": c.client.Privileges,
}).Struct(&res)
}).Bytes()
if errs != nil {
return errs[0]
}
fmt.Println("Response Body:", string(body))
if err != nil {
return err[0]
if err := json.Unmarshal(body, &res); err != nil {
return err
}
return nil

165
privilege/privilege_test.go Normal file

@ -0,0 +1,165 @@
package privilege
import (
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestPublishPrivileges(t *testing.T) {
client := Client{
URL: "http://localhost:8001",
ServiceName: "key",
Privileges: []Privilege{
{
ID: "1",
PrivilegeID: "test1",
Name: "Test 1",
ServiceKey: "key",
Description: "This is a test",
Type: "count",
Value: "100",
Price: 10.0,
Amount: 5,
IsDeleted: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: time.Time{},
},
{
ID: "2",
PrivilegeID: "test2",
Name: "Test 2",
ServiceKey: "key2",
Description: "This is a test",
Type: "count",
Value: "100",
Price: 15.0,
Amount: 3,
IsDeleted: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: time.Time{},
},
},
}
fiberClient := fiber.Client{}
controller := NewPrivilege(client, &fiberClient)
err := controller.PublishPrivileges()
assert.Nil(t, err)
}
func TestGetActualPrivileges(t *testing.T) {
client := Client{
URL: "http://localhost:8001",
ServiceName: "key",
Privileges: []Privilege{},
}
fiberClient := fiber.Client{}
controller := NewPrivilege(client, &fiberClient)
privileges, err := controller.getActualPrivileges()
fmt.Println(privileges)
assert.Nil(t, err)
assert.NotNil(t, privileges)
}
func TestUpdatePrivileges(t *testing.T) {
client := Client{
URL: "http://localhost:8001",
ServiceName: "key",
Privileges: []Privilege{
{
ID: "2",
PrivilegeID: "64e60d28eac51324f2296753",
Name: "100 генераций",
ServiceKey: "test",
Description: "This is a test 100 генераций",
Type: "count",
Value: "100",
Price: 10.0,
Amount: 5,
IsDeleted: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: time.Time{},
},
},
}
fiberClient := fiber.Client{}
controller := NewPrivilege(client, &fiberClient)
err := controller.updatePrivileges()
assert.Nil(t, err)
}
func TestSetupActualPrivileges(t *testing.T) {
client := Client{
URL: "http://localhost:8001",
ServiceName: "key",
Privileges: []Privilege{
{
Name: "TestSetupActualPrivileges",
PrivilegeID: "test1",
ServiceKey: "key",
Description: "This is a test",
Type: "count",
Value: "100",
Price: 10.0,
Amount: 5,
IsDeleted: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: time.Time{},
},
},
}
fiberClient := fiber.Client{}
controller := NewPrivilege(client, &fiberClient)
err := controller.setupActualPrivileges()
assert.Nil(t, err, "error")
}
func TestRemovePrivilege(t *testing.T) {
client := Client{
URL: "http://localhost:8001",
ServiceName: "key",
Privileges: []Privilege{
{
ID: "1",
PrivilegeID: "test1",
Name: "Test1",
ServiceKey: "key",
Description: "This is a test",
Type: "count",
Value: "100",
Price: 10.0,
Amount: 5,
IsDeleted: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: time.Time{},
},
},
}
fiberClient := fiber.Client{}
controller := NewPrivilege(client, &fiberClient)
err := controller.removePrivilege("64e60d28eac51324f2296753")
assert.Nil(t, err)
}