| 1 |
package flags |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"flag" |
| 6 |
"fmt" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/smallstep/assert" |
| 10 |
"github.com/urfave/cli" |
| 11 |
) |
| 12 |
|
| 13 |
func TestParseCaURL(t *testing.T) { |
| 14 |
|
| 15 |
app := &cli.App{} |
| 16 |
set := flag.NewFlagSet("contrive", 0) |
| 17 |
_ = set.String("ca-url", "", "") |
| 18 |
ctx := cli.NewContext(app, set, nil) |
| 19 |
|
| 20 |
type test struct { |
| 21 |
name, caURL, ret string |
| 22 |
err error |
| 23 |
} |
| 24 |
tests := []test{ |
| 25 |
{name: "fail/empty", caURL: "", ret: "", err: errors.New("' ' requires the '--ca-url' flag")}, |
| 26 |
{name: "fail/badCaURL", caURL: "git://[email protected]", ret: "", err: errors.New("invalid value 'git://[email protected]' for flag '--ca-url'; must have https scheme")}, |
| 27 |
{name: "ok", caURL: "https://ca.smallstep.com:8080", ret: "https://ca.smallstep.com:8080"}, |
| 28 |
} |
| 29 |
for _, tc := range tests { |
| 30 |
t.Run(tc.name, func(t *testing.T) { |
| 31 |
ctx.Set("ca-url", tc.caURL) |
| 32 |
ret, err := ParseCaURL(ctx) |
| 33 |
if err != nil && assert.NotNil(t, tc.err, fmt.Sprintf("expected no error but got <%s>", err)) { |
| 34 |
assert.HasPrefix(t, err.Error(), tc.err.Error()) |
| 35 |
} else if assert.Nil(t, tc.err, fmt.Sprintf("expected error <%s> but got nil", tc.err)) { |
| 36 |
assert.Equals(t, ret, tc.ret) |
| 37 |
} |
| 38 |
}) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func TestParseCaURLIfExists(t *testing.T) { |
| 43 |
|
| 44 |
app := &cli.App{} |
| 45 |
set := flag.NewFlagSet("contrive", 0) |
| 46 |
_ = set.String("ca-url", "", "") |
| 47 |
ctx := cli.NewContext(app, set, nil) |
| 48 |
|
| 49 |
type test struct { |
| 50 |
name, caURL, ret string |
| 51 |
err error |
| 52 |
} |
| 53 |
tests := []test{ |
| 54 |
{name: "fail/badCaURL", caURL: "git://[email protected]", ret: "", err: errors.New("invalid value 'git://[email protected]' for flag '--ca-url'; must have https scheme")}, |
| 55 |
{name: "ok/empty", caURL: "", ret: ""}, |
| 56 |
{name: "ok", caURL: "https://ca.smallstep.com:8080", ret: "https://ca.smallstep.com:8080"}, |
| 57 |
} |
| 58 |
for _, tc := range tests { |
| 59 |
t.Run(tc.name, func(t *testing.T) { |
| 60 |
ctx.Set("ca-url", tc.caURL) |
| 61 |
ret, err := ParseCaURLIfExists(ctx) |
| 62 |
if err != nil && assert.NotNil(t, tc.err, fmt.Sprintf("expected no error but got <%s>", err)) { |
| 63 |
assert.HasPrefix(t, err.Error(), tc.err.Error()) |
| 64 |
} else if assert.Nil(t, tc.err, fmt.Sprintf("expected error <%s> but got nil", tc.err)) { |
| 65 |
assert.Equals(t, ret, tc.ret) |
| 66 |
} |
| 67 |
}) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
func Test_parseCaURL(t *testing.T) { |
| 72 |
|
| 73 |
app := &cli.App{} |
| 74 |
set := flag.NewFlagSet("contrive", 0) |
| 75 |
_ = set.String("ca-url", "", "") |
| 76 |
ctx := cli.NewContext(app, set, nil) |
| 77 |
|
| 78 |
type test struct { |
| 79 |
name, caURL, ret string |
| 80 |
err error |
| 81 |
} |
| 82 |
tests := []test{ |
| 83 |
{name: "fail/invalidURL", caURL: "#$%@#://&%^&%#$^#$", ret: "", err: errors.New("invalid value '#$%@#://&%^&%#$^#$' for flag '--ca-url'; invalid URL")}, |
| 84 |
{name: "fail/invalidScheme-git", caURL: "git://[email protected]", ret: "", err: errors.New("invalid value 'git://[email protected]' for flag '--ca-url'; must have https scheme")}, |
| 85 |
{name: "fail/invalidScheme-http", caURL: "http://ca.smallstep.com:8080", ret: "", err: errors.New("invalid value 'http://ca.smallstep.com:8080' for flag '--ca-url'; must have https scheme")}, |
| 86 |
{name: "ok", caURL: "https://ca.smallstep.com:8080", ret: "https://ca.smallstep.com:8080"}, |
| 87 |
{name: "ok/provide-scheme", caURL: "ca.smallstep.com:8080", ret: "https://ca.smallstep.com:8080"}, |
| 88 |
} |
| 89 |
for _, tc := range tests { |
| 90 |
t.Run(tc.name, func(t *testing.T) { |
| 91 |
ret, err := parseCaURL(ctx, tc.caURL) |
| 92 |
if err != nil && assert.NotNil(t, tc.err, fmt.Sprintf("expected no error but got <%s>", err)) { |
| 93 |
assert.HasPrefix(t, err.Error(), tc.err.Error()) |
| 94 |
} else if assert.Nil(t, tc.err, fmt.Sprintf("expected error <%s> but got nil", tc.err)) { |
| 95 |
assert.Equals(t, ret, tc.ret) |
| 96 |
} |
| 97 |
}) |
| 98 |
} |
| 99 |
} |