aboutsummaryrefslogtreecommitdiff
path: root/internal/db/migrations/v20_test.go
blob: b95360de7d4bb584055e1bf3e86c2834412face4 (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
// Copyright 2022 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
	"testing"

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

	"gogs.io/gogs/internal/dbtest"
)

type accessTokenPreV20 struct {
	ID          int64
	UserID      int64 `gorm:"COLUMN:uid;INDEX"`
	Name        string
	Sha1        string `gorm:"TYPE:VARCHAR(40);UNIQUE"`
	CreatedUnix int64
	UpdatedUnix int64
}

func (*accessTokenPreV20) TableName() string {
	return "access_token"
}

type accessTokenV20 struct {
	ID          int64
	UserID      int64 `gorm:"column:uid;index"`
	Name        string
	Sha1        string `gorm:"type:VARCHAR(40);unique"`
	SHA256      string `gorm:"type:VARCHAR(64);unique;not null"`
	CreatedUnix int64
	UpdatedUnix int64
}

func (*accessTokenV20) TableName() string {
	return "access_token"
}

func TestMigrateAccessTokenToSHA256(t *testing.T) {
	if testing.Short() {
		t.Skip()
	}
	t.Parallel()

	db := dbtest.NewDB(t, "migrateAccessTokenToSHA256", new(accessTokenPreV20))
	err := db.Create(
		&accessTokenPreV20{
			ID:          1,
			UserID:      1,
			Name:        "test",
			Sha1:        "73da7bb9d2a475bbc2ab79da7d4e94940cb9f9d5",
			CreatedUnix: db.NowFunc().Unix(),
			UpdatedUnix: db.NowFunc().Unix(),
		},
	).Error
	require.NoError(t, err)

	err = migrateAccessTokenToSHA256(db)
	require.NoError(t, err)

	var got accessTokenV20
	err = db.Where("id = ?", 1).First(&got).Error
	require.NoError(t, err)
	assert.Equal(t, "73da7bb9d2a475bbc2ab79da7d4e94940cb9f9d5", got.Sha1)
	assert.Equal(t, "ab144c7bd170691bb9bb995f1541c608e33a78b40174f30fc8a1616c0bc3a477", got.SHA256)
}