summaryrefslogtreecommitdiff
path: root/internal/gitlabnet/lfsauthenticate/client_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/gitlabnet/lfsauthenticate/client_test.go')
-rw-r--r--internal/gitlabnet/lfsauthenticate/client_test.go117
1 files changed, 117 insertions, 0 deletions
diff --git a/internal/gitlabnet/lfsauthenticate/client_test.go b/internal/gitlabnet/lfsauthenticate/client_test.go
new file mode 100644
index 0000000..6faaa63
--- /dev/null
+++ b/internal/gitlabnet/lfsauthenticate/client_test.go
@@ -0,0 +1,117 @@
+package lfsauthenticate
+
+import (
+ "encoding/json"
+ "io/ioutil"
+ "net/http"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
+ "gitlab.com/gitlab-org/gitlab-shell/internal/config"
+ "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver"
+)
+
+const (
+ keyId = "123"
+ repo = "group/repo"
+ action = commandargs.UploadPack
+)
+
+func setup(t *testing.T) []testserver.TestRequestHandler {
+ requests := []testserver.TestRequestHandler{
+ {
+ Path: "/api/v4/internal/lfs_authenticate",
+ Handler: func(w http.ResponseWriter, r *http.Request) {
+ b, err := ioutil.ReadAll(r.Body)
+ defer r.Body.Close()
+ require.NoError(t, err)
+
+ var request *Request
+ require.NoError(t, json.Unmarshal(b, &request))
+
+ switch request.KeyId {
+ case keyId:
+ body := map[string]interface{}{
+ "username": "john",
+ "lfs_token": "sometoken",
+ "repository_http_path": "https://gitlab.com/repo/path",
+ "expires_in": 1800,
+ }
+ require.NoError(t, json.NewEncoder(w).Encode(body))
+ case "forbidden":
+ w.WriteHeader(http.StatusForbidden)
+ case "broken":
+ w.WriteHeader(http.StatusInternalServerError)
+ }
+ },
+ },
+ }
+
+ return requests
+}
+
+func TestFailedRequests(t *testing.T) {
+ requests := setup(t)
+ url, cleanup := testserver.StartHttpServer(t, requests)
+ defer cleanup()
+
+ testCases := []struct {
+ desc string
+ args *commandargs.Shell
+ expectedOutput string
+ }{
+ {
+ desc: "With bad response",
+ args: &commandargs.Shell{GitlabKeyId: "-1", CommandType: commandargs.UploadPack},
+ expectedOutput: "Parsing failed",
+ },
+ {
+ desc: "With API returns an error",
+ args: &commandargs.Shell{GitlabKeyId: "forbidden", CommandType: commandargs.UploadPack},
+ expectedOutput: "Internal API error (403)",
+ },
+ {
+ desc: "With API fails",
+ args: &commandargs.Shell{GitlabKeyId: "broken", CommandType: commandargs.UploadPack},
+ expectedOutput: "Internal API error (500)",
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.desc, func(t *testing.T) {
+ client, err := NewClient(&config.Config{GitlabUrl: url}, tc.args)
+ require.NoError(t, err)
+
+ repo := "group/repo"
+
+ _, err = client.Authenticate(tc.args.CommandType, repo, "")
+ require.Error(t, err)
+
+ require.Equal(t, tc.expectedOutput, err.Error())
+ })
+ }
+}
+
+func TestSuccessfulRequests(t *testing.T) {
+ requests := setup(t)
+ url, cleanup := testserver.StartHttpServer(t, requests)
+ defer cleanup()
+
+ args := &commandargs.Shell{GitlabKeyId: keyId, CommandType: commandargs.LfsAuthenticate}
+ client, err := NewClient(&config.Config{GitlabUrl: url}, args)
+ require.NoError(t, err)
+
+ response, err := client.Authenticate(action, repo, "")
+ require.NoError(t, err)
+
+ expectedResponse := &Response{
+ Username: "john",
+ LfsToken: "sometoken",
+ RepoPath: "https://gitlab.com/repo/path",
+ ExpiresIn: 1800,
+ }
+
+ require.Equal(t, expectedResponse, response)
+}