Skip to content

Commit 4613556

Browse files
committed
fix
1 parent 775421e commit 4613556

File tree

5 files changed

+59
-37
lines changed

5 files changed

+59
-37
lines changed

models/migrations/fixtures/Test_StoreWebauthnCredentialIDAsBytes/webauthn_credential.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
name: "u2fkey-correctly-migrated"
55
user_id: 1
66
credential_id: "TVHE44TOH7DF7V48SEAIT3EMMJ7TGBOQ289E5AQB34S98LFCUFJ7U2NAVI8RJG6K2F4TC8AQ8KBNO7AGEOQOL9NE43GR63HTEHJSLOG="
7-
public_key: 0x040d0967a2cad045011631187576492a0beb5b377954b4f694c5afc8bdf25270f87f09a9ab6ce9c282f447ba71b2f2bae2105b32b847e0704f310f48644e3eddf2
7+
public_key: ::HEX::040d0967a2cad045011631187576492a0beb5b377954b4f694c5afc8bdf25270f87f09a9ab6ce9c282f447ba71b2f2bae2105b32b847e0704f310f48644e3eddf2
88
attestation_type: 'fido-u2f'
99
sign_count: 1
1010
clone_warning: false
@@ -14,7 +14,7 @@
1414
name: "non-u2f-key"
1515
user_id: 1
1616
credential_id: "051CLMMKB62S6M9M2A4H54K7MMCQALFJ36G4TGB2S9A47APLTILU6C6744CEBG4EKCGV357N21BSLH8JD33GQMFAR6DQ70S76P34J6FR"
17-
public_key: 0x040d0967a2cad045011631187576492a0beb5b377954b4f694c5afc8bdf25270f87f09a9ab6ce9c282f447ba71b2f2bae2105b32b847e0704f310f48644e3eddf2
17+
public_key: ::HEX::040d0967a2cad045011631187576492a0beb5b377954b4f694c5afc8bdf25270f87f09a9ab6ce9c282f447ba71b2f2bae2105b32b847e0704f310f48644e3eddf2
1818
attestation_type: 'none'
1919
sign_count: 1
2020
clone_warning: false
@@ -24,7 +24,7 @@
2424
name: "packed-key"
2525
user_id: 1
2626
credential_id: "APU4B1NDTEVTEM60V4T0FRL7SRJMO9KIE2AKFQ8JDGTQ7VHFI41FDEFTDLBVQEAE4ER49QV2GTGVFDNBO31BPOA3OQN6879OT6MTU3G="
27-
public_key: 0x040d0967a2cad045011631187576492a0beb5b377954b4f694c5afc8bdf25270f87f09a9ab6ce9c282f447ba71b2f2bae2105b32b847e0704f310f48644e3eddf2
27+
public_key: ::HEX::040d0967a2cad045011631187576492a0beb5b377954b4f694c5afc8bdf25270f87f09a9ab6ce9c282f447ba71b2f2bae2105b32b847e0704f310f48644e3eddf2
2828
attestation_type: 'fido-u2f'
2929
sign_count: 1
3030
clone_warning: false

models/migrations/v1_16/v210_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"code.gitea.io/gitea/modules/timeutil"
1111

1212
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1314
"xorm.io/xorm/schemas"
1415
)
1516

@@ -56,10 +57,7 @@ func Test_RemigrateU2FCredentials(t *testing.T) {
5657
}
5758

5859
// Run the migration
59-
if err := RemigrateU2FCredentials(x); err != nil {
60-
assert.NoError(t, err)
61-
return
62-
}
60+
require.NoError(t, RemigrateU2FCredentials(x))
6361

