//go:build unit || all
// +build unit all

package procedure

import (
	"testing"

	. "github.com/onsi/gomega"
)

func TestGetRootRole(t *testing.T) {
	cases := map[string]struct {
		tenantRole    string
		rootPrincipal string
		expectErr     bool
	}{
		"wrong-prefix": {
			tenantRole:    "wrong-prefix",
			rootPrincipal: "",
			expectErr:     true,
		},
		"invalid": {
			tenantRole:    "arn:aws:iam::123456789012",
			rootPrincipal: "",
			expectErr:     true,
		},
		"non-alias": {
			tenantRole:    "arn:aws:iam::123456789012:role/some-tenant-role",
			rootPrincipal: "arn:aws:iam::123456789012:root",
		},
		"alias": {
			tenantRole:    "arn:aws:iam::pingcap:role/some-tenant-role",
			rootPrincipal: "arn:aws:iam::pingcap:root",
		},
	}
	for name, tc := range cases {
		t.Run(name, func(t *testing.T) {
			g := NewGomegaWithT(t)
			rootPrincipal, err := getRootPrincipal(tc.tenantRole)
			if tc.expectErr {
				g.Expect(err).To(HaveOccurred())
			} else {
				g.Expect(err).NotTo(HaveOccurred())
				g.Expect(rootPrincipal).To(Equal(tc.rootPrincipal))
			}
		})
	}
}
