merge all branch and change project structure

This commit is contained in:
Gordon
2021-11-10 15:24:59 +08:00
91 changed files with 3612 additions and 1225 deletions
+68
View File
@@ -0,0 +1,68 @@
package utils
import (
"Open_IM/pkg/utils"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func init() {
gin.SetMode(gin.TestMode)
}
func performRequest(r http.Handler, method, origin string) *httptest.ResponseRecorder {
return performRequestWithHeaders(r, method, origin, http.Header{})
}
func performRequestWithHeaders(r http.Handler, method, origin string, header http.Header) *httptest.ResponseRecorder {
req, _ := http.NewRequest(method, "/", nil)
// From go/net/http/request.go:
// For incoming requests, the Host header is promoted to the
// Request.Host field and removed from the Header map.
req.Host = header.Get("Host")
header.Del("Host")
if len(origin) > 0 {
header.Set("Origin", origin)
}
req.Header = header
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}
func newTestRouter() *gin.Engine {
router := gin.New()
router.Use(utils.CorsHandler())
router.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "get")
})
router.POST("/", func(c *gin.Context) {
c.String(http.StatusOK, "post")
})
router.PATCH("/", func(c *gin.Context) {
c.String(http.StatusOK, "patch")
})
return router
}
func Test_CorsHandler(t *testing.T) {
router := newTestRouter()
// no CORS request, origin == ""
w := performRequest(router, "GET", "")
assert.Equal(t, "get", w.Body.String())
assert.Equal(t, w.Header().Get("Access-Control-Allow-Origin"), "*")
assert.Equal(t, w.Header().Get("Access-Control-Allow-Methods"), "*")
assert.Equal(t, w.Header().Get("Access-Control-Allow-Headers"), "*")
assert.Equal(t, w.Header().Get("Access-Control-Expose-Headers"), "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar")
assert.Equal(t, w.Header().Get("Access-Control-Max-Age"), "172800")
assert.Equal(t, w.Header().Get("Access-Control-Allow-Credentials"), "false")
assert.Equal(t, w.Header().Get("content-type"), "application/json")
w = performRequest(router, "OPTIONS", "")
assert.Equal(t, w.Body.String(), "\"Options Request!\"")
}
+13
View File
@@ -0,0 +1,13 @@
package utils
import (
"Open_IM/pkg/utils"
"net"
"testing"
)
func TestServerIP(t *testing.T) {
if net.ParseIP(utils.ServerIP) == nil {
t.Fail()
}
}
+28
View File
@@ -0,0 +1,28 @@
package utils
import (
"github.com/bwmarrin/snowflake"
)
func init() {
var err error
idGenerator, err = snowflake.NewNode(getNodeNum())
if err != nil {
panic(err)
}
}
func getNodeNum() int64 {
return 1
}
var idGenerator *snowflake.Node
func GenID() string {
return idGenerator.Generate().String()
}
func GenIDs(count int) []string {
//impl
return []string{}
}
+15
View File
@@ -0,0 +1,15 @@
package utils
import "testing"
func TestGenID(t *testing.T) {
m := map[string]struct{}{}
for i := 0; i < 2000; i++ {
got := GenID()
if _, ok := m[got]; !ok {
m[got] = struct{}{}
} else {
t.Error("id generate error", got)
}
}
}
+28
View File
@@ -0,0 +1,28 @@
package utils
import (
"Open_IM/pkg/utils"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
)
var (
_, b, _, _ = runtime.Caller(0)
// Root folder of this project
Root = filepath.Join(filepath.Dir(b), "../..")
)
func Test_GenSmallImage(t *testing.T) {
println(Root)
err := utils.GenSmallImage(Root+"/docs/open-im-logo.png", Root+"/out-test/open-im-logo-test.png")
assert.Nil(t, err)
err = utils.GenSmallImage(Root+"/docs/open-im-logo.png", "out-test/open-im-logo-test.png")
assert.Nil(t, err)
err = utils.GenSmallImage(Root+"/docs/Architecture.jpg", "out-test/Architecture-test.jpg")
assert.Nil(t, err)
}
+82
View File
@@ -0,0 +1,82 @@
package utils
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/utils"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func Test_BuildClaims(t *testing.T) {
uid := "1"
platform := "PC"
ttl := int64(-1)
claim := utils.BuildClaims(uid, platform, ttl)
now := time.Now().Unix()
assert.Equal(t, claim.UID, uid, "uid should equal")
assert.Equal(t, claim.Platform, platform, "platform should equal")
assert.Equal(t, claim.RegisteredClaims.ExpiresAt, int64(-1), "StandardClaims.ExpiresAt should be equal")
// time difference within 1s
assert.Equal(t, claim.RegisteredClaims.IssuedAt, now, "StandardClaims.IssuedAt should be equal")
assert.Equal(t, claim.RegisteredClaims.NotBefore, now, "StandardClaims.NotBefore should be equal")
ttl = int64(60)
now = time.Now().Unix()
claim = utils.BuildClaims(uid, platform, ttl)
// time difference within 1s
assert.Equal(t, claim.RegisteredClaims.ExpiresAt, int64(60)+now, "StandardClaims.ExpiresAt should be equal")
assert.Equal(t, claim.RegisteredClaims.IssuedAt, now, "StandardClaims.IssuedAt should be equal")
assert.Equal(t, claim.RegisteredClaims.NotBefore, now, "StandardClaims.NotBefore should be equal")
}
func Test_CreateToken(t *testing.T) {
uid := "1"
platform := int32(1)
now := time.Now().Unix()
tokenString, expiresAt, err := utils.CreateToken(uid, platform)
assert.NotEmpty(t, tokenString)
assert.Equal(t, expiresAt, 604800+now)
assert.Nil(t, err)
}
func Test_VerifyToken(t *testing.T) {
uid := "1"
platform := int32(1)
tokenString, _, _ := utils.CreateToken(uid, platform)
result := utils.VerifyToken(tokenString, uid)
assert.True(t, result)
result = utils.VerifyToken(tokenString, "2")
assert.False(t, result)
}
func Test_ParseRedisInterfaceToken(t *testing.T) {
uid := "1"
platform := int32(1)
tokenString, _, _ := utils.CreateToken(uid, platform)
claims, err := utils.ParseRedisInterfaceToken([]uint8(tokenString))
assert.Nil(t, err)
assert.Equal(t, claims.UID, uid)
// timeout
config.Config.TokenPolicy.AccessExpire = -80
tokenString, _, _ = utils.CreateToken(uid, platform)
claims, err = utils.ParseRedisInterfaceToken([]uint8(tokenString))
assert.Equal(t, err, utils.TokenExpired)
assert.Nil(t, claims)
}
func Test_ParseToken(t *testing.T) {
uid := "1"
platform := int32(1)
tokenString, _, _ := utils.CreateToken(uid, platform)
claims, err := utils.ParseToken(tokenString)
if err == nil {
assert.Equal(t, claims.UID, uid)
}
}
+16
View File
@@ -0,0 +1,16 @@
package utils
import (
"Open_IM/pkg/utils"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_Md5(t *testing.T) {
result := utils.Md5("go")
assert.Equal(t, result, "34d1f91fb2e514b8576fab1a75a89a6b")
result2 := utils.Md5("go")
assert.Equal(t, result, result2)
}
@@ -0,0 +1,46 @@
package utils
import (
"Open_IM/pkg/utils"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_PlatformIDToName(t *testing.T) {
assert.Equal(t, utils.PlatformIDToName(1), "IOS")
assert.Equal(t, utils.PlatformIDToName(2), "Android")
assert.Equal(t, utils.PlatformIDToName(3), "Windows")
assert.Equal(t, utils.PlatformIDToName(4), "OSX")
assert.Equal(t, utils.PlatformIDToName(5), "Web")
assert.Equal(t, utils.PlatformIDToName(6), "MiniWeb")
assert.Equal(t, utils.PlatformIDToName(7), "Linux")
assert.Equal(t, utils.PlatformIDToName(0), "")
}
func Test_PlatformNameToID(t *testing.T) {
assert.Equal(t, utils.PlatformNameToID("IOS"), int32(1))
assert.Equal(t, utils.PlatformNameToID("Android"), int32(2))
assert.Equal(t, utils.PlatformNameToID("Windows"), int32(3))
assert.Equal(t, utils.PlatformNameToID("OSX"), int32(4))
assert.Equal(t, utils.PlatformNameToID("Web"), int32(5))
assert.Equal(t, utils.PlatformNameToID("MiniWeb"), int32(6))
assert.Equal(t, utils.PlatformNameToID("Linux"), int32(7))
assert.Equal(t, utils.PlatformNameToID("UnknownDevice"), int32(0))
assert.Equal(t, utils.PlatformNameToID(""), int32(0))
}
func Test_PlatformNameToClass(t *testing.T) {
assert.Equal(t, utils.PlatformNameToClass("IOS"), "Mobile")
assert.Equal(t, utils.PlatformNameToClass("Android"), "Mobile")
assert.Equal(t, utils.PlatformNameToClass("OSX"), "PC")
assert.Equal(t, utils.PlatformNameToClass("Windows"), "PC")
assert.Equal(t, utils.PlatformNameToClass("Web"), "PC")
assert.Equal(t, utils.PlatformNameToClass("MiniWeb"), "Mobile")
assert.Equal(t, utils.PlatformNameToClass("Linux"), "PC")
assert.Equal(t, utils.PlatformNameToClass("UnknownDevice"), "")
assert.Equal(t, utils.PlatformNameToClass(""), "")
}