summaryrefslogtreecommitdiff
path: root/go/internal/command/commandargs/command_args_test.go
blob: 10c46fe7cd54f16019ddf44fe26d4c9a4181c0ab (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
package commandargs

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/testhelper"
)

func TestParseSuccess(t *testing.T) {
	testCases := []struct {
		desc         string
		arguments    []string
		environment  map[string]string
		expectedArgs *CommandArgs
	}{
		// Setting the used env variables for every case to ensure we're
		// not using anything set in the original env.
		{
			desc: "It sets discover as the command when the command string was empty",
			environment: map[string]string{
				"SSH_CONNECTION":       "1",
				"SSH_ORIGINAL_COMMAND": "",
			},
			expectedArgs: &CommandArgs{CommandType: Discover},
		},
		{
			desc: "It passes on the original ssh command from the environment",
			environment: map[string]string{
				"SSH_CONNECTION":       "1",
				"SSH_ORIGINAL_COMMAND": "hello world",
			},
			expectedArgs: &CommandArgs{SshCommand: "hello world"},
		}, {
			desc: "It finds the key id in any passed arguments",
			environment: map[string]string{
				"SSH_CONNECTION":       "1",
				"SSH_ORIGINAL_COMMAND": "",
			},
			arguments:    []string{"hello", "key-123"},
			expectedArgs: &CommandArgs{CommandType: Discover, GitlabKeyId: "123"},
		}, {
			desc: "It finds the username in any passed arguments",
			environment: map[string]string{
				"SSH_CONNECTION":       "1",
				"SSH_ORIGINAL_COMMAND": "",
			},
			arguments:    []string{"hello", "username-jane-doe"},
			expectedArgs: &CommandArgs{CommandType: Discover, GitlabUsername: "jane-doe"},
		},
	}

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

			result, err := Parse(tc.arguments)

			assert.NoError(t, err)
			assert.Equal(t, tc.expectedArgs, result)
		})
	}
}

func TestParseFailure(t *testing.T) {
	t.Run("It fails if SSH connection is not set", func(t *testing.T) {
		_, err := Parse([]string{})

		assert.Error(t, err, "Only ssh allowed")
	})

}