|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/harrisoncramer/gitlab.nvim/cmd/app/git" |
| 10 | + gitlab "gitlab.com/gitlab-org/api/client-go" |
| 11 | +) |
| 12 | + |
| 13 | +type fakeGraphQLClient struct { |
| 14 | + err error |
| 15 | + jsonData []byte |
| 16 | +} |
| 17 | + |
| 18 | +func (f fakeGraphQLClient) Do(query gitlab.GraphQLQuery, response any, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) { |
| 19 | + if f.err != nil { |
| 20 | + return nil, f.err |
| 21 | + } |
| 22 | + |
| 23 | + // Actually unmarshal JSON into the response struct |
| 24 | + if err := json.Unmarshal(f.jsonData, response); err != nil { |
| 25 | + return nil, err |
| 26 | + } |
| 27 | + |
| 28 | + // if resp, ok := response.(mergeabilityChecksGraphQLResponse); ok { |
| 29 | + // resp.Data.Project.MergeRequest.MergeabilityChecks = f.checks |
| 30 | + // } |
| 31 | + |
| 32 | + return makeResponse(http.StatusOK), nil |
| 33 | +} |
| 34 | + |
| 35 | +var testMergeabilityData = data{ |
| 36 | + projectInfo: &ProjectInfo{MergeId: 123}, |
| 37 | + gitInfo: &git.GitData{ |
| 38 | + BranchName: "feature-branch", |
| 39 | + Namespace: "test-namespace", |
| 40 | + ProjectName: "test-project", |
| 41 | + }, |
| 42 | +} |
| 43 | + |
| 44 | +func TestMergeabilityChecksHandler(t *testing.T) { |
| 45 | + t.Run("Returns mergeability checks", func(t *testing.T) { |
| 46 | + request := makeRequest(t, http.MethodGet, "/mr/mergeability_checks", nil) |
| 47 | + client := fakeGraphQLClient{ |
| 48 | + jsonData: []byte(`{ |
| 49 | + "data": { |
| 50 | + "project": { |
| 51 | + "mergeRequest": { |
| 52 | + "mergeabilityChecks": [ |
| 53 | + {"identifier": "CI_MUST_PASS", "status": "SUCCESS"}, |
| 54 | + {"identifier": "CONFLICT", "status": "FAILED"} |
| 55 | + ] |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + }`), |
| 60 | + } |
| 61 | + svc := middleware( |
| 62 | + mergeabilityChecksService{testMergeabilityData, client}, |
| 63 | + withMethodCheck(http.MethodGet), |
| 64 | + ) |
| 65 | + |
| 66 | + res := httptest.NewRecorder() |
| 67 | + svc.ServeHTTP(res, request) |
| 68 | + |
| 69 | + var data MergeabilityChecksResponse |
| 70 | + err := json.Unmarshal(res.Body.Bytes(), &data) |
| 71 | + assert(t, err, nil) |
| 72 | + |
| 73 | + assert(t, data.Message, "Mergeability checks retrieved") |
| 74 | + assert(t, len(data.MergeabilityChecks), 2) |
| 75 | + assert(t, data.MergeabilityChecks[0].Identifier, "CI_MUST_PASS") |
| 76 | + assert(t, data.MergeabilityChecks[0].Status, "SUCCESS") |
| 77 | + assert(t, data.MergeabilityChecks[1].Identifier, "CONFLICT") |
| 78 | + assert(t, data.MergeabilityChecks[1].Status, "FAILED") |
| 79 | + }) |
| 80 | + |
| 81 | + t.Run("Returns empty list when there are no checks", func(t *testing.T) { |
| 82 | + request := makeRequest(t, http.MethodGet, "/mr/mergeability_checks", nil) |
| 83 | + client := fakeGraphQLClient{ |
| 84 | + jsonData: []byte(`{ |
| 85 | + "data": { |
| 86 | + "project": { |
| 87 | + "mergeRequest": { |
| 88 | + "mergeabilityChecks": [] |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + }`), |
| 93 | + } |
| 94 | + svc := middleware( |
| 95 | + mergeabilityChecksService{testMergeabilityData, client}, |
| 96 | + withMethodCheck(http.MethodGet), |
| 97 | + ) |
| 98 | + |
| 99 | + res := httptest.NewRecorder() |
| 100 | + svc.ServeHTTP(res, request) |
| 101 | + |
| 102 | + var data MergeabilityChecksResponse |
| 103 | + err := json.Unmarshal(res.Body.Bytes(), &data) |
| 104 | + assert(t, err, nil) |
| 105 | + |
| 106 | + assert(t, data.Message, "Mergeability checks retrieved") |
| 107 | + assert(t, len(data.MergeabilityChecks), 0) |
| 108 | + }) |
| 109 | + |
| 110 | + t.Run("Handles errors from Gitlab client", func(t *testing.T) { |
| 111 | + request := makeRequest(t, http.MethodGet, "/mr/mergeability_checks", nil) |
| 112 | + client := fakeGraphQLClient{err: errorFromGitlab} |
| 113 | + svc := middleware( |
| 114 | + mergeabilityChecksService{testMergeabilityData, client}, |
| 115 | + withMethodCheck(http.MethodGet), |
| 116 | + ) |
| 117 | + data, _ := getFailData(t, svc, request) |
| 118 | + assert(t, data.Message, "Could not get mergeability checks") |
| 119 | + assert(t, data.Details, "failed to fetch mergeability checks: "+errorFromGitlab.Error()) |
| 120 | + }) |
| 121 | +} |
0 commit comments