jojo/services/actions/secret_test.go
Mathieu Fenniak 9b2f7c557b feat: support jobs.<job_id>.secrets with reusable workflow expansion (#10627)
Follow-up to #10525; adds support for `jobs.<job_id>.secrets` to expanded reusable workflows (when no `runs-on` is specified in a job that `uses: ...` another workflow).

## 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.
  - [ ] 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)).
- **end-to-end testing**: [prepared, PR n](https://code.forgejo.org/forgejo/end-to-end/pulls/1351)

### 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.
    - [ ] Doc to be created
- [ ] 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/10627
Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
2025-12-30 17:33:21 +01:00

108 lines
3.1 KiB
Go

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package actions
import (
"testing"
actions_model "forgejo.org/models/actions"
secret_model "forgejo.org/models/secret"
"forgejo.org/models/unittest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetSecretsOfJob(t *testing.T) {
tests := []struct {
name string
runJobID int64
secrets map[string]string
}{
{
name: "push run",
runJobID: 600,
secrets: map[string]string{
"SECRET_1": "the sky is blue",
"SECRET_2": "the ocean is also blue",
},
},
{
name: "on: pull_request_target workflow, local PR (not fork)",
runJobID: 601,
secrets: map[string]string{
"SECRET_1": "the sky is blue",
"SECRET_2": "the ocean is also blue",
},
},
{
name: "on: pull_request_target workflow, fork PR",
runJobID: 602,
secrets: map[string]string{
"SECRET_1": "the sky is blue",
"SECRET_2": "the ocean is also blue",
},
},
{
name: "on: pull_request workflow, local PR (not fork)",
runJobID: 603,
secrets: map[string]string{
"SECRET_1": "the sky is blue",
"SECRET_2": "the ocean is also blue",
},
},
{
name: "on: pull_request workflow, fork PR",
runJobID: 604,
secrets: map[string]string{},
},
{
name: "workflow call inner job, inherit secrets",
runJobID: 605,
secrets: map[string]string{
"SECRET_1": "the sky is blue",
"SECRET_2": "the ocean is also blue",
},
},
{
name: "workflow call two layer inner job, inherit secrets",
runJobID: 607,
secrets: map[string]string{
// Even though we're 'inherit' in this case, we're inheriting from the parent call which is a subset
// (and modification) of the secrets -- so shouldn't see SECRET_2.
"SECRET_1": "the sky is blue -- but are you sure?",
},
},
{
name: "workflow call inner job, defined secrets",
runJobID: 610,
secrets: map[string]string{
"FORGEJO": "context forgejo = refs/heads/main",
"INPUTS": "context inputs = some_wd_input_value",
"MATRIX": "context matrix = some-dimension-value",
"NEEDS": "context needs = abcdefghijklmnopqrstuvwxyz",
"SECRETS": "context secrets = the sky is blue",
"STRATEGY": "context strategy = false",
"VARS": "context vars = this is a repo variable",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer unittest.OverrideFixtures("services/actions/TestGetSecretsOfJob")()
require.NoError(t, unittest.PrepareTestDatabase())
// Due to encryption, more maintainable to do this rather than create secrets in fixture data
_, err := secret_model.InsertEncryptedSecret(t.Context(), 2, 0, "secret_1", "the sky is blue")
require.NoError(t, err)
_, err = secret_model.InsertEncryptedSecret(t.Context(), 0, 63, "secret_2", "the ocean is also blue")
require.NoError(t, err)
runJob := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunJob{ID: tt.runJobID})
actualSecrets, err := getSecretsOfJob(t.Context(), runJob)
require.NoError(t, err)
assert.Equal(t, tt.secrets, actualSecrets)
})
}
}