Merge pull request #560 from mateuscelio/master

Adds ClientParams strcut to handle creation and update params
This commit is contained in:
Jannis Mattheis 2023-04-28 16:22:57 +02:00 committed by GitHub
commit a18970ea19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 10 deletions

View File

@ -25,6 +25,19 @@ type ClientAPI struct {
NotifyDeleted func(uint, string) 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. // UpdateClient updates a client by its id.
// swagger:operation PUT /client/{id} client updateClient // swagger:operation PUT /client/{id} client updateClient
// //
@ -40,7 +53,7 @@ type ClientAPI struct {
// description: the client to update // description: the client to update
// required: true // required: true
// schema: // schema:
// $ref: "#/definitions/Client" // $ref: "#/definitions/ClientParams"
// - name: id // - name: id
// in: path // in: path
// description: the client id // description: the client id
@ -75,8 +88,8 @@ func (a *ClientAPI) UpdateClient(ctx *gin.Context) {
return return
} }
if client != nil && client.UserID == auth.GetUserID(ctx) { if client != nil && client.UserID == auth.GetUserID(ctx) {
newValues := &model.Client{} newValues := ClientParams{}
if err := ctx.Bind(newValues); err == nil { if err := ctx.Bind(&newValues); err == nil {
client.Name = newValues.Name client.Name = newValues.Name
if success := successOrAbort(ctx, 500, a.DB.UpdateClient(client)); !success { if success := successOrAbort(ctx, 500, a.DB.UpdateClient(client)); !success {
@ -105,7 +118,7 @@ func (a *ClientAPI) UpdateClient(ctx *gin.Context) {
// description: the client to add // description: the client to add
// required: true // required: true
// schema: // schema:
// $ref: "#/definitions/Client" // $ref: "#/definitions/ClientParams"
// responses: // responses:
// 200: // 200:
// description: Ok // description: Ok
@ -124,10 +137,14 @@ func (a *ClientAPI) UpdateClient(ctx *gin.Context) {
// schema: // schema:
// $ref: "#/definitions/Error" // $ref: "#/definitions/Error"
func (a *ClientAPI) CreateClient(ctx *gin.Context) { func (a *ClientAPI) CreateClient(ctx *gin.Context) {
client := model.Client{} clientParams := ClientParams{}
if err := ctx.Bind(&client); err == nil { if err := ctx.Bind(&clientParams); err == nil {
client.Token = auth.GenerateNotExistingToken(generateClientToken, a.clientExists) client := model.Client{
client.UserID = auth.GetUserID(ctx) Name: clientParams.Name,
Token: auth.GenerateNotExistingToken(generateClientToken, a.clientExists),
UserID: auth.GetUserID(ctx),
}
if success := successOrAbort(ctx, 500, a.DB.CreateClient(&client)); !success { if success := successOrAbort(ctx, 500, a.DB.CreateClient(&client)); !success {
return return
} }

View File

@ -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() { func (s *ClientSuite) Test_CreateClient_expectBadRequestOnEmptyName() {
s.db.User(5) s.db.User(5)

View File

@ -599,7 +599,7 @@
"in": "body", "in": "body",
"required": true, "required": true,
"schema": { "schema": {
"$ref": "#/definitions/Client" "$ref": "#/definitions/ClientParams"
} }
} }
], ],
@ -665,7 +665,7 @@
"in": "body", "in": "body",
"required": true, "required": true,
"schema": { "schema": {
"$ref": "#/definitions/Client" "$ref": "#/definitions/ClientParams"
} }
}, },
{ {
@ -2092,6 +2092,23 @@
}, },
"x-go-package": "github.com/gotify/server/v2/model" "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": { "CreateUserExternal": {
"description": "Used for user creation.", "description": "Used for user creation.",
"type": "object", "type": "object",