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