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
|
package command_test
import (
"testing"
"github.com/stretchr/testify/require"
cmd "gitlab.com/gitlab-org/gitlab-shell/v14/cmd/gitlab-shell-authorized-principals-check/command"
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/authorizedprincipals"
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/executable"
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/sshenv"
)
var (
authorizedPrincipalsExec = &executable.Executable{Name: executable.AuthorizedPrincipalsCheck}
basicConfig = &config.Config{GitlabUrl: "http+unix://gitlab.socket"}
)
func TestNew(t *testing.T) {
testCases := []struct {
desc string
executable *executable.Executable
env sshenv.Env
arguments []string
config *config.Config
expectedType interface{}
}{
{
desc: "it returns a AuthorizedPrincipals command",
executable: authorizedPrincipalsExec,
arguments: []string{"key", "principal"},
config: basicConfig,
expectedType: &authorizedprincipals.Command{},
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
command, err := cmd.New(tc.arguments, tc.config, nil)
require.NoError(t, err)
require.IsType(t, tc.expectedType, command)
})
}
}
func TestParseSuccess(t *testing.T) {
testCases := []struct {
desc string
executable *executable.Executable
env sshenv.Env
arguments []string
expectedArgs commandargs.CommandArgs
expectError bool
}{
{
desc: "It parses authorized-principals command",
executable: &executable.Executable{Name: executable.AuthorizedPrincipalsCheck},
arguments: []string{"key", "principal-1", "principal-2"},
expectedArgs: &commandargs.AuthorizedPrincipals{Arguments: []string{"key", "principal-1", "principal-2"}, KeyId: "key", Principals: []string{"principal-1", "principal-2"}},
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
result, err := cmd.Parse(tc.arguments)
if !tc.expectError {
require.NoError(t, err)
require.Equal(t, tc.expectedArgs, result)
} else {
require.Error(t, err)
}
})
}
}
func TestParseFailure(t *testing.T) {
testCases := []struct {
desc string
executable *executable.Executable
env sshenv.Env
arguments []string
expectedError string
}{
{
desc: "With not enough arguments for the AuthorizedPrincipalsCheck",
executable: &executable.Executable{Name: executable.AuthorizedPrincipalsCheck},
arguments: []string{"key"},
expectedError: "# Insufficient arguments. 1. Usage\n#\tgitlab-shell-authorized-principals-check <key-id> <principal1> [<principal2>...]",
},
{
desc: "With missing key_id for the AuthorizedPrincipalsCheck",
executable: &executable.Executable{Name: executable.AuthorizedPrincipalsCheck},
arguments: []string{"", "principal"},
expectedError: "# No key_id provided",
},
{
desc: "With blank principal for the AuthorizedPrincipalsCheck",
executable: &executable.Executable{Name: executable.AuthorizedPrincipalsCheck},
arguments: []string{"key", "principal", ""},
expectedError: "# An invalid principal was provided",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
_, err := cmd.Parse(tc.arguments)
require.EqualError(t, err, tc.expectedError)
})
}
}
|