mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 16:24:03 +03:00
Add method for validation of the secret
This commit is contained in:
+2
-2
@@ -55,8 +55,8 @@ type Config struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) Validate() error {
|
func (c *Config) Validate() error {
|
||||||
if len(c.Secret.Key) == 0 || c.Secret.Host == "" {
|
if !c.Secret.Valid() {
|
||||||
return fmt.Errorf("incorrect secret %s", c.Secret.String())
|
return fmt.Errorf("invalid secret %s", c.Secret.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
+7
-3
@@ -22,11 +22,11 @@ type Secret struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s Secret) MarshalText() ([]byte, error) {
|
func (s Secret) MarshalText() ([]byte, error) {
|
||||||
if s.Key == secretEmptyKey {
|
if s.Valid() {
|
||||||
return nil, nil
|
return []byte(s.String()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return []byte(s.String()), nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Secret) UnmarshalText(data []byte) error {
|
func (s *Secret) UnmarshalText(data []byte) error {
|
||||||
@@ -72,6 +72,10 @@ func (s *Secret) UnmarshalText(data []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s Secret) Valid() bool {
|
||||||
|
return s.Key != secretEmptyKey && s.Host != ""
|
||||||
|
}
|
||||||
|
|
||||||
func (s Secret) String() string {
|
func (s Secret) String() string {
|
||||||
return s.Base64()
|
return s.Base64()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,6 +99,17 @@ func (suite *SecretTestSuite) TestInvariant() {
|
|||||||
suite.Equal("google.com", parsed.Host)
|
suite.Equal("google.com", parsed.Host)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (suite *SecretTestSuite) TestValid() {
|
||||||
|
s := mtglib.Secret{}
|
||||||
|
suite.False(s.Valid())
|
||||||
|
|
||||||
|
s.Key[0] = 1
|
||||||
|
suite.False(s.Valid())
|
||||||
|
|
||||||
|
s.Host = "11"
|
||||||
|
suite.True(s.Valid())
|
||||||
|
}
|
||||||
|
|
||||||
func TestSecret(t *testing.T) {
|
func TestSecret(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
suite.Run(t, &SecretTestSuite{})
|
suite.Run(t, &SecretTestSuite{})
|
||||||
|
|||||||
Reference in New Issue
Block a user