summaryrefslogtreecommitdiff
path: root/go/internal/command/command_test.go
blob: b651c784f8e3200f57e9c51c586cdcb989fae3e0 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package command

import (
	"testing"

	"github.com/stretchr/testify/require"

	"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/authorizedkeys"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/discover"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/fallback"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/lfsauthenticate"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/receivepack"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/twofactorrecover"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/uploadarchive"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/uploadpack"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/config"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/executable"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/testhelper"
)

func TestNew(t *testing.T) {
	testCases := []struct {
		desc         string
		executable   *executable.Executable
		config       *config.Config
		environment  map[string]string
		arguments    []string
		expectedType interface{}
	}{
		{
			desc:       "it returns a Discover command if the feature is enabled",
			executable: &executable.Executable{Name: executable.GitlabShell},
			config: &config.Config{
				GitlabUrl: "http+unix://gitlab.socket",
				Migration: config.MigrationConfig{Enabled: true, Features: []string{"discover"}},
			},
			environment: map[string]string{
				"SSH_CONNECTION":       "1",
				"SSH_ORIGINAL_COMMAND": "",
			},
			arguments:    []string{},
			expectedType: &discover.Command{},
		},
		{
			desc:       "it returns a Fallback command no feature is enabled",
			executable: &executable.Executable{Name: executable.GitlabShell},
			config: &config.Config{
				GitlabUrl: "http+unix://gitlab.socket",
				Migration: config.MigrationConfig{Enabled: false},
			},
			environment: map[string]string{
				"SSH_CONNECTION":       "1",
				"SSH_ORIGINAL_COMMAND": "",
			},
			arguments:    []string{},
			expectedType: &fallback.Command{},
		},
		{
			desc:       "it returns a TwoFactorRecover command if the feature is enabled",
			executable: &executable.Executable{Name: executable.GitlabShell},
			config: &config.Config{
				GitlabUrl: "http+unix://gitlab.socket",
				Migration: config.MigrationConfig{Enabled: true, Features: []string{"2fa_recovery_codes"}},
			},
			environment: map[string]string{
				"SSH_CONNECTION":       "1",
				"SSH_ORIGINAL_COMMAND": "2fa_recovery_codes",
			},
			arguments:    []string{},
			expectedType: &twofactorrecover.Command{},
		},
		{
			desc:       "it returns an LfsAuthenticate command if the feature is enabled",
			executable: &executable.Executable{Name: executable.GitlabShell},
			config: &config.Config{
				GitlabUrl: "http+unix://gitlab.socket",
				Migration: config.MigrationConfig{Enabled: true, Features: []string{"git-lfs-authenticate"}},
			},
			environment: map[string]string{
				"SSH_CONNECTION":       "1",
				"SSH_ORIGINAL_COMMAND": "git-lfs-authenticate",
			},
			arguments:    []string{},
			expectedType: &lfsauthenticate.Command{},
		},
		{
			desc:       "it returns a ReceivePack command if the feature is enabled",
			executable: &executable.Executable{Name: executable.GitlabShell},
			config: &config.Config{
				GitlabUrl: "http+unix://gitlab.socket",
				Migration: config.MigrationConfig{Enabled: true, Features: []string{"git-receive-pack"}},
			},
			environment: map[string]string{
				"SSH_CONNECTION":       "1",
				"SSH_ORIGINAL_COMMAND": "git-receive-pack",
			},
			arguments:    []string{},
			expectedType: &receivepack.Command{},
		},
		{
			desc:       "it returns an UploadPack command if the feature is enabled",
			executable: &executable.Executable{Name: executable.GitlabShell},
			config: &config.Config{
				GitlabUrl: "http+unix://gitlab.socket",
				Migration: config.MigrationConfig{Enabled: true, Features: []string{"git-upload-pack"}},
			},
			environment: map[string]string{
				"SSH_CONNECTION":       "1",
				"SSH_ORIGINAL_COMMAND": "git-upload-pack",
			},
			arguments:    []string{},
			expectedType: &uploadpack.Command{},
		},
		{
			desc:       "it returns an UploadArchive command if the feature is enabled",
			executable: &executable.Executable{Name: executable.GitlabShell},
			config: &config.Config{
				GitlabUrl: "http+unix://gitlab.socket",
				Migration: config.MigrationConfig{Enabled: true, Features: []string{"git-upload-archive"}},
			},
			environment: map[string]string{
				"SSH_CONNECTION":       "1",
				"SSH_ORIGINAL_COMMAND": "git-upload-archive",
			},
			arguments:    []string{},
			expectedType: &uploadarchive.Command{},
		},
		{
			desc:       "it returns a AuthorizedKeys command if the feature is enabled",
			executable: &executable.Executable{Name: executable.AuthorizedKeysCheck},
			config: &config.Config{
				Migration: config.MigrationConfig{Enabled: true, Features: []string{"gitlab-shell-authorized-keys-check"}},
			},
			environment:  map[string]string{},
			arguments:    []string{"git", "git", "key"},
			expectedType: &authorizedkeys.Command{},
		},
		{
			desc:       "it returns a Fallback command if the feature is unimplemented",
			executable: &executable.Executable{Name: executable.GitlabShell},
			config: &config.Config{
				GitlabUrl: "http+unix://gitlab.socket",
				Migration: config.MigrationConfig{Enabled: true, Features: []string{"git-unimplemented-feature"}},
			},
			environment: map[string]string{
				"SSH_CONNECTION":       "1",
				"SSH_ORIGINAL_COMMAND": "git-unimplemented-feature",
			},
			arguments:    []string{},
			expectedType: &fallback.Command{},
		},
		{
			desc:         "it returns a Fallback command if executable is unknown",
			executable:   &executable.Executable{Name: "unknown"},
			config:       &config.Config{},
			arguments:    []string{},
			expectedType: &fallback.Command{},
		},
	}

	for _, tc := range testCases {
		t.Run(tc.desc, func(t *testing.T) {
			restoreEnv := testhelper.TempEnv(tc.environment)
			defer restoreEnv()

			command, err := New(tc.executable, tc.arguments, tc.config, nil)

			require.NoError(t, err)
			require.IsType(t, tc.expectedType, command)
		})
	}
}

func TestFailingNew(t *testing.T) {
	t.Run("It returns an error parsing arguments failed", func(t *testing.T) {
		_, err := New(&executable.Executable{Name: executable.GitlabShell}, []string{}, &config.Config{}, nil)

		require.Error(t, err)
	})
}