Skip to content

Commit

Permalink
add tests for new functionality in auth/oauth.go
Browse files Browse the repository at this point in the history
  • Loading branch information
bohde committed Nov 19, 2024
1 parent 6e876cc commit 59dedf4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
18 changes: 18 additions & 0 deletions models/fixtures/action_task.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
-
attempt: 3
runner_id: 1
status: 3 # 3 is the status code for "cancelled"
started: 1683636528
stopped: 1683636626
repo_id: 4
owner_id: 1
commit_sha: c2d72f548424103f01ee1dc02889c1e2bff816b0
is_fork_pull_request: 0
token_hash: 6d8ef48297195edcc8e22c70b3020eaa06c52976db67d39b4260c64a69a2cc1508825121b7b8394e48e00b1bf8718b2aaaaa
token_salt: eeeeeeee
token_last_eight: eeeeeeee
log_filename: artifact-test2/2f/47.log
log_in_storage: 1
log_length: 707
log_size: 90179
log_expired: 0
-
id: 47
job_id: 192
Expand Down
52 changes: 52 additions & 0 deletions services/auth/oauth2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package auth

Check failure on line 1 in services/auth/oauth2_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

Copyright not found

Check failure on line 1 in services/auth/oauth2_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

Copyright not found

import (
"context"
"testing"

"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/web/middleware"
"code.gitea.io/gitea/services/actions"

"github.com/stretchr/testify/assert"
)

func TestUserIDFromToken(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())

t.Run("Actions JWT", func(t *testing.T) {
const RunningTaskID = 47
token, err := actions.CreateAuthorizationToken(RunningTaskID, 1, 2)
assert.NoError(t, err)

ds := make(middleware.ContextData)

o := OAuth2{}
uid := o.userIDFromToken(context.Background(), token, ds)
assert.Equal(t, int64(user_model.ActionsUserID), uid)
assert.Equal(t, ds["IsActionsToken"], true)
assert.Equal(t, ds["ActionsTaskID"], int64(RunningTaskID))
})
}

func TestCheckTaskID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())

cases := map[string]struct {
TaskID int64
Expected bool
}{
"Running": {TaskID: 47, Expected: true},
"Missing": {TaskID: 1, Expected: false},
"Cancelled": {TaskID: 46, Expected: false},
}

for name := range cases {
c := cases[name]
t.Run(name, func(t *testing.T) {
actual := CheckTaskID(context.Background(), c.TaskID)
assert.Equal(t, c.Expected, actual)
})
}
}

0 comments on commit 59dedf4

Please sign in to comment.