[#71] Make title in message not necessary field

This commit is contained in:
Eugene Gavrilov 2018-11-13 01:40:01 +05:00 committed by Jannis Mattheis
parent d1c035d43a
commit 0ca18b817c
7 changed files with 54 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package api
import ( import (
"errors" "errors"
"strings"
"time" "time"
"strconv" "strconv"
@ -129,7 +130,11 @@ func (a *MessageAPI) DeleteMessage(ctx *gin.Context) {
func (a *MessageAPI) CreateMessage(ctx *gin.Context) { 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 application := a.DB.GetApplicationByToken(auth.GetTokenID(ctx))
message.ApplicationID = application.ID
if strings.TrimSpace(message.Title) == "" {
message.Title = application.Name
}
message.Date = timeNow() 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)

View File

@ -332,7 +332,7 @@ func (s *MessageSuite) Test_CreateMessage_onJson_allParams() {
assert.True(s.T(), s.notified) 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") t, _ := time.Parse("2006/01/02", "2017/01/02")
timeNow = func() time.Time { return t } timeNow = func() time.Time { return t }
defer func() { timeNow = time.Now }() defer func() { timeNow = time.Now }()
@ -366,18 +366,36 @@ func (s *MessageSuite) Test_CreateMessage_failWhenNoMessage() {
assert.False(s.T(), s.notified) 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") 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 = httptest.NewRequest("POST", "/token", strings.NewReader(`{"message": "mymessage"}`))
s.ctx.Request.Header.Set("Content-Type", "application/json") s.ctx.Request.Header.Set("Content-Type", "application/json")
s.a.CreateMessage(s.ctx) s.a.CreateMessage(s.ctx)
assert.Empty(s.T(), s.db.GetMessagesByApplication(8)) msgs := s.db.GetMessagesByApplication(8)
assert.Equal(s.T(), 400, s.recorder.Code) assert.Len(s.T(), msgs, 1)
assert.False(s.T(), s.notified) 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() { func (s *MessageSuite) Test_CreateMessage_failWhenPriorityNotNumber() {

View File

@ -16,7 +16,7 @@
// //
// Schemes: http, https // Schemes: http, https
// Host: localhost // Host: localhost
// Version: 1.0.4 // Version: 1.0.5
// License: MIT https://github.com/gotify/server/blob/master/LICENSE // License: MIT https://github.com/gotify/server/blob/master/LICENSE
// //
// Consumes: // Consumes:

View File

@ -17,7 +17,7 @@
"name": "MIT", "name": "MIT",
"url": "https://github.com/gotify/server/blob/master/LICENSE" "url": "https://github.com/gotify/server/blob/master/LICENSE"
}, },
"version": "1.0.4" "version": "1.0.5"
}, },
"host": "localhost", "host": "localhost",
"paths": { "paths": {
@ -1255,7 +1255,6 @@
"id", "id",
"appid", "appid",
"message", "message",
"title",
"date" "date"
], ],
"properties": { "properties": {

View File

@ -27,9 +27,8 @@ type Message struct {
Message string `form:"message" query:"message" json:"message" binding:"required"` Message string `form:"message" query:"message" json:"message" binding:"required"`
// The title of the message. // The title of the message.
// //
// required: true
// example: Backup // 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. // The priority of the message.
// //
// example: 2 // example: 2

View File

@ -80,6 +80,19 @@ func (ab *AppClientBuilder) NewAppWithToken(id uint, token string) *model.Applic
return application 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. // Client creates a client and returns itself.
func (ab *AppClientBuilder) Client(id uint) *AppClientBuilder { func (ab *AppClientBuilder) Client(id uint) *AppClientBuilder {
return ab.ClientWithToken(id, "client"+fmt.Sprint(id)) return ab.ClientWithToken(id, "client"+fmt.Sprint(id))

View File

@ -100,12 +100,20 @@ func (s *DatabaseSuite) Test_Apps() {
userTwoExpected := []*model.Application{{ID: 5, Token: "app5", UserID: 2}} userTwoExpected := []*model.Application{{ID: 5, Token: "app5", UserID: 2}}
assert.Equal(s.T(), userTwoExpected, s.db.GetApplicationsByUser(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(1)
s.db.AssertAppExist(2) s.db.AssertAppExist(2)
s.db.AssertAppNotExist(3) s.db.AssertAppNotExist(3)
s.db.AssertAppNotExist(4) s.db.AssertAppNotExist(4)
s.db.AssertAppExist(5) s.db.AssertAppExist(5)
s.db.AssertAppNotExist(6) s.db.AssertAppNotExist(6)
s.db.AssertAppExist(7)
s.db.AssertAppExist(8)
s.db.DeleteApplicationByID(2) s.db.DeleteApplicationByID(2)