Skip to content

Commit ce7d574

Browse files
committed
fix
1 parent 775421e commit ce7d574

File tree

5 files changed

+73
-41
lines changed

5 files changed

+73
-41
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: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,46 @@
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"
1719
)
1820

1921
type fixturesLoader struct {
20-
engine *xorm.Engine
21-
opts FixturesOptions
22-
quoteObject func(string) string
22+
engine *xorm.Engine
23+
opts FixturesOptions
24+
quoteObject func(string) string
25+
paramPlaceholder func(idx int) string
2326
}
2427

2528
func (f *fixturesLoader) prepareFieldValue(v any) any {
2629
if s, ok := v.(string); ok {
2730
if strings.HasPrefix(s, "::HEX::") {
28-
b, _ := hex.DecodeString(s[8:])
31+
b, _ := hex.DecodeString(s[7:])
2932
return b
3033
}
3134
}
3235
return v
3336
}
37+
38+
func (f *fixturesLoader) mssqlTableHasIdentityColumn(db *sql.DB, tableName string) (bool, error) {
39+
row := db.QueryRow(`SELECT COUNT(*) FROM sys.identity_columns WHERE OBJECT_ID = OBJECT_ID(?)`, tableName)
40+
var count int
41+
if err := row.Scan(&count); err != nil {
42+
return false, err
43+
}
44+
return count > 0, nil
45+
}
46+
3447
func (f *fixturesLoader) loadFixtures(file string) error {
3548
data, err := os.ReadFile(file)
3649
if err != nil {
@@ -49,12 +62,29 @@ func (f *fixturesLoader) loadFixtures(file string) error {
4962
return err
5063
}
5164

65+
goDB := f.engine.DB().DB
66+
tx, err := goDB.Begin()
67+
if err != nil {
68+
return err
69+
}
70+
defer func() {
71+
if tx != nil {
72+
_ = tx.Rollback()
73+
}
74+
}()
75+
5276
switch f.engine.Dialect().URI().DBType {
5377
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-
}()
78+
hasIdentityColumn, err := f.mssqlTableHasIdentityColumn(goDB, tableName)
79+
if err != nil {
80+
return err
81+
}
82+
if hasIdentityColumn {
83+
_, err = tx.Exec(fmt.Sprintf("SET IDENTITY_INSERT %s ON", tableNameQuoted))
84+
if err != nil {
85+
return err
86+
}
87+
}
5888
}
5989

6090
var sqlBuf []byte
@@ -68,23 +98,39 @@ func (f *fixturesLoader) loadFixtures(file string) error {
6898
}
6999
sqlBuf = sqlBuf[:len(sqlBuf)-1]
70100
sqlBuf = append(sqlBuf, ") VALUES ("...)
101+
paramIdx := 1
71102
for range item {
72-
sqlBuf = append(sqlBuf, "?,"...)
103+
sqlBuf = append(sqlBuf, f.paramPlaceholder(paramIdx)...)
104+
sqlBuf = append(sqlBuf, ',')
105+
paramIdx++
73106
}
74107
sqlBuf[len(sqlBuf)-1] = ')'
75-
_, err = f.engine.Exec(append([]any{util.BytesToReadOnlyString(sqlBuf)}, sqlArguments...)...)
108+
_, err = tx.Exec(util.UnsafeBytesToString(sqlBuf), sqlArguments...)
76109
if err != nil {
77110
return err
78111
}
79112
sqlBuf = sqlBuf[:0]
80113
sqlArguments = sqlArguments[:0]
81114
}
82-
return nil
115+
err = tx.Commit()
116+
tx = nil
117+
return err
83118
}
84119

85120
func (f *fixturesLoader) Load() error {
86-
f.quoteObject = func(s string) string {
87-
return fmt.Sprintf("`%s`", s)
121+
switch f.engine.Dialect().URI().DBType {
122+
case schemas.SQLITE:
123+
f.quoteObject = func(s string) string { return fmt.Sprintf(`"%s"`, s) }
124+
f.paramPlaceholder = func(idx int) string { return "?" }
125+
case schemas.POSTGRES:
126+
f.quoteObject = func(s string) string { return fmt.Sprintf(`"%s"`, s) }
127+
f.paramPlaceholder = func(idx int) string { return fmt.Sprintf(`$%d`, idx) }
128+
case schemas.MYSQL:
129+
f.quoteObject = func(s string) string { return fmt.Sprintf("`%s`", s) }
130+
f.paramPlaceholder = func(idx int) string { return "?" }
131+
case schemas.MSSQL:
132+
f.quoteObject = func(s string) string { return fmt.Sprintf("[%s]", s) }
133+
f.paramPlaceholder = func(idx int) string { return "?" }
88134
}
89135
if len(f.opts.Files) == 0 {
90136
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, &TestOptions{})
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)