jx/internal/resource/openpgp_keyring_test.go
Matthew Rich 07808c62fd
Some checks failed
Declarative Tests / test (push) Waiting to run
Lint / golangci-lint (push) Has been cancelled
add support for remote command execution
2024-11-10 10:24:06 -08:00

109 lines
4.2 KiB
Go

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package resource
import (
"context"
"github.com/stretchr/testify/assert"
"testing"
"fmt"
"decl/internal/data"
)
func TestNewOpenPGPKeyRingResource(t *testing.T) {
assert.NotNil(t, NewOpenPGPKeyRing())
}
func TestCreateKeyRing(t *testing.T) {
ctx := context.Background()
declarationAttributes := `
name: TestUser1
comment: TestUser1
email: testuser@rosskeen.house
`
testKeyRing := NewOpenPGPKeyRing()
e := testKeyRing.LoadDecl(declarationAttributes)
assert.Nil(t, e)
testKeyRing.UseConfig(MockConfigValueGetter(func(key string) (any, error) {
switch key {
case "passphrase":
return "foo", nil
}
return nil, fmt.Errorf("%w: %s", data.ErrUnknownConfigurationKey, key)
}))
err := testKeyRing.Create(ctx)
assert.Nil(t, err)
assert.Greater(t, len(testKeyRing.entityList), 0)
assert.Contains(t, testKeyRing.entityList[0].Identities, "TestUser1 (TestUser1) <testuser@rosskeen.house>")
assert.Contains(t, testKeyRing.KeyRing, "-----END PGP PUBLIC KEY BLOCK-----")
assert.Contains(t, testKeyRing.KeyRing, "-----END PGP PRIVATE KEY BLOCK-----")
assert.True(t, testKeyRing.IsEncrypted(0))
}
func TestReadKeyRing(t *testing.T) {
ctx := context.Background()
declarationAttributes := `
keyring: |-
-----BEGIN PGP PUBLIC KEY BLOCK-----
mQGNBGctCH8BDADGmdabVG6gDuSRk0razrEMEproTMT5w9zUMWH5uUeLY9eM9g5S
/5I062ume5jj6MIC1lq7tqJXh3Zwcv7Lf7ER1SWa1h6BGruHaF4o9GiR01FHfyVM
YTMTkMxFi1wnY87Mr0f+EIv1i9u2nD1o9moBXzEXT0JFFGyla8DvnblQhLhrwfNl
lN0L2LQJDTVnrPj4eMaFshqP2UdqNi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3T01zrNRUo6I7xu5kA==
=yFbn
-----END PGP PUBLIC KEY BLOCK-----
`
testKeyRing := NewOpenPGPKeyRing()
e := testKeyRing.LoadDecl(declarationAttributes)
assert.Nil(t, e)
y, err := testKeyRing.Read(ctx)
assert.Nil(t, err)
assert.NotNil(t, y)
assert.Greater(t, len(testKeyRing.entityList), 0)
assert.Contains(t, testKeyRing.entityList[0].Identities, "TestUser (TestUser) <matthewrich.conf@gmail.com>")
}