public_keys_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package database
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "gogs.io/gogs/internal/conf"
  11. )
  12. func TestPublicKeys(t *testing.T) {
  13. if testing.Short() {
  14. t.Skip()
  15. }
  16. t.Parallel()
  17. ctx := context.Background()
  18. s := &PublicKeysStore{
  19. db: newTestDB(t, "PublicKeysStore"),
  20. }
  21. for _, tc := range []struct {
  22. name string
  23. test func(t *testing.T, ctx context.Context, s *PublicKeysStore)
  24. }{
  25. {"RewriteAuthorizedKeys", publicKeysRewriteAuthorizedKeys},
  26. } {
  27. t.Run(tc.name, func(t *testing.T) {
  28. t.Cleanup(func() {
  29. err := clearTables(t, s.db)
  30. require.NoError(t, err)
  31. })
  32. tc.test(t, ctx, s)
  33. })
  34. if t.Failed() {
  35. break
  36. }
  37. }
  38. }
  39. func publicKeysRewriteAuthorizedKeys(t *testing.T, ctx context.Context, s *PublicKeysStore) {
  40. // TODO: Use PublicKeys.Add to replace SQL hack when the method is available.
  41. publicKey := &PublicKey{
  42. OwnerID: 1,
  43. Name: "test-key",
  44. Fingerprint: "12:f8:7e:78:61:b4:bf:e2:de:24:15:96:4e:d4:72:53",
  45. Content: "test-key-content",
  46. }
  47. err := s.db.Create(publicKey).Error
  48. require.NoError(t, err)
  49. tempSSHRootPath := filepath.Join(os.TempDir(), "publicKeysRewriteAuthorizedKeys-tempSSHRootPath")
  50. conf.SetMockSSH(t, conf.SSHOpts{RootPath: tempSSHRootPath})
  51. err = s.RewriteAuthorizedKeys()
  52. require.NoError(t, err)
  53. authorizedKeys, err := os.ReadFile(authorizedKeysPath())
  54. require.NoError(t, err)
  55. assert.Contains(t, string(authorizedKeys), fmt.Sprintf("key-%d", publicKey.ID))
  56. assert.Contains(t, string(authorizedKeys), publicKey.Content)
  57. }