package utils

import (
	"testing"

	"github.com/stretchr/testify/require"
)

func Test_DecryptAnsibleVault(t *testing.T) {
	ansibleVaultEncrypted := []byte{
		36, 65, 78, 83, 73, 66, 76, 69, 95, 86, 65, 85, 76, 84, 59, 49, 46, 49, 59, 65, 69, 83, 50, 53, 54, 13, 10, 54, 53, 51, 50, 51, 51, 54, 53,
		51, 54, 54, 53, 54, 51, 54, 52, 51, 54, 51, 51, 51, 52, 54, 49, 51, 56, 51, 48, 51, 51, 51, 53, 54, 51, 51, 50, 54, 52, 54, 49, 51, 53, 51,
		48, 51, 54, 54, 52, 51, 52, 51, 54, 51, 57, 51, 57, 54, 54, 51, 48, 51, 55, 51, 51, 51, 57, 51, 53, 51, 50, 54, 51, 51, 57, 51, 56, 51, 56,
		51, 51, 13, 10, 51, 48, 51, 53, 51, 55, 51, 55, 51, 57, 51, 53, 51, 52, 51, 57, 54, 54, 51, 56, 51, 52, 51, 56, 51, 51, 51, 57, 54, 51, 54,
		50, 51, 54, 54, 49, 51, 49, 51, 50, 54, 53, 54, 53, 51, 50, 54, 50, 48, 97, 51, 52, 51, 50, 54, 51, 54, 54, 54, 49, 51, 55, 51, 51, 51, 57,
		51, 50, 51, 49, 51, 48, 51, 50, 54, 51, 51, 54, 51, 53, 13, 10, 54, 54, 51, 49, 54, 49, 51, 52, 51, 56, 54, 51, 54, 52, 54, 53, 51, 55, 51,
		54, 51, 53, 54, 53, 51, 49, 51, 50, 54, 50, 54, 54, 51, 49, 54, 50, 51, 52, 51, 48, 51, 51, 51, 51, 51, 54, 54, 53, 51, 49, 54, 51, 51, 57,
		54, 49, 51, 54, 51, 49, 51, 55, 54, 50, 51, 56, 51, 50, 51, 57, 54, 49, 51, 53, 51, 57, 51, 48, 54, 50, 13, 10, 54, 54, 51, 56, 51, 53, 51,
		50, 51, 54, 54, 51, 54, 52, 51, 52, 54, 52, 48, 97, 54, 53, 51, 52, 51, 55, 51, 48, 51, 49, 51, 49, 54, 49, 51, 48, 54, 54, 54, 50, 54, 54,
		51, 55, 51, 53, 51, 49, 51, 52, 54, 52, 51, 52, 51, 50, 51, 55, 54, 54, 51, 49, 51, 57, 51, 55, 54, 53, 51, 50, 54, 49, 51, 53, 54, 54, 54,
		52, 54, 50, 13, 10, 54, 54, 51, 54, 54, 52, 51, 56, 54, 49, 51, 48, 54, 51, 51, 57, 54, 51, 51, 54, 51, 54, 51, 52, 54, 50, 54, 50, 51, 54,
		51, 57, 51, 52, 54, 49, 51, 57, 51, 48, 51, 53, 51, 55, 51, 48, 51, 57, 54, 50, 51, 49, 51, 54, 51, 54, 51, 57, 51, 55, 51, 48, 51, 49, 51,
		52, 51, 53, 51, 50, 51, 56, 51, 49, 51, 52, 51, 57, 51, 53, 13, 10, 51, 57, 54, 53, 54, 52, 51, 50, 51, 52, 51, 57, 54, 51, 51, 48, 51, 48,
		54, 52, 54, 52, 51, 51, 51, 50, 51, 55, 51, 56, 54, 54, 54, 53, 51, 53, 51, 48, 54, 51, 54, 54, 51, 52, 54, 54, 54, 50, 51, 48, 51, 55, 54,
		50, 51, 48, 51, 51, 51, 57, 54, 51, 54, 50, 51, 50, 54, 49, 54, 51, 51, 52, 54, 53, 54, 50, 54, 52, 51, 56, 13, 10, 54, 53, 51, 48, 54, 53,
		51, 52, 54, 54, 51, 56, 51, 48, 51, 52, 54, 49, 51, 57, 51, 57, 54, 49, 51, 56, 51, 54, 51, 54, 54, 54, 51, 48, 54, 54, 51, 53, 51, 51, 51,
		54, 51, 56, 54, 51, 54, 50, 54, 54, 51, 52, 54, 52, 51, 52, 51, 53, 51, 50, 54, 54, 54, 53, 51, 54, 54, 54, 51, 48, 51, 52, 51, 52, 51, 52,
		51, 50, 51, 48, 13, 10, 54, 49, 54, 51, 54, 49, 51, 51, 54, 51, 51, 52, 51, 52, 54, 53, 51, 56, 51, 48, 54, 52, 51, 53, 54, 51, 54, 51, 51,
		52, 51, 55, 51, 57, 51, 56, 51, 53, 54, 50, 54, 53, 51, 51, 51, 51, 54, 53, 51, 49, 51, 56, 54, 49, 51, 50, 51, 54, 51, 51, 51, 54, 54, 51,
		51, 56, 51, 53, 54, 50, 54, 52, 51, 52, 51, 57, 54, 50, 51, 52, 13, 10, 51, 54, 51, 51, 51, 48, 54, 49, 51, 56, 54, 52, 51, 48, 51, 53, 51,
		55, 51, 57, 51, 57, 51, 52, 51, 57, 54, 50, 54, 53, 51, 56, 54, 49, 51, 49, 51, 51, 54, 54, 51, 55, 51, 49, 54, 51, 51, 48, 51, 50, 51, 50,
		54, 51, 54, 54, 54, 54, 54, 49, 51, 48, 54, 50, 54, 54, 54, 50, 51, 52, 51, 50, 51, 53, 54, 53, 51, 54, 51, 57, 13, 10, 54, 54, 51, 51, 51,
		54, 51, 54, 51, 53, 51, 49, 51, 48, 51, 56, 54, 50, 54, 51, 51, 57, 51, 55, 51, 55, 51, 50, 54, 49, 51, 50, 51, 55, 54, 49, 51, 54, 51, 49,
		51, 52, 54, 52, 51, 57, 54, 52, 54, 49, 51, 57, 51, 51, 51, 55, 51, 51, 51, 54, 51, 54, 51, 49, 54, 50, 51, 55, 51, 52, 51, 48, 51, 56, 51,
		53, 51, 52, 51, 48, 13, 10, 51, 55, 51, 54, 51, 52, 51, 54, 54, 54, 51, 48, 54, 51, 51, 48, 51, 54, 51, 50, 51, 49, 54, 51, 51, 50, 54, 54,
		54, 54, 54, 53, 54, 54, 51, 54, 13, 10,
	}

	tests := []struct {
		name                  string
		secret                string
		ansibleVaultDecrypted []byte
	}{
		{
			name:   "should return the file decrypted",
			secret: "cosmic",
			ansibleVaultDecrypted: []byte{
				45, 32, 110, 97, 109, 101, 58, 32, 109, 121, 95, 101, 108, 98, 95, 97, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 10, 32, 32,
				99, 111, 109, 109, 117, 110, 105, 116, 121, 46, 97, 119, 115, 46, 101, 108, 98, 95, 97, 112, 112, 108, 105, 99, 97, 116, 105, 111,
				110, 95, 108, 98, 58, 10, 32, 32, 32, 32, 110, 97, 109, 101, 58, 32, 109, 121, 101, 108, 98, 10, 32, 32, 32, 32, 115, 101, 99, 117,
				114, 105, 116, 121, 95, 103, 114, 111, 117, 112, 115, 58, 10, 32, 32, 32, 32, 32, 32, 45, 32, 115, 103, 45, 49, 50, 51, 52, 53, 54,
				55, 56, 10, 32, 32, 32, 32, 32, 32, 45, 32, 109, 121, 45, 115, 101, 99, 45, 103, 114, 111, 117, 112, 10,
			},
		},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			got := DecryptAnsibleVault(ansibleVaultEncrypted, "cosmic")
			require.Equal(t, test.ansibleVaultDecrypted, got)
		})
	}
}
