mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-03-26 02:33:03 -04:00
This PR is part of a series (#11311). Adds support for reading and creating repo-secific access tokens through the API via the `GET /users/{username}/tokens`, `POST /users/{username}/tokens`, and `DELETE /users/{username}/tokens/{id}` APIs. Validation rules are included to [restrict repo-specific access tokens to specific scopes](https://codeberg.org/forgejo/design/issues/50#issuecomment-11093951). ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests for Go changes (can be removed for JavaScript changes) - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I ran... - [x] `make pr-go` before pushing ### 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 - [x] 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. - [ ] 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. <!--start release-notes-assistant--> ## Release notes <!--URL:https://codeberg.org/forgejo/forgejo--> - Features - [PR](https://codeberg.org/forgejo/forgejo/pulls/11504): <!--number 11504 --><!--line 0 --><!--description cmVhZCwgY3JlYXRlLCAmIGRlbGV0ZSByZXBvLXNwZWNpZmljIGFjY2VzcyB0b2tlbnMgdmlhIEFQSQ==-->read, create, & delete repo-specific access tokens via API<!--description--> <!--end release-notes-assistant--> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11504 Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org> Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net> Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
123 lines
4 KiB
Go
123 lines
4 KiB
Go
// Copyright 2026 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package auth_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
auth_model "forgejo.org/models/auth"
|
|
"forgejo.org/models/unittest"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGetRepositoriesAccessibleWithToken(t *testing.T) {
|
|
defer unittest.OverrideFixtures("models/auth/TestGetRepositoriesAccessibleWithToken")()
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
t.Run("No Resources", func(t *testing.T) {
|
|
resources, err := auth_model.GetRepositoriesAccessibleWithToken(t.Context(), 999)
|
|
require.NoError(t, err)
|
|
assert.Empty(t, resources)
|
|
})
|
|
|
|
t.Run("Has Resources", func(t *testing.T) {
|
|
resources, err := auth_model.GetRepositoriesAccessibleWithToken(t.Context(), 3)
|
|
require.NoError(t, err)
|
|
require.Len(t, resources, 3)
|
|
|
|
// Verify all expected repo IDs are present
|
|
repoIDs := make([]int64, len(resources))
|
|
for i, res := range resources {
|
|
repoIDs[i] = res.RepoID
|
|
}
|
|
assert.Contains(t, repoIDs, int64(1))
|
|
assert.Contains(t, repoIDs, int64(2))
|
|
assert.Contains(t, repoIDs, int64(3))
|
|
})
|
|
}
|
|
|
|
func TestGetRepositoriesAccessibleWithTokens(t *testing.T) {
|
|
defer unittest.OverrideFixtures("models/auth/TestGetRepositoriesAccessibleWithTokens")()
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
token1 := unittest.AssertExistsAndLoadBean(t, &auth_model.AccessToken{ID: 1})
|
|
token2 := unittest.AssertExistsAndLoadBean(t, &auth_model.AccessToken{ID: 2})
|
|
token3 := unittest.AssertExistsAndLoadBean(t, &auth_model.AccessToken{ID: 3})
|
|
|
|
t.Run("No Tokens", func(t *testing.T) {
|
|
resources, err := auth_model.GetRepositoriesAccessibleWithTokens(t.Context(), []*auth_model.AccessToken{})
|
|
require.NoError(t, err)
|
|
assert.Empty(t, resources)
|
|
})
|
|
|
|
t.Run("Multiple Access Tokens", func(t *testing.T) {
|
|
resources, err := auth_model.GetRepositoriesAccessibleWithTokens(t.Context(), []*auth_model.AccessToken{token1, token2, token3})
|
|
require.NoError(t, err)
|
|
|
|
repos1, ok := resources[token1.ID]
|
|
assert.False(t, ok)
|
|
assert.Empty(t, repos1)
|
|
|
|
repos2, ok := resources[token2.ID]
|
|
assert.True(t, ok)
|
|
assert.Len(t, repos2, 2)
|
|
|
|
repos3, ok := resources[token3.ID]
|
|
assert.True(t, ok)
|
|
assert.Len(t, repos3, 3)
|
|
})
|
|
}
|
|
|
|
func TestInsertAccessTokenResourceRepos(t *testing.T) {
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
token1 := unittest.AssertExistsAndLoadBean(t, &auth_model.AccessToken{ID: 1})
|
|
token2 := unittest.AssertExistsAndLoadBean(t, &auth_model.AccessToken{ID: 2})
|
|
token3 := unittest.AssertExistsAndLoadBean(t, &auth_model.AccessToken{ID: 3})
|
|
|
|
t.Run("blank insert", func(t *testing.T) {
|
|
err := auth_model.InsertAccessTokenResourceRepos(t.Context(), token1.ID, nil)
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
t.Run("multiple insert", func(t *testing.T) {
|
|
resRepo1 := &auth_model.AccessTokenResourceRepo{
|
|
TokenID: token2.ID,
|
|
RepoID: 1,
|
|
}
|
|
resRepo3 := &auth_model.AccessTokenResourceRepo{
|
|
TokenID: token2.ID,
|
|
RepoID: 3,
|
|
}
|
|
err := auth_model.InsertAccessTokenResourceRepos(t.Context(), token2.ID,
|
|
[]*auth_model.AccessTokenResourceRepo{resRepo1, resRepo3})
|
|
require.NoError(t, err)
|
|
|
|
unittest.AssertCount(t, &auth_model.AccessTokenResourceRepo{TokenID: token2.ID}, 2)
|
|
})
|
|
|
|
t.Run("in tx", func(t *testing.T) {
|
|
// Pre-condition: count is 0.
|
|
unittest.AssertCount(t, &auth_model.AccessTokenResourceRepo{TokenID: token3.ID}, 0)
|
|
|
|
// Verify that InsertAccessTokenResourceRepos performs inserts in a TX by having a second one with an invalid
|
|
// RepoID, causing a foreign key violation
|
|
resRepo1 := &auth_model.AccessTokenResourceRepo{
|
|
TokenID: token3.ID,
|
|
RepoID: 1,
|
|
}
|
|
resRepo3 := &auth_model.AccessTokenResourceRepo{
|
|
TokenID: token3.ID,
|
|
RepoID: 30000, // invalid
|
|
}
|
|
err := auth_model.InsertAccessTokenResourceRepos(t.Context(), token3.ID,
|
|
[]*auth_model.AccessTokenResourceRepo{resRepo1, resRepo3})
|
|
require.ErrorContains(t, err, "foreign key")
|
|
|
|
// Count remains 0; the first record was not inserted.
|
|
unittest.AssertCount(t, &auth_model.AccessTokenResourceRepo{TokenID: token3.ID}, 0)
|
|
})
|
|
}
|