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
|
package command_test
import (
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-shell/v14/cmd/gitlab-shell-authorized-keys-check/command"
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/authorizedkeys"
"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 (
authorizedKeysExec = &executable.Executable{Name: executable.AuthorizedKeysCheck}
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 AuthorizedKeys command",
executable: authorizedKeysExec,
arguments: []string{"git", "git", "key"},
config: basicConfig,
expectedType: &authorizedkeys.Command{},
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
command, err := command.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-keys command",
executable: &executable.Executable{Name: executable.AuthorizedKeysCheck},
arguments: []string{"git", "git", "key"},
expectedArgs: &commandargs.AuthorizedKeys{Arguments: []string{"git", "git", "key"}, ExpectedUser: "git", ActualUser: "git", Key: "key"},
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
result, err := command.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 AuthorizedKeysCheck",
executable: &executable.Executable{Name: executable.AuthorizedKeysCheck},
arguments: []string{"user"},
expectedError: "# Insufficient arguments. 1. Usage\n#\tgitlab-shell-authorized-keys-check <expected-username> <actual-username> <key>",
},
{
desc: "With too many arguments for the AuthorizedKeysCheck",
executable: &executable.Executable{Name: executable.AuthorizedKeysCheck},
arguments: []string{"user", "user", "key", "something-else"},
expectedError: "# Insufficient arguments. 4. Usage\n#\tgitlab-shell-authorized-keys-check <expected-username> <actual-username> <key>",
},
{
desc: "With missing username for the AuthorizedKeysCheck",
executable: &executable.Executable{Name: executable.AuthorizedKeysCheck},
arguments: []string{"user", "", "key"},
expectedError: "# No username provided",
},
{
desc: "With missing key for the AuthorizedKeysCheck",
executable: &executable.Executable{Name: executable.AuthorizedKeysCheck},
arguments: []string{"user", "user", ""},
expectedError: "# No key provided",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
_, err := command.Parse(tc.arguments)
require.EqualError(t, err, tc.expectedError)
})
}
}
|