Przeglądaj źródła

Add method for validation of the secret

tags/v2.0.0-rc1
9seconds 5 lat temu
rodzic
commit
4daf927a72
3 zmienionych plików z 20 dodań i 5 usunięć
  1. 2
    2
      config/config.go
  2. 7
    3
      mtglib/secret.go
  3. 11
    0
      mtglib/secret_test.go

+ 2
- 2
config/config.go Wyświetl plik

@@ -55,8 +55,8 @@ type Config struct {
55 55
 }
56 56
 
57 57
 func (c *Config) Validate() error {
58
-	if len(c.Secret.Key) == 0 || c.Secret.Host == "" {
59
-		return fmt.Errorf("incorrect secret %s", c.Secret.String())
58
+	if !c.Secret.Valid() {
59
+		return fmt.Errorf("invalid secret %s", c.Secret.String())
60 60
 	}
61 61
 
62 62
 	return nil

+ 7
- 3
mtglib/secret.go Wyświetl plik

@@ -22,11 +22,11 @@ type Secret struct {
22 22
 }
23 23
 
24 24
 func (s Secret) MarshalText() ([]byte, error) {
25
-	if s.Key == secretEmptyKey {
26
-		return nil, nil
25
+	if s.Valid() {
26
+		return []byte(s.String()), nil
27 27
 	}
28 28
 
29
-	return []byte(s.String()), nil
29
+	return nil, nil
30 30
 }
31 31
 
32 32
 func (s *Secret) UnmarshalText(data []byte) error {
@@ -72,6 +72,10 @@ func (s *Secret) UnmarshalText(data []byte) error {
72 72
 	return nil
73 73
 }
74 74
 
75
+func (s Secret) Valid() bool {
76
+	return s.Key != secretEmptyKey && s.Host != ""
77
+}
78
+
75 79
 func (s Secret) String() string {
76 80
 	return s.Base64()
77 81
 }

+ 11
- 0
mtglib/secret_test.go Wyświetl plik

@@ -99,6 +99,17 @@ func (suite *SecretTestSuite) TestInvariant() {
99 99
 	suite.Equal("google.com", parsed.Host)
100 100
 }
101 101
 
102
+func (suite *SecretTestSuite) TestValid() {
103
+	s := mtglib.Secret{}
104
+	suite.False(s.Valid())
105
+
106
+	s.Key[0] = 1
107
+	suite.False(s.Valid())
108
+
109
+	s.Host = "11"
110
+	suite.True(s.Valid())
111
+}
112
+
102 113
 func TestSecret(t *testing.T) {
103 114
 	t.Parallel()
104 115
 	suite.Run(t, &SecretTestSuite{})

Ładowanie…
Anuluj
Zapisz