2021-06-14 19:20:43 +02:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-14 19:20:43 +02:00
|
|
|
|
2022-06-06 10:01:49 +02:00
|
|
|
package repo_test
|
2021-06-14 19:20:43 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-07-30 18:45:59 +02:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-06 10:01:49 +02:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 15:36:47 +01:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-06-14 19:20:43 +02:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 21:41:10 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-06-14 19:20:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPushMirrorsIterate(t *testing.T) {
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2021-06-14 19:20:43 +02:00
|
|
|
|
|
|
|
now := timeutil.TimeStampNow()
|
|
|
|
|
2023-12-25 21:25:29 +01:00
|
|
|
db.Insert(db.DefaultContext, &repo_model.PushMirror{
|
2021-06-14 19:20:43 +02:00
|
|
|
RemoteName: "test-1",
|
|
|
|
LastUpdateUnix: now,
|
|
|
|
Interval: 1,
|
|
|
|
})
|
|
|
|
|
|
|
|
long, _ := time.ParseDuration("24h")
|
2023-12-25 21:25:29 +01:00
|
|
|
db.Insert(db.DefaultContext, &repo_model.PushMirror{
|
2021-06-14 19:20:43 +02:00
|
|
|
RemoteName: "test-2",
|
|
|
|
LastUpdateUnix: now,
|
|
|
|
Interval: long,
|
|
|
|
})
|
|
|
|
|
2023-12-25 21:25:29 +01:00
|
|
|
db.Insert(db.DefaultContext, &repo_model.PushMirror{
|
2021-06-14 19:20:43 +02:00
|
|
|
RemoteName: "test-3",
|
|
|
|
LastUpdateUnix: now,
|
|
|
|
Interval: 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
|
2023-07-04 20:36:08 +02:00
|
|
|
repo_model.PushMirrorsIterate(db.DefaultContext, 1, func(idx int, bean any) error {
|
2022-06-06 10:01:49 +02:00
|
|
|
m, ok := bean.(*repo_model.PushMirror)
|
2021-06-14 19:20:43 +02:00
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "test-1", m.RemoteName)
|
|
|
|
assert.Equal(t, m.RemoteName, m.GetRemoteName())
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2024-08-04 20:46:05 +02:00
|
|
|
|
|
|
|
func TestPushMirrorPrivatekey(t *testing.T) {
|
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
m := &repo_model.PushMirror{
|
|
|
|
RemoteName: "test-privatekey",
|
|
|
|
}
|
|
|
|
require.NoError(t, db.Insert(db.DefaultContext, m))
|
|
|
|
|
|
|
|
privateKey := []byte{0x00, 0x01, 0x02, 0x04, 0x08, 0x10}
|
|
|
|
t.Run("Set privatekey", func(t *testing.T) {
|
|
|
|
require.NoError(t, m.SetPrivatekey(db.DefaultContext, privateKey))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Normal retrieval", func(t *testing.T) {
|
|
|
|
actualPrivateKey, err := m.Privatekey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, privateKey, actualPrivateKey)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Incorrect retrieval", func(t *testing.T) {
|
|
|
|
m.ID++
|
|
|
|
actualPrivateKey, err := m.Privatekey()
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Empty(t, actualPrivateKey)
|
|
|
|
})
|
|
|
|
}
|