Skip to content
Open
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
2 changes: 1 addition & 1 deletion database/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (d *GormDatabase) GetApplicationsByUser(userID uint) ([]*model.Application,

// UpdateApplication updates an application.
func (d *GormDatabase) UpdateApplication(app *model.Application) error {
return d.DB.Save(app).Error
return d.updateExisting(app)
}

// UpdateApplicationTokenLastUsed updates the last used time of the application token.
Expand Down
14 changes: 14 additions & 0 deletions database/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/gotify/server/v3/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func (s *DatabaseSuite) TestApplication() {
Expand Down Expand Up @@ -99,3 +100,16 @@ func (s *DatabaseSuite) TestDeleteAppDeletesMessages() {
assert.NotEmpty(s.T(), msgs)
}
}

func (s *DatabaseSuite) TestUpdateDeletedApplicationDoesNotRecreateIt() {
app := &model.Application{UserID: 1, Token: "ASTALE", Name: "backup"}
require.NoError(s.T(), s.db.CreateApplication(app))
require.NoError(s.T(), s.db.DeleteApplicationByID(app.ID))

app.Name = "renamed"
assert.NoError(s.T(), s.db.UpdateApplication(app))

if a, err := s.db.GetApplicationByID(app.ID); assert.NoError(s.T(), err) {
assert.Nil(s.T(), a)
}
}
4 changes: 2 additions & 2 deletions database/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (d *GormDatabase) DeleteClientByID(id uint) error {
// UpdateClient updates a client.
func (d *GormDatabase) UpdateClient(client *model.Client) error {
client.PopulateExpiresAt()
return d.DB.Save(client).Error
return d.updateExisting(client)
}

// UpdateClientTokensLastUsed updates the last used timestamp of clients and
Expand All @@ -74,7 +74,7 @@ func (d *GormDatabase) UpdateClientTokensLastUsedAndExpiresAt(tokens []string, t
for _, c := range clients {
c.LastUsed = t
c.PopulateExpiresAt()
if err := tx.Save(c).Error; err != nil {
if err := tx.Model(c).Select("last_used", "expires_at").Updates(c).Error; err != nil {
return err
}
}
Expand Down
17 changes: 17 additions & 0 deletions database/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/gotify/server/v3/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func (s *DatabaseSuite) TestClient() {
Expand Down Expand Up @@ -194,3 +195,19 @@ func (s *DatabaseSuite) TestClientSelectHidesExpired() {
assert.Equal(s.T(), valid.Token, cs[0].Token)
}
}

func (s *DatabaseSuite) TestUpdateDeletedClientDoesNotRecreateIt() {
user := &model.User{Name: "stale", Pass: []byte{1}}
require.NoError(s.T(), s.db.CreateUser(user))

client := &model.Client{UserID: user.ID, Token: "CSTALE", Name: "android"}
require.NoError(s.T(), s.db.CreateClient(client))
require.NoError(s.T(), s.db.DeleteClientByID(client.ID))

client.Name = "renamed"
assert.NoError(s.T(), s.db.UpdateClient(client))

if c, err := s.db.GetClientByID(client.ID); assert.NoError(s.T(), err) {
assert.Nil(s.T(), c)
}
}
4 changes: 4 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ func New(dialect, connection, defaultUser, defaultPass string, strength int, cre
return &GormDatabase{DB: db}, nil
}

func (d *GormDatabase) updateExisting(value any) error {
return d.DB.Select("*").Updates(value).Error
}

func fillMissingCreatedAt(db *gorm.DB, now time.Time) error {
models := []any{
new(model.User),
Expand Down
2 changes: 1 addition & 1 deletion database/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (d *GormDatabase) GetPluginConfByID(id uint) (*model.PluginConf, error) {

// UpdatePluginConf updates plugin configuration.
func (d *GormDatabase) UpdatePluginConf(p *model.PluginConf) error {
return d.DB.Save(p).Error
return d.updateExisting(p)
}

// DeletePluginConfByID deletes a plugin configuration by its id.
Expand Down
13 changes: 13 additions & 0 deletions database/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,16 @@ func (s *DatabaseSuite) TestPluginConf() {
assert.Equal(s.T(), false, pluginConf.Enabled)
assert.Equal(s.T(), testConf, string(pluginConf.Config))
}

func (s *DatabaseSuite) TestUpdateDeletedPluginConfDoesNotRecreateIt() {
conf := &model.PluginConf{UserID: 1, ModulePath: "github.com/gotify/stale", Token: "PSTALE"}
require.NoError(s.T(), s.db.CreatePluginConf(conf))
require.NoError(s.T(), s.db.DeletePluginConfByID(conf.ID))

conf.Enabled = true
assert.NoError(s.T(), s.db.UpdatePluginConf(conf))

if c, err := s.db.GetPluginConfByToken(conf.Token); assert.NoError(s.T(), err) {
assert.Nil(s.T(), c)
}
}
2 changes: 1 addition & 1 deletion database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (d *GormDatabase) DeleteUserByID(id uint) error {

// UpdateUser updates a user.
func (d *GormDatabase) UpdateUser(user *model.User) error {
return d.DB.Save(user).Error
return d.updateExisting(user)
}

// CreateUser creates a user.
Expand Down
13 changes: 13 additions & 0 deletions database/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,16 @@ func (s *DatabaseSuite) TestDeleteUserDeletesApplicationsAndClientsAndPluginConf
require.NoError(s.T(), err)
assert.NotNil(s.T(), msg)
}

func (s *DatabaseSuite) TestUpdateDeletedUserDoesNotRecreateIt() {
user := &model.User{Name: "stale", Pass: []byte{1}}
require.NoError(s.T(), s.db.CreateUser(user))
require.NoError(s.T(), s.db.DeleteUserByID(user.ID))

user.Admin = true
assert.NoError(s.T(), s.db.UpdateUser(user))

if u, err := s.db.GetUserByID(user.ID); assert.NoError(s.T(), err) {
assert.Nil(s.T(), u)
}
}
Loading