summaryrefslogtreecommitdiffstats
path: root/models/migrations/v1_20/v259_test.go
blob: ae219ea814f9ad5ebe4f7c6ab80e9cb38d9b36c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package v1_20 //nolint

import (
	"sort"
	"strings"
	"testing"

	migration_tests "code.gitea.io/gitea/models/migrations/test"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

type testCase struct {
	Old OldAccessTokenScope
	New AccessTokenScope
}

func createOldTokenScope(scopes ...OldAccessTokenScope) OldAccessTokenScope {
	s := make([]string, 0, len(scopes))
	for _, os := range scopes {
		s = append(s, string(os))
	}
	return OldAccessTokenScope(strings.Join(s, ","))
}

func createNewTokenScope(scopes ...AccessTokenScope) AccessTokenScope {
	s := make([]string, 0, len(scopes))
	for _, os := range scopes {
		s = append(s, string(os))
	}
	return AccessTokenScope(strings.Join(s, ","))
}

func Test_ConvertScopedAccessTokens(t *testing.T) {
	tests := []testCase{
		{
			createOldTokenScope(OldAccessTokenScopeRepo, OldAccessTokenScopeUserFollow),
			createNewTokenScope(AccessTokenScopeWriteRepository, AccessTokenScopeWriteUser),
		},
		{
			createOldTokenScope(OldAccessTokenScopeUser, OldAccessTokenScopeWritePackage, OldAccessTokenScopeSudo),
			createNewTokenScope(AccessTokenScopeWriteAdmin, AccessTokenScopeWritePackage, AccessTokenScopeWriteUser),
		},
		{
			createOldTokenScope(),
			createNewTokenScope(),
		},
		{
			createOldTokenScope(OldAccessTokenScopeReadGPGKey, OldAccessTokenScopeReadOrg, OldAccessTokenScopeAll),
			createNewTokenScope(AccessTokenScopeAll),
		},
		{
			createOldTokenScope(OldAccessTokenScopeReadGPGKey, "invalid"),
			createNewTokenScope("invalid", AccessTokenScopeReadUser),
		},
	}

	// add a test for each individual mapping
	for oldScope, newScope := range accessTokenScopeMap {
		tests = append(tests, testCase{
			oldScope,
			createNewTokenScope(newScope...),
		})
	}

	x, deferable := migration_tests.PrepareTestEnv(t, 0, new(AccessToken))
	defer deferable()
	if x == nil || t.Failed() {
		t.Skip()
		return
	}

	// verify that no fixtures were loaded
	count, err := x.Count(&AccessToken{})
	require.NoError(t, err)
	assert.Equal(t, int64(0), count)

	for _, tc := range tests {
		_, err = x.Insert(&AccessToken{
			Scope: string(tc.Old),
		})
		require.NoError(t, err)
	}

	// migrate the scopes
	err = ConvertScopedAccessTokens(x)
	require.NoError(t, err)

	// migrate the scopes again (migration should be idempotent)
	err = ConvertScopedAccessTokens(x)
	require.NoError(t, err)

	tokens := make([]AccessToken, 0)
	err = x.Find(&tokens)
	require.NoError(t, err)
	assert.Equal(t, len(tests), len(tokens))

	// sort the tokens (insertion order by auto-incrementing primary key)
	sort.Slice(tokens, func(i, j int) bool {
		return tokens[i].ID < tokens[j].ID
	})

	// verify that the converted scopes are equal to the expected test result
	for idx, newToken := range tokens {
		assert.Equal(t, string(tests[idx].New), newToken.Scope)
	}
}