Skip to content

Commit

Permalink
Merge pull request #560 from mateuscelio/master
Browse files Browse the repository at this point in the history
Adds ClientParams strcut to handle creation and update params
  • Loading branch information
jmattheis committed Apr 28, 2023
2 parents 9d4e37a + f3d121b commit a18970e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
33 changes: 25 additions & 8 deletions api/client.go
Expand Up @@ -25,6 +25,19 @@ type ClientAPI struct {
NotifyDeleted func(uint, string)
}

// Client Params Model
//
// Params allowed to create or update Clients
//
// swagger:model ClientParams
type ClientParams struct {
// The client name
//
// required: true
// example: My Client
Name string `form:"name" query:"name" json:"name" binding:"required"`
}

// UpdateClient updates a client by its id.
// swagger:operation PUT /client/{id} client updateClient
//
Expand All @@ -40,7 +53,7 @@ type ClientAPI struct {
// description: the client to update
// required: true
// schema:
// $ref: "#/definitions/Client"
// $ref: "#/definitions/ClientParams"
// - name: id
// in: path
// description: the client id
Expand Down Expand Up @@ -75,8 +88,8 @@ func (a *ClientAPI) UpdateClient(ctx *gin.Context) {
return
}
if client != nil && client.UserID == auth.GetUserID(ctx) {
newValues := &model.Client{}
if err := ctx.Bind(newValues); err == nil {
newValues := ClientParams{}
if err := ctx.Bind(&newValues); err == nil {
client.Name = newValues.Name

if success := successOrAbort(ctx, 500, a.DB.UpdateClient(client)); !success {
Expand Down Expand Up @@ -105,7 +118,7 @@ func (a *ClientAPI) UpdateClient(ctx *gin.Context) {
// description: the client to add
// required: true
// schema:
// $ref: "#/definitions/Client"
// $ref: "#/definitions/ClientParams"
// responses:
// 200:
// description: Ok
Expand All @@ -124,10 +137,14 @@ func (a *ClientAPI) UpdateClient(ctx *gin.Context) {
// schema:
// $ref: "#/definitions/Error"
func (a *ClientAPI) CreateClient(ctx *gin.Context) {
client := model.Client{}
if err := ctx.Bind(&client); err == nil {
client.Token = auth.GenerateNotExistingToken(generateClientToken, a.clientExists)
client.UserID = auth.GetUserID(ctx)
clientParams := ClientParams{}
if err := ctx.Bind(&clientParams); err == nil {
client := model.Client{
Name: clientParams.Name,
Token: auth.GenerateNotExistingToken(generateClientToken, a.clientExists),
UserID: auth.GetUserID(ctx),
}

if success := successOrAbort(ctx, 500, a.DB.CreateClient(&client)); !success {
return
}
Expand Down
15 changes: 15 additions & 0 deletions api/client_test.go
Expand Up @@ -76,6 +76,21 @@ func (s *ClientSuite) Test_CreateClient_mapAllParameters() {
}
}

func (s *ClientSuite) Test_CreateClient_ignoresReadOnlyPropertiesInParams() {
s.db.User(5)
test.WithUser(s.ctx, 5)

s.withFormData("name=myclient&ID=45&Token=12341234&UserID=333")

s.a.CreateClient(s.ctx)
expected := &model.Client{ID: 1, UserID: 5, Token: firstClientToken, Name: "myclient"}

assert.Equal(s.T(), 200, s.recorder.Code)
if clients, err := s.db.GetClientsByUser(5); assert.NoError(s.T(), err) {
assert.Contains(s.T(), clients, expected)
}
}

func (s *ClientSuite) Test_CreateClient_expectBadRequestOnEmptyName() {
s.db.User(5)

Expand Down
21 changes: 19 additions & 2 deletions docs/spec.json
Expand Up @@ -599,7 +599,7 @@
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/Client"
"$ref": "#/definitions/ClientParams"
}
}
],
Expand Down Expand Up @@ -665,7 +665,7 @@
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/Client"
"$ref": "#/definitions/ClientParams"
}
},
{
Expand Down Expand Up @@ -2092,6 +2092,23 @@
},
"x-go-package": "github.com/gotify/server/v2/model"
},
"ClientParams": {
"description": "Params allowed to create or update Clients",
"type": "object",
"title": "Client Params Model",
"required": [
"name"
],
"properties": {
"name": {
"description": "The client name",
"type": "string",
"x-go-name": "Name",
"example": "My Client"
}
},
"x-go-package": "github.com/gotify/server/v2/api"
},
"CreateUserExternal": {
"description": "Used for user creation.",
"type": "object",
Expand Down

0 comments on commit a18970e

Please sign in to comment.