Fix Sonar Part2
This commit is contained in:
parent
2c9cd98d03
commit
9c392a3a09
@ -24,3 +24,4 @@
|
|||||||
|
|
||||||
## Fix Sonar
|
## Fix Sonar
|
||||||
- [4473](https://github.com/beego/beego/pull/4473)
|
- [4473](https://github.com/beego/beego/pull/4473)
|
||||||
|
- [4474](https://github.com/beego/beego/pull/4474)
|
||||||
184
adapter/cache/cache_test.go
vendored
184
adapter/cache/cache_test.go
vendored
@ -19,6 +19,8 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -53,147 +55,95 @@ func TestCacheIncr(t *testing.T) {
|
|||||||
|
|
||||||
func TestCache(t *testing.T) {
|
func TestCache(t *testing.T) {
|
||||||
bm, err := NewCache("memory", `{"interval":20}`)
|
bm, err := NewCache("memory", `{"interval":20}`)
|
||||||
if err != nil {
|
|
||||||
t.Error(initError)
|
|
||||||
}
|
|
||||||
timeoutDuration := 10 * time.Second
|
|
||||||
if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
|
|
||||||
t.Error(setError, err)
|
|
||||||
}
|
|
||||||
if !bm.IsExist("astaxie") {
|
|
||||||
t.Error(checkError)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v := bm.Get("astaxie"); v.(int) != 1 {
|
assert.Nil(t, err)
|
||||||
t.Error(getError)
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(30 * time.Second)
|
timeoutDuration := 5 * time.Second
|
||||||
|
err = bm.Put("astaxie", 1, timeoutDuration)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
if bm.IsExist("astaxie") {
|
assert.True(t, bm.IsExist("astaxie"))
|
||||||
t.Error(checkError)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
|
assert.Equal(t, 1, bm.Get("astaxie"))
|
||||||
t.Error(setError, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = bm.Incr("astaxie"); err != nil {
|
time.Sleep(10 * time.Second)
|
||||||
t.Error("Incr Error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v := bm.Get("astaxie"); v.(int) != 2 {
|
assert.False(t, bm.IsExist("astaxie"))
|
||||||
t.Error(getError)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = bm.Decr("astaxie"); err != nil {
|
err = bm.Put("astaxie", 1, timeoutDuration)
|
||||||
t.Error("Decr Error", err)
|
assert.Nil(t, err)
|
||||||
}
|
|
||||||
|
|
||||||
if v := bm.Get("astaxie"); v.(int) != 1 {
|
err = bm.Incr("astaxie")
|
||||||
t.Error(getError)
|
assert.Nil(t, err)
|
||||||
}
|
|
||||||
bm.Delete("astaxie")
|
|
||||||
if bm.IsExist("astaxie") {
|
|
||||||
t.Error("delete err")
|
|
||||||
}
|
|
||||||
|
|
||||||
// test GetMulti
|
assert.Equal(t, 2, bm.Get("astaxie"))
|
||||||
if err = bm.Put("astaxie", "author", timeoutDuration); err != nil {
|
|
||||||
t.Error(setError, err)
|
|
||||||
}
|
|
||||||
if !bm.IsExist("astaxie") {
|
|
||||||
t.Error(checkError)
|
|
||||||
}
|
|
||||||
if v := bm.Get("astaxie"); v.(string) != "author" {
|
|
||||||
t.Error(getError)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil {
|
assert.Nil(t, bm.Decr("astaxie"))
|
||||||
t.Error(setError, err)
|
|
||||||
}
|
assert.Equal(t, 1, bm.Get("astaxie"))
|
||||||
if !bm.IsExist("astaxie1") {
|
|
||||||
t.Error(checkError)
|
assert.Nil(t, bm.Delete("astaxie"))
|
||||||
}
|
|
||||||
|
assert.False(t, bm.IsExist("astaxie"))
|
||||||
|
|
||||||
|
assert.Nil(t, bm.Put("astaxie", "author", timeoutDuration))
|
||||||
|
|
||||||
|
assert.True(t, bm.IsExist("astaxie"))
|
||||||
|
|
||||||
|
assert.Equal(t, "author", bm.Get("astaxie"))
|
||||||
|
|
||||||
|
assert.Nil(t, bm.Put("astaxie1", "author1", timeoutDuration))
|
||||||
|
|
||||||
|
assert.True(t, bm.IsExist("astaxie1"))
|
||||||
|
|
||||||
vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
|
vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
|
||||||
if len(vv) != 2 {
|
|
||||||
t.Error(getMultiError)
|
assert.Equal(t, 2, len(vv))
|
||||||
}
|
|
||||||
if vv[0].(string) != "author" {
|
|
||||||
t.Error(getMultiError)
|
assert.Equal(t, "author", vv[0])
|
||||||
}
|
|
||||||
if vv[1].(string) != "author1" {
|
assert.Equal(t, "author1", vv[1])
|
||||||
t.Error(getMultiError)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFileCache(t *testing.T) {
|
func TestFileCache(t *testing.T) {
|
||||||
bm, err := NewCache("file", `{"CachePath":"cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}`)
|
bm, err := NewCache("file", `{"CachePath":"cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}`)
|
||||||
if err != nil {
|
|
||||||
t.Error(initError)
|
|
||||||
}
|
|
||||||
timeoutDuration := 10 * time.Second
|
|
||||||
if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
|
|
||||||
t.Error(setError, err)
|
|
||||||
}
|
|
||||||
if !bm.IsExist("astaxie") {
|
|
||||||
t.Error(checkError)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v := bm.Get("astaxie"); v.(int) != 1 {
|
assert.Nil(t, err)
|
||||||
t.Error(getError)
|
timeoutDuration := 5 * time.Second
|
||||||
}
|
|
||||||
|
|
||||||
if err = bm.Incr("astaxie"); err != nil {
|
assert.Nil(t, bm.Put("astaxie", 1, timeoutDuration))
|
||||||
t.Error("Incr Error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v := bm.Get("astaxie"); v.(int) != 2 {
|
assert.True(t, bm.IsExist("astaxie"))
|
||||||
t.Error(getError)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = bm.Decr("astaxie"); err != nil {
|
assert.Equal(t, 1, bm.Get("astaxie"))
|
||||||
t.Error("Decr Error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v := bm.Get("astaxie"); v.(int) != 1 {
|
assert.Nil(t, bm.Incr("astaxie"))
|
||||||
t.Error(getError)
|
|
||||||
}
|
|
||||||
bm.Delete("astaxie")
|
|
||||||
if bm.IsExist("astaxie") {
|
|
||||||
t.Error("delete err")
|
|
||||||
}
|
|
||||||
|
|
||||||
// test string
|
assert.Equal(t, 2, bm.Get("astaxie"))
|
||||||
if err = bm.Put("astaxie", "author", timeoutDuration); err != nil {
|
|
||||||
t.Error(setError, err)
|
|
||||||
}
|
|
||||||
if !bm.IsExist("astaxie") {
|
|
||||||
t.Error(checkError)
|
|
||||||
}
|
|
||||||
if v := bm.Get("astaxie"); v.(string) != "author" {
|
|
||||||
t.Error(getError)
|
|
||||||
}
|
|
||||||
|
|
||||||
// test GetMulti
|
assert.Nil(t, bm.Decr("astaxie"))
|
||||||
if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil {
|
|
||||||
t.Error(setError, err)
|
assert.Equal(t, 1, bm.Get("astaxie"))
|
||||||
}
|
assert.Nil(t, bm.Delete("astaxie"))
|
||||||
if !bm.IsExist("astaxie1") {
|
|
||||||
t.Error(checkError)
|
assert.False(t, bm.IsExist("astaxie"))
|
||||||
}
|
|
||||||
|
assert.Nil(t, bm.Put("astaxie", "author", timeoutDuration))
|
||||||
|
|
||||||
|
assert.True(t, bm.IsExist("astaxie"))
|
||||||
|
|
||||||
|
assert.Equal(t, "author", bm.Get("astaxie"))
|
||||||
|
|
||||||
|
assert.Nil(t, bm.Put("astaxie1", "author1", timeoutDuration))
|
||||||
|
|
||||||
|
assert.True(t, bm.IsExist("astaxie1"))
|
||||||
|
|
||||||
vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
|
vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
|
||||||
if len(vv) != 2 {
|
|
||||||
t.Error(getMultiError)
|
|
||||||
}
|
|
||||||
if vv[0].(string) != "author" {
|
|
||||||
t.Error(getMultiError)
|
|
||||||
}
|
|
||||||
if vv[1].(string) != "author1" {
|
|
||||||
t.Error(getMultiError)
|
|
||||||
}
|
|
||||||
|
|
||||||
os.RemoveAll("cache")
|
assert.Equal(t, 2, len(vv))
|
||||||
|
|
||||||
|
assert.Equal(t, "author", vv[0])
|
||||||
|
assert.Equal(t, "author1", vv[1])
|
||||||
|
assert.Nil(t, os.RemoveAll("cache"))
|
||||||
}
|
}
|
||||||
|
|||||||
101
adapter/cache/memcache/memcache_test.go
vendored
101
adapter/cache/memcache/memcache_test.go
vendored
@ -21,6 +21,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/beego/beego/v2/adapter/cache"
|
"github.com/beego/beego/v2/adapter/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -40,83 +42,52 @@ func TestMemcacheCache(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bm, err := cache.NewCache("memcache", fmt.Sprintf(`{"conn": "%s"}`, addr))
|
bm, err := cache.NewCache("memcache", fmt.Sprintf(`{"conn": "%s"}`, addr))
|
||||||
if err != nil {
|
assert.Nil(t, err)
|
||||||
t.Error(initError)
|
timeoutDuration := 5 * time.Second
|
||||||
}
|
|
||||||
timeoutDuration := 10 * time.Second
|
assert.Nil(t, bm.Put("astaxie", "1", timeoutDuration))
|
||||||
if err = bm.Put("astaxie", "1", timeoutDuration); err != nil {
|
|
||||||
t.Error(setError, err)
|
assert.True(t, bm.IsExist("astaxie"))
|
||||||
}
|
|
||||||
if !bm.IsExist("astaxie") {
|
|
||||||
t.Error(checkError)
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(11 * time.Second)
|
time.Sleep(11 * time.Second)
|
||||||
|
|
||||||
if bm.IsExist("astaxie") {
|
assert.False(t, bm.IsExist("astaxie"))
|
||||||
t.Error(checkError)
|
|
||||||
}
|
|
||||||
if err = bm.Put("astaxie", "1", timeoutDuration); err != nil {
|
|
||||||
t.Error(setError, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v, err := strconv.Atoi(string(bm.Get("astaxie").([]byte))); err != nil || v != 1 {
|
assert.Nil(t, bm.Put("astaxie", "1", timeoutDuration))
|
||||||
t.Error(getError)
|
v, err := strconv.Atoi(string(bm.Get("astaxie").([]byte)))
|
||||||
}
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, 1, v)
|
||||||
|
|
||||||
if err = bm.Incr("astaxie"); err != nil {
|
assert.Nil(t, bm.Incr("astaxie"))
|
||||||
t.Error("Incr Error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v, err := strconv.Atoi(string(bm.Get("astaxie").([]byte))); err != nil || v != 2 {
|
v, err = strconv.Atoi(string(bm.Get("astaxie").([]byte)))
|
||||||
t.Error(getError)
|
assert.Nil(t, err)
|
||||||
}
|
assert.Equal(t, 2, v)
|
||||||
|
|
||||||
if err = bm.Decr("astaxie"); err != nil {
|
assert.Nil(t, bm.Decr("astaxie"))
|
||||||
t.Error("Decr Error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v, err := strconv.Atoi(string(bm.Get("astaxie").([]byte))); err != nil || v != 1 {
|
v, err = strconv.Atoi(string(bm.Get("astaxie").([]byte)))
|
||||||
t.Error(getError)
|
assert.Nil(t, err)
|
||||||
}
|
assert.Equal(t, 1, v)
|
||||||
bm.Delete("astaxie")
|
|
||||||
if bm.IsExist("astaxie") {
|
|
||||||
t.Error("delete err")
|
|
||||||
}
|
|
||||||
|
|
||||||
// test string
|
assert.Nil(t, bm.Delete("astaxie"))
|
||||||
if err = bm.Put("astaxie", "author", timeoutDuration); err != nil {
|
|
||||||
t.Error(setError, err)
|
|
||||||
}
|
|
||||||
if !bm.IsExist("astaxie") {
|
|
||||||
t.Error(checkError)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v := bm.Get("astaxie").([]byte); string(v) != "author" {
|
assert.False(t, bm.IsExist("astaxie"))
|
||||||
t.Error(getError)
|
|
||||||
}
|
|
||||||
|
|
||||||
// test GetMulti
|
assert.Nil(t, bm.Put("astaxie", "author", timeoutDuration))
|
||||||
if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil {
|
|
||||||
t.Error(setError, err)
|
assert.True(t, bm.IsExist("astaxie"))
|
||||||
}
|
|
||||||
if !bm.IsExist("astaxie1") {
|
assert.Equal(t, []byte("author"), bm.Get("astaxie"))
|
||||||
t.Error(checkError)
|
|
||||||
}
|
assert.Nil(t, bm.Put("astaxie1", "author1", timeoutDuration))
|
||||||
|
|
||||||
|
assert.True(t, bm.IsExist("astaxie1"))
|
||||||
|
|
||||||
vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
|
vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
|
||||||
if len(vv) != 2 {
|
assert.Equal(t, 2, len(vv))
|
||||||
t.Error(getMultiError)
|
assert.Equal(t, "author", vv[0])
|
||||||
}
|
assert.Equal(t, "author1", vv[1])
|
||||||
if string(vv[0].([]byte)) != "author" && string(vv[0].([]byte)) != "author1" {
|
|
||||||
t.Error(getMultiError)
|
|
||||||
}
|
|
||||||
if string(vv[1].([]byte)) != "author1" && string(vv[1].([]byte)) != "author" {
|
|
||||||
t.Error(getMultiError)
|
|
||||||
}
|
|
||||||
|
|
||||||
// test clear all
|
assert.Nil(t, bm.ClearAll())
|
||||||
if err = bm.ClearAll(); err != nil {
|
|
||||||
t.Error("clear all err")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
114
adapter/cache/redis/redis_test.go
vendored
114
adapter/cache/redis/redis_test.go
vendored
@ -21,6 +21,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gomodule/redigo/redis"
|
"github.com/gomodule/redigo/redis"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/beego/beego/v2/adapter/cache"
|
"github.com/beego/beego/v2/adapter/cache"
|
||||||
)
|
)
|
||||||
@ -40,88 +41,69 @@ func TestRedisCache(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bm, err := cache.NewCache("redis", fmt.Sprintf(`{"conn": "%s"}`, redisAddr))
|
bm, err := cache.NewCache("redis", fmt.Sprintf(`{"conn": "%s"}`, redisAddr))
|
||||||
if err != nil {
|
assert.Nil(t, err)
|
||||||
t.Error(initError)
|
timeoutDuration := 5 * time.Second
|
||||||
}
|
|
||||||
timeoutDuration := 10 * time.Second
|
|
||||||
if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
|
|
||||||
t.Error(setError, err)
|
|
||||||
}
|
|
||||||
if !bm.IsExist("astaxie") {
|
|
||||||
t.Error(checkError)
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(11 * time.Second)
|
assert.Nil(t, bm.Put("astaxie", 1, timeoutDuration))
|
||||||
|
|
||||||
if bm.IsExist("astaxie") {
|
assert.True(t, bm.IsExist("astaxie"))
|
||||||
t.Error(checkError)
|
|
||||||
}
|
|
||||||
if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
|
|
||||||
t.Error(setError, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 {
|
time.Sleep(7 * time.Second)
|
||||||
t.Error(getError)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = bm.Incr("astaxie"); err != nil {
|
assert.False(t, bm.IsExist("astaxie"))
|
||||||
t.Error("Incr Error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v, _ := redis.Int(bm.Get("astaxie"), err); v != 2 {
|
assert.Nil(t, bm.Put("astaxie", 1, timeoutDuration))
|
||||||
t.Error(getError)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = bm.Decr("astaxie"); err != nil {
|
v, err := redis.Int(bm.Get("astaxie"), err)
|
||||||
t.Error("Decr Error", err)
|
assert.Nil(t, err)
|
||||||
}
|
assert.Equal(t, 1, v)
|
||||||
|
|
||||||
if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 {
|
assert.Nil(t, bm.Incr("astaxie"))
|
||||||
t.Error(getError)
|
|
||||||
}
|
|
||||||
bm.Delete("astaxie")
|
|
||||||
if bm.IsExist("astaxie") {
|
|
||||||
t.Error("delete err")
|
|
||||||
}
|
|
||||||
|
|
||||||
// test string
|
v, err = redis.Int(bm.Get("astaxie"), err)
|
||||||
if err = bm.Put("astaxie", "author", timeoutDuration); err != nil {
|
assert.Nil(t, err)
|
||||||
t.Error(setError, err)
|
assert.Equal(t, 2, v)
|
||||||
}
|
|
||||||
if !bm.IsExist("astaxie") {
|
|
||||||
t.Error(checkError)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v, _ := redis.String(bm.Get("astaxie"), err); v != "author" {
|
assert.Nil(t, bm.Decr("astaxie"))
|
||||||
t.Error(getError)
|
|
||||||
}
|
|
||||||
|
|
||||||
// test GetMulti
|
v, err = redis.Int(bm.Get("astaxie"), err)
|
||||||
if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil {
|
assert.Nil(t, err)
|
||||||
t.Error(setError, err)
|
assert.Equal(t, 1, v)
|
||||||
}
|
|
||||||
if !bm.IsExist("astaxie1") {
|
assert.Nil(t, bm.Delete("astaxie"))
|
||||||
t.Error(checkError)
|
|
||||||
}
|
assert.False(t, bm.IsExist("astaxie"))
|
||||||
|
|
||||||
|
assert.Nil(t, bm.Put("astaxie", "author", timeoutDuration))
|
||||||
|
assert.True(t, bm.IsExist("astaxie"))
|
||||||
|
|
||||||
|
vs, err := redis.String(bm.Get("astaxie"), err)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, "author", vs)
|
||||||
|
|
||||||
|
assert.Nil(t, bm.Put("astaxie1", "author1", timeoutDuration))
|
||||||
|
|
||||||
|
assert.False(t, bm.IsExist("astaxie1"))
|
||||||
|
|
||||||
vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
|
vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
|
||||||
if len(vv) != 2 {
|
|
||||||
t.Error(getMultiError)
|
|
||||||
}
|
|
||||||
if v, _ := redis.String(vv[0], nil); v != "author" {
|
|
||||||
t.Error(getMultiError)
|
|
||||||
}
|
|
||||||
if v, _ := redis.String(vv[1], nil); v != "author1" {
|
|
||||||
t.Error(getMultiError)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
assert.Equal(t, 2, len(vv))
|
||||||
|
|
||||||
|
vs, err = redis.String(vv[0], nil)
|
||||||
|
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, "author", vs)
|
||||||
|
|
||||||
|
vs, err = redis.String(vv[1], nil)
|
||||||
|
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, "author1", vs)
|
||||||
|
|
||||||
|
assert.Nil(t, bm.ClearAll())
|
||||||
// test clear all
|
// test clear all
|
||||||
if err = bm.ClearAll(); err != nil {
|
|
||||||
t.Error("clear all err")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCache_Scan(t *testing.T) {
|
func TestCacheScan(t *testing.T) {
|
||||||
timeoutDuration := 10 * time.Second
|
timeoutDuration := 10 * time.Second
|
||||||
// init
|
// init
|
||||||
bm, err := cache.NewCache("redis", `{"conn": "127.0.0.1:6379"}`)
|
bm, err := cache.NewCache("redis", `{"conn": "127.0.0.1:6379"}`)
|
||||||
|
|||||||
114
adapter/cache/ssdb/ssdb_test.go
vendored
114
adapter/cache/ssdb/ssdb_test.go
vendored
@ -7,6 +7,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/beego/beego/v2/adapter/cache"
|
"github.com/beego/beego/v2/adapter/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -25,95 +27,59 @@ func TestSsdbcacheCache(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ssdb, err := cache.NewCache("ssdb", fmt.Sprintf(`{"conn": "%s"}`, ssdbAddr))
|
ssdb, err := cache.NewCache("ssdb", fmt.Sprintf(`{"conn": "%s"}`, ssdbAddr))
|
||||||
if err != nil {
|
|
||||||
t.Error(initError)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
assert.False(t, ssdb.IsExist("ssdb"))
|
||||||
// test put and exist
|
// test put and exist
|
||||||
if ssdb.IsExist("ssdb") {
|
timeoutDuration := 3 * time.Second
|
||||||
t.Error(checkError)
|
|
||||||
}
|
|
||||||
timeoutDuration := 10 * time.Second
|
|
||||||
// timeoutDuration := -10*time.Second if timeoutDuration is negtive,it means permanent
|
// timeoutDuration := -10*time.Second if timeoutDuration is negtive,it means permanent
|
||||||
if err = ssdb.Put("ssdb", "ssdb", timeoutDuration); err != nil {
|
assert.Nil(t, ssdb.Put("ssdb", "ssdb", timeoutDuration))
|
||||||
t.Error(setError, err)
|
assert.True(t, ssdb.IsExist("ssdb"))
|
||||||
}
|
|
||||||
if !ssdb.IsExist("ssdb") {
|
|
||||||
t.Error(checkError)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get test done
|
assert.NotNil(t, ssdb.Put("ssdb", "ssdb", timeoutDuration))
|
||||||
if err = ssdb.Put("ssdb", "ssdb", timeoutDuration); err != nil {
|
|
||||||
t.Error(setError, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v := ssdb.Get("ssdb"); v != "ssdb" {
|
assert.Equal(t, "ssdb", ssdb.Get("ssdb"))
|
||||||
t.Error("get Error")
|
|
||||||
}
|
|
||||||
|
|
||||||
// inc/dec test done
|
// inc/dec test done
|
||||||
if err = ssdb.Put("ssdb", "2", timeoutDuration); err != nil {
|
assert.Nil(t, ssdb.Put("ssdb", "2", timeoutDuration))
|
||||||
t.Error(setError, err)
|
|
||||||
}
|
|
||||||
if err = ssdb.Incr("ssdb"); err != nil {
|
|
||||||
t.Error("incr Error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v, err := strconv.Atoi(ssdb.Get("ssdb").(string)); err != nil || v != 3 {
|
assert.Nil(t, ssdb.Incr("ssdb"))
|
||||||
t.Error(getError)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = ssdb.Decr("ssdb"); err != nil {
|
v, err := strconv.Atoi(ssdb.Get("ssdb").(string))
|
||||||
t.Error("decr error")
|
assert.Nil(t, err)
|
||||||
}
|
assert.Equal(t, 3, v)
|
||||||
|
|
||||||
|
assert.Nil(t, ssdb.Decr("ssdb"))
|
||||||
|
|
||||||
|
assert.Nil(t, ssdb.Put("ssdb", "3", timeoutDuration))
|
||||||
|
|
||||||
// test del
|
// test del
|
||||||
if err = ssdb.Put("ssdb", "3", timeoutDuration); err != nil {
|
v, err = strconv.Atoi(ssdb.Get("ssdb").(string))
|
||||||
t.Error(setError, err)
|
assert.Nil(t, err)
|
||||||
}
|
assert.Equal(t, 3, v)
|
||||||
if v, err := strconv.Atoi(ssdb.Get("ssdb").(string)); err != nil || v != 3 {
|
|
||||||
t.Error(getError)
|
assert.Nil(t, ssdb.Delete("ssdb"))
|
||||||
}
|
assert.False(t, ssdb.IsExist("ssdb"))
|
||||||
if err := ssdb.Delete("ssdb"); err == nil {
|
|
||||||
if ssdb.IsExist("ssdb") {
|
|
||||||
t.Error("delete err")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// test string
|
// test string
|
||||||
if err = ssdb.Put("ssdb", "ssdb", -10*time.Second); err != nil {
|
assert.Nil(t, ssdb.Put("ssdb", "ssdb", -10*time.Second))
|
||||||
t.Error(setError, err)
|
|
||||||
}
|
assert.True(t, ssdb.IsExist("ssdb"))
|
||||||
if !ssdb.IsExist("ssdb") {
|
assert.Equal(t, "ssdb", ssdb.Get("ssdb"))
|
||||||
t.Error(checkError)
|
|
||||||
}
|
|
||||||
if v := ssdb.Get("ssdb").(string); v != "ssdb" {
|
|
||||||
t.Error(getError)
|
|
||||||
}
|
|
||||||
|
|
||||||
// test GetMulti done
|
// test GetMulti done
|
||||||
if err = ssdb.Put("ssdb1", "ssdb1", -10*time.Second); err != nil {
|
assert.Nil(t, ssdb.Put("ssdb1", "ssdb1", -10*time.Second))
|
||||||
t.Error(setError, err)
|
assert.True(t, ssdb.IsExist("ssdb1") )
|
||||||
}
|
|
||||||
if !ssdb.IsExist("ssdb1") {
|
|
||||||
t.Error(checkError)
|
|
||||||
}
|
|
||||||
vv := ssdb.GetMulti([]string{"ssdb", "ssdb1"})
|
|
||||||
if len(vv) != 2 {
|
|
||||||
t.Error(getMultiError)
|
|
||||||
}
|
|
||||||
if vv[0].(string) != "ssdb" {
|
|
||||||
t.Error(getMultiError)
|
|
||||||
}
|
|
||||||
if vv[1].(string) != "ssdb1" {
|
|
||||||
t.Error(getMultiError)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
vv := ssdb.GetMulti([]string{"ssdb", "ssdb1"})
|
||||||
|
assert.Equal(t, 2, len(vv))
|
||||||
|
|
||||||
|
assert.Equal(t, "ssdb", vv[0])
|
||||||
|
assert.Equal(t, "ssdb1", vv[1])
|
||||||
|
|
||||||
|
assert.Nil(t, ssdb.ClearAll())
|
||||||
|
assert.False(t, ssdb.IsExist("ssdb"))
|
||||||
|
assert.False(t, ssdb.IsExist("ssdb1"))
|
||||||
// test clear all done
|
// test clear all done
|
||||||
if err = ssdb.ClearAll(); err != nil {
|
|
||||||
t.Error("clear all err")
|
|
||||||
}
|
|
||||||
if ssdb.IsExist("ssdb") || ssdb.IsExist("ssdb1") {
|
|
||||||
t.Error(checkError)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,6 +18,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestJsonStartsWithArray(t *testing.T) {
|
func TestJsonStartsWithArray(t *testing.T) {
|
||||||
@ -169,56 +171,39 @@ func TestJson(t *testing.T) {
|
|||||||
default:
|
default:
|
||||||
value, err = jsonconf.DIY(k)
|
value, err = jsonconf.DIY(k)
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("get key %q value fatal,%v err %s", k, v, err)
|
assert.Nil(t, err)
|
||||||
} else if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", value) {
|
assert.Equal(t, fmt.Sprintf("%v", v), fmt.Sprintf("%v", value))
|
||||||
t.Fatalf("get key %q value, want %v got %v .", k, v, value)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
assert.Nil(t, jsonconf.Set("name", "astaxie"))
|
||||||
if err = jsonconf.Set("name", "astaxie"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if jsonconf.String("name") != "astaxie" {
|
|
||||||
t.Fatal("get name error")
|
|
||||||
}
|
|
||||||
|
|
||||||
if db, err := jsonconf.DIY("database"); err != nil {
|
assert.Equal(t, "astaxie", jsonconf.String("name"))
|
||||||
t.Fatal(err)
|
|
||||||
} else if m, ok := db.(map[string]interface{}); !ok {
|
|
||||||
t.Log(db)
|
|
||||||
t.Fatal("db not map[string]interface{}")
|
|
||||||
} else {
|
|
||||||
if m["host"].(string) != "host" {
|
|
||||||
t.Fatal("get host err")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := jsonconf.Int("unknown"); err == nil {
|
db, err := jsonconf.DIY("database")
|
||||||
t.Error("unknown keys should return an error when expecting an Int")
|
assert.Nil(t, err)
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := jsonconf.Int64("unknown"); err == nil {
|
m, ok := db.(map[string]interface{})
|
||||||
t.Error("unknown keys should return an error when expecting an Int64")
|
assert.True(t, ok)
|
||||||
}
|
assert.Equal(t,"host" , m["host"])
|
||||||
|
|
||||||
if _, err := jsonconf.Float("unknown"); err == nil {
|
_, err = jsonconf.Int("unknown")
|
||||||
t.Error("unknown keys should return an error when expecting a Float")
|
assert.NotNil(t, err)
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := jsonconf.DIY("unknown"); err == nil {
|
_, err = jsonconf.Int64("unknown")
|
||||||
t.Error("unknown keys should return an error when expecting an interface{}")
|
assert.NotNil(t, err)
|
||||||
}
|
|
||||||
|
|
||||||
if val := jsonconf.String("unknown"); val != "" {
|
_, err = jsonconf.Float("unknown")
|
||||||
t.Error("unknown keys should return an empty string when expecting a String")
|
assert.NotNil(t, err)
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := jsonconf.Bool("unknown"); err == nil {
|
_, err = jsonconf.DIY("unknown")
|
||||||
t.Error("unknown keys should return an error when expecting a Bool")
|
assert.NotNil(t, err)
|
||||||
}
|
|
||||||
|
|
||||||
if !jsonconf.DefaultBool("unknown", true) {
|
val := jsonconf.String("unknown")
|
||||||
t.Error("unknown keys with default value wrong")
|
assert.Equal(t, "", val)
|
||||||
}
|
|
||||||
|
_, err = jsonconf.Bool("unknown")
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
|
assert.True(t, jsonconf.DefaultBool("unknown", true))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,7 +20,7 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMethodParam_String(t *testing.T) {
|
func TestMethodParamString(t *testing.T) {
|
||||||
method := New("myName", IsRequired, InHeader, Default("abc"))
|
method := New("myName", IsRequired, InHeader, Default("abc"))
|
||||||
s := method.String()
|
s := method.String()
|
||||||
assert.Equal(t, `param.New("myName", param.IsRequired, param.InHeader, param.Default("abc"))`, s)
|
assert.Equal(t, `param.New("myName", param.IsRequired, param.InHeader, param.Default("abc"))`, s)
|
||||||
|
|||||||
@ -25,8 +25,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const getUrl = "http://httpbin.org/get"
|
||||||
|
const ipUrl = "http://httpbin.org/ip"
|
||||||
|
|
||||||
func TestResponse(t *testing.T) {
|
func TestResponse(t *testing.T) {
|
||||||
req := Get("http://httpbin.org/get")
|
req := Get(getUrl)
|
||||||
resp, err := req.Response()
|
resp, err := req.Response()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@ -63,7 +66,8 @@ func TestDoRequest(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGet(t *testing.T) {
|
func TestGet(t *testing.T) {
|
||||||
req := Get("http://httpbin.org/get")
|
|
||||||
|
req := Get(getUrl)
|
||||||
b, err := req.Bytes()
|
b, err := req.Bytes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@ -205,7 +209,7 @@ func TestWithSetting(t *testing.T) {
|
|||||||
setting.ReadWriteTimeout = 5 * time.Second
|
setting.ReadWriteTimeout = 5 * time.Second
|
||||||
SetDefaultSetting(setting)
|
SetDefaultSetting(setting)
|
||||||
|
|
||||||
str, err := Get("http://httpbin.org/get").String()
|
str, err := Get(getUrl).String()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -218,7 +222,8 @@ func TestWithSetting(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestToJson(t *testing.T) {
|
func TestToJson(t *testing.T) {
|
||||||
req := Get("http://httpbin.org/ip")
|
|
||||||
|
req := Get(ipUrl)
|
||||||
resp, err := req.Response()
|
resp, err := req.Response()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@ -249,7 +254,7 @@ func TestToJson(t *testing.T) {
|
|||||||
|
|
||||||
func TestToFile(t *testing.T) {
|
func TestToFile(t *testing.T) {
|
||||||
f := "beego_testfile"
|
f := "beego_testfile"
|
||||||
req := Get("http://httpbin.org/ip")
|
req := Get(ipUrl)
|
||||||
err := req.ToFile(f)
|
err := req.ToFile(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@ -263,7 +268,7 @@ func TestToFile(t *testing.T) {
|
|||||||
|
|
||||||
func TestToFileDir(t *testing.T) {
|
func TestToFileDir(t *testing.T) {
|
||||||
f := "./files/beego_testfile"
|
f := "./files/beego_testfile"
|
||||||
req := Get("http://httpbin.org/ip")
|
req := Get(ipUrl)
|
||||||
err := req.ToFile(f)
|
err := req.ToFile(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|||||||
@ -18,7 +18,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestBeeLogger_Info(t *testing.T) {
|
func TestBeeLoggerInfo(t *testing.T) {
|
||||||
log := NewLogger(1000)
|
log := NewLogger(1000)
|
||||||
log.SetLogger("file", `{"net":"tcp","addr":":7020"}`)
|
log.SetLogger("file", `{"net":"tcp","addr":":7020"}`)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,7 @@
|
|||||||
package metric
|
package metric
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
@ -26,7 +27,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestPrometheusMiddleWare(t *testing.T) {
|
func TestPrometheusMiddleWare(t *testing.T) {
|
||||||
middleware := PrometheusMiddleWare(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
middleware := PrometheusMiddleWare(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
|
||||||
|
fmt.Print("you are coming")
|
||||||
|
}))
|
||||||
writer := &context.Response{}
|
writer := &context.Response{}
|
||||||
request := &http.Request{
|
request := &http.Request{
|
||||||
URL: &url.URL{
|
URL: &url.URL{
|
||||||
|
|||||||
@ -21,14 +21,16 @@ import (
|
|||||||
type baseQuerySetter struct {
|
type baseQuerySetter struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const shouldNotInvoke = "you should not invoke this method."
|
||||||
|
|
||||||
func (b *baseQuerySetter) ForceIndex(indexes ...string) orm.QuerySeter {
|
func (b *baseQuerySetter) ForceIndex(indexes ...string) orm.QuerySeter {
|
||||||
panic("you should not invoke this method.")
|
panic(shouldNotInvoke)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *baseQuerySetter) UseIndex(indexes ...string) orm.QuerySeter {
|
func (b *baseQuerySetter) UseIndex(indexes ...string) orm.QuerySeter {
|
||||||
panic("you should not invoke this method.")
|
panic(shouldNotInvoke)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *baseQuerySetter) IgnoreIndex(indexes ...string) orm.QuerySeter {
|
func (b *baseQuerySetter) IgnoreIndex(indexes ...string) orm.QuerySeter {
|
||||||
panic("you should not invoke this method.")
|
panic(shouldNotInvoke)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,6 +16,8 @@ package orm
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCamelString(t *testing.T) {
|
func TestCamelString(t *testing.T) {
|
||||||
@ -29,9 +31,7 @@ func TestCamelString(t *testing.T) {
|
|||||||
|
|
||||||
for _, v := range snake {
|
for _, v := range snake {
|
||||||
res := camelString(v)
|
res := camelString(v)
|
||||||
if res != answer[v] {
|
assert.Equal(t, answer[v], res)
|
||||||
t.Error("Unit Test Fail:", v, res, answer[v])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,9 +46,7 @@ func TestSnakeString(t *testing.T) {
|
|||||||
|
|
||||||
for _, v := range camel {
|
for _, v := range camel {
|
||||||
res := snakeString(v)
|
res := snakeString(v)
|
||||||
if res != answer[v] {
|
assert.Equal(t, answer[v], res)
|
||||||
t.Error("Unit Test Fail:", v, res, answer[v])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,8 +61,6 @@ func TestSnakeStringWithAcronym(t *testing.T) {
|
|||||||
|
|
||||||
for _, v := range camel {
|
for _, v := range camel {
|
||||||
res := snakeStringWithAcronym(v)
|
res := snakeStringWithAcronym(v)
|
||||||
if res != answer[v] {
|
assert.Equal(t, answer[v], res)
|
||||||
t.Error("Unit Test Fail:", v, res, answer[v])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,6 +26,11 @@ import (
|
|||||||
"github.com/beego/beego/v2/adapter/plugins/auth"
|
"github.com/beego/beego/v2/adapter/plugins/auth"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
authCfg = "authz_model.conf"
|
||||||
|
authCsv = "authz_policy.csv"
|
||||||
|
)
|
||||||
|
|
||||||
func testRequest(t *testing.T, handler *beego.ControllerRegister, user string, path string, method string, code int) {
|
func testRequest(t *testing.T, handler *beego.ControllerRegister, user string, path string, method string, code int) {
|
||||||
r, _ := http.NewRequest(method, path, nil)
|
r, _ := http.NewRequest(method, path, nil)
|
||||||
r.SetBasicAuth(user, "123")
|
r.SetBasicAuth(user, "123")
|
||||||
@ -40,70 +45,79 @@ func testRequest(t *testing.T, handler *beego.ControllerRegister, user string, p
|
|||||||
func TestBasic(t *testing.T) {
|
func TestBasic(t *testing.T) {
|
||||||
handler := beego.NewControllerRegister()
|
handler := beego.NewControllerRegister()
|
||||||
|
|
||||||
handler.InsertFilter("*", beego.BeforeRouter, auth.Basic("alice", "123"))
|
_ = handler.InsertFilter("*", beego.BeforeRouter, auth.Basic("alice", "123"))
|
||||||
handler.InsertFilter("*", beego.BeforeRouter, NewAuthorizer(casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")))
|
|
||||||
|
_ = handler.InsertFilter("*", beego.BeforeRouter, NewAuthorizer(casbin.NewEnforcer(authCfg, authCsv)))
|
||||||
|
|
||||||
handler.Any("*", func(ctx *context.Context) {
|
handler.Any("*", func(ctx *context.Context) {
|
||||||
ctx.Output.SetStatus(200)
|
ctx.Output.SetStatus(200)
|
||||||
})
|
})
|
||||||
|
|
||||||
testRequest(t, handler, "alice", "/dataset1/resource1", "GET", 200)
|
const d1r1 = "/dataset1/resource1"
|
||||||
testRequest(t, handler, "alice", "/dataset1/resource1", "POST", 200)
|
testRequest(t, handler, "alice", d1r1, "GET", 200)
|
||||||
testRequest(t, handler, "alice", "/dataset1/resource2", "GET", 200)
|
testRequest(t, handler, "alice", d1r1, "POST", 200)
|
||||||
testRequest(t, handler, "alice", "/dataset1/resource2", "POST", 403)
|
const d1r2 = "/dataset1/resource2"
|
||||||
|
testRequest(t, handler, "alice", d1r2, "GET", 200)
|
||||||
|
testRequest(t, handler, "alice", d1r2, "POST", 403)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPathWildcard(t *testing.T) {
|
func TestPathWildcard(t *testing.T) {
|
||||||
handler := beego.NewControllerRegister()
|
handler := beego.NewControllerRegister()
|
||||||
|
|
||||||
handler.InsertFilter("*", beego.BeforeRouter, auth.Basic("bob", "123"))
|
_ = handler.InsertFilter("*", beego.BeforeRouter, auth.Basic("bob", "123"))
|
||||||
handler.InsertFilter("*", beego.BeforeRouter, NewAuthorizer(casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")))
|
_ = handler.InsertFilter("*", beego.BeforeRouter, NewAuthorizer(casbin.NewEnforcer(authCfg, authCsv)))
|
||||||
|
|
||||||
handler.Any("*", func(ctx *context.Context) {
|
handler.Any("*", func(ctx *context.Context) {
|
||||||
ctx.Output.SetStatus(200)
|
ctx.Output.SetStatus(200)
|
||||||
})
|
})
|
||||||
|
|
||||||
testRequest(t, handler, "bob", "/dataset2/resource1", "GET", 200)
|
const d2r1 = "/dataset2/resource1"
|
||||||
testRequest(t, handler, "bob", "/dataset2/resource1", "POST", 200)
|
testRequest(t, handler, "bob", d2r1, "GET", 200)
|
||||||
testRequest(t, handler, "bob", "/dataset2/resource1", "DELETE", 200)
|
testRequest(t, handler, "bob", d2r1, "POST", 200)
|
||||||
testRequest(t, handler, "bob", "/dataset2/resource2", "GET", 200)
|
testRequest(t, handler, "bob", d2r1, "DELETE", 200)
|
||||||
testRequest(t, handler, "bob", "/dataset2/resource2", "POST", 403)
|
const d2r2 = "/dataset2/resource2"
|
||||||
testRequest(t, handler, "bob", "/dataset2/resource2", "DELETE", 403)
|
testRequest(t, handler, "bob", d2r2, "GET", 200)
|
||||||
|
testRequest(t, handler, "bob", d2r2, "POST", 403)
|
||||||
|
testRequest(t, handler, "bob", d2r2, "DELETE", 403)
|
||||||
|
|
||||||
testRequest(t, handler, "bob", "/dataset2/folder1/item1", "GET", 403)
|
const item1 = "/dataset2/folder1/item1"
|
||||||
testRequest(t, handler, "bob", "/dataset2/folder1/item1", "POST", 200)
|
testRequest(t, handler, "bob", item1, "GET", 403)
|
||||||
testRequest(t, handler, "bob", "/dataset2/folder1/item1", "DELETE", 403)
|
testRequest(t, handler, "bob", item1, "POST", 200)
|
||||||
testRequest(t, handler, "bob", "/dataset2/folder1/item2", "GET", 403)
|
testRequest(t, handler, "bob", item1, "DELETE", 403)
|
||||||
testRequest(t, handler, "bob", "/dataset2/folder1/item2", "POST", 200)
|
const item2 = "/dataset2/folder1/item2"
|
||||||
testRequest(t, handler, "bob", "/dataset2/folder1/item2", "DELETE", 403)
|
testRequest(t, handler, "bob", item2, "GET", 403)
|
||||||
|
testRequest(t, handler, "bob", item2, "POST", 200)
|
||||||
|
testRequest(t, handler, "bob", item2, "DELETE", 403)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRBAC(t *testing.T) {
|
func TestRBAC(t *testing.T) {
|
||||||
handler := beego.NewControllerRegister()
|
handler := beego.NewControllerRegister()
|
||||||
|
|
||||||
handler.InsertFilter("*", beego.BeforeRouter, auth.Basic("cathy", "123"))
|
_ = handler.InsertFilter("*", beego.BeforeRouter, auth.Basic("cathy", "123"))
|
||||||
e := casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")
|
e := casbin.NewEnforcer(authCfg, authCsv)
|
||||||
handler.InsertFilter("*", beego.BeforeRouter, NewAuthorizer(e))
|
_ = handler.InsertFilter("*", beego.BeforeRouter, NewAuthorizer(e))
|
||||||
|
|
||||||
handler.Any("*", func(ctx *context.Context) {
|
handler.Any("*", func(ctx *context.Context) {
|
||||||
ctx.Output.SetStatus(200)
|
ctx.Output.SetStatus(200)
|
||||||
})
|
})
|
||||||
|
|
||||||
// cathy can access all /dataset1/* resources via all methods because it has the dataset1_admin role.
|
// cathy can access all /dataset1/* resources via all methods because it has the dataset1_admin role.
|
||||||
testRequest(t, handler, "cathy", "/dataset1/item", "GET", 200)
|
const dataSet1 = "/dataset1/item"
|
||||||
testRequest(t, handler, "cathy", "/dataset1/item", "POST", 200)
|
testRequest(t, handler, "cathy", dataSet1, "GET", 200)
|
||||||
testRequest(t, handler, "cathy", "/dataset1/item", "DELETE", 200)
|
testRequest(t, handler, "cathy", dataSet1, "POST", 200)
|
||||||
testRequest(t, handler, "cathy", "/dataset2/item", "GET", 403)
|
testRequest(t, handler, "cathy", dataSet1, "DELETE", 200)
|
||||||
testRequest(t, handler, "cathy", "/dataset2/item", "POST", 403)
|
const dataSet2 = "/dataset2/item"
|
||||||
testRequest(t, handler, "cathy", "/dataset2/item", "DELETE", 403)
|
testRequest(t, handler, "cathy", dataSet2, "GET", 403)
|
||||||
|
testRequest(t, handler, "cathy", dataSet2, "POST", 403)
|
||||||
|
testRequest(t, handler, "cathy", dataSet2, "DELETE", 403)
|
||||||
|
|
||||||
// delete all roles on user cathy, so cathy cannot access any resources now.
|
// delete all roles on user cathy, so cathy cannot access any resources now.
|
||||||
e.DeleteRolesForUser("cathy")
|
e.DeleteRolesForUser("cathy")
|
||||||
|
|
||||||
testRequest(t, handler, "cathy", "/dataset1/item", "GET", 403)
|
testRequest(t, handler, "cathy", dataSet1, "GET", 403)
|
||||||
testRequest(t, handler, "cathy", "/dataset1/item", "POST", 403)
|
testRequest(t, handler, "cathy", dataSet1, "POST", 403)
|
||||||
testRequest(t, handler, "cathy", "/dataset1/item", "DELETE", 403)
|
testRequest(t, handler, "cathy", dataSet1, "DELETE", 403)
|
||||||
testRequest(t, handler, "cathy", "/dataset2/item", "GET", 403)
|
testRequest(t, handler, "cathy", dataSet2, "GET", 403)
|
||||||
testRequest(t, handler, "cathy", "/dataset2/item", "POST", 403)
|
testRequest(t, handler, "cathy", dataSet2, "POST", 403)
|
||||||
testRequest(t, handler, "cathy", "/dataset2/item", "DELETE", 403)
|
testRequest(t, handler, "cathy", dataSet2, "DELETE", 403)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,8 @@ import (
|
|||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/beego/beego/v2/adapter/session"
|
"github.com/beego/beego/v2/adapter/session"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -19,71 +21,52 @@ func TestRedisSentinel(t *testing.T) {
|
|||||||
ProviderConfig: "127.0.0.1:6379,100,,0,master",
|
ProviderConfig: "127.0.0.1:6379,100,,0,master",
|
||||||
}
|
}
|
||||||
globalSessions, e := session.NewManager("redis_sentinel", sessionConfig)
|
globalSessions, e := session.NewManager("redis_sentinel", sessionConfig)
|
||||||
if e != nil {
|
|
||||||
t.Log(e)
|
assert.Nil(t, e)
|
||||||
return
|
|
||||||
}
|
|
||||||
// todo test if e==nil
|
|
||||||
go globalSessions.GC()
|
go globalSessions.GC()
|
||||||
|
|
||||||
r, _ := http.NewRequest("GET", "/", nil)
|
r, _ := http.NewRequest("GET", "/", nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
sess, err := globalSessions.SessionStart(w, r)
|
sess, err := globalSessions.SessionStart(w, r)
|
||||||
if err != nil {
|
assert.Nil(t, err)
|
||||||
t.Fatal("session start failed:", err)
|
|
||||||
}
|
|
||||||
defer sess.SessionRelease(w)
|
defer sess.SessionRelease(w)
|
||||||
|
|
||||||
// SET AND GET
|
// SET AND GET
|
||||||
err = sess.Set("username", "astaxie")
|
err = sess.Set("username", "astaxie")
|
||||||
if err != nil {
|
assert.Nil(t, err)
|
||||||
t.Fatal("set username failed:", err)
|
|
||||||
}
|
|
||||||
username := sess.Get("username")
|
username := sess.Get("username")
|
||||||
if username != "astaxie" {
|
assert.Equal(t, "astaxie", username)
|
||||||
t.Fatal("get username failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
// DELETE
|
// DELETE
|
||||||
err = sess.Delete("username")
|
err = sess.Delete("username")
|
||||||
if err != nil {
|
assert.Nil(t, err)
|
||||||
t.Fatal("delete username failed:", err)
|
|
||||||
}
|
|
||||||
username = sess.Get("username")
|
username = sess.Get("username")
|
||||||
if username != nil {
|
assert.Nil(t, username)
|
||||||
t.Fatal("delete username failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
// FLUSH
|
// FLUSH
|
||||||
err = sess.Set("username", "astaxie")
|
err = sess.Set("username", "astaxie")
|
||||||
if err != nil {
|
assert.Nil(t, err)
|
||||||
t.Fatal("set failed:", err)
|
|
||||||
}
|
|
||||||
err = sess.Set("password", "1qaz2wsx")
|
err = sess.Set("password", "1qaz2wsx")
|
||||||
if err != nil {
|
assert.Nil(t, err)
|
||||||
t.Fatal("set failed:", err)
|
|
||||||
}
|
|
||||||
username = sess.Get("username")
|
username = sess.Get("username")
|
||||||
if username != "astaxie" {
|
assert.Equal(t, "astaxie", username)
|
||||||
t.Fatal("get username failed")
|
|
||||||
}
|
|
||||||
password := sess.Get("password")
|
password := sess.Get("password")
|
||||||
if password != "1qaz2wsx" {
|
assert.Equal(t, "1qaz2wsx", password)
|
||||||
t.Fatal("get password failed")
|
|
||||||
}
|
|
||||||
err = sess.Flush()
|
err = sess.Flush()
|
||||||
if err != nil {
|
assert.Nil(t, err)
|
||||||
t.Fatal("flush failed:", err)
|
|
||||||
}
|
|
||||||
username = sess.Get("username")
|
username = sess.Get("username")
|
||||||
if username != nil {
|
assert.Nil(t, username)
|
||||||
t.Fatal("flush failed")
|
|
||||||
}
|
|
||||||
password = sess.Get("password")
|
password = sess.Get("password")
|
||||||
if password != nil {
|
assert.Nil(t, password)
|
||||||
t.Fatal("flush failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
sess.SessionRelease(w)
|
sess.SessionRelease(w)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user