Adjust token database impl
This commit is contained in:
parent
4078358aaa
commit
a10fe96ea7
|
|
@ -4,8 +4,18 @@ import (
|
||||||
"github.com/gotify/server/model"
|
"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.
|
// 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)
|
app := new(model.Application)
|
||||||
d.DB.Where("id = ?", id).Find(app)
|
d.DB.Where("id = ?", id).Find(app)
|
||||||
if app.ID == id {
|
if app.ID == id {
|
||||||
|
|
@ -20,7 +30,7 @@ func (d *GormDatabase) CreateApplication(application *model.Application) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteApplicationByID deletes an application by its id.
|
// 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
|
return d.DB.Where("id = ?", id).Delete(&model.Application{}).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *DatabaseSuite) TestApplication() {
|
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}}
|
user := &model.User{Name: "test", Pass: []byte{1}}
|
||||||
s.db.CreateUser(user)
|
s.db.CreateUser(user)
|
||||||
|
|
@ -15,14 +16,17 @@ func (s *DatabaseSuite) TestApplication() {
|
||||||
apps := s.db.GetApplicationsByUser(user.ID)
|
apps := s.db.GetApplicationsByUser(user.ID)
|
||||||
assert.Empty(s.T(), apps)
|
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)
|
s.db.CreateApplication(app)
|
||||||
|
|
||||||
apps = s.db.GetApplicationsByUser(user.ID)
|
apps = s.db.GetApplicationsByUser(user.ID)
|
||||||
assert.Len(s.T(), apps, 1)
|
assert.Len(s.T(), apps, 1)
|
||||||
assert.Contains(s.T(), apps, app)
|
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)
|
assert.Equal(s.T(), app, newApp)
|
||||||
|
|
||||||
s.db.DeleteApplicationByID(app.ID)
|
s.db.DeleteApplicationByID(app.ID)
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ package database
|
||||||
import "github.com/gotify/server/model"
|
import "github.com/gotify/server/model"
|
||||||
|
|
||||||
// GetClientByID returns the client for the given id or nil.
|
// 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)
|
client := new(model.Client)
|
||||||
d.DB.Where("id = ?", id).Find(client)
|
d.DB.Where("id = ?", id).Find(client)
|
||||||
if client.ID == id {
|
if client.ID == id {
|
||||||
|
|
@ -12,6 +12,16 @@ func (d *GormDatabase) GetClientByID(id string) *model.Client {
|
||||||
return nil
|
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.
|
// CreateClient creates a client.
|
||||||
func (d *GormDatabase) CreateClient(client *model.Client) error {
|
func (d *GormDatabase) CreateClient(client *model.Client) error {
|
||||||
return d.DB.Create(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.
|
// 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
|
return d.DB.Where("id = ?", id).Delete(&model.Client{}).Error
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *DatabaseSuite) TestClient() {
|
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}}
|
user := &model.User{Name: "test", Pass: []byte{1}}
|
||||||
s.db.CreateUser(user)
|
s.db.CreateUser(user)
|
||||||
|
|
@ -15,7 +16,7 @@ func (s *DatabaseSuite) TestClient() {
|
||||||
clients := s.db.GetClientsByUser(user.ID)
|
clients := s.db.GetClientsByUser(user.ID)
|
||||||
assert.Empty(s.T(), clients)
|
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)
|
s.db.CreateClient(client)
|
||||||
|
|
||||||
clients = s.db.GetClientsByUser(user.ID)
|
clients = s.db.GetClientsByUser(user.ID)
|
||||||
|
|
@ -25,6 +26,9 @@ func (s *DatabaseSuite) TestClient() {
|
||||||
newClient := s.db.GetClientByID(client.ID)
|
newClient := s.db.GetClientByID(client.ID)
|
||||||
assert.Equal(s.T(), client, newClient)
|
assert.Equal(s.T(), client, newClient)
|
||||||
|
|
||||||
|
newClient = s.db.GetClientByToken(client.Token)
|
||||||
|
assert.Equal(s.T(), client, newClient)
|
||||||
|
|
||||||
s.db.DeleteClientByID(client.ID)
|
s.db.DeleteClientByID(client.ID)
|
||||||
|
|
||||||
clients = s.db.GetClientsByUser(user.ID)
|
clients = s.db.GetClientsByUser(user.ID)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue