|
| 1 | +package authorizedcerts |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "gitlab.com/gitlab-org/gitlab-shell/v14/client" |
| 11 | + "gitlab.com/gitlab-org/gitlab-shell/v14/client/testserver" |
| 12 | + "gitlab.com/gitlab-org/gitlab-shell/v14/internal/config" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + requests []testserver.TestRequestHandler |
| 17 | +) |
| 18 | + |
| 19 | +func init() { |
| 20 | + requests = []testserver.TestRequestHandler{ |
| 21 | + { |
| 22 | + Path: "/api/v4/internal/authorized_certs", |
| 23 | + Handler: func(w http.ResponseWriter, r *http.Request) { |
| 24 | + if r.URL.Query().Get("key") == "key" { |
| 25 | + body := &Response{ |
| 26 | + Namespace: "group", |
| 27 | + Username: r.URL.Query().Get("user_identifier"), |
| 28 | + } |
| 29 | + json.NewEncoder(w).Encode(body) |
| 30 | + } else if r.URL.Query().Get("key") == "broken-message" { |
| 31 | + w.WriteHeader(http.StatusForbidden) |
| 32 | + body := &client.ErrorResponse{ |
| 33 | + Message: "Not allowed!", |
| 34 | + } |
| 35 | + json.NewEncoder(w).Encode(body) |
| 36 | + } else if r.URL.Query().Get("key") == "broken-json" { |
| 37 | + w.Write([]byte("{ \"message\": \"broken json!\"")) |
| 38 | + } else if r.URL.Query().Get("key") == "broken-empty" { |
| 39 | + w.WriteHeader(http.StatusForbidden) |
| 40 | + } else { |
| 41 | + w.WriteHeader(http.StatusNotFound) |
| 42 | + } |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestGetByKey(t *testing.T) { |
| 49 | + client := setup(t) |
| 50 | + |
| 51 | + result, err := client.GetByKey(context.Background(), "user-id", "key") |
| 52 | + require.NoError(t, err) |
| 53 | + require.Equal(t, &Response{Namespace: "group", Username: "user-id"}, result) |
| 54 | +} |
| 55 | + |
| 56 | +func TestGetByKeyErrorResponses(t *testing.T) { |
| 57 | + client := setup(t) |
| 58 | + |
| 59 | + testCases := []struct { |
| 60 | + desc string |
| 61 | + key string |
| 62 | + expectedError string |
| 63 | + }{ |
| 64 | + { |
| 65 | + desc: "A response with an error message", |
| 66 | + key: "broken-message", |
| 67 | + expectedError: "Not allowed!", |
| 68 | + }, |
| 69 | + { |
| 70 | + desc: "A response with bad JSON", |
| 71 | + key: "broken-json", |
| 72 | + expectedError: "Parsing failed", |
| 73 | + }, |
| 74 | + { |
| 75 | + desc: "A forbidden (403) response without message", |
| 76 | + key: "broken-empty", |
| 77 | + expectedError: "Internal API error (403)", |
| 78 | + }, |
| 79 | + { |
| 80 | + desc: "A not found (404) response without message", |
| 81 | + key: "not-found", |
| 82 | + expectedError: "Internal API error (404)", |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + for _, tc := range testCases { |
| 87 | + t.Run(tc.desc, func(t *testing.T) { |
| 88 | + resp, err := client.GetByKey(context.Background(), "user-id", tc.key) |
| 89 | + |
| 90 | + require.EqualError(t, err, tc.expectedError) |
| 91 | + require.Nil(t, resp) |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func setup(t *testing.T) *Client { |
| 97 | + url := testserver.StartSocketHttpServer(t, requests) |
| 98 | + |
| 99 | + client, err := NewClient(&config.Config{GitlabUrl: url}) |
| 100 | + require.NoError(t, err) |
| 101 | + |
| 102 | + return client |
| 103 | +} |
0 commit comments