Skip to content

Commit

Permalink
Add plugin feature
Browse files Browse the repository at this point in the history
Fixed database migration
Added a plugin system based on the go plugin package
  • Loading branch information
eternal-flame-AD authored and jmattheis committed Feb 9, 2019
1 parent 06d13d2 commit e5b24f4
Show file tree
Hide file tree
Showing 88 changed files with 5,869 additions and 222 deletions.
9 changes: 1 addition & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@ test-race:
go test -v -race ./...

test-coverage:
echo "" > coverage.txt
for d in $(shell go list ./... | grep -v vendor); do \
go test -v -coverprofile=profile.out -covermode=atomic $$d ; \
if [ -f profile.out ]; then \
cat profile.out >> coverage.txt ; \
rm profile.out ; \
fi \
done
go test -v -coverprofile=coverage.txt -covermode=atomic ./...

format:
goimports -w $(shell find . -type f -name '*.go' -not -path "./vendor/*")
Expand Down
7 changes: 6 additions & 1 deletion api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ type ApplicationAPI struct {
func (a *ApplicationAPI) CreateApplication(ctx *gin.Context) {
app := model.Application{}
if err := ctx.Bind(&app); err == nil {
app.Token = generateNotExistingToken(auth.GenerateApplicationToken, a.applicationExists)
app.Token = auth.GenerateNotExistingToken(auth.GenerateApplicationToken, a.applicationExists)
app.UserID = auth.GetUserID(ctx)
app.Internal = false
a.DB.CreateApplication(&app)
ctx.JSON(200, withAbsoluteURL(ctx, &app))
}
Expand Down Expand Up @@ -143,6 +144,10 @@ func (a *ApplicationAPI) GetApplications(ctx *gin.Context) {
func (a *ApplicationAPI) DeleteApplication(ctx *gin.Context) {
withID(ctx, "id", func(id uint) {
if app := a.DB.GetApplicationByID(id); app != nil && app.UserID == auth.GetUserID(ctx) {
if app.Internal {
ctx.AbortWithError(400, errors.New("cannot delete internal application"))
return
}
a.DB.DeleteApplicationByID(id)
if app.Image != "" {
os.Remove(a.ImageDir + app.Image)
Expand Down
20 changes: 17 additions & 3 deletions api/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/gotify/server/mode"
"github.com/gotify/server/model"
"github.com/gotify/server/test"
"github.com/gotify/server/test/testdb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
Expand All @@ -31,7 +32,7 @@ func TestApplicationSuite(t *testing.T) {

type ApplicationSuite struct {
suite.Suite
db *test.Database
db *testdb.Database
a *ApplicationAPI
ctx *gin.Context
recorder *httptest.ResponseRecorder
Expand All @@ -41,7 +42,7 @@ func (s *ApplicationSuite) BeforeTest(suiteName, testName string) {
mode.Set(mode.TestDev)
rand.Seed(50)
s.recorder = httptest.NewRecorder()
s.db = test.NewDB(s.T())
s.db = testdb.NewDB(s.T())
s.ctx, _ = gin.CreateTestContext(s.recorder)
withURL(s.ctx, "http", "example.com")
s.a = &ApplicationAPI{DB: s.db}
Expand Down Expand Up @@ -76,8 +77,9 @@ func (s *ApplicationSuite) Test_ensureApplicationHasCorrectJsonRepresentation()
Name: "myapp",
Description: "mydesc",
Image: "asd",
Internal: true,
}
test.JSONEquals(s.T(), actual, `{"id":1,"token":"Aasdasfgeeg","name":"myapp","description":"mydesc", "image": "asd"}`)
test.JSONEquals(s.T(), actual, `{"id":1,"token":"Aasdasfgeeg","name":"myapp","description":"mydesc", "image": "asd", "internal":true}`)
}
func (s *ApplicationSuite) Test_CreateApplication_expectBadRequestOnEmptyName() {
s.db.User(5)
Expand Down Expand Up @@ -183,6 +185,18 @@ func (s *ApplicationSuite) Test_GetApplications_WithImage() {
test.BodyEquals(s.T(), []*model.Application{first, second}, s.recorder)
}

func (s *ApplicationSuite) Test_DeleteApplication_internal_expectBadRequest() {
s.db.User(5).InternalApp(10)

test.WithUser(s.ctx, 5)
s.ctx.Request = httptest.NewRequest("DELETE", "/token/"+firstApplicationToken, nil)
s.ctx.Params = gin.Params{{Key: "id", Value: "10"}}

s.a.DeleteApplication(s.ctx)

assert.Equal(s.T(), 400, s.recorder.Code)
}

func (s *ApplicationSuite) Test_DeleteApplication_expectNotFound() {
s.db.User(5)

Expand Down
11 changes: 1 addition & 10 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type ClientAPI struct {
func (a *ClientAPI) CreateClient(ctx *gin.Context) {
client := model.Client{}
if err := ctx.Bind(&client); err == nil {
client.Token = generateNotExistingToken(auth.GenerateClientToken, a.clientExists)
client.Token = auth.GenerateNotExistingToken(auth.GenerateClientToken, a.clientExists)
client.UserID = auth.GetUserID(ctx)
a.DB.CreateClient(&client)
ctx.JSON(200, client)
Expand Down Expand Up @@ -145,12 +145,3 @@ func (a *ClientAPI) DeleteClient(ctx *gin.Context) {
func (a *ClientAPI) clientExists(token string) bool {
return a.DB.GetClientByToken(token) != nil
}

func generateNotExistingToken(generateToken func() string, tokenExists func(token string) bool) string {
for {
token := generateToken()
if !tokenExists(token) {
return token
}
}
}
5 changes: 3 additions & 2 deletions api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gotify/server/mode"
"github.com/gotify/server/model"
"github.com/gotify/server/test"
"github.com/gotify/server/test/testdb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
Expand All @@ -26,7 +27,7 @@ func TestClientSuite(t *testing.T) {

type ClientSuite struct {
suite.Suite
db *test.Database
db *testdb.Database
a *ClientAPI
ctx *gin.Context
recorder *httptest.ResponseRecorder
Expand All @@ -37,7 +38,7 @@ func (s *ClientSuite) BeforeTest(suiteName, testName string) {
mode.Set(mode.TestDev)
rand.Seed(50)
s.recorder = httptest.NewRecorder()
s.db = test.NewDB(s.T())
s.db = testdb.NewDB(s.T())
s.ctx, _ = gin.CreateTestContext(s.recorder)
withURL(s.ctx, "http", "example.com")
s.notified = false
Expand Down
5 changes: 3 additions & 2 deletions api/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/gotify/server/mode"
"github.com/gotify/server/model"
"github.com/gotify/server/test"
"github.com/gotify/server/test/testdb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
Expand All @@ -22,7 +23,7 @@ func TestMessageSuite(t *testing.T) {

type MessageSuite struct {
suite.Suite
db *test.Database
db *testdb.Database
a *MessageAPI
ctx *gin.Context
recorder *httptest.ResponseRecorder
Expand All @@ -34,7 +35,7 @@ func (s *MessageSuite) BeforeTest(suiteName, testName string) {
s.recorder = httptest.NewRecorder()
s.ctx, _ = gin.CreateTestContext(s.recorder)
s.ctx.Request = httptest.NewRequest("GET", "/irrelevant", nil)
s.db = test.NewDB(s.T())
s.db = testdb.NewDB(s.T())
s.notifiedMessage = nil
s.a = &MessageAPI{DB: s.db, Notifier: s}
}
Expand Down
Loading

0 comments on commit e5b24f4

Please sign in to comment.