forked from 1Password/shell-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_secrets_test.go
165 lines (143 loc) · 4.11 KB
/
example_secrets_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package plugintest
import (
"fmt"
"regexp"
"strings"
"testing"
"github.com/1Password/shell-plugins/sdk/schema"
"github.com/stretchr/testify/assert"
)
func TestSecretContainsSuffix(t *testing.T) {
v := schema.ValueComposition{
Length: 10,
Charset: schema.Charset{
Uppercase: true,
},
}
result := ExampleSecretFromComposition(v)
hasExampleSuffix := strings.HasSuffix(result, secretExampleSuffix)
assert.Equal(t, true, hasExampleSuffix, fmt.Sprintf("should contain %s suffix", secretExampleSuffix))
}
func TestSecretContainsLowercaseSuffix(t *testing.T) {
v := schema.ValueComposition{
Length: 10,
Charset: schema.Charset{
Lowercase: true,
},
}
result := ExampleSecretFromComposition(v)
hasExampleSuffix := strings.HasSuffix(result, strings.ToLower(secretExampleSuffix))
assert.Equal(t, true, hasExampleSuffix, fmt.Sprintf("should contain lowercase %s suffix", secretExampleSuffix))
}
func TestSecretHasNoSuffix(t *testing.T) {
cases := map[string]struct {
secretLength int
charset schema.Charset
}{
"when secret length equal suffix length": {
secretLength: len(secretExampleSuffix),
charset: schema.Charset{Uppercase: true},
},
"when secret length less than suffix length": {
secretLength: len(secretExampleSuffix) - 1,
charset: schema.Charset{Uppercase: true},
},
"when no Uppercase or Lowercase charset": {
secretLength: len(secretExampleSuffix) + 1,
charset: schema.Charset{Symbols: true},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
v := schema.ValueComposition{
Length: tc.secretLength,
Charset: tc.charset,
}
result := ExampleSecretFromComposition(v)
hasNoSuffix := !strings.HasSuffix(result, secretExampleSuffix)
assert.Equal(t, true, hasNoSuffix, fmt.Sprintln("should not has suffix"))
})
}
}
func TestSecretContainsDigitsOnly(t *testing.T) {
v := schema.ValueComposition{
Length: 10,
Charset: schema.Charset{
Digits: true,
},
}
result := ExampleSecretFromComposition(v)
hasDigitsOnly, _ := containsOnlyDigits(result)
assert.Equal(t, true, hasDigitsOnly, fmt.Sprintln("should contain digits only"))
}
func TestSecretContainsPrefix(t *testing.T) {
prefix := "ABC"
v := schema.ValueComposition{
Length: 10,
Prefix: prefix,
Charset: schema.Charset{
Uppercase: true,
},
}
result := ExampleSecretFromComposition(v)
hasExampleSuffix := strings.HasPrefix(result, prefix)
assert.Equal(t, true, hasExampleSuffix, fmt.Sprintf("should contain %s prefix", prefix))
}
func TestSecretWithExpectedLength(t *testing.T) {
expectedLength := 20
v := schema.ValueComposition{
Length: expectedLength,
Charset: schema.Charset{
Uppercase: true,
},
}
result := ExampleSecretFromComposition(v)
assert.Equal(t, expectedLength, len(result), fmt.Sprintf("should have %d chars length", expectedLength))
}
func TestStingFromCharsetReturnErrorWhenNoCharsetProvided(t *testing.T) {
_, err := stringFromCharset(10, "")
if err == nil {
t.FailNow()
}
}
func TestStringFromCharsetContainsOnly(t *testing.T) {
cases := map[string]struct {
charset schema.Charset
containsFunc func(str string) (bool, error)
}{
"capital letters": {
charset: schema.Charset{Uppercase: true},
containsFunc: containsOnlyCapitalLetters,
},
"lowercase letters": {
charset: schema.Charset{Lowercase: true},
containsFunc: containsOnlyLowercaseLetters,
},
"digits": {
charset: schema.Charset{Digits: true},
containsFunc: containsOnlyDigits,
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
stringLength := 20
charset := charsToUse(tc.charset)
result, _ := stringFromCharset(stringLength, charset)
hasOnly, err := tc.containsFunc(result)
if err != nil {
t.Log(err)
t.FailNow()
}
assert.Equal(t, true, hasOnly)
})
}
}
func containsOnlyCapitalLetters(str string) (bool, error) {
return regexp.Match("^[A-Z]+$", []byte(str))
}
func containsOnlyLowercaseLetters(str string) (bool, error) {
return regexp.Match("^[a-z]+$", []byte(str))
}
func containsOnlyDigits(str string) (bool, error) {
return regexp.Match("^[0-9]+$", []byte(str))
}