gitea源码

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package actions
  4. import (
  5. "os"
  6. "testing"
  7. actions_model "code.gitea.io/gitea/models/actions"
  8. "code.gitea.io/gitea/models/db"
  9. "code.gitea.io/gitea/models/unittest"
  10. "code.gitea.io/gitea/modules/util"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. func TestMain(m *testing.M) {
  15. unittest.MainTest(m)
  16. os.Exit(m.Run())
  17. }
  18. func TestInitToken(t *testing.T) {
  19. assert.NoError(t, unittest.PrepareTestDatabase())
  20. t.Run("NoToken", func(t *testing.T) {
  21. _, _ = db.Exec(t.Context(), "DELETE FROM action_runner_token")
  22. t.Setenv("GITEA_RUNNER_REGISTRATION_TOKEN", "")
  23. t.Setenv("GITEA_RUNNER_REGISTRATION_TOKEN_FILE", "")
  24. err := initGlobalRunnerToken(t.Context())
  25. require.NoError(t, err)
  26. notEmpty, err := db.IsTableNotEmpty(&actions_model.ActionRunnerToken{})
  27. require.NoError(t, err)
  28. assert.False(t, notEmpty)
  29. })
  30. t.Run("EnvToken", func(t *testing.T) {
  31. tokenValue, _ := util.CryptoRandomString(32)
  32. t.Setenv("GITEA_RUNNER_REGISTRATION_TOKEN", tokenValue)
  33. t.Setenv("GITEA_RUNNER_REGISTRATION_TOKEN_FILE", "")
  34. err := initGlobalRunnerToken(t.Context())
  35. require.NoError(t, err)
  36. token := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunnerToken{Token: tokenValue})
  37. assert.True(t, token.IsActive)
  38. // init with the same token again, should not create a new token
  39. err = initGlobalRunnerToken(t.Context())
  40. require.NoError(t, err)
  41. token2 := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunnerToken{Token: tokenValue})
  42. assert.Equal(t, token.ID, token2.ID)
  43. assert.True(t, token.IsActive)
  44. })
  45. t.Run("EnvFileToken", func(t *testing.T) {
  46. tokenValue, _ := util.CryptoRandomString(32)
  47. f := t.TempDir() + "/token"
  48. _ = os.WriteFile(f, []byte(tokenValue), 0o644)
  49. t.Setenv("GITEA_RUNNER_REGISTRATION_TOKEN", "")
  50. t.Setenv("GITEA_RUNNER_REGISTRATION_TOKEN_FILE", f)
  51. err := initGlobalRunnerToken(t.Context())
  52. require.NoError(t, err)
  53. token := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunnerToken{Token: tokenValue})
  54. assert.True(t, token.IsActive)
  55. // if the env token is invalidated by another new token, then it shouldn't be active anymore
  56. _, err = actions_model.NewRunnerToken(t.Context(), 0, 0)
  57. require.NoError(t, err)
  58. token = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunnerToken{Token: tokenValue})
  59. assert.False(t, token.IsActive)
  60. })
  61. t.Run("InvalidToken", func(t *testing.T) {
  62. t.Setenv("GITEA_RUNNER_REGISTRATION_TOKEN", "abc")
  63. err := initGlobalRunnerToken(t.Context())
  64. assert.ErrorContains(t, err, "must be at least")
  65. })
  66. }