6462
expected := []ExpectedWebauthnCredential{}
6563
if err := x.Table("expected_webauthn_credential").Asc("id").Find(&expected); !assert.NoError(t, err) {

models/unittest/fixtures_loader.go

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
package unittest
55

66
import (
7+
"database/sql"
78
"encoding/hex"
89
"fmt"
910
"os"
1011
"path/filepath"
1112
"strings"
1213

13-
"github.com/yuin/goldmark/util"
14+
"code.gitea.io/gitea/modules/util"
15+
1416
"gopkg.in/yaml.v3"
1517
"xorm.io/xorm"
1618
"xorm.io/xorm/schemas"
@@ -25,12 +27,22 @@ type fixturesLoader struct {
2527
func (f *fixturesLoader) prepareFieldValue(v any) any {
2628
if s, ok := v.(string); ok {
2729
if strings.HasPrefix(s, "::HEX::") {
28-
b, _ := hex.DecodeString(s[8:])
30+
b, _ := hex.DecodeString(s[7:])
2931
return b
3032
}
3133
}
3234
return v
3335
}
36+
37+
func (f *fixturesLoader) mssqlTableHasIdentityColumn(db *sql.DB, tableName string) (bool, error) {
38+
row := db.QueryRow(`SELECT COUNT(*) FROM sys.identity_columns WHERE OBJECT_ID = OBJECT_ID(?)`, tableName)
39+
var count int
40+
if err := row.Scan(&count); err != nil {
41+
return false, err
42+
}
43+
return count > 0, nil
44+
}
45+
3446
func (f *fixturesLoader) loadFixtures(file string) error {
3547
data, err := os.ReadFile(file)
3648
if err != nil {
@@ -49,12 +61,29 @@ func (f *fixturesLoader) loadFixtures(file string) error {
4961
return err
5062
}
5163

64+
goDB := f.engine.DB().DB
65+
tx, err := goDB.Begin()
66+
if err != nil {
67+
return err
68+
}
69+
defer func() {
70+
if tx != nil {
71+
_ = tx.Rollback()
72+
}
73+
}()
74+
5275
switch f.engine.Dialect().URI().DBType {
5376
case schemas.MSSQL:
54-
_, _ = f.engine.Exec("SET IDENTITY_INSERT [%s] ON", tableName)
55-
defer func() {
56-
_, _ = f.engine.Exec("SET IDENTITY_INSERT [%s] OFF", tableName)
57-
}()
77+
hasIdentityColumn, err := f.mssqlTableHasIdentityColumn(goDB, tableName)
78+
if err != nil {
79+
return err
80+
}
81+
if hasIdentityColumn {
82+
_, err = tx.Exec(fmt.Sprintf("SET IDENTITY_INSERT %s ON", tableNameQuoted))
83+
if err != nil {
84+
return err
85+
}
86+
}
5887
}
5988

6089
var sqlBuf []byte
@@ -72,19 +101,26 @@ func (f *fixturesLoader) loadFixtures(file string) error {
72101
sqlBuf = append(sqlBuf, "?,"...)
73102
}
74103
sqlBuf[len(sqlBuf)-1] = ')'
75-
_, err = f.engine.Exec(append([]any{util.BytesToReadOnlyString(sqlBuf)}, sqlArguments...)...)
104+
_, err = tx.Exec(util.UnsafeBytesToString(sqlBuf), sqlArguments...)
76105
if err != nil {
77106
return err
78107
}
79108
sqlBuf = sqlBuf[:0]
80109
sqlArguments = sqlArguments[:0]
81110
}
82-
return nil
111+
err = tx.Commit()
112+
tx = nil
113+
return err
83114
}
84115

85116
func (f *fixturesLoader) Load() error {
86-
f.quoteObject = func(s string) string {
87-
return fmt.Sprintf("`%s`", s)
117+
switch f.engine.Dialect().URI().DBType {
118+
case schemas.SQLITE, schemas.POSTGRES:
119+
f.quoteObject = func(s string) string { return fmt.Sprintf(`"%s"`, s) }
120+
case schemas.MYSQL:
121+
f.quoteObject = func(s string) string { return fmt.Sprintf("`%s`", s) }
122+
case schemas.MSSQL:
123+
f.quoteObject = func(s string) string { return fmt.Sprintf("[%s]", s) }
88124
}
89125
if len(f.opts.Files) == 0 {
90126
entries, err := os.ReadDir(f.opts.Dir)

models/unittest/testdb.go

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ type TestOptions struct {
7979

8080
// MainTest a reusable TestMain(..) function for unit tests that need to use a
8181
// test database. Creates the test database, and sets necessary settings.
82-
func MainTest(m *testing.M, testOpts ...*TestOptions) {
82+
func MainTest(m *testing.M, testOptsArg ...*TestOptions) {
83+
testOpts := util.OptionalArg(testOptsArg)
8384
searchDir, _ := os.Getwd()
8485
for searchDir != "" {
8586
if _, err := os.Stat(filepath.Join(searchDir, "go.mod")); err == nil {
@@ -99,19 +100,8 @@ func MainTest(m *testing.M, testOpts ...*TestOptions) {
99100
setting.CustomPath = filepath.Join(giteaRoot, "custom")
100101
InitSettings()
101102

102-
fixturesDir = filepath.Join(giteaRoot, "models", "fixtures")
103-
var opts FixturesOptions
104-
if len(testOpts) == 0 || len(testOpts[0].FixtureFiles) == 0 {
105-
opts.Dir = fixturesDir
106-
} else {
107-
for _, f := range testOpts[0].FixtureFiles {
108-
if len(f) != 0 {
109-
opts.Files = append(opts.Files, filepath.Join(fixturesDir, f))
110-
}
111-
}
112-
}
113-
114-
if err := CreateTestEngine(opts); err != nil {
103+
fixturesOpts := FixturesOptions{Dir: filepath.Join(giteaRoot, "models", "fixtures"), Files: testOpts.FixtureFiles}
104+
if err := CreateTestEngine(fixturesOpts); err != nil {
115105
fatalTestError("Error creating test engine: %v\n", err)
116106
}
117107

@@ -172,16 +162,16 @@ func MainTest(m *testing.M, testOpts ...*TestOptions) {
172162
fatalTestError("git.Init: %v\n", err)
173163
}
174164

175-
if len(testOpts) > 0 && testOpts[0].SetUp != nil {
176-
if err := testOpts[0].SetUp(); err != nil {
165+
if testOpts.SetUp != nil {
166+
if err := testOpts.SetUp(); err != nil {
177167
fatalTestError("set up failed: %v\n", err)
178168
}
179169
}
180170

181171
exitStatus := m.Run()
182172

183-
if len(testOpts) > 0 && testOpts[0].TearDown != nil {
184-
if err := testOpts[0].TearDown(); err != nil {
173+
if testOpts.TearDown != nil {
174+
if err := testOpts.TearDown(); err != nil {
185175
fatalTestError("tear down failed: %v\n", err)
186176
}
187177
}

modules/system/appstate_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ func (*testItem2) Name() string {
3636
}
3737

3838
func TestAppStateDB(t *testing.T) {
39-
assert.NoError(t, unittest.PrepareTestDatabase())
40-
4139
as := &DBStore{}
4240

4341
item1 := new(testItem1)

0 commit comments

Comments
 (0)