forgejo/services/secrets/validation_test.go
zokki eeb47e3340 fix: actions variable and secret names validation (#10682)
Fixed action variables and secrets according to [Docu](https://forgejo.org/docs/next/user/actions/basic-concepts/#name-constraints):
> Variable names must not start with the FORGEJO_, GITHUB_ or GITEA_ prefix.
This wasn't correctly enforced, so I changed the regex

## 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

- 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 added test coverage for JavaScript changes...
  - [ ] in `web_src/js/*.test.js` if it can be unit tested.
  - [ ] 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)).

### Documentation

- [x] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [ ] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [ ] I do not want this change to show in the release notes.
- [x] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10682
Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org>
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
Co-authored-by: zokki <zokki.softwareschmiede@gmail.com>
Co-committed-by: zokki <zokki.softwareschmiede@gmail.com>
2026-01-14 04:19:21 +01:00

57 lines
1.2 KiB
Go

// Copyright 2026 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package secrets
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestValidateName(t *testing.T) {
testCases := []struct {
name string
valid bool
}{
{"FORGEJO_", false},
{"FORGEJO_123", false},
{"FORGEJO_ABC", false},
{"GITEA_", false},
{"GITEA_123", false},
{"GITEA_ABC", false},
{"GITHUB_", false},
{"GITHUB_123", false},
{"GITHUB_ABC", false},
{"123_TEST", false},
{"CI", true},
{"_CI", true},
{"CI_", true},
{"CI123", true},
{"CIABC", true},
{"FORGEJO", true},
{"FORGEJO123", true},
{"FORGEJOABC", true},
{"GITEA", true},
{"GITEA123", true},
{"GITEAABC", true},
{"GITHUB", true},
{"GITHUB123", true},
{"GITHUBABC", true},
{"_123_TEST", true},
}
for _, tC := range testCases {
t.Run(tC.name, func(t *testing.T) {
t.Helper()
if tC.valid {
assert.NoError(t, ValidateName(tC.name))
assert.NoError(t, ValidateName(strings.ToLower(tC.name)))
} else {
require.ErrorIs(t, ValidateName(tC.name), ErrInvalidName)
require.ErrorIs(t, ValidateName(strings.ToLower(tC.name)), ErrInvalidName)
}
})
}
}