Skip to content

Commit 80a0ab3

Browse files
authored
Add unit tests for action runner token (#27670)
In case the behavior of the register token changes.
1 parent 776b092 commit 80a0ab3

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

models/actions/main_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package actions
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/unittest"
10+
)
11+
12+
func TestMain(m *testing.M) {
13+
unittest.MainTest(m, &unittest.TestOptions{
14+
FixtureFiles: []string{
15+
"action_runner_token.yml",
16+
},
17+
})
18+
}

models/actions/runner_token_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package actions
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/db"
10+
"code.gitea.io/gitea/models/unittest"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestGetLatestRunnerToken(t *testing.T) {
16+
assert.NoError(t, unittest.PrepareTestDatabase())
17+
token := unittest.AssertExistsAndLoadBean(t, &ActionRunnerToken{ID: 3})
18+
expectedToken, err := GetLatestRunnerToken(db.DefaultContext, 1, 0)
19+
assert.NoError(t, err)
20+
assert.EqualValues(t, token, expectedToken)
21+
}
22+
23+
func TestNewRunnerToken(t *testing.T) {
24+
assert.NoError(t, unittest.PrepareTestDatabase())
25+
token, err := NewRunnerToken(db.DefaultContext, 1, 0)
26+
assert.NoError(t, err)
27+
expectedToken, err := GetLatestRunnerToken(db.DefaultContext, 1, 0)
28+
assert.NoError(t, err)
29+
assert.EqualValues(t, token, expectedToken)
30+
}
31+
32+
func TestUpdateRunnerToken(t *testing.T) {
33+
assert.NoError(t, unittest.PrepareTestDatabase())
34+
token := unittest.AssertExistsAndLoadBean(t, &ActionRunnerToken{ID: 3})
35+
token.IsActive = true
36+
assert.NoError(t, UpdateRunnerToken(db.DefaultContext, token))
37+
expectedToken, err := GetLatestRunnerToken(db.DefaultContext, 1, 0)
38+
assert.NoError(t, err)
39+
assert.EqualValues(t, token, expectedToken)
40+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
-
2+
id: 1 # instance scope
3+
token: xeiWBL5kuTYxGPynHCqQdoeYmJAeG3IzGXCYTrDX
4+
owner_id: 0
5+
repo_id: 0
6+
is_active: 1
7+
created: 1695617748
8+
updated: 1695617748
9+
10+
-
11+
id: 2 # user scope and can't be used
12+
token: vohJB9QcZuSv1gAXESTk2uqpSjHhsKT9j4zYF84x
13+
owner_id: 1
14+
repo_id: 0
15+
is_active: 0
16+
created: 1695617749
17+
updated: 1695617749
18+
19+
-
20+
id: 3 # user scope and can be used
21+
token: gjItAeJ3CA74hNPmPPo0Zco8I1eMaNcP1jVifjOE
22+
owner_id: 1
23+
repo_id: 0
24+
is_active: 1
25+
created: 1695617750
26+
updated: 1695617750
27+
28+
-
29+
id: 4 # repo scope
30+
token: NOjLubxzFxPGhPXflZknys0gjVvQNhomFbAYuhbH
31+
owner_id: 0
32+
repo_id: 1
33+
is_active: 1
34+
created: 1695617751
35+
updated: 1695617751

0 commit comments

Comments
 (0)