1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-22 12:54:53 -05:00
forgejo/tests/integration/api_repo_activities_test.go
Gusted f78e397dd6
[TESTS] Move CreateDeclarativeRepo to more accessible location
- This allows `CreateDeclarativeRepo` to be used by other testing
packages such as E2EE testing.
- Removes unused function in `services/webhook/sourcehut/builds_test.go`.
2024-08-25 02:54:43 +02:00

88 lines
2.7 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 "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/tests"
"github.com/stretchr/testify/assert"
)
func TestAPIRepoActivitiyFeeds(t *testing.T) {
defer tests.PrepareTestEnv(t)()
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, owner, tests.DeclarativeRepoOptions{})
defer f()
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 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
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)
})
}