|
| 1 | +// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package integration |
| 5 | + |
| 6 | +import ( |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/models/db" |
| 12 | + "code.gitea.io/gitea/models/unittest" |
| 13 | + webhook_model "code.gitea.io/gitea/models/webhook" |
| 14 | + "code.gitea.io/gitea/modules/json" |
| 15 | + webhook_module "code.gitea.io/gitea/modules/webhook" |
| 16 | + |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | +) |
| 19 | + |
| 20 | +func TestWebhookPayloadRef(t *testing.T) { |
| 21 | + onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { |
| 22 | + w := unittest.AssertExistsAndLoadBean(t, &webhook_model.Webhook{ID: 1}) |
| 23 | + w.HookEvent = &webhook_module.HookEvent{ |
| 24 | + SendEverything: true, |
| 25 | + } |
| 26 | + assert.NoError(t, w.UpdateEvent()) |
| 27 | + assert.NoError(t, webhook_model.UpdateWebhook(db.DefaultContext, w)) |
| 28 | + |
| 29 | + hookTasks := retrieveHookTasks(t, w.ID, true) |
| 30 | + hookTasksLenBefore := len(hookTasks) |
| 31 | + |
| 32 | + session := loginUser(t, "user2") |
| 33 | + // create new branch |
| 34 | + csrf := GetCSRF(t, session, "user2/repo1") |
| 35 | + req := NewRequestWithValues(t, "POST", "user2/repo1/branches/_new/branch/master", |
| 36 | + map[string]string{ |
| 37 | + "_csrf": csrf, |
| 38 | + "new_branch_name": "arbre", |
| 39 | + "create_tag": "false", |
| 40 | + }, |
| 41 | + ) |
| 42 | + session.MakeRequest(t, req, http.StatusSeeOther) |
| 43 | + // delete the created branch |
| 44 | + req = NewRequestWithValues(t, "POST", "user2/repo1/branches/delete?name=arbre", |
| 45 | + map[string]string{ |
| 46 | + "_csrf": csrf, |
| 47 | + }, |
| 48 | + ) |
| 49 | + session.MakeRequest(t, req, http.StatusOK) |
| 50 | + |
| 51 | + // check the newly created hooktasks |
| 52 | + hookTasks = retrieveHookTasks(t, w.ID, false) |
| 53 | + expected := map[webhook_module.HookEventType]bool{ |
| 54 | + webhook_module.HookEventCreate: true, |
| 55 | + webhook_module.HookEventPush: true, // the branch creation also creates a push event |
| 56 | + webhook_module.HookEventDelete: true, |
| 57 | + } |
| 58 | + for _, hookTask := range hookTasks[:len(hookTasks)-hookTasksLenBefore] { |
| 59 | + if !expected[hookTask.EventType] { |
| 60 | + t.Errorf("unexpected (or duplicated) event %q", hookTask.EventType) |
| 61 | + } |
| 62 | + |
| 63 | + var payload struct { |
| 64 | + Ref string `json:"ref"` |
| 65 | + } |
| 66 | + assert.NoError(t, json.Unmarshal([]byte(hookTask.PayloadContent), &payload)) |
| 67 | + assert.Equal(t, "refs/heads/arbre", payload.Ref, "unexpected ref for %q event", hookTask.EventType) |
| 68 | + delete(expected, hookTask.EventType) |
| 69 | + } |
| 70 | + assert.Empty(t, expected) |
| 71 | + }) |
| 72 | +} |
0 commit comments