jojo/routers/web/user/setting/access_token_test.go
Mathieu Fenniak 35b872f383 feat(ui): create repo-specific access tokens (#11696)
Adds a user interface for creating repo-specific access tokens (#11311).  When the new option "Specific repositories" is selected, a search option appears.  Each repository in the search result has an "Add" button to include it on the access token, and once included, a repository can be removed with the "Remove" button.  This is a JS-free form.

## 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.
  - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I ran...
  - [x] `make pr-go` before pushing

### Tests for JavaScript changes

- I added test coverage for JavaScript changes...
  - [ ] in `web_src/js/*.test.js` if it can be unit tested.
  - [x] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/
README.md#end-to-end-tests)).
    - Technically there are no "JavaScript changes" in this PR, but e2e tests were added for browser interaction testing.

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
    - TODO: planning to create documentation in https://forgejo.org/docs/next/user/token-scope/; there is none for public only tokens but I think this seems like a good place to add both.
- [ ] 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.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11696
Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
2026-03-23 15:29:08 +01:00

159 lines
6.1 KiB
Go

// Copyright 2026 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package setting
import (
"net/http"
"net/http/httptest"
"slices"
"testing"
repo_model "forgejo.org/models/repo"
"forgejo.org/models/unittest"
"forgejo.org/modules/templates"
"forgejo.org/modules/web"
"forgejo.org/services/context"
"forgejo.org/services/contexttest"
"forgejo.org/services/forms"
"code.forgejo.org/go-chi/binding"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAccessTokenCreate(t *testing.T) {
unittest.PrepareTestEnv(t)
ctx, resp := contexttest.MockContext(t, "user/settings/applications/tokens/new",
contexttest.MockContextOption{Render: templates.HTMLRenderer()})
contexttest.LoadUser(t, ctx, 2)
web.SetForm(ctx, &forms.NewAccessTokenGetForm{})
AccessTokenCreate(ctx)
assert.Equal(t, http.StatusOK, resp.Result().StatusCode)
assert.False(t, ctx.HasError(), "error: %s", ctx.GetErrMsg())
// check empty-form GET ctx.Data values
assert.Equal(t, "name", ctx.Data["Autofocus"])
assert.Len(t, ctx.Data["Repos"], 10)
assert.Empty(t, ctx.Data["SelectedRepos"])
// check that repo in the search is rendered in the content
assert.Contains(t, resp.Body.String(), "org17/big_test_private_4")
}
func TestAccessTokenCreatePost(t *testing.T) {
unittest.PrepareTestEnv(t)
post := func(t *testing.T, form *forms.NewAccessTokenPostForm) (*context.Context, *httptest.ResponseRecorder) {
t.Helper()
ctx, resp := contexttest.MockContext(t, "user/settings/applications/tokens/new",
contexttest.MockContextOption{Render: templates.HTMLRenderer()})
contexttest.LoadUser(t, ctx, 2)
ctx.AppendContextValue(context.WebContextKey, ctx)
binding.Bind(ctx.Req.WithContext(ctx), form)
web.SetForm(ctx, form)
AccessTokenCreatePost(ctx)
return ctx, resp
}
render := func(t *testing.T, form *forms.NewAccessTokenPostForm) (*context.Context, string) {
t.Helper()
ctx, resp := post(t, form)
assert.Equal(t, http.StatusOK, resp.Result().StatusCode)
return ctx, resp.Body.String()
}
t.Run("retains form info on missing token name", func(t *testing.T) {
ctx, body := render(t, &forms.NewAccessTokenPostForm{
Name: "", // absent
Resource: "repo-specific",
Scope: []string{"read:repository"},
SelectedRepo: []string{"org17/big_test_private_4"},
})
require.Contains(t, body, "settings.token_nameform.require_error")
assert.Equal(t, "repo-specific", ctx.Data["resource"])
assert.Contains(t, ctx.Data["scope"], "read:repository")
assert.True(t, slices.ContainsFunc(ctx.Data["SelectedRepos"].([]*repo_model.Repository), func(r *repo_model.Repository) bool {
return r.OwnerName == "org17" && r.Name == "big_test_private_4"
}), "SelectedRepos missing org17/big_test_private_4")
})
t.Run("retains form info on missing scopes", func(t *testing.T) {
ctx, body := render(t, &forms.NewAccessTokenPostForm{
Name: "my new token",
Resource: "repo-specific",
Scope: []string{"", "", ""}, // absent
SelectedRepo: []string{"org17/big_test_private_4"},
})
require.Contains(t, body, "settings.at_least_one_permission")
assert.Equal(t, "my new token", ctx.Data["name"])
assert.Equal(t, "repo-specific", ctx.Data["resource"])
assert.True(t, slices.ContainsFunc(ctx.Data["SelectedRepos"].([]*repo_model.Repository), func(r *repo_model.Repository) bool {
return r.OwnerName == "org17" && r.Name == "big_test_private_4"
}), "SelectedRepos missing org17/big_test_private_4")
})
t.Run("retains form info on duplicate token name", func(t *testing.T) {
ctx, body := render(t, &forms.NewAccessTokenPostForm{
Name: "Token A", // duplicate
Resource: "repo-specific",
Scope: []string{"read:repository"},
SelectedRepo: []string{"org17/big_test_private_4"},
})
require.Contains(t, body, "settings.generate_token_name_duplicate")
assert.Equal(t, "Token A", ctx.Data["name"])
assert.Equal(t, "repo-specific", ctx.Data["resource"])
assert.Contains(t, ctx.Data["scope"], "read:repository")
assert.True(t, slices.ContainsFunc(ctx.Data["SelectedRepos"].([]*repo_model.Repository), func(r *repo_model.Repository) bool {
return r.OwnerName == "org17" && r.Name == "big_test_private_4"
}), "SelectedRepos missing org17/big_test_private_4")
})
t.Run("retains form info on ValidateAccessToken error", func(t *testing.T) {
ctx, body := render(t, &forms.NewAccessTokenPostForm{
Name: "my new token",
Resource: "repo-specific",
Scope: []string{"read:admin"}, // not permitted for repo-specific
SelectedRepo: []string{"org17/big_test_private_4"},
})
require.Contains(t, body, "access_token.error.specified_repos_and_invalid_scope")
assert.Equal(t, "my new token", ctx.Data["name"])
assert.Equal(t, "repo-specific", ctx.Data["resource"])
assert.Contains(t, ctx.Data["scope"], "read:admin")
assert.True(t, slices.ContainsFunc(ctx.Data["SelectedRepos"].([]*repo_model.Repository), func(r *repo_model.Repository) bool {
return r.OwnerName == "org17" && r.Name == "big_test_private_4"
}), "SelectedRepos missing org17/big_test_private_4")
})
t.Run("invalid repo selected", func(t *testing.T) {
_, resp := post(t, &forms.NewAccessTokenPostForm{
Name: "my new token",
Resource: "repo-specific",
Scope: []string{"read:admin"}, // not permitted for repo-specific
SelectedRepo: []string{"org17/big_test_private_4000000_does_not_exist"},
})
assert.Equal(t, http.StatusBadRequest, resp.Result().StatusCode)
assert.Equal(t, "GetRepositoryByOwnerAndName\n", resp.Body.String())
})
t.Run("non-visible repo selected via IDOR", func(t *testing.T) {
_, resp := post(t, &forms.NewAccessTokenPostForm{
Name: "my new token",
Resource: "repo-specific",
Scope: []string{"read:repository"},
SelectedRepo: []string{"user30/empty"}, // private repo, user2 has no visibility
})
assert.Equal(t, http.StatusBadRequest, resp.Result().StatusCode)
assert.Equal(t, "GetRepositoryByOwnerAndName\n", resp.Body.String()) // should be exact same response as "invalid repo selected" case
})
}