package lb_test

import (
	"testing"

	"github.com/scaleway/scaleway-cli/v2/core"
	"github.com/scaleway/scaleway-cli/v2/internal/namespaces/lb/v1"
)

func Test_CreateCertificate(t *testing.T) {
	// You can generate a private key and a Certificate Signing Request (CSR) using:
	// openssl req -new -newkey rsa:4096 -nodes -keyout private_key.txt -out csr.txt -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com"
	// Generate Certificate
	//	openssl x509 -req -sha256 -days 365 -in csr.txt -signkey private_key.txt -out certificate.txt -extensions req_ext

	customCertificateChain := `
-----BEGIN PRIVATE KEY-----
MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQCjiSWSVhiskTAU
0sANXc08+zRqCS1y1kMjqtqzoqnxtrmdw48vgE42+v5j/v0oBBR4kuBEwpr2WT1l
OGyE5vhYBIjs8kPBKnaZR+Sa8OeMYpuNvBE8nYgEoIIaIkkhPjPasyuETiK63yx0
D5qwRsm9Aoa/O5kKOpAw17Nz3bLJNTkYWjwLpfb7JyG2VhF9Pa2dfr7sahY1TBu9
GY8HNVvXpWCGEWCNW8DQp3By23R6BsUrwMerf88Xf5hkxM44uHVCTwJazcdMhPEY
hs4sa7nq/tWDUWFx3oPWPxcYYENoCCJu6VUIGnPcDXAXFMaolBSD78o4j0oCt0qA
ZpeBgJd3dZz+fswGoVdFDLcvRM9+ZSav6niwU49r8lN+Ae3RAARvSTJZSsVS3J/e
8Q40i9tnEklget8AxwdaFViwZ5Ui2Q2RS7lnQFt7dOar55MInqoLa6yLm8QpRfVT
/dnMnoIGDT5hd2LKamAsabNtmPST1OJv3MOl0XP7gSVAOgDJ8Njh1JN5X4qtO3Gv
TUzBojfqbegcMLe8GU8ZUi3PTEjqJ+0xnXXi4ZHTn3r1awIVymmCVOP/pXbDUifl
fBbVWZOQu5OF0lJPIeoJS5Ax5R3C0uDpCt8O0/C2kla8FTsOgSFord+fbON/+0rj
QPK3ryJWCYKt9ynlgxr/b52ah2U4iwIDAQABAoICAC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V8bVNNsnUQzAqhwozp
FKQ9WcK8j+KuYrWQJihn/omlWXSQ+zs12N7yKVLVRuY8aw4XuWZwvuu4EkRLYcUD
5h467I4rNC2+9W+AcNkzCE2i/dCyt3f3y1Rtu8l0
-----END CERTIFICATE-----
`
	t.Run("Import", core.Test(&core.TestConfig{
		Commands: lb.GetCommands(),
		BeforeFunc: core.BeforeFuncCombine(
			createLB(),
		),
		Args: []string{
			"scw", "lb", "certificate", "create",
			"lb-id={{ .LB.ID }}", "custom-certificate-chain=" + customCertificateChain,
		},
		Check: core.TestCheckGolden(),
		AfterFunc: core.AfterFuncCombine(
			deleteLB(),
			deleteLBFlexibleIP(),
		),
		TmpHomeDir: true,
	}))
}
