|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/CircleCI-Public/circleci-cli/settings" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestLegacyFlow(t *testing.T) { |
| 14 | + t.Run("tests that the compiler defaults to the graphQL resolver should the original API request fail with 404", func(t *testing.T) { |
| 15 | + mux := http.NewServeMux() |
| 16 | + |
| 17 | + mux.HandleFunc("/compile-config-with-defaults", func(w http.ResponseWriter, r *http.Request) { |
| 18 | + w.WriteHeader(http.StatusNotFound) |
| 19 | + }) |
| 20 | + |
| 21 | + mux.HandleFunc("/me/collaborations", func(w http.ResponseWriter, r *http.Request) { |
| 22 | + w.Header().Set("Content-Type", "application/json") |
| 23 | + fmt.Fprintf(w, `[{"vcs_type":"circleci","slug":"gh/test","id":"2345"}]`) |
| 24 | + }) |
| 25 | + |
| 26 | + mux.HandleFunc("/graphql-unstable", func(w http.ResponseWriter, r *http.Request) { |
| 27 | + w.Header().Set("Content-Type", "application/json") |
| 28 | + fmt.Fprintf(w, `{"data":{"buildConfig": {"valid":true,"sourceYaml":"%s","outputYaml":"%s","errors":[]}}}`, testYaml, testYaml) |
| 29 | + }) |
| 30 | + |
| 31 | + svr := httptest.NewServer(mux) |
| 32 | + defer svr.Close() |
| 33 | + |
| 34 | + compiler := New(&settings.Config{ |
| 35 | + Host: svr.URL, |
| 36 | + Endpoint: "/graphql-unstable", |
| 37 | + HTTPClient: http.DefaultClient, |
| 38 | + Token: "", |
| 39 | + }) |
| 40 | + resp, err := compiler.ConfigQuery("testdata/config.yml", "1234", Parameters{}, Values{}) |
| 41 | + |
| 42 | + assert.Equal(t, true, resp.Valid) |
| 43 | + assert.NoError(t, err) |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("tests that the compiler handles errors properly when returned from the graphQL endpoint", func(t *testing.T) { |
| 47 | + mux := http.NewServeMux() |
| 48 | + |
| 49 | + mux.HandleFunc("/compile-config-with-defaults", func(w http.ResponseWriter, r *http.Request) { |
| 50 | + w.WriteHeader(http.StatusNotFound) |
| 51 | + }) |
| 52 | + |
| 53 | + mux.HandleFunc("/me/collaborations", func(w http.ResponseWriter, r *http.Request) { |
| 54 | + w.Header().Set("Content-Type", "application/json") |
| 55 | + fmt.Fprintf(w, `[{"vcs_type":"circleci","slug":"gh/test","id":"2345"}]`) |
| 56 | + }) |
| 57 | + |
| 58 | + mux.HandleFunc("/graphql-unstable", func(w http.ResponseWriter, r *http.Request) { |
| 59 | + w.Header().Set("Content-Type", "application/json") |
| 60 | + fmt.Fprintf(w, `{"data":{"buildConfig":{"errors":[{"message": "failed to validate"}]}}}`) |
| 61 | + }) |
| 62 | + |
| 63 | + svr := httptest.NewServer(mux) |
| 64 | + defer svr.Close() |
| 65 | + |
| 66 | + compiler := New(&settings.Config{ |
| 67 | + Host: svr.URL, |
| 68 | + Endpoint: "/graphql-unstable", |
| 69 | + HTTPClient: http.DefaultClient, |
| 70 | + Token: "", |
| 71 | + }) |
| 72 | + _, err := compiler.ConfigQuery("testdata/config.yml", "1234", Parameters{}, Values{}) |
| 73 | + assert.Error(t, err) |
| 74 | + assert.Contains(t, err.Error(), "failed to validate") |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("tests that the compiler fails out completely when a non-404 is returned from the http endpoint", func(t *testing.T) { |
| 78 | + mux := http.NewServeMux() |
| 79 | + gqlHitCounter := 0 |
| 80 | + |
| 81 | + mux.HandleFunc("/compile-config-with-defaults", func(w http.ResponseWriter, r *http.Request) { |
| 82 | + w.WriteHeader(http.StatusInternalServerError) |
| 83 | + |
| 84 | + }) |
| 85 | + |
| 86 | + mux.HandleFunc("/me/collaborations", func(w http.ResponseWriter, r *http.Request) { |
| 87 | + w.Header().Set("Content-Type", "application/json") |
| 88 | + fmt.Fprintf(w, `[{"vcs_type":"circleci","slug":"gh/test","id":"2345"}]`) |
| 89 | + }) |
| 90 | + |
| 91 | + mux.HandleFunc("/graphql-unstable", func(w http.ResponseWriter, r *http.Request) { |
| 92 | + w.Header().Set("Content-Type", "application/json") |
| 93 | + fmt.Fprintf(w, `{"data":{"buildConfig":{"errors":[{"message": "failed to validate"}]}}}`) |
| 94 | + gqlHitCounter++ |
| 95 | + }) |
| 96 | + |
| 97 | + svr := httptest.NewServer(mux) |
| 98 | + defer svr.Close() |
| 99 | + |
| 100 | + compiler := New(&settings.Config{ |
| 101 | + Host: svr.URL, |
| 102 | + Endpoint: "/graphql-unstable", |
| 103 | + HTTPClient: http.DefaultClient, |
| 104 | + Token: "", |
| 105 | + }) |
| 106 | + _, err := compiler.ConfigQuery("testdata/config.yml", "1234", Parameters{}, Values{}) |
| 107 | + assert.Error(t, err) |
| 108 | + assert.Contains(t, err.Error(), "config compilation request returned an error:") |
| 109 | + assert.Equal(t, 0, gqlHitCounter) |
| 110 | + }) |
| 111 | +} |
0 commit comments