File: flags_test.go

package info (click to toggle)
golang-github-smallstep-cli 0.15.16%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,404 kB
  • sloc: sh: 512; makefile: 99
file content (99 lines) | stat: -rw-r--r-- 3,689 bytes parent folder | download | duplicates (2)
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
package flags

import (
	"errors"
	"flag"
	"fmt"
	"testing"

	"github.com/smallstep/assert"
	"github.com/urfave/cli"
)

func TestParseCaURL(t *testing.T) {
	// This is just to get a simple CLI context
	app := &cli.App{}
	set := flag.NewFlagSet("contrive", 0)
	_ = set.String("ca-url", "", "")
	ctx := cli.NewContext(app, set, nil)

	type test struct {
		name, caURL, ret string
		err              error
	}
	tests := []test{
		{name: "fail/empty", caURL: "", ret: "", err: errors.New("' ' requires the '--ca-url' flag")},
		{name: "fail/badCaURL", caURL: "git://git@github.com", ret: "", err: errors.New("invalid value 'git://git@github.com' for flag '--ca-url'; must have https scheme")},
		{name: "ok", caURL: "https://ca.smallstep.com:8080", ret: "https://ca.smallstep.com:8080"},
	}
	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			ctx.Set("ca-url", tc.caURL)
			ret, err := ParseCaURL(ctx)
			if err != nil && assert.NotNil(t, tc.err, fmt.Sprintf("expected no error but got <%s>", err)) {
				assert.HasPrefix(t, err.Error(), tc.err.Error())
			} else if assert.Nil(t, tc.err, fmt.Sprintf("expected error <%s> but got nil", tc.err)) {
				assert.Equals(t, ret, tc.ret)
			}
		})
	}
}

func TestParseCaURLIfExists(t *testing.T) {
	// This is just to get a simple CLI context
	app := &cli.App{}
	set := flag.NewFlagSet("contrive", 0)
	_ = set.String("ca-url", "", "")
	ctx := cli.NewContext(app, set, nil)

	type test struct {
		name, caURL, ret string
		err              error
	}
	tests := []test{
		{name: "fail/badCaURL", caURL: "git://git@github.com", ret: "", err: errors.New("invalid value 'git://git@github.com' for flag '--ca-url'; must have https scheme")},
		{name: "ok/empty", caURL: "", ret: ""},
		{name: "ok", caURL: "https://ca.smallstep.com:8080", ret: "https://ca.smallstep.com:8080"},
	}
	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			ctx.Set("ca-url", tc.caURL)
			ret, err := ParseCaURLIfExists(ctx)
			if err != nil && assert.NotNil(t, tc.err, fmt.Sprintf("expected no error but got <%s>", err)) {
				assert.HasPrefix(t, err.Error(), tc.err.Error())
			} else if assert.Nil(t, tc.err, fmt.Sprintf("expected error <%s> but got nil", tc.err)) {
				assert.Equals(t, ret, tc.ret)
			}
		})
	}
}

func Test_parseCaURL(t *testing.T) {
	// This is just to get a simple CLI context
	app := &cli.App{}
	set := flag.NewFlagSet("contrive", 0)
	_ = set.String("ca-url", "", "")
	ctx := cli.NewContext(app, set, nil)

	type test struct {
		name, caURL, ret string
		err              error
	}
	tests := []test{
		{name: "fail/invalidURL", caURL: "#$%@#://&%^&%#$^#$", ret: "", err: errors.New("invalid value '#$%@#://&%^&%#$^#$' for flag '--ca-url'; invalid URL")},
		{name: "fail/invalidScheme-git", caURL: "git://git@github.com", ret: "", err: errors.New("invalid value 'git://git@github.com' for flag '--ca-url'; must have https scheme")},
		{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")},
		{name: "ok", caURL: "https://ca.smallstep.com:8080", ret: "https://ca.smallstep.com:8080"},
		{name: "ok/provide-scheme", caURL: "ca.smallstep.com:8080", ret: "https://ca.smallstep.com:8080"},
	}
	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			ret, err := parseCaURL(ctx, tc.caURL)
			if err != nil && assert.NotNil(t, tc.err, fmt.Sprintf("expected no error but got <%s>", err)) {
				assert.HasPrefix(t, err.Error(), tc.err.Error())
			} else if assert.Nil(t, tc.err, fmt.Sprintf("expected error <%s> but got nil", tc.err)) {
				assert.Equals(t, ret, tc.ret)
			}
		})
	}
}