diff --git a/api/message.go b/api/message.go index 7429496..cfa1e07 100644 --- a/api/message.go +++ b/api/message.go @@ -2,6 +2,7 @@ package api import ( "errors" + "strings" "time" "strconv" @@ -129,7 +130,11 @@ func (a *MessageAPI) DeleteMessage(ctx *gin.Context) { func (a *MessageAPI) CreateMessage(ctx *gin.Context) { message := model.Message{} if err := ctx.Bind(&message); err == nil { - message.ApplicationID = a.DB.GetApplicationByToken(auth.GetTokenID(ctx)).ID + application := a.DB.GetApplicationByToken(auth.GetTokenID(ctx)) + message.ApplicationID = application.ID + if strings.TrimSpace(message.Title) == "" { + message.Title = application.Name + } message.Date = timeNow() a.DB.CreateMessage(&message) a.Notifier.Notify(auth.GetUserID(ctx), &message) diff --git a/api/message_test.go b/api/message_test.go index 8ba49ca..64b7953 100644 --- a/api/message_test.go +++ b/api/message_test.go @@ -332,7 +332,7 @@ func (s *MessageSuite) Test_CreateMessage_onJson_allParams() { assert.True(s.T(), s.notified) } -func (s *MessageSuite) Test_CreateMessage_onlyRequired() { +func (s *MessageSuite) Test_CreateMessage_WithTitle() { t, _ := time.Parse("2006/01/02", "2017/01/02") timeNow = func() time.Time { return t } defer func() { timeNow = time.Now }() @@ -366,18 +366,36 @@ func (s *MessageSuite) Test_CreateMessage_failWhenNoMessage() { assert.False(s.T(), s.notified) } -func (s *MessageSuite) Test_CreateMessage_failWhenNoTitle() { +func (s *MessageSuite) Test_CreateMessage_WithoutTitle() { auth.RegisterAuthentication(s.ctx, nil, 4, "app-token") - s.db.User(4).AppWithToken(8, "app-token") + s.db.User(4).AppWithTokenAndName(8, "app-token", "Application name") s.ctx.Request = httptest.NewRequest("POST", "/token", strings.NewReader(`{"message": "mymessage"}`)) s.ctx.Request.Header.Set("Content-Type", "application/json") s.a.CreateMessage(s.ctx) - assert.Empty(s.T(), s.db.GetMessagesByApplication(8)) - assert.Equal(s.T(), 400, s.recorder.Code) - assert.False(s.T(), s.notified) + msgs := s.db.GetMessagesByApplication(8) + assert.Len(s.T(), msgs, 1) + assert.Equal(s.T(), "Application name", msgs[0].Title) + assert.Equal(s.T(), 200, s.recorder.Code) + assert.True(s.T(), s.notified) +} + +func (s *MessageSuite) Test_CreateMessage_WithBlankTitle() { + auth.RegisterAuthentication(s.ctx, nil, 4, "app-token") + s.db.User(4).AppWithTokenAndName(8, "app-token", "Application name") + + s.ctx.Request = httptest.NewRequest("POST", "/token", strings.NewReader(`{"message": "mymessage", "title": " "}`)) + s.ctx.Request.Header.Set("Content-Type", "application/json") + + s.a.CreateMessage(s.ctx) + + msgs := s.db.GetMessagesByApplication(8) + assert.Len(s.T(), msgs, 1) + assert.Equal(s.T(), "Application name", msgs[0].Title) + assert.Equal(s.T(), 200, s.recorder.Code) + assert.True(s.T(), s.notified) } func (s *MessageSuite) Test_CreateMessage_failWhenPriorityNotNumber() { diff --git a/docs/package.go b/docs/package.go index bfe0665..f77ab9e 100644 --- a/docs/package.go +++ b/docs/package.go @@ -16,7 +16,7 @@ // // Schemes: http, https // Host: localhost -// Version: 1.0.4 +// Version: 1.0.5 // License: MIT https://github.com/gotify/server/blob/master/LICENSE // // Consumes: diff --git a/docs/spec.json b/docs/spec.json index d1a5d31..4c792be 100644 --- a/docs/spec.json +++ b/docs/spec.json @@ -17,7 +17,7 @@ "name": "MIT", "url": "https://github.com/gotify/server/blob/master/LICENSE" }, - "version": "1.0.4" + "version": "1.0.5" }, "host": "localhost", "paths": { @@ -1255,7 +1255,6 @@ "id", "appid", "message", - "title", "date" ], "properties": { diff --git a/model/message.go b/model/message.go index 7607160..2821e20 100644 --- a/model/message.go +++ b/model/message.go @@ -27,9 +27,8 @@ type Message struct { Message string `form:"message" query:"message" json:"message" binding:"required"` // The title of the message. // - // required: true // example: Backup - Title string `form:"title" query:"title" json:"title" binding:"required"` + Title string `form:"title" query:"title" json:"title"` // The priority of the message. // // example: 2 diff --git a/test/database.go b/test/database.go index 9d75d92..0b48ae3 100644 --- a/test/database.go +++ b/test/database.go @@ -80,6 +80,19 @@ func (ab *AppClientBuilder) NewAppWithToken(id uint, token string) *model.Applic return application } +// AppWithTokenAndName creates an application with a token and name and returns a message builder. +func (ab *AppClientBuilder) AppWithTokenAndName(id uint, token, name string) *MessageBuilder { + ab.NewAppWithTokenAndName(id, token, name) + return &MessageBuilder{db: ab.db, appID: id} +} + +// NewAppWithTokenAndName creates an application with a token and name and returns the app. +func (ab *AppClientBuilder) NewAppWithTokenAndName(id uint, token, name string) *model.Application { + application := &model.Application{ID: id, UserID: ab.userID, Token: token, Name: name} + ab.db.CreateApplication(application) + return application +} + // Client creates a client and returns itself. func (ab *AppClientBuilder) Client(id uint) *AppClientBuilder { return ab.ClientWithToken(id, "client"+fmt.Sprint(id)) diff --git a/test/database_test.go b/test/database_test.go index 7ff22b4..9bc4b78 100644 --- a/test/database_test.go +++ b/test/database_test.go @@ -100,12 +100,20 @@ func (s *DatabaseSuite) Test_Apps() { userTwoExpected := []*model.Application{{ID: 5, Token: "app5", UserID: 2}} assert.Equal(s.T(), userTwoExpected, s.db.GetApplicationsByUser(2)) + newAppWithName := userBuilder.NewAppWithTokenAndName(7, "test-token", "app name") + newAppWithNameExpected := &model.Application{ID: 7, Token: "test-token", UserID: 1, Name: "app name"} + assert.Equal(s.T(), newAppWithNameExpected, newAppWithName) + + userBuilder.AppWithTokenAndName(8, "test-token-2", "app name") + s.db.AssertAppExist(1) s.db.AssertAppExist(2) s.db.AssertAppNotExist(3) s.db.AssertAppNotExist(4) s.db.AssertAppExist(5) s.db.AssertAppNotExist(6) + s.db.AssertAppExist(7) + s.db.AssertAppExist(8) s.db.DeleteApplicationByID(2)