TestWebhookIntegration tests webhook functionality.
(t *testing.T)
| 134 | |
| 135 | // TestWebhookIntegration tests webhook functionality. |
| 136 | func TestWebhookIntegration(t *testing.T) { |
| 137 | // Allow localhost for testing |
| 138 | webhookplugin.TestOnlyAllowedDomains[storepb.WebhookType_SLACK] = []string{"127.0.0.1", "localhost", "[::1]"} |
| 139 | defer func() { |
| 140 | // Clean up after test |
| 141 | delete(webhookplugin.TestOnlyAllowedDomains, storepb.WebhookType_SLACK) |
| 142 | }() |
| 143 | |
| 144 | ctx := context.Background() |
| 145 | ctl := &controller{} |
| 146 | ctx, err := ctl.StartServerWithExternalPg(ctx) |
| 147 | require.NoError(t, err) |
| 148 | defer ctl.Close(ctx) |
| 149 | |
| 150 | // Test webhook server. |
| 151 | // |
| 152 | // Body MUST be the literal "ok" — the Slack plugin's postMessage |
| 153 | // (backend/plugin/webhook/slack/slack.go:247-249) treats any other body as |
| 154 | // a delivery failure and triggers common.Retry (up to 3 attempts, 5s |
| 155 | // exponential backoff). Without an "ok" response, every webhook event |
| 156 | // would be delivered 1-3 times depending on test timing, causing |
| 157 | // nondeterministic counts in the requireWebhookCount assertions across |
| 158 | // the trigger matrix. |
| 159 | collector := &webhookCollector{} |
| 160 | webhookServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 161 | if err := collector.addRequest(r); err != nil { |
| 162 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 163 | return |
| 164 | } |
| 165 | w.WriteHeader(http.StatusOK) |
| 166 | _, _ = w.Write([]byte("ok")) |
| 167 | })) |
| 168 | defer webhookServer.Close() |
| 169 | |
| 170 | // Create a single instance for all tests |
| 171 | instanceRootDir := t.TempDir() |
| 172 | instanceDir, err := ctl.provisionSQLiteInstance(instanceRootDir, "testInstance") |
| 173 | require.NoError(t, err) |
| 174 | |
| 175 | instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{ |
| 176 | InstanceId: generateRandomString("instance"), |
| 177 | Instance: &v1pb.Instance{ |
| 178 | Title: "test instance", |
| 179 | Engine: v1pb.Engine_SQLITE, |
| 180 | Environment: new("environments/prod"), |
| 181 | Activation: true, |
| 182 | DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: instanceDir, Id: "admin"}}, |
| 183 | }, |
| 184 | })) |
| 185 | require.NoError(t, err) |
| 186 | instance := instanceResp.Msg |
| 187 | |
| 188 | t.Run("IssueWithPlanWebhookPayload", func(t *testing.T) { |
| 189 | collector.reset() |
| 190 | |
| 191 | project := ctl.createTestProject(ctx, t, "byt9398-i1") |
| 192 | addWebhookForEvents(ctx, t, ctl, project, webhookServer.URL, []v1pb.Activity_Type{v1pb.Activity_ISSUE_CREATED}) |
| 193 |
nothing calls this directly
no test coverage detected