gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package v1_20
  4. import (
  5. "sort"
  6. "strings"
  7. "testing"
  8. "code.gitea.io/gitea/models/migrations/base"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. type testCase struct {
  12. Old OldAccessTokenScope
  13. New AccessTokenScope
  14. }
  15. func createOldTokenScope(scopes ...OldAccessTokenScope) OldAccessTokenScope {
  16. s := make([]string, 0, len(scopes))
  17. for _, os := range scopes {
  18. s = append(s, string(os))
  19. }
  20. return OldAccessTokenScope(strings.Join(s, ","))
  21. }
  22. func createNewTokenScope(scopes ...AccessTokenScope) AccessTokenScope {
  23. s := make([]string, 0, len(scopes))
  24. for _, os := range scopes {
  25. s = append(s, string(os))
  26. }
  27. return AccessTokenScope(strings.Join(s, ","))
  28. }
  29. func Test_ConvertScopedAccessTokens(t *testing.T) {
  30. tests := []testCase{
  31. {
  32. createOldTokenScope(OldAccessTokenScopeRepo, OldAccessTokenScopeUserFollow),
  33. createNewTokenScope(AccessTokenScopeWriteRepository, AccessTokenScopeWriteUser),
  34. },
  35. {
  36. createOldTokenScope(OldAccessTokenScopeUser, OldAccessTokenScopeWritePackage, OldAccessTokenScopeSudo),
  37. createNewTokenScope(AccessTokenScopeWriteAdmin, AccessTokenScopeWritePackage, AccessTokenScopeWriteUser),
  38. },
  39. {
  40. createOldTokenScope(),
  41. createNewTokenScope(),
  42. },
  43. {
  44. createOldTokenScope(OldAccessTokenScopeReadGPGKey, OldAccessTokenScopeReadOrg, OldAccessTokenScopeAll),
  45. createNewTokenScope(AccessTokenScopeAll),
  46. },
  47. {
  48. createOldTokenScope(OldAccessTokenScopeReadGPGKey, "invalid"),
  49. createNewTokenScope("invalid", AccessTokenScopeReadUser),
  50. },
  51. }
  52. // add a test for each individual mapping
  53. for oldScope, newScope := range accessTokenScopeMap {
  54. tests = append(tests, testCase{
  55. oldScope,
  56. createNewTokenScope(newScope...),
  57. })
  58. }
  59. x, deferable := base.PrepareTestEnv(t, 0, new(AccessToken))
  60. defer deferable()
  61. if x == nil || t.Failed() {
  62. t.Skip()
  63. return
  64. }
  65. // verify that no fixtures were loaded
  66. count, err := x.Count(&AccessToken{})
  67. assert.NoError(t, err)
  68. assert.Equal(t, int64(0), count)
  69. for _, tc := range tests {
  70. _, err = x.Insert(&AccessToken{
  71. Scope: string(tc.Old),
  72. })
  73. assert.NoError(t, err)
  74. }
  75. // migrate the scopes
  76. err = ConvertScopedAccessTokens(x)
  77. assert.NoError(t, err)
  78. // migrate the scopes again (migration should be idempotent)
  79. err = ConvertScopedAccessTokens(x)
  80. assert.NoError(t, err)
  81. tokens := make([]AccessToken, 0)
  82. err = x.Find(&tokens)
  83. assert.NoError(t, err)
  84. assert.Len(t, tokens, len(tests))
  85. // sort the tokens (insertion order by auto-incrementing primary key)
  86. sort.Slice(tokens, func(i, j int) bool {
  87. return tokens[i].ID < tokens[j].ID
  88. })
  89. // verify that the converted scopes are equal to the expected test result
  90. for idx, newToken := range tokens {
  91. assert.Equal(t, string(tests[idx].New), newToken.Scope)
  92. }
  93. }