Some checks failed
Build and Release / Create Release (push) Successful in 0s
Trigger Vault Plugin Rebuild / Trigger Vault Rebuild (push) Successful in 0s
Build and Release / Integration Tests (PostgreSQL) (push) Successful in 2m48s
Build and Release / Lint (push) Failing after 5m2s
Build and Release / Build Binaries (amd64, windows, windows-latest) (push) Has been skipped
Build and Release / Build Binaries (amd64, darwin, linux-latest) (push) Has been skipped
Build and Release / Build Binaries (amd64, linux, linux-latest) (push) Has been skipped
Build and Release / Build Binaries (arm64, darwin, linux-latest) (push) Has been skipped
Build and Release / Build Binaries (arm64, linux, linux-latest) (push) Has been skipped
Build and Release / Unit Tests (push) Successful in 5m37s
Go's semantic import versioning requires v2+ modules to include the major version in the module path. This enables using proper version tags (v3.x.x) instead of pseudo-versions. Updated module path: code.gitcaddy.com/server/v3
79 lines
2.6 KiB
Go
79 lines
2.6 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package actions
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
actions_model "code.gitcaddy.com/server/v3/models/actions"
|
|
"code.gitcaddy.com/server/v3/models/db"
|
|
"code.gitcaddy.com/server/v3/models/unittest"
|
|
"code.gitcaddy.com/server/v3/modules/util"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
unittest.MainTest(m)
|
|
os.Exit(m.Run())
|
|
}
|
|
|
|
func TestInitToken(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
t.Run("NoToken", func(t *testing.T) {
|
|
_, _ = db.Exec(t.Context(), "DELETE FROM action_runner_token")
|
|
t.Setenv("GITEA_RUNNER_REGISTRATION_TOKEN", "")
|
|
t.Setenv("GITEA_RUNNER_REGISTRATION_TOKEN_FILE", "")
|
|
err := initGlobalRunnerToken(t.Context())
|
|
require.NoError(t, err)
|
|
notEmpty, err := db.IsTableNotEmpty(&actions_model.ActionRunnerToken{})
|
|
require.NoError(t, err)
|
|
assert.False(t, notEmpty)
|
|
})
|
|
|
|
t.Run("EnvToken", func(t *testing.T) {
|
|
tokenValue, _ := util.CryptoRandomString(32)
|
|
t.Setenv("GITEA_RUNNER_REGISTRATION_TOKEN", tokenValue)
|
|
t.Setenv("GITEA_RUNNER_REGISTRATION_TOKEN_FILE", "")
|
|
err := initGlobalRunnerToken(t.Context())
|
|
require.NoError(t, err)
|
|
token := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunnerToken{Token: tokenValue})
|
|
assert.True(t, token.IsActive)
|
|
|
|
// init with the same token again, should not create a new token
|
|
err = initGlobalRunnerToken(t.Context())
|
|
require.NoError(t, err)
|
|
token2 := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunnerToken{Token: tokenValue})
|
|
assert.Equal(t, token.ID, token2.ID)
|
|
assert.True(t, token.IsActive)
|
|
})
|
|
|
|
t.Run("EnvFileToken", func(t *testing.T) {
|
|
tokenValue, _ := util.CryptoRandomString(32)
|
|
f := t.TempDir() + "/token"
|
|
_ = os.WriteFile(f, []byte(tokenValue), 0o644)
|
|
t.Setenv("GITEA_RUNNER_REGISTRATION_TOKEN", "")
|
|
t.Setenv("GITEA_RUNNER_REGISTRATION_TOKEN_FILE", f)
|
|
err := initGlobalRunnerToken(t.Context())
|
|
require.NoError(t, err)
|
|
token := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunnerToken{Token: tokenValue})
|
|
assert.True(t, token.IsActive)
|
|
|
|
// if the env token is invalidated by another new token, then it shouldn't be active anymore
|
|
_, err = actions_model.NewRunnerToken(t.Context(), 0, 0)
|
|
require.NoError(t, err)
|
|
token = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunnerToken{Token: tokenValue})
|
|
assert.False(t, token.IsActive)
|
|
})
|
|
|
|
t.Run("InvalidToken", func(t *testing.T) {
|
|
t.Setenv("GITEA_RUNNER_REGISTRATION_TOKEN", "abc")
|
|
err := initGlobalRunnerToken(t.Context())
|
|
assert.ErrorContains(t, err, "must be at least")
|
|
})
|
|
}
|