From a10fe96ea7e9062dd1ea963c74a1d42c40505ad2 Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Sat, 10 Mar 2018 21:34:56 +0100 Subject: [PATCH] Adjust token database impl --- database/application.go | 14 ++++++++++++-- database/application_test.go | 10 +++++++--- database/client.go | 14 ++++++++++++-- database/client_test.go | 8 ++++++-- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/database/application.go b/database/application.go index 268a208..1291f13 100644 --- a/database/application.go +++ b/database/application.go @@ -4,8 +4,18 @@ import ( "github.com/gotify/server/model" ) +// GetApplicationByToken returns the application for the given token or nil. +func (d *GormDatabase) GetApplicationByToken(token string) *model.Application { + app := new(model.Application) + d.DB.Where("token = ?", token).Find(app) + if app.Token == token { + return app + } + return nil +} + // GetApplicationByID returns the application for the given id or nil. -func (d *GormDatabase) GetApplicationByID(id string) *model.Application { +func (d *GormDatabase) GetApplicationByID(id uint) *model.Application { app := new(model.Application) d.DB.Where("id = ?", id).Find(app) if app.ID == id { @@ -20,7 +30,7 @@ func (d *GormDatabase) CreateApplication(application *model.Application) error { } // DeleteApplicationByID deletes an application by its id. -func (d *GormDatabase) DeleteApplicationByID(id string) error { +func (d *GormDatabase) DeleteApplicationByID(id uint) error { return d.DB.Where("id = ?", id).Delete(&model.Application{}).Error } diff --git a/database/application_test.go b/database/application_test.go index 7e326e9..58a775c 100644 --- a/database/application_test.go +++ b/database/application_test.go @@ -6,7 +6,8 @@ import ( ) func (s *DatabaseSuite) TestApplication() { - assert.Nil(s.T(), s.db.GetApplicationByID("asdasdf"), "not existing app") + assert.Nil(s.T(), s.db.GetApplicationByToken("asdasdf"), "not existing app") + assert.Nil(s.T(), s.db.GetApplicationByID(uint(1)), "not existing app") user := &model.User{Name: "test", Pass: []byte{1}} s.db.CreateUser(user) @@ -15,14 +16,17 @@ func (s *DatabaseSuite) TestApplication() { apps := s.db.GetApplicationsByUser(user.ID) assert.Empty(s.T(), apps) - app := &model.Application{UserID: user.ID, ID: "C0000000000", Name: "backupserver"} + app := &model.Application{UserID: user.ID, Token: "C0000000000", Name: "backupserver"} s.db.CreateApplication(app) apps = s.db.GetApplicationsByUser(user.ID) assert.Len(s.T(), apps, 1) assert.Contains(s.T(), apps, app) - newApp := s.db.GetApplicationByID(app.ID) + newApp := s.db.GetApplicationByToken(app.Token) + assert.Equal(s.T(), app, newApp) + + newApp = s.db.GetApplicationByID(app.ID) assert.Equal(s.T(), app, newApp) s.db.DeleteApplicationByID(app.ID) diff --git a/database/client.go b/database/client.go index 24f0097..369cf18 100644 --- a/database/client.go +++ b/database/client.go @@ -3,7 +3,7 @@ package database import "github.com/gotify/server/model" // GetClientByID returns the client for the given id or nil. -func (d *GormDatabase) GetClientByID(id string) *model.Client { +func (d *GormDatabase) GetClientByID(id uint) *model.Client { client := new(model.Client) d.DB.Where("id = ?", id).Find(client) if client.ID == id { @@ -12,6 +12,16 @@ func (d *GormDatabase) GetClientByID(id string) *model.Client { return nil } +// GetClientByToken returns the client for the given token or nil. +func (d *GormDatabase) GetClientByToken(token string) *model.Client { + client := new(model.Client) + d.DB.Where("token = ?", token).Find(client) + if client.Token == token { + return client + } + return nil +} + // CreateClient creates a client. func (d *GormDatabase) CreateClient(client *model.Client) error { return d.DB.Create(client).Error @@ -25,6 +35,6 @@ func (d *GormDatabase) GetClientsByUser(userID uint) []*model.Client { } // DeleteClientByID deletes a client by its id. -func (d *GormDatabase) DeleteClientByID(id string) error { +func (d *GormDatabase) DeleteClientByID(id uint) error { return d.DB.Where("id = ?", id).Delete(&model.Client{}).Error } diff --git a/database/client_test.go b/database/client_test.go index 3e7441c..b309154 100644 --- a/database/client_test.go +++ b/database/client_test.go @@ -6,7 +6,8 @@ import ( ) func (s *DatabaseSuite) TestClient() { - assert.Nil(s.T(), s.db.GetClientByID("asdasdf"), "not existing client") + assert.Nil(s.T(), s.db.GetClientByID(1), "not existing client") + assert.Nil(s.T(), s.db.GetClientByToken("asdasd"), "not existing client") user := &model.User{Name: "test", Pass: []byte{1}} s.db.CreateUser(user) @@ -15,7 +16,7 @@ func (s *DatabaseSuite) TestClient() { clients := s.db.GetClientsByUser(user.ID) assert.Empty(s.T(), clients) - client := &model.Client{UserID: user.ID, ID: "C0000000000", Name: "android"} + client := &model.Client{UserID: user.ID, Token: "C0000000000", Name: "android"} s.db.CreateClient(client) clients = s.db.GetClientsByUser(user.ID) @@ -25,6 +26,9 @@ func (s *DatabaseSuite) TestClient() { newClient := s.db.GetClientByID(client.ID) assert.Equal(s.T(), client, newClient) + newClient = s.db.GetClientByToken(client.Token) + assert.Equal(s.T(), client, newClient) + s.db.DeleteClientByID(client.ID) clients = s.db.GetClientsByUser(user.ID)