瀏覽代碼

Add method for validation of the secret

tags/v2.0.0-rc1
9seconds 5 年之前
父節點
當前提交
4daf927a72
共有 3 個文件被更改,包括 20 次插入5 次删除
  1. 2
    2
      config/config.go
  2. 7
    3
      mtglib/secret.go
  3. 11
    0
      mtglib/secret_test.go

+ 2
- 2
config/config.go 查看文件

55
 }
55
 }
56
 
56
 
57
 func (c *Config) Validate() error {
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
 	return nil
62
 	return nil

+ 7
- 3
mtglib/secret.go 查看文件

22
 }
22
 }
23
 
23
 
24
 func (s Secret) MarshalText() ([]byte, error) {
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
 func (s *Secret) UnmarshalText(data []byte) error {
32
 func (s *Secret) UnmarshalText(data []byte) error {
72
 	return nil
72
 	return nil
73
 }
73
 }
74
 
74
 
75
+func (s Secret) Valid() bool {
76
+	return s.Key != secretEmptyKey && s.Host != ""
77
+}
78
+
75
 func (s Secret) String() string {
79
 func (s Secret) String() string {
76
 	return s.Base64()
80
 	return s.Base64()
77
 }
81
 }

+ 11
- 0
mtglib/secret_test.go 查看文件

99
 	suite.Equal("google.com", parsed.Host)
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
 func TestSecret(t *testing.T) {
113
 func TestSecret(t *testing.T) {
103
 	t.Parallel()
114
 	t.Parallel()
104
 	suite.Run(t, &SecretTestSuite{})
115
 	suite.Run(t, &SecretTestSuite{})

Loading…
取消
儲存