Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for action runner token #27670

Merged
merged 12 commits into from
Oct 19, 2023
18 changes: 18 additions & 0 deletions models/actions/main_test.go
Original file line number Diff line number Diff line change
@@ -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",
},
})
}
40 changes: 40 additions & 0 deletions models/actions/runner_token_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
35 changes: 35 additions & 0 deletions models/fixtures/action_runner_token.yml
Original file line number Diff line number Diff line change
@@ -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