2022-11-03 14:23:20 -04:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-11-03 14:23:20 -04:00
|
|
|
|
|
|
|
package v1_19 //nolint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-07-14 09:34:42 -04:00
|
|
|
migration_tests "code.gitea.io/gitea/models/migrations/test"
|
2022-11-03 14:23:20 -04:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
|
|
|
"code.gitea.io/gitea/modules/secret"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2023-01-01 10:23:15 -05:00
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2022-11-03 14:23:20 -04:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 15:41:10 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-11-03 14:23:20 -04:00
|
|
|
)
|
|
|
|
|
2022-11-07 23:07:46 -05:00
|
|
|
func Test_AddHeaderAuthorizationEncryptedColWebhook(t *testing.T) {
|
2022-11-03 14:23:20 -04:00
|
|
|
// Create Webhook table
|
|
|
|
type Webhook struct {
|
2023-01-01 10:23:15 -05:00
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
Type webhook_module.HookType `xorm:"VARCHAR(16) 'type'"`
|
|
|
|
Meta string `xorm:"TEXT"` // store hook-specific attributes
|
2022-11-03 14:23:20 -04:00
|
|
|
|
|
|
|
// HeaderAuthorizationEncrypted should be accessed using HeaderAuthorization() and SetHeaderAuthorization()
|
|
|
|
HeaderAuthorizationEncrypted string `xorm:"TEXT"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExpectedWebhook struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
Meta string
|
|
|
|
HeaderAuthorization string
|
|
|
|
}
|
|
|
|
|
|
|
|
type HookTask struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
HookID int64
|
|
|
|
PayloadContent string `xorm:"LONGTEXT"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare and load the testing database
|
2024-07-14 09:34:42 -04:00
|
|
|
x, deferable := migration_tests.PrepareTestEnv(t, 0, new(Webhook), new(ExpectedWebhook), new(HookTask))
|
2022-11-03 14:23:20 -04:00
|
|
|
defer deferable()
|
|
|
|
if x == nil || t.Failed() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := AddHeaderAuthorizationEncryptedColWebhook(x); err != nil {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-11-03 14:23:20 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []ExpectedWebhook{}
|
2024-07-30 15:41:10 -04:00
|
|
|
err := x.Table("expected_webhook").Asc("id").Find(&expected)
|
|
|
|
require.NoError(t, err)
|
2022-11-03 14:23:20 -04:00
|
|
|
|
|
|
|
got := []Webhook{}
|
2024-07-30 15:41:10 -04:00
|
|
|
err = x.Table("webhook").Select("id, meta, header_authorization_encrypted").Asc("id").Find(&got)
|
|
|
|
require.NoError(t, err)
|
2022-11-03 14:23:20 -04:00
|
|
|
|
|
|
|
for i, e := range expected {
|
|
|
|
assert.Equal(t, e.Meta, got[i].Meta)
|
|
|
|
|
|
|
|
if e.HeaderAuthorization == "" {
|
|
|
|
assert.Equal(t, "", got[i].HeaderAuthorizationEncrypted)
|
|
|
|
} else {
|
|
|
|
cipherhex := got[i].HeaderAuthorizationEncrypted
|
|
|
|
cleartext, err := secret.DecryptSecret(setting.SecretKey, cipherhex)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-11-03 14:23:20 -04:00
|
|
|
assert.Equal(t, e.HeaderAuthorization, cleartext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure that no hook_task has some remaining "access_token"
|
|
|
|
hookTasks := []HookTask{}
|
2024-07-30 15:41:10 -04:00
|
|
|
err = x.Table("hook_task").Select("id, payload_content").Asc("id").Find(&hookTasks)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-11-03 14:23:20 -04:00
|
|
|
for _, h := range hookTasks {
|
2023-07-04 14:36:08 -04:00
|
|
|
var m map[string]any
|
2022-11-03 14:23:20 -04:00
|
|
|
err := json.Unmarshal([]byte(h.PayloadContent), &m)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-11-03 14:23:20 -04:00
|
|
|
assert.Nil(t, m["access_token"])
|
|
|
|
}
|
|
|
|
}
|