Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin Support #110

Merged
merged 3 commits into from
Feb 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
sudo: required
dist: xenial
language: go
go:
- "1.11.5"
Expand Down
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