Remove monkey dependency
This commit is contained in:
parent
0f9d3bde81
commit
80eec6ae3a
|
|
@ -7,12 +7,6 @@
|
||||||
revision = "b26d9c308763d68093482582cea63d69be07a0f0"
|
revision = "b26d9c308763d68093482582cea63d69be07a0f0"
|
||||||
version = "v0.3.0"
|
version = "v0.3.0"
|
||||||
|
|
||||||
[[projects]]
|
|
||||||
branch = "master"
|
|
||||||
name = "github.com/bouk/monkey"
|
|
||||||
packages = ["."]
|
|
||||||
revision = "b96e337f6e5b36906584cd0cf9803bbab09284f7"
|
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
name = "github.com/davecgh/go-spew"
|
name = "github.com/davecgh/go-spew"
|
||||||
packages = ["spew"]
|
packages = ["spew"]
|
||||||
|
|
@ -203,6 +197,6 @@
|
||||||
[solve-meta]
|
[solve-meta]
|
||||||
analyzer-name = "dep"
|
analyzer-name = "dep"
|
||||||
analyzer-version = 1
|
analyzer-version = 1
|
||||||
inputs-digest = "272b085d12075d5deae7a7eac7472448f76f798644d80b17a4c6a4d5c7a8cfe9"
|
inputs-digest = "dd487dcd1de5f16454e33fbf7d886d98272b1b9d78052a17ad785efba4f9c17b"
|
||||||
solver-name = "gps-cdcl"
|
solver-name = "gps-cdcl"
|
||||||
solver-version = 1
|
solver-version = 1
|
||||||
|
|
|
||||||
2
Makefile
2
Makefile
|
|
@ -56,7 +56,7 @@ check-swagger: update-swagger
|
||||||
|
|
||||||
extract-licenses:
|
extract-licenses:
|
||||||
mkdir ${LICENSE_DIR} || true
|
mkdir ${LICENSE_DIR} || true
|
||||||
for LICENSE in $(shell find vendor/* -name LICENSE | grep -v monkey); do \
|
for LICENSE in $(shell find vendor/* -name LICENSE); do \
|
||||||
DIR=`echo $$LICENSE | tr "/" _ | sed -e 's/vendor_//; s/_LICENSE//'` ; \
|
DIR=`echo $$LICENSE | tr "/" _ | sed -e 's/vendor_//; s/_LICENSE//'` ; \
|
||||||
cp $$LICENSE ${LICENSE_DIR}$$DIR ; \
|
cp $$LICENSE ${LICENSE_DIR}$$DIR ; \
|
||||||
done
|
done
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,8 @@ type MessageDatabase interface {
|
||||||
GetApplicationByToken(token string) *model.Application
|
GetApplicationByToken(token string) *model.Application
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var timeNow = time.Now
|
||||||
|
|
||||||
// Notifier notifies when a new message was created.
|
// Notifier notifies when a new message was created.
|
||||||
type Notifier interface {
|
type Notifier interface {
|
||||||
Notify(userID uint, message *model.Message)
|
Notify(userID uint, message *model.Message)
|
||||||
|
|
@ -128,7 +130,7 @@ func (a *MessageAPI) CreateMessage(ctx *gin.Context) {
|
||||||
message := model.Message{}
|
message := model.Message{}
|
||||||
if err := ctx.Bind(&message); err == nil {
|
if err := ctx.Bind(&message); err == nil {
|
||||||
message.ApplicationID = a.DB.GetApplicationByToken(auth.GetTokenID(ctx)).ID
|
message.ApplicationID = a.DB.GetApplicationByToken(auth.GetTokenID(ctx)).ID
|
||||||
message.Date = time.Now()
|
message.Date = timeNow()
|
||||||
a.DB.CreateMessage(&message)
|
a.DB.CreateMessage(&message)
|
||||||
a.Notifier.Notify(auth.GetUserID(ctx), &message)
|
a.Notifier.Notify(auth.GetUserID(ctx), &message)
|
||||||
ctx.JSON(200, message)
|
ctx.JSON(200, message)
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,6 @@ import (
|
||||||
|
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/bouk/monkey"
|
|
||||||
"github.com/gotify/server/auth"
|
"github.com/gotify/server/auth"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -314,8 +313,9 @@ func (s *MessageSuite) Test_DeleteMessages() {
|
||||||
|
|
||||||
func (s *MessageSuite) Test_CreateMessage_onJson_allParams() {
|
func (s *MessageSuite) Test_CreateMessage_onJson_allParams() {
|
||||||
t, _ := time.Parse("2006/01/02", "2017/01/02")
|
t, _ := time.Parse("2006/01/02", "2017/01/02")
|
||||||
patch := monkey.Patch(time.Now, func() time.Time { return t })
|
|
||||||
defer patch.Unpatch()
|
timeNow = func() time.Time { return t }
|
||||||
|
defer func() { timeNow = time.Now }()
|
||||||
|
|
||||||
auth.RegisterAuthentication(s.ctx, nil, 4, "app-token")
|
auth.RegisterAuthentication(s.ctx, nil, 4, "app-token")
|
||||||
s.db.User(4).AppWithToken(7, "app-token")
|
s.db.User(4).AppWithToken(7, "app-token")
|
||||||
|
|
@ -334,8 +334,8 @@ func (s *MessageSuite) Test_CreateMessage_onJson_allParams() {
|
||||||
|
|
||||||
func (s *MessageSuite) Test_CreateMessage_onlyRequired() {
|
func (s *MessageSuite) Test_CreateMessage_onlyRequired() {
|
||||||
t, _ := time.Parse("2006/01/02", "2017/01/02")
|
t, _ := time.Parse("2006/01/02", "2017/01/02")
|
||||||
patch := monkey.Patch(time.Now, func() time.Time { return t })
|
timeNow = func() time.Time { return t }
|
||||||
defer patch.Unpatch()
|
defer func() { timeNow = time.Now }()
|
||||||
|
|
||||||
auth.RegisterAuthentication(s.ctx, nil, 4, "app-token")
|
auth.RegisterAuthentication(s.ctx, nil, 4, "app-token")
|
||||||
s.db.User(4).AppWithToken(5, "app-token")
|
s.db.User(4).AppWithToken(5, "app-token")
|
||||||
|
|
@ -399,8 +399,8 @@ func (s *MessageSuite) Test_CreateMessage_onQueryData() {
|
||||||
s.db.User(4).AppWithToken(2, "app-token")
|
s.db.User(4).AppWithToken(2, "app-token")
|
||||||
|
|
||||||
t, _ := time.Parse("2006/01/02", "2017/01/02")
|
t, _ := time.Parse("2006/01/02", "2017/01/02")
|
||||||
patch := monkey.Patch(time.Now, func() time.Time { return t })
|
timeNow = func() time.Time { return t }
|
||||||
defer patch.Unpatch()
|
defer func() { timeNow = time.Now }()
|
||||||
|
|
||||||
s.ctx.Request = httptest.NewRequest("POST", "/token?title=mytitle&message=mymessage&priority=1", nil)
|
s.ctx.Request = httptest.NewRequest("POST", "/token?title=mytitle&message=mymessage&priority=1", nil)
|
||||||
s.ctx.Request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
s.ctx.Request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
|
@ -421,8 +421,8 @@ func (s *MessageSuite) Test_CreateMessage_onFormData() {
|
||||||
s.db.User(4).AppWithToken(99, "app-token")
|
s.db.User(4).AppWithToken(99, "app-token")
|
||||||
|
|
||||||
t, _ := time.Parse("2006/01/02", "2017/01/02")
|
t, _ := time.Parse("2006/01/02", "2017/01/02")
|
||||||
patch := monkey.Patch(time.Now, func() time.Time { return t })
|
timeNow = func() time.Time { return t }
|
||||||
defer patch.Unpatch()
|
defer func() { timeNow = time.Now }()
|
||||||
|
|
||||||
s.ctx.Request = httptest.NewRequest("POST", "/token", strings.NewReader("title=mytitle&message=mymessage&priority=1"))
|
s.ctx.Request = httptest.NewRequest("POST", "/token", strings.NewReader("title=mytitle&message=mymessage&priority=1"))
|
||||||
s.ctx.Request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
s.ctx.Request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,14 @@ const (
|
||||||
writeWait = 2 * time.Second
|
writeWait = 2 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ping = func(conn *websocket.Conn) error {
|
||||||
|
return conn.WriteMessage(websocket.PingMessage, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
var writeJSON = func(conn *websocket.Conn, v interface{}) error {
|
||||||
|
return conn.WriteJSON(v)
|
||||||
|
}
|
||||||
|
|
||||||
type client struct {
|
type client struct {
|
||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
onClose func(*client)
|
onClose func(*client)
|
||||||
|
|
@ -84,12 +92,12 @@ func (c *client) startWriteHandler(pingPeriod time.Duration) {
|
||||||
}
|
}
|
||||||
|
|
||||||
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||||
if err := c.conn.WriteJSON(message); err != nil {
|
if err := writeJSON(c.conn, message); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case <-pingTicker.C:
|
case <-pingTicker.C:
|
||||||
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||||
if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
|
if err := ping(c.conn); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,8 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/bouk/monkey"
|
|
||||||
"github.com/fortytw2/leaktest"
|
"github.com/fortytw2/leaktest"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
|
@ -41,7 +38,14 @@ func TestFailureOnNormalHttpRequest(t *testing.T) {
|
||||||
|
|
||||||
func TestWriteMessageFails(t *testing.T) {
|
func TestWriteMessageFails(t *testing.T) {
|
||||||
mode.Set(mode.TestDev)
|
mode.Set(mode.TestDev)
|
||||||
|
oldWrite := writeJSON
|
||||||
|
// try emulate an write error, mostly this should kill the ReadMessage goroutine first but you'll never know.
|
||||||
|
writeJSON = func(conn *websocket.Conn, v interface{}) error {
|
||||||
|
return errors.New("asd")
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
writeJSON = oldWrite
|
||||||
|
}()
|
||||||
defer leaktest.Check(t)()
|
defer leaktest.Check(t)()
|
||||||
|
|
||||||
server, api := bootTestServer(func(context *gin.Context) {
|
server, api := bootTestServer(func(context *gin.Context) {
|
||||||
|
|
@ -58,18 +62,20 @@ func TestWriteMessageFails(t *testing.T) {
|
||||||
clients := clients(api, 1)
|
clients := clients(api, 1)
|
||||||
assert.NotEmpty(t, clients)
|
assert.NotEmpty(t, clients)
|
||||||
|
|
||||||
// try emulate an write error, mostly this should kill the ReadMessage goroutine first but you'll never know.
|
|
||||||
patch := monkey.PatchInstanceMethod(reflect.TypeOf(clients[0].conn), "WriteJSON", func(*websocket.Conn, interface{}) error {
|
|
||||||
return errors.New("could not do something")
|
|
||||||
})
|
|
||||||
defer patch.Unpatch()
|
|
||||||
|
|
||||||
api.Notify(1, &model.Message{Message: "HI"})
|
api.Notify(1, &model.Message{Message: "HI"})
|
||||||
user.expectNoMessage()
|
user.expectNoMessage()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWritePingFails(t *testing.T) {
|
func TestWritePingFails(t *testing.T) {
|
||||||
mode.Set(mode.TestDev)
|
mode.Set(mode.TestDev)
|
||||||
|
oldPing := ping
|
||||||
|
// try emulate an write error, mostly this should kill the ReadMessage gorouting first but you'll never know.
|
||||||
|
ping = func(conn *websocket.Conn) error {
|
||||||
|
return errors.New("asd")
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
ping = oldPing
|
||||||
|
}()
|
||||||
|
|
||||||
defer leaktest.CheckTimeout(t, 10*time.Second)()
|
defer leaktest.CheckTimeout(t, 10*time.Second)()
|
||||||
|
|
||||||
|
|
@ -86,11 +92,6 @@ func TestWritePingFails(t *testing.T) {
|
||||||
clients := clients(api, 1)
|
clients := clients(api, 1)
|
||||||
|
|
||||||
assert.NotEmpty(t, clients)
|
assert.NotEmpty(t, clients)
|
||||||
// try emulate an write error, mostly this should kill the ReadMessage gorouting first but you'll never know.
|
|
||||||
patch := monkey.PatchInstanceMethod(reflect.TypeOf(clients[0].conn), "WriteMessage", func(*websocket.Conn, int, []byte) error {
|
|
||||||
return errors.New("could not do something")
|
|
||||||
})
|
|
||||||
defer patch.Unpatch()
|
|
||||||
|
|
||||||
time.Sleep(5 * time.Second) // waiting for ping
|
time.Sleep(5 * time.Second) // waiting for ping
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,14 +10,11 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"io/ioutil"
|
|
||||||
|
|
||||||
"github.com/bouk/monkey"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/gotify/server/mode"
|
"github.com/gotify/server/mode"
|
||||||
"github.com/gotify/server/model"
|
"github.com/gotify/server/model"
|
||||||
|
|
@ -455,15 +452,11 @@ func (s *TokenSuite) Test_UploadAppImage_WithSaveError_expectServerError() {
|
||||||
|
|
||||||
cType, buffer, err := upload(map[string]*os.File{"file": mustOpen("../test/assets/image.png")})
|
cType, buffer, err := upload(map[string]*os.File{"file": mustOpen("../test/assets/image.png")})
|
||||||
assert.Nil(s.T(), err)
|
assert.Nil(s.T(), err)
|
||||||
s.ctx.Request = httptest.NewRequest("POST", "/irrelevant", &buffer)
|
s.ctx.Request = httptest.NewRequest("POST", "/irrelevant/", &buffer)
|
||||||
|
s.a.ImageDir = "asdasd/asdasda/asdasd"
|
||||||
s.ctx.Request.Header.Set("Content-Type", cType)
|
s.ctx.Request.Header.Set("Content-Type", cType)
|
||||||
test.WithUser(s.ctx, 5)
|
test.WithUser(s.ctx, 5)
|
||||||
s.ctx.Params = gin.Params{{Key: "id", Value: "1"}}
|
s.ctx.Params = gin.Params{{Key: "id", Value: "1"}}
|
||||||
// try emulate a save error.
|
|
||||||
patch := monkey.PatchInstanceMethod(reflect.TypeOf(s.ctx), "SaveUploadedFile", func(ctx *gin.Context, file *multipart.FileHeader, dst string) error {
|
|
||||||
return errors.New("could not do something")
|
|
||||||
})
|
|
||||||
defer patch.Unpatch()
|
|
||||||
|
|
||||||
s.a.UploadApplicationImage(s.ctx)
|
s.a.UploadApplicationImage(s.ctx)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ import (
|
||||||
_ "github.com/jinzhu/gorm/dialects/sqlite" // enable the sqlite3 dialect
|
_ "github.com/jinzhu/gorm/dialects/sqlite" // enable the sqlite3 dialect
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var mkdirAll = os.MkdirAll
|
||||||
|
|
||||||
// New creates a new wrapper for the gorm database framework.
|
// New creates a new wrapper for the gorm database framework.
|
||||||
func New(dialect, connection, defaultUser, defaultPass string, strength int, createDefaultUser bool) (*GormDatabase, error) {
|
func New(dialect, connection, defaultUser, defaultPass string, strength int, createDefaultUser bool) (*GormDatabase, error) {
|
||||||
createDirectoryIfSqlite(dialect, connection)
|
createDirectoryIfSqlite(dialect, connection)
|
||||||
|
|
@ -47,7 +49,7 @@ func New(dialect, connection, defaultUser, defaultPass string, strength int, cre
|
||||||
func createDirectoryIfSqlite(dialect string, connection string) {
|
func createDirectoryIfSqlite(dialect string, connection string) {
|
||||||
if dialect == "sqlite3" {
|
if dialect == "sqlite3" {
|
||||||
if _, err := os.Stat(filepath.Dir(connection)); os.IsNotExist(err) {
|
if _, err := os.Stat(filepath.Dir(connection)); os.IsNotExist(err) {
|
||||||
if err := os.MkdirAll(filepath.Dir(connection), 0777); err != nil {
|
if err := mkdirAll(filepath.Dir(connection), 0777); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,10 @@
|
||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"errors"
|
|
||||||
|
|
||||||
"github.com/bouk/monkey"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
)
|
)
|
||||||
|
|
@ -62,12 +60,11 @@ func TestWithAlreadyExistingSqliteFolder(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPanicsOnMkdirError(t *testing.T) {
|
func TestPanicsOnMkdirError(t *testing.T) {
|
||||||
patch := monkey.Patch(os.MkdirAll, func(string, os.FileMode) error { return errors.New("whoops") })
|
|
||||||
defer patch.Unpatch()
|
|
||||||
// ensure path not exists
|
|
||||||
os.RemoveAll("somepath")
|
os.RemoveAll("somepath")
|
||||||
|
mkdirAll = func(path string, perm os.FileMode) error {
|
||||||
|
return errors.New("ERROR")
|
||||||
|
}
|
||||||
assert.Panics(t, func() {
|
assert.Panics(t, func() {
|
||||||
New("sqlite3", "somepath/testdb.db", "defaultUser", "defaultPass", 5, true)
|
New("sqlite3", "somepath/test.db", "defaultUser", "defaultPass", 5, true)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue