mirror of
				https://codeberg.org/forgejo/forgejo.git
				synced 2025-10-31 06:21:11 +00:00 
			
		
		
		
	Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8422 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: Renovate Bot <forgejo-renovate-action@forgejo.org> Co-committed-by: Renovate Bot <forgejo-renovate-action@forgejo.org>
		
			
				
	
	
		
			54 lines
		
	
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			54 lines
		
	
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2024 The Forgejo Authors. All rights reserved.
 | |
| // SPDX-License-Identifier: MIT
 | |
| 
 | |
| package forgejo_migrations
 | |
| 
 | |
| import (
 | |
| 	"testing"
 | |
| 
 | |
| 	"forgejo.org/models/auth"
 | |
| 	migration_tests "forgejo.org/models/migrations/test"
 | |
| 	"forgejo.org/modules/keying"
 | |
| 	"forgejo.org/modules/timeutil"
 | |
| 
 | |
| 	"github.com/stretchr/testify/assert"
 | |
| 	"github.com/stretchr/testify/require"
 | |
| )
 | |
| 
 | |
| func Test_MigrateTwoFactorToKeying(t *testing.T) {
 | |
| 	type TwoFactor struct { //revive:disable-line:exported
 | |
| 		ID               int64 `xorm:"pk autoincr"`
 | |
| 		UID              int64 `xorm:"UNIQUE"`
 | |
| 		Secret           string
 | |
| 		ScratchSalt      string
 | |
| 		ScratchHash      string
 | |
| 		LastUsedPasscode string             `xorm:"VARCHAR(10)"`
 | |
| 		CreatedUnix      timeutil.TimeStamp `xorm:"INDEX created"`
 | |
| 		UpdatedUnix      timeutil.TimeStamp `xorm:"INDEX updated"`
 | |
| 	}
 | |
| 
 | |
| 	// Prepare and load the testing database
 | |
| 	x, deferable := migration_tests.PrepareTestEnv(t, 0, new(TwoFactor))
 | |
| 	defer deferable()
 | |
| 	if x == nil || t.Failed() {
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	cnt, err := x.Table("two_factor").Count()
 | |
| 	require.NoError(t, err)
 | |
| 	assert.EqualValues(t, 2, cnt)
 | |
| 
 | |
| 	require.NoError(t, MigrateTwoFactorToKeying(x))
 | |
| 
 | |
| 	cnt, err = x.Table("two_factor").Count()
 | |
| 	require.NoError(t, err)
 | |
| 	assert.EqualValues(t, 1, cnt)
 | |
| 
 | |
| 	var twofactor auth.TwoFactor
 | |
| 	_, err = x.Table("two_factor").ID(1).Get(&twofactor)
 | |
| 	require.NoError(t, err)
 | |
| 
 | |
| 	secretBytes, err := keying.DeriveKey(keying.ContextTOTP).Decrypt(twofactor.Secret, keying.ColumnAndID("secret", twofactor.ID))
 | |
| 	require.NoError(t, err)
 | |
| 	assert.Equal(t, []byte("AVDYS32OPIAYSNBG2NKYV4AHBVEMKKKIGBQ46OXTLMJO664G4TIECOGEANMSNBLS"), secretBytes)
 | |
| }
 |