|
| 1 | +package twofactorverify |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + "gitlab.com/gitlab-org/gitlab-shell/v14/client/testserver" |
| 14 | + "gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/commandargs" |
| 15 | + "gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/readwriter" |
| 16 | + "gitlab.com/gitlab-org/gitlab-shell/v14/internal/config" |
| 17 | + "gitlab.com/gitlab-org/gitlab-shell/v14/internal/gitlabnet/twofactorverify" |
| 18 | +) |
| 19 | + |
| 20 | +type blockingReader struct{} |
| 21 | + |
| 22 | +func (*blockingReader) Read([]byte) (int, error) { |
| 23 | + waitInfinitely := make(chan struct{}) |
| 24 | + <-waitInfinitely |
| 25 | + |
| 26 | + return 0, nil |
| 27 | +} |
| 28 | + |
| 29 | +func setup(t *testing.T) []testserver.TestRequestHandler { |
| 30 | + waitInfinitely := make(chan struct{}) |
| 31 | + requests := []testserver.TestRequestHandler{ |
| 32 | + { |
| 33 | + Path: "/api/v4/internal/two_factor_manual_otp_check", |
| 34 | + Handler: func(w http.ResponseWriter, r *http.Request) { |
| 35 | + b, err := io.ReadAll(r.Body) |
| 36 | + defer r.Body.Close() |
| 37 | + |
| 38 | + require.NoError(t, err) |
| 39 | + |
| 40 | + var requestBody *twofactorverify.RequestBody |
| 41 | + require.NoError(t, json.Unmarshal(b, &requestBody)) |
| 42 | + |
| 43 | + switch requestBody.KeyId { |
| 44 | + case "verify_via_otp", "verify_via_otp_with_push_error": |
| 45 | + body := map[string]interface{}{ |
| 46 | + "success": true, |
| 47 | + } |
| 48 | + json.NewEncoder(w).Encode(body) |
| 49 | + case "wait_infinitely": |
| 50 | + <-waitInfinitely |
| 51 | + case "error": |
| 52 | + body := map[string]interface{}{ |
| 53 | + "success": false, |
| 54 | + "message": "error message", |
| 55 | + } |
| 56 | + require.NoError(t, json.NewEncoder(w).Encode(body)) |
| 57 | + case "broken": |
| 58 | + w.WriteHeader(http.StatusInternalServerError) |
| 59 | + } |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + Path: "/api/v4/internal/two_factor_push_otp_check", |
| 64 | + Handler: func(w http.ResponseWriter, r *http.Request) { |
| 65 | + b, err := io.ReadAll(r.Body) |
| 66 | + defer r.Body.Close() |
| 67 | + |
| 68 | + require.NoError(t, err) |
| 69 | + |
| 70 | + var requestBody *twofactorverify.RequestBody |
| 71 | + require.NoError(t, json.Unmarshal(b, &requestBody)) |
| 72 | + |
| 73 | + switch requestBody.KeyId { |
| 74 | + case "verify_via_push": |
| 75 | + body := map[string]interface{}{ |
| 76 | + "success": true, |
| 77 | + } |
| 78 | + json.NewEncoder(w).Encode(body) |
| 79 | + case "verify_via_otp_with_push_error": |
| 80 | + w.WriteHeader(http.StatusInternalServerError) |
| 81 | + default: |
| 82 | + <-waitInfinitely |
| 83 | + } |
| 84 | + }, |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + return requests |
| 89 | +} |
| 90 | + |
| 91 | +const errorHeader = "OTP validation failed: " |
| 92 | + |
| 93 | +func TestExecute(t *testing.T) { |
| 94 | + requests := setup(t) |
| 95 | + |
| 96 | + url := testserver.StartSocketHttpServer(t, requests) |
| 97 | + |
| 98 | + testCases := []struct { |
| 99 | + desc string |
| 100 | + arguments *commandargs.Shell |
| 101 | + input io.Reader |
| 102 | + expectedOutput string |
| 103 | + }{ |
| 104 | + { |
| 105 | + desc: "Verify via OTP", |
| 106 | + arguments: &commandargs.Shell{GitlabKeyId: "verify_via_otp"}, |
| 107 | + expectedOutput: "OTP validation successful. Git operations are now allowed.\n", |
| 108 | + }, |
| 109 | + { |
| 110 | + desc: "Verify via OTP", |
| 111 | + arguments: &commandargs.Shell{GitlabKeyId: "verify_via_otp_with_push_error"}, |
| 112 | + expectedOutput: "OTP validation successful. Git operations are now allowed.\n", |
| 113 | + }, |
| 114 | + { |
| 115 | + desc: "Verify via push authentication", |
| 116 | + arguments: &commandargs.Shell{GitlabKeyId: "verify_via_push"}, |
| 117 | + input: &blockingReader{}, |
| 118 | + expectedOutput: "OTP has been validated by Push Authentication. Git operations are now allowed.\n", |
| 119 | + }, |
| 120 | + { |
| 121 | + desc: "With an empty OTP", |
| 122 | + arguments: &commandargs.Shell{GitlabKeyId: "verify_via_otp"}, |
| 123 | + input: bytes.NewBufferString("\n"), |
| 124 | + expectedOutput: errorHeader + "OTP cannot be blank.\n", |
| 125 | + }, |
| 126 | + { |
| 127 | + desc: "With bad response", |
| 128 | + arguments: &commandargs.Shell{GitlabKeyId: "-1"}, |
| 129 | + expectedOutput: errorHeader + "Parsing failed\n", |
| 130 | + }, |
| 131 | + { |
| 132 | + desc: "With API returns an error", |
| 133 | + arguments: &commandargs.Shell{GitlabKeyId: "error"}, |
| 134 | + expectedOutput: errorHeader + "error message\n", |
| 135 | + }, |
| 136 | + { |
| 137 | + desc: "With API fails", |
| 138 | + arguments: &commandargs.Shell{GitlabKeyId: "broken"}, |
| 139 | + expectedOutput: errorHeader + "Internal API error (500)\n", |
| 140 | + }, |
| 141 | + { |
| 142 | + desc: "With missing arguments", |
| 143 | + arguments: &commandargs.Shell{}, |
| 144 | + expectedOutput: errorHeader + "who='' is invalid\n", |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + for _, tc := range testCases { |
| 149 | + t.Run(tc.desc, func(t *testing.T) { |
| 150 | + output := &bytes.Buffer{} |
| 151 | + |
| 152 | + input := tc.input |
| 153 | + if input == nil { |
| 154 | + input = bytes.NewBufferString("123456\n") |
| 155 | + } |
| 156 | + |
| 157 | + cmd := &Command{ |
| 158 | + Config: &config.Config{GitlabUrl: url}, |
| 159 | + Args: tc.arguments, |
| 160 | + ReadWriter: &readwriter.ReadWriter{Out: output, In: input}, |
| 161 | + } |
| 162 | + |
| 163 | + err := cmd.Execute(context.Background()) |
| 164 | + |
| 165 | + require.NoError(t, err) |
| 166 | + require.Equal(t, prompt+"\n"+tc.expectedOutput, output.String()) |
| 167 | + }) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func TestCanceledContext(t *testing.T) { |
| 172 | + requests := setup(t) |
| 173 | + |
| 174 | + output := &bytes.Buffer{} |
| 175 | + |
| 176 | + url := testserver.StartSocketHttpServer(t, requests) |
| 177 | + cmd := &Command{ |
| 178 | + Config: &config.Config{GitlabUrl: url}, |
| 179 | + Args: &commandargs.Shell{GitlabKeyId: "wait_infinitely"}, |
| 180 | + ReadWriter: &readwriter.ReadWriter{Out: output, In: &bytes.Buffer{}}, |
| 181 | + } |
| 182 | + |
| 183 | + ctx, cancel := context.WithCancel(context.Background()) |
| 184 | + |
| 185 | + errCh := make(chan error) |
| 186 | + go func() { errCh <- cmd.Execute(ctx) }() |
| 187 | + cancel() |
| 188 | + |
| 189 | + require.NoError(t, <-errCh) |
| 190 | + require.Equal(t, prompt+"\n"+errorHeader+"context canceled\n", output.String()) |
| 191 | +} |
0 commit comments