|
| 1 | +package setup_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/fastly/cli/pkg/commands/compute/setup" |
| 12 | + "github.com/fastly/cli/pkg/manifest" |
| 13 | + "github.com/fastly/cli/pkg/mock" |
| 14 | + "github.com/fastly/cli/pkg/testutil" |
| 15 | + "github.com/fastly/cli/pkg/text" |
| 16 | +) |
| 17 | + |
| 18 | +// TestProductsCreate tests the `Create` method of the `Products` struct. |
| 19 | +func TestProductsCreate(t *testing.T) { |
| 20 | + scenarios := []struct { |
| 21 | + name string |
| 22 | + setup *manifest.SetupProducts |
| 23 | + client *mock.HTTPClient |
| 24 | + wantOutput string |
| 25 | + wantError string |
| 26 | + expectedRequests []testutil.ExpectedRequest |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "successfully enables a single product", |
| 30 | + setup: &manifest.SetupProducts{ |
| 31 | + APIDiscovery: &manifest.SetupProduct{ |
| 32 | + Enable: true, |
| 33 | + }, |
| 34 | + }, |
| 35 | + client: mock.NewHTTPClientWithResponses([]*http.Response{ |
| 36 | + { |
| 37 | + StatusCode: http.StatusOK, |
| 38 | + Body: io.NopCloser(strings.NewReader(`{"id":"api_discovery","name":"API Discovery"}`)), |
| 39 | + }, |
| 40 | + }), |
| 41 | + expectedRequests: []testutil.ExpectedRequest{ |
| 42 | + { |
| 43 | + Method: http.MethodPut, |
| 44 | + Path: "/enabled-products/v1/api_discovery/services/123", |
| 45 | + }, |
| 46 | + }, |
| 47 | + wantOutput: "Enabling product 'api_discovery'...", |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "successfully enables multiple products", |
| 51 | + setup: &manifest.SetupProducts{ |
| 52 | + APIDiscovery: &manifest.SetupProduct{ |
| 53 | + Enable: true, |
| 54 | + }, |
| 55 | + OriginInspector: &manifest.SetupProduct{ |
| 56 | + Enable: true, |
| 57 | + }, |
| 58 | + }, |
| 59 | + client: mock.NewHTTPClientWithResponses([]*http.Response{ |
| 60 | + { |
| 61 | + StatusCode: http.StatusOK, |
| 62 | + Body: io.NopCloser(strings.NewReader(`{"id":"some_product","name":"Some Product"}`)), |
| 63 | + }, |
| 64 | + { |
| 65 | + StatusCode: http.StatusOK, |
| 66 | + Body: io.NopCloser(strings.NewReader(`{"id":"some_product","name":"Some Product"}`)), |
| 67 | + }, |
| 68 | + }), |
| 69 | + expectedRequests: []testutil.ExpectedRequest{ |
| 70 | + { |
| 71 | + Method: http.MethodPut, |
| 72 | + Path: "/enabled-products/v1/api_discovery/services/123", |
| 73 | + }, |
| 74 | + { |
| 75 | + Method: http.MethodPut, |
| 76 | + Path: "/enabled-products/v1/origin_inspector/services/123", |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "handles API error when enabling a product", |
| 82 | + setup: &manifest.SetupProducts{ |
| 83 | + APIDiscovery: &manifest.SetupProduct{ |
| 84 | + Enable: true, |
| 85 | + }, |
| 86 | + }, |
| 87 | + client: mock.NewHTTPClientWithErrors([]error{ |
| 88 | + testutil.Err, |
| 89 | + }), |
| 90 | + expectedRequests: []testutil.ExpectedRequest{ |
| 91 | + { |
| 92 | + Method: http.MethodPut, |
| 93 | + Path: "/enabled-products/v1/api_discovery/services/123", |
| 94 | + }, |
| 95 | + }, |
| 96 | + wantError: "error enabling product [api_discovery]: Put \"https://api.example.com/enabled-products/v1/api_discovery/services/123\": test error", |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "no API calls when no products are configured", |
| 100 | + setup: &manifest.SetupProducts{}, |
| 101 | + client: mock.NewHTTPClientWithResponses([]*http.Response{ |
| 102 | + { |
| 103 | + StatusCode: http.StatusOK, |
| 104 | + Body: io.NopCloser(strings.NewReader(`{"id":"some_product","name":"Some Product"}`)), |
| 105 | + }, |
| 106 | + }), |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "successfully enables ngwaf with workspace id", |
| 110 | + setup: &manifest.SetupProducts{ |
| 111 | + Ngwaf: &manifest.SetupProductNgwaf{ |
| 112 | + SetupProduct: manifest.SetupProduct{ |
| 113 | + Enable: true, |
| 114 | + }, |
| 115 | + WorkspaceID: "w-123", |
| 116 | + }, |
| 117 | + }, |
| 118 | + client: mock.NewHTTPClientWithResponses([]*http.Response{ |
| 119 | + { |
| 120 | + StatusCode: http.StatusOK, |
| 121 | + Body: io.NopCloser(strings.NewReader(`{"id":"ngwaf","name":"Next-Gen WAF"}`)), |
| 122 | + }, |
| 123 | + { |
| 124 | + StatusCode: http.StatusOK, |
| 125 | + Body: io.NopCloser(strings.NewReader(`{"id":"some_product","name":"Some Product"}`)), |
| 126 | + }, |
| 127 | + }), |
| 128 | + wantOutput: "Enabling product 'ngwaf'...", |
| 129 | + expectedRequests: []testutil.ExpectedRequest{ |
| 130 | + { |
| 131 | + Method: http.MethodPut, |
| 132 | + Path: "/enabled-products/v1/ngwaf/services/123", |
| 133 | + WantJSON: testutil.StrPtr("{\"workspace_id\":\"w-123\"}"), |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + for _, testcase := range scenarios { |
| 140 | + t.Run(testcase.name, func(t *testing.T) { |
| 141 | + apiClient, err := mock.NewFastlyClient(testcase.client) |
| 142 | + if err != nil { |
| 143 | + t.Fatal(fmt.Errorf("failed to mock fastly.client: %w", err)) |
| 144 | + } |
| 145 | + |
| 146 | + var out bytes.Buffer |
| 147 | + spinner, err := text.NewSpinner(&out) |
| 148 | + if err != nil { |
| 149 | + t.Fatal(err) |
| 150 | + } |
| 151 | + |
| 152 | + products := setup.Products{ |
| 153 | + APIClient: apiClient, |
| 154 | + ServiceID: "123", |
| 155 | + Spinner: spinner, |
| 156 | + Stdout: &out, |
| 157 | + Setup: testcase.setup, |
| 158 | + } |
| 159 | + |
| 160 | + err = products.Configure() |
| 161 | + testutil.AssertNoError(t, err) |
| 162 | + |
| 163 | + err = products.Create() |
| 164 | + |
| 165 | + if testcase.wantError != "" { |
| 166 | + testutil.AssertErrorContains(t, err, testcase.wantError) |
| 167 | + } else { |
| 168 | + testutil.AssertNoError(t, err) |
| 169 | + if testcase.wantOutput != "" { |
| 170 | + testutil.AssertStringContains(t, out.String(), testcase.wantOutput) |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + if len(testcase.expectedRequests) != len(testcase.client.Requests) { |
| 175 | + t.Errorf("expected %d API calls, but got %d", len(testcase.expectedRequests), len(testcase.client.Requests)) |
| 176 | + } |
| 177 | + |
| 178 | + for i, expectedRequest := range testcase.expectedRequests { |
| 179 | + testutil.AssertRequest(t, &testcase.client.Requests[i], expectedRequest) |
| 180 | + } |
| 181 | + }) |
| 182 | + } |
| 183 | +} |
0 commit comments