diff options
author | Joe Chen <jc@unknwon.io> | 2023-02-07 23:39:00 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-07 23:39:00 +0800 |
commit | 133b9d90441008ee175e1f8e6369e06309e1392a (patch) | |
tree | 70f29798055962f6700ba6a93b023a8328a5eff4 /internal/db/public_keys_test.go | |
parent | 7c453d5b3632a6bbdbd99205c518303a9e25a4e1 (diff) |
refactor(db): finish migrate methods off `user.go` (#7337)
Diffstat (limited to 'internal/db/public_keys_test.go')
-rw-r--r-- | internal/db/public_keys_test.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/internal/db/public_keys_test.go b/internal/db/public_keys_test.go new file mode 100644 index 00000000..1e83014b --- /dev/null +++ b/internal/db/public_keys_test.go @@ -0,0 +1,69 @@ +// Copyright 2023 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 db + +import ( + "fmt" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "gogs.io/gogs/internal/conf" + "gogs.io/gogs/internal/dbtest" +) + +func TestPublicKeys(t *testing.T) { + if testing.Short() { + t.Skip() + } + t.Parallel() + + tables := []any{new(PublicKey)} + db := &publicKeys{ + DB: dbtest.NewDB(t, "publicKeys", tables...), + } + + for _, tc := range []struct { + name string + test func(t *testing.T, db *publicKeys) + }{ + {"RewriteAuthorizedKeys", publicKeysRewriteAuthorizedKeys}, + } { + t.Run(tc.name, func(t *testing.T) { + t.Cleanup(func() { + err := clearTables(t, db.DB, tables...) + require.NoError(t, err) + }) + tc.test(t, db) + }) + if t.Failed() { + break + } + } +} + +func publicKeysRewriteAuthorizedKeys(t *testing.T, db *publicKeys) { + // TODO: Use PublicKeys.Add to replace SQL hack when the method is available. + publicKey := &PublicKey{ + OwnerID: 1, + Name: "test-key", + Fingerprint: "12:f8:7e:78:61:b4:bf:e2:de:24:15:96:4e:d4:72:53", + Content: "test-key-content", + } + err := db.DB.Create(publicKey).Error + require.NoError(t, err) + tempSSHRootPath := filepath.Join(os.TempDir(), "publicKeysRewriteAuthorizedKeys-tempSSHRootPath") + conf.SetMockSSH(t, conf.SSHOpts{RootPath: tempSSHRootPath}) + err = db.RewriteAuthorizedKeys() + require.NoError(t, err) + + authorizedKeys, err := os.ReadFile(authorizedKeysPath()) + require.NoError(t, err) + assert.Contains(t, string(authorizedKeys), fmt.Sprintf("key-%d", publicKey.ID)) + assert.Contains(t, string(authorizedKeys), publicKey.Content) +} |