mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-07-25 02:48:05 +00:00
### Context Following the feedback in forgejo/discussions#170 (and my ambitious attempt in forgejo/forgejo#10985), it appears that having an easy-to-use factory package would greatly help get rid of the global fixtures. I think that the global fixtures are quite harmful (recent example: https://codeberg.org/forgejo/forgejo/pulls/9906#issuecomment-10826066): - hard to write (contributor must know where to add them) - hard to change (may break some unrelated tests) - hard to review (not located near the test code) - they require the tests to execute sequentially ### Proposed way forward The `forgery` package (the name represents faking/crafting and sounds good with Forgejo) is meant to replace global yaml fixtures with local go factories. The forgery can currently: - create users - create repos - create organisations This allowed me to drop `CreateDeclarativeRepoWithOptions` (and deprecate `CreateDeclarativeRepo`). I think that further changes should be delayed to other PRs (I have a local branch to create `Project`) ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - [x] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change. *The decision if the pull request will be shown in the release notes is up to the mergers / release team.* The content of the `release-notes/<pull request number>.md` file will serve as the basis for the release notes. If the file does not exist, the title of the pull request will be used instead. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11356 Reviewed-by: limiting-factor <limiting-factor@noreply.codeberg.org>
88 lines
2.5 KiB
Go
88 lines
2.5 KiB
Go
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package integration
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
|
|
auth_model "forgejo.org/models/auth"
|
|
api "forgejo.org/modules/structs"
|
|
notify_service "forgejo.org/services/notify"
|
|
"forgejo.org/tests"
|
|
"forgejo.org/tests/forgery"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestAPIRepoActivityFeeds(t *testing.T) {
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
repo := forgery.CreateRepository(t, nil, nil)
|
|
owner := repo.Owner
|
|
notify_service.CreateRepository(t.Context(), owner, owner, repo)
|
|
|
|
feedURL := fmt.Sprintf("/api/v1/repos/%s/activities/feeds", repo.FullName())
|
|
assertAndReturnActivities := func(t *testing.T, length int) []api.Activity {
|
|
t.Helper()
|
|
|
|
req := NewRequest(t, "GET", feedURL)
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
var activities []api.Activity
|
|
DecodeJSON(t, resp, &activities)
|
|
|
|
assert.Len(t, activities, length)
|
|
|
|
return activities
|
|
}
|
|
createIssue := func(t *testing.T) {
|
|
session := loginUser(t, owner.Name)
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
|
|
urlStr := fmt.Sprintf("/api/v1/repos/%s/issues?state=all", repo.FullName())
|
|
req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateIssueOption{
|
|
Title: "ActivityFeed test",
|
|
Body: "Nothing to see here!",
|
|
}).AddTokenAuth(token)
|
|
MakeRequest(t, req, http.StatusCreated)
|
|
}
|
|
|
|
t.Run("repo creation", func(t *testing.T) {
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
// Upon repo creation, there's a single activity.
|
|
assertAndReturnActivities(t, 1)
|
|
})
|
|
|
|
t.Run("single watcher, single issue", func(t *testing.T) {
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
// After creating an issue, we'll have two activities.
|
|
createIssue(t)
|
|
assertAndReturnActivities(t, 2)
|
|
})
|
|
|
|
t.Run("a new watcher, no new activities", func(t *testing.T) {
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
watcher := forgery.CreateUser(t, nil)
|
|
watcherSession := loginUser(t, watcher.Name)
|
|
watcherToken := getTokenForLoggedInUser(t, watcherSession, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeReadUser)
|
|
|
|
req := NewRequest(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/subscription", repo.FullName())).
|
|
AddTokenAuth(watcherToken)
|
|
MakeRequest(t, req, http.StatusOK)
|
|
|
|
assertAndReturnActivities(t, 2)
|
|
})
|
|
|
|
t.Run("multiple watchers, new issue", func(t *testing.T) {
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
// After creating a second issue, we'll have three activities, even
|
|
// though we have multiple watchers.
|
|
createIssue(t)
|
|
assertAndReturnActivities(t, 3)
|
|
})
|
|
}
|