Skip to content

Commit a1667f2

Browse files
Igor Drozdove_forbes
authored andcommitted
Merge branch 'ef-remove-redundant-ruby-spec-tests' into 'main'
Removes and refactors Ruby Spec tests into Go See merge request https://gitlab.com/gitlab-org/gitlab-shell/-/merge_requests/1364 Merged-by: Igor Drozdov <idrozdov@gitlab.com> Approved-by: Vasilii Iakliushin <viakliushin@gitlab.com> Approved-by: Igor Drozdov <idrozdov@gitlab.com> Co-authored-by: e_forbes <eforbes@gitlab.com>
2 parents fa2bdf6 + a4d4274 commit a1667f2

6 files changed

Lines changed: 44 additions & 192 deletions

File tree

cmd/gitlab-shell-authorized-principals-check/command/command_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,29 @@ func TestParseSuccess(t *testing.T) {
5555
expectError bool
5656
}{
5757
{
58-
desc: "It parses authorized-principals command",
58+
desc: "it parses authorized-principals command",
5959
executable: &executable.Executable{Name: executable.AuthorizedPrincipalsCheck},
6060
arguments: []string{"key", "principal-1", "principal-2"},
6161
expectedArgs: &commandargs.AuthorizedPrincipals{Arguments: []string{"key", "principal-1", "principal-2"}, KeyID: "key", Principals: []string{"principal-1", "principal-2"}},
6262
},
63+
{
64+
desc: "it fails when a principal is empty",
65+
executable: &executable.Executable{Name: executable.AuthorizedPrincipalsCheck},
66+
arguments: []string{"key", "principal-1", ""},
67+
expectError: true,
68+
},
69+
{
70+
desc: "it fails when a key_id is empty",
71+
executable: &executable.Executable{Name: executable.AuthorizedPrincipalsCheck},
72+
arguments: []string{"", "principal-1"},
73+
expectError: true,
74+
},
75+
{
76+
desc: "it fails when not enough arguments are present",
77+
executable: &executable.Executable{Name: executable.AuthorizedPrincipalsCheck},
78+
arguments: []string{"key"},
79+
expectError: true,
80+
},
6381
}
6482

6583
for _, tc := range testCases {

cmd/gitlab-shell/command/command_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package command_test
22

33
import (
4-
"errors"
54
"testing"
65

76
"github.com/prometheus/client_golang/prometheus/testutil"
@@ -216,7 +215,7 @@ func TestFailingNew(t *testing.T) {
216215
{
217216
desc: "Parsing environment failed",
218217
executable: gitlabShellExec,
219-
expectedError: errors.New("Only SSH allowed"),
218+
expectedError: commandargs.ErrOnlySSHAllowed,
220219
},
221220
{
222221
desc: "Unknown command given",

internal/command/commandargs/shell.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package commandargs
44

55
import (
6+
"errors"
67
"fmt"
78
"regexp"
89
"strings"
@@ -33,6 +34,12 @@ var (
3334
GitCommands = []CommandType{LfsAuthenticate, UploadPack, ReceivePack, UploadArchive}
3435
)
3536

37+
var (
38+
// ErrOnlySSHAllowed - represents the error returned when the
39+
// a non ssh connection is passed.
40+
ErrOnlySSHAllowed = errors.New("Only SSH allowed")
41+
)
42+
3643
// Shell represents a parsed shell command with its arguments and related information.
3744
type Shell struct {
3845
Arguments []string
@@ -62,7 +69,7 @@ func (s *Shell) GetArguments() []string {
6269

6370
func (s *Shell) validate() error {
6471
if !s.Env.IsSSHConnection {
65-
return fmt.Errorf("Only SSH allowed") //nolint:stylecheck //message is customer facing
72+
return ErrOnlySSHAllowed //nolint:stylecheck //message is customer facing
6673
}
6774

6875
if err := s.ParseCommand(s.Env.OriginalCommand); err != nil {

internal/command/discover/discover_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ func TestExecute(t *testing.T) {
7272
arguments: &commandargs.Shell{GitlabUsername: "unknown"},
7373
expectedUsername: "Anonymous",
7474
},
75+
{
76+
desc: "with a known username - when other input is also given",
77+
arguments: &commandargs.Shell{
78+
Arguments: []string{"foo"},
79+
GitlabUsername: "alex-doe",
80+
},
81+
expectedUsername: "@alex-doe",
82+
},
83+
{
84+
desc: "with a known key id - when other input is also given",
85+
arguments: &commandargs.Shell{
86+
Arguments: []string{"2foo"},
87+
GitlabKeyID: "1",
88+
},
89+
expectedUsername: "@alex-doe",
90+
},
7591
}
7692

7793
for _, tc := range testCases {

spec/gitlab_shell_authorized_principals_check_spec.rb

Lines changed: 0 additions & 57 deletions
This file was deleted.

spec/gitlab_shell_discover_spec.rb

Lines changed: 0 additions & 131 deletions
This file was deleted.

0 commit comments

Comments
 (0)