From 42892f91e1695a0aaf1608b8146303d9d52947d9 Mon Sep 17 00:00:00 2001 From: tsmethurst Date: Thu, 2 Sep 2021 12:17:14 +0200 Subject: [PATCH] client_test --- internal/gtsmodel/client.go | 2 +- internal/validate/client_test.go | 102 +++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 internal/validate/client_test.go diff --git a/internal/gtsmodel/client.go b/internal/gtsmodel/client.go index de9bd569a..e924bd190 100644 --- a/internal/gtsmodel/client.go +++ b/internal/gtsmodel/client.go @@ -26,6 +26,6 @@ type Client struct { CreatedAt time.Time `validate:"-" bun:"type:timestamp,nullzero,notnull,default:current_timestamp"` // when was item created UpdatedAt time.Time `validate:"-" bun:"type:timestamp,nullzero,notnull,default:current_timestamp"` // when was item last updated Secret string `validate:"required,uuid" bun:",nullzero,notnull"` // secret generated when client was created - Domain string `validate:"required" bun:",nullzero,notnull"` // domain requested for client + Domain string `validate:"required,uri" bun:",nullzero,notnull"` // domain requested for client UserID string `validate:"omitempty,ulid" bun:"type:CHAR(26),nullzero"` // id of the user that this client acts on behalf of } diff --git a/internal/validate/client_test.go b/internal/validate/client_test.go new file mode 100644 index 000000000..d3c2466cf --- /dev/null +++ b/internal/validate/client_test.go @@ -0,0 +1,102 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package validate_test + +import ( + "testing" + "time" + + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/validate" +) + +func happyClient() *gtsmodel.Client { + return >smodel.Client{ + ID: "01FE91RJR88PSEEE30EV35QR8N", + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + Secret: "bd740cf1-024a-4e4d-8c39-866538f52fe6", + Domain: "oauth2redirect://com.keylesspalace.tusky/", + UserID: "01FEEDMF6C0QD589MRK7919Z0R", + } +} + +type ClientValidateTestSuite struct { + suite.Suite +} + +func (suite *ClientValidateTestSuite) TestValidateClientHappyPath() { + // no problem here + c := happyClient() + err := validate.Struct(*c) + suite.NoError(err) +} + +func (suite *ClientValidateTestSuite) TestValidateClientBadID() { + c := happyClient() + + c.ID = "" + err := validate.Struct(*c) + suite.EqualError(err, "Key: 'Client.ID' Error:Field validation for 'ID' failed on the 'required' tag") + + c.ID = "01FE96W293ZPRG9FQQP48HK8N001FE96W32AT24VYBGM12WN3GKB" + err = validate.Struct(*c) + suite.EqualError(err, "Key: 'Client.ID' Error:Field validation for 'ID' failed on the 'ulid' tag") +} + +func (suite *ClientValidateTestSuite) TestValidateClientNoCreatedAt() { + c := happyClient() + + c.CreatedAt = time.Time{} + err := validate.Struct(*c) + suite.NoError(err) +} + +func (suite *ClientValidateTestSuite) TestValidateClientDomain() { + c := happyClient() + + c.Domain = "invalid-uri" + err := validate.Struct(*c) + suite.EqualError(err, "Key: 'Client.Domain' Error:Field validation for 'Domain' failed on the 'uri' tag") + + c.Domain = "" + err = validate.Struct(*c) + suite.EqualError(err, "Key: 'Client.Domain' Error:Field validation for 'Domain' failed on the 'required' tag") + + c.Domain = "urn:ietf:wg:oauth:2.0:oob" + err = validate.Struct(*c) + suite.NoError(err) +} + +func (suite *ClientValidateTestSuite) TestValidateSecret() { + a := happyClient() + + a.Secret = "invalid-uuid" + err := validate.Struct(*a) + suite.EqualError(err, "Key: 'Client.Secret' Error:Field validation for 'Secret' failed on the 'uuid' tag") + + a.Secret = "" + err = validate.Struct(*a) + suite.EqualError(err, "Key: 'Client.Secret' Error:Field validation for 'Secret' failed on the 'required' tag") +} + +func TestClientValidateTestSuite(t *testing.T) { + suite.Run(t, new(ClientValidateTestSuite)) +}