gitea源码

v210_test.go 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package v1_16
  4. import (
  5. "testing"
  6. "code.gitea.io/gitea/models/migrations/base"
  7. "code.gitea.io/gitea/modules/timeutil"
  8. "github.com/stretchr/testify/assert"
  9. "xorm.io/xorm/schemas"
  10. )
  11. func Test_RemigrateU2FCredentials(t *testing.T) {
  12. // Create webauthnCredential table
  13. type WebauthnCredential struct {
  14. ID int64 `xorm:"pk autoincr"`
  15. Name string
  16. LowerName string `xorm:"unique(s)"`
  17. UserID int64 `xorm:"INDEX unique(s)"`
  18. CredentialID string `xorm:"INDEX VARCHAR(410)"` // CredentalID in U2F is at most 255bytes / 5 * 8 = 408 - add a few extra characters for safety
  19. PublicKey []byte
  20. AttestationType string
  21. SignCount uint32 `xorm:"BIGINT"`
  22. CloneWarning bool
  23. }
  24. // Now migrate the old u2f registrations to the new format
  25. type U2fRegistration struct {
  26. ID int64 `xorm:"pk autoincr"`
  27. Name string
  28. UserID int64 `xorm:"INDEX"`
  29. Raw []byte
  30. Counter uint32 `xorm:"BIGINT"`
  31. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  32. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  33. }
  34. type ExpectedWebauthnCredential struct {
  35. ID int64 `xorm:"pk autoincr"`
  36. CredentialID string `xorm:"INDEX VARCHAR(410)"` // CredentalID in U2F is at most 255bytes / 5 * 8 = 408 - add a few extra characters for safety
  37. }
  38. // Prepare and load the testing database
  39. x, deferable := base.PrepareTestEnv(t, 0, new(WebauthnCredential), new(U2fRegistration), new(ExpectedWebauthnCredential))
  40. if x == nil || t.Failed() {
  41. defer deferable()
  42. return
  43. }
  44. defer deferable()
  45. if x.Dialect().URI().DBType == schemas.SQLITE {
  46. return
  47. }
  48. // Run the migration
  49. if err := RemigrateU2FCredentials(x); err != nil {
  50. assert.NoError(t, err)
  51. return
  52. }
  53. expected := []ExpectedWebauthnCredential{}
  54. if err := x.Table("expected_webauthn_credential").Asc("id").Find(&expected); !assert.NoError(t, err) {
  55. return
  56. }
  57. got := []ExpectedWebauthnCredential{}
  58. if err := x.Table("webauthn_credential").Select("id, credential_id").Asc("id").Find(&got); !assert.NoError(t, err) {
  59. return
  60. }
  61. assert.Equal(t, expected, got)
  62. }