Semaphore/db/AccessKey_test.go

92 lines
1.7 KiB
Go
Raw Normal View History

2021-09-01 16:38:28 +02:00
package db
import (
"encoding/base64"
"github.com/ansible-semaphore/semaphore/util"
"testing"
)
func TestSetSecret(t *testing.T) {
accessKey := AccessKey{
Type: AccessKeySSH,
SshKey: SshKey{
PrivateKey: "qerphqeruqoweurqwerqqeuiqwpavqr",
},
}
util.Config = &util.ConfigType{}
err := accessKey.SerializeSecret()
if err != nil {
t.Error(err)
}
secret, err := base64.StdEncoding.DecodeString(*accessKey.Secret)
if err != nil {
t.Error(err)
}
2021-09-13 15:22:08 +02:00
if string(secret) != "{\"login\":\"\",\"passphrase\":\"\",\"private_key\":\"qerphqeruqoweurqwerqqeuiqwpavqr\"}" {
t.Error("invalid secret")
}
}
2021-09-01 16:38:28 +02:00
func TestGetSecret(t *testing.T) {
secret := base64.StdEncoding.EncodeToString([]byte(`{
"passphrase": "123456",
"private_key": "qerphqeruqoweurqwerqqeuiqwpavqr"
}`))
util.Config = &util.ConfigType{}
accessKey := AccessKey{
Secret: &secret,
Type: AccessKeySSH,
2021-09-01 16:38:28 +02:00
}
err := accessKey.DeserializeSecret()
if err != nil {
t.Error(err)
}
if accessKey.SshKey.Passphrase != "123456" {
t.Errorf("")
}
if accessKey.SshKey.PrivateKey != "qerphqeruqoweurqwerqqeuiqwpavqr" {
t.Errorf("")
}
}
func TestSetGetSecretWithEncryption(t *testing.T) {
accessKey := AccessKey{
Type: AccessKeySSH,
SshKey: SshKey{
PrivateKey: "qerphqeruqoweurqwerqqeuiqwpavqr",
},
}
util.Config = &util.ConfigType{
AccessKeyEncryption: "hHYgPrhQTZYm7UFTvcdNfKJMB3wtAXtJENUButH+DmM=",
}
err := accessKey.SerializeSecret()
if err != nil {
t.Error(err)
}
//accessKey.ClearSecret()
err = accessKey.DeserializeSecret()
if err != nil {
t.Error(err)
}
if accessKey.SshKey.PrivateKey != "qerphqeruqoweurqwerqqeuiqwpavqr" {
t.Error("invalid secret")
}
}