summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorNanguan Lin <70063547+lng2020@users.noreply.github.com>2023-10-19 15:24:24 +0800
committerGitHub <noreply@github.com>2023-10-19 07:24:24 +0000
commit80a0ab350bf22b9af283698704b3c3dec812ff83 (patch)
tree8c8b2f24b920706eea5d715b81ac4c7f78bfc9e7 /models
parent776b0928639fc3fbf152b831b377349433ee7acc (diff)
downloadgitea-80a0ab350bf22b9af283698704b3c3dec812ff83.tar.gz
gitea-80a0ab350bf22b9af283698704b3c3dec812ff83.zip
Add unit tests for action runner token (#27670)
In case the behavior of the register token changes.
Diffstat (limited to 'models')
-rw-r--r--models/actions/main_test.go18
-rw-r--r--models/actions/runner_token_test.go40
-rw-r--r--models/fixtures/action_runner_token.yml35
3 files changed, 93 insertions, 0 deletions
diff --git a/models/actions/main_test.go b/models/actions/main_test.go
new file mode 100644
index 0000000000..5d5089e3bb
--- /dev/null
+++ b/models/actions/main_test.go
@@ -0,0 +1,18 @@
+// Copyright 2023 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package actions
+
+import (
+ "testing"
+
+ "code.gitea.io/gitea/models/unittest"
+)
+
+func TestMain(m *testing.M) {
+ unittest.MainTest(m, &unittest.TestOptions{
+ FixtureFiles: []string{
+ "action_runner_token.yml",
+ },
+ })
+}
diff --git a/models/actions/runner_token_test.go b/models/actions/runner_token_test.go
new file mode 100644
index 0000000000..e85e99abe5
--- /dev/null
+++ b/models/actions/runner_token_test.go
@@ -0,0 +1,40 @@
+// Copyright 2023 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package actions
+
+import (
+ "testing"
+
+ "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestGetLatestRunnerToken(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+ token := unittest.AssertExistsAndLoadBean(t, &ActionRunnerToken{ID: 3})
+ expectedToken, err := GetLatestRunnerToken(db.DefaultContext, 1, 0)
+ assert.NoError(t, err)
+ assert.EqualValues(t, token, expectedToken)
+}
+
+func TestNewRunnerToken(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+ token, err := NewRunnerToken(db.DefaultContext, 1, 0)
+ assert.NoError(t, err)
+ expectedToken, err := GetLatestRunnerToken(db.DefaultContext, 1, 0)
+ assert.NoError(t, err)
+ assert.EqualValues(t, token, expectedToken)
+}
+
+func TestUpdateRunnerToken(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+ token := unittest.AssertExistsAndLoadBean(t, &ActionRunnerToken{ID: 3})
+ token.IsActive = true
+ assert.NoError(t, UpdateRunnerToken(db.DefaultContext, token))
+ expectedToken, err := GetLatestRunnerToken(db.DefaultContext, 1, 0)
+ assert.NoError(t, err)
+ assert.EqualValues(t, token, expectedToken)
+}
diff --git a/models/fixtures/action_runner_token.yml b/models/fixtures/action_runner_token.yml
new file mode 100644
index 0000000000..6520b7f6fb
--- /dev/null
+++ b/models/fixtures/action_runner_token.yml
@@ -0,0 +1,35 @@
+-
+ id: 1 # instance scope
+ token: xeiWBL5kuTYxGPynHCqQdoeYmJAeG3IzGXCYTrDX
+ owner_id: 0
+ repo_id: 0
+ is_active: 1
+ created: 1695617748
+ updated: 1695617748
+
+-
+ id: 2 # user scope and can't be used
+ token: vohJB9QcZuSv1gAXESTk2uqpSjHhsKT9j4zYF84x
+ owner_id: 1
+ repo_id: 0
+ is_active: 0
+ created: 1695617749
+ updated: 1695617749
+
+-
+ id: 3 # user scope and can be used
+ token: gjItAeJ3CA74hNPmPPo0Zco8I1eMaNcP1jVifjOE
+ owner_id: 1
+ repo_id: 0
+ is_active: 1
+ created: 1695617750
+ updated: 1695617750
+
+-
+ id: 4 # repo scope
+ token: NOjLubxzFxPGhPXflZknys0gjVvQNhomFbAYuhbH
+ owner_id: 0
+ repo_id: 1
+ is_active: 1
+ created: 1695617751
+ updated: 1695617751