github.com/hairyhenderson/templater@v3.5.0+incompatible/aws/kms_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/service/kms"
     8  	b64 "github.com/hairyhenderson/gomplate/base64"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  // MockKMS is a mock KMSAPI implementation
    13  type MockKMS struct{}
    14  
    15  // Mocks Encrypt operation returns an upper case version of plaintext
    16  func (m *MockKMS) Encrypt(input *kms.EncryptInput) (*kms.EncryptOutput, error) {
    17  	return &kms.EncryptOutput{
    18  		CiphertextBlob: []byte(strings.ToUpper(string(input.Plaintext))),
    19  	}, nil
    20  }
    21  
    22  // Mocks Decrypt operation
    23  func (m *MockKMS) Decrypt(input *kms.DecryptInput) (*kms.DecryptOutput, error) {
    24  	s := []byte(strings.ToLower(string(input.CiphertextBlob)))
    25  	return &kms.DecryptOutput{
    26  		Plaintext: s,
    27  	}, nil
    28  }
    29  
    30  func TestEncrypt(t *testing.T) {
    31  	// create a mock KMS client
    32  	c := &MockKMS{}
    33  	kmsClient := &KMS{Client: c}
    34  
    35  	// Success
    36  	resp, err := kmsClient.Encrypt("dummykey", "plaintextvalue")
    37  	assert.NoError(t, err)
    38  	expectedResp, _ := b64.Encode([]byte("PLAINTEXTVALUE"))
    39  	assert.EqualValues(t, expectedResp, resp)
    40  }
    41  
    42  func TestDecrypt(t *testing.T) {
    43  	// create a mock KMS client
    44  	c := &MockKMS{}
    45  	kmsClient := &KMS{Client: c}
    46  	encodedCiphertextBlob, _ := b64.Encode([]byte("CIPHERVALUE"))
    47  
    48  	// Success
    49  	resp, err := kmsClient.Decrypt(encodedCiphertextBlob)
    50  	assert.NoError(t, err)
    51  	assert.EqualValues(t, "ciphervalue", resp)
    52  }