summaryrefslogtreecommitdiffstats
path: root/tests/integration/webhook_test.go
blob: 60d4d48d7c971c935e7a473a34ade42ae00b65d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
	"net/http"
	"net/url"
	"testing"

	"code.gitea.io/gitea/models/db"
	repo_model "code.gitea.io/gitea/models/repo"
	"code.gitea.io/gitea/models/unittest"
	user_model "code.gitea.io/gitea/models/user"
	webhook_model "code.gitea.io/gitea/models/webhook"
	"code.gitea.io/gitea/modules/git"
	"code.gitea.io/gitea/modules/gitrepo"
	"code.gitea.io/gitea/modules/json"
	webhook_module "code.gitea.io/gitea/modules/webhook"
	"code.gitea.io/gitea/services/release"
	"code.gitea.io/gitea/tests"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestWebhookPayloadRef(t *testing.T) {
	onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
		w := unittest.AssertExistsAndLoadBean(t, &webhook_model.Webhook{ID: 1})
		w.HookEvent = &webhook_module.HookEvent{
			SendEverything: true,
		}
		require.NoError(t, w.UpdateEvent())
		require.NoError(t, webhook_model.UpdateWebhook(db.DefaultContext, w))

		hookTasks := retrieveHookTasks(t, w.ID, true)
		hookTasksLenBefore := len(hookTasks)

		session := loginUser(t, "user2")
		// create new branch
		csrf := GetCSRF(t, session, "user2/repo1")
		req := NewRequestWithValues(t, "POST", "user2/repo1/branches/_new/branch/master",
			map[string]string{
				"_csrf":           csrf,
				"new_branch_name": "arbre",
				"create_tag":      "false",
			},
		)
		session.MakeRequest(t, req, http.StatusSeeOther)
		// delete the created branch
		req = NewRequestWithValues(t, "POST", "user2/repo1/branches/delete?name=arbre",
			map[string]string{
				"_csrf": csrf,
			},
		)
		session.MakeRequest(t, req, http.StatusOK)

		// check the newly created hooktasks
		hookTasks = retrieveHookTasks(t, w.ID, false)
		expected := map[webhook_module.HookEventType]bool{
			webhook_module.HookEventCreate: true,
			webhook_module.HookEventPush:   true, // the branch creation also creates a push event
			webhook_module.HookEventDelete: true,
		}
		for _, hookTask := range hookTasks[:len(hookTasks)-hookTasksLenBefore] {
			if !expected[hookTask.EventType] {
				t.Errorf("unexpected (or duplicated) event %q", hookTask.EventType)
			}

			var payload struct {
				Ref string `json:"ref"`
			}
			require.NoError(t, json.Unmarshal([]byte(hookTask.PayloadContent), &payload))
			assert.Equal(t, "refs/heads/arbre", payload.Ref, "unexpected ref for %q event", hookTask.EventType)
			delete(expected, hookTask.EventType)
		}
		assert.Empty(t, expected)
	})
}

func TestWebhookReleaseEvents(t *testing.T) {
	defer tests.PrepareTestEnv(t)()

	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
	w := unittest.AssertExistsAndLoadBean(t, &webhook_model.Webhook{
		ID:     1,
		RepoID: repo.ID,
	})
	w.HookEvent = &webhook_module.HookEvent{
		SendEverything: true,
	}
	require.NoError(t, w.UpdateEvent())
	require.NoError(t, webhook_model.UpdateWebhook(db.DefaultContext, w))

	hookTasks := retrieveHookTasks(t, w.ID, true)

	gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo)
	require.NoError(t, err)
	defer gitRepo.Close()

	t.Run("CreateRelease", func(t *testing.T) {
		require.NoError(t, release.CreateRelease(gitRepo, &repo_model.Release{
			RepoID:       repo.ID,
			Repo:         repo,
			PublisherID:  user.ID,
			Publisher:    user,
			TagName:      "v1.1.1",
			Target:       "master",
			Title:        "v1.1.1 is released",
			Note:         "v1.1.1 is released",
			IsDraft:      false,
			IsPrerelease: false,
			IsTag:        false,
		}, "", nil))

		// check the newly created hooktasks
		hookTasksLenBefore := len(hookTasks)
		hookTasks = retrieveHookTasks(t, w.ID, false)

		checkHookTasks(t, map[webhook_module.HookEventType]string{
			webhook_module.HookEventRelease: "published",
			webhook_module.HookEventCreate:  "", // a tag was created as well
			webhook_module.HookEventPush:    "", // the tag creation also means a push event
		}, hookTasks[:len(hookTasks)-hookTasksLenBefore])

		t.Run("UpdateRelease", func(t *testing.T) {
			rel := unittest.AssertExistsAndLoadBean(t, &repo_model.Release{RepoID: repo.ID, TagName: "v1.1.1"})
			require.NoError(t, release.UpdateRelease(db.DefaultContext, user, gitRepo, rel, false, nil))

			// check the newly created hooktasks
			hookTasksLenBefore := len(hookTasks)
			hookTasks = retrieveHookTasks(t, w.ID, false)

			checkHookTasks(t, map[webhook_module.HookEventType]string{
				webhook_module.HookEventRelease: "updated",
			}, hookTasks[:len(hookTasks)-hookTasksLenBefore])
		})
	})

	t.Run("CreateNewTag", func(t *testing.T) {
		require.NoError(t, release.CreateNewTag(db.DefaultContext,
			user,
			repo,
			"master",
			"v1.1.2",
			"v1.1.2 is tagged",
		))

		// check the newly created hooktasks
		hookTasksLenBefore := len(hookTasks)
		hookTasks = retrieveHookTasks(t, w.ID, false)

		checkHookTasks(t, map[webhook_module.HookEventType]string{
			webhook_module.HookEventCreate: "", // tag was created as well
			webhook_module.HookEventPush:   "", // the tag creation also means a push event
		}, hookTasks[:len(hookTasks)-hookTasksLenBefore])

		t.Run("UpdateRelease", func(t *testing.T) {
			rel := unittest.AssertExistsAndLoadBean(t, &repo_model.Release{RepoID: repo.ID, TagName: "v1.1.2"})
			require.NoError(t, release.UpdateRelease(db.DefaultContext, user, gitRepo, rel, true, nil))

			// check the newly created hooktasks
			hookTasksLenBefore := len(hookTasks)
			hookTasks = retrieveHookTasks(t, w.ID, false)

			checkHookTasks(t, map[webhook_module.HookEventType]string{
				webhook_module.HookEventRelease: "published",
			}, hookTasks[:len(hookTasks)-hookTasksLenBefore])
		})
	})
}

func checkHookTasks(t *testing.T, expectedActions map[webhook_module.HookEventType]string, hookTasks []*webhook_model.HookTask) {
	t.Helper()
	for _, hookTask := range hookTasks {
		expectedAction, ok := expectedActions[hookTask.EventType]
		if !ok {
			t.Errorf("unexpected (or duplicated) event %q", hookTask.EventType)
		}
		var payload struct {
			Action string `json:"action"`
		}
		require.NoError(t, json.Unmarshal([]byte(hookTask.PayloadContent), &payload))
		assert.Equal(t, expectedAction, payload.Action, "unexpected action for %q event", hookTask.EventType)
		delete(expectedActions, hookTask.EventType)
	}
	assert.Empty(t, expectedActions)
}