lego/providers/dns/otc/otc_test.go

131 lines
3.1 KiB
Go
Raw Normal View History

2019-03-11 16:56:48 +00:00
package otc
import (
"fmt"
"os"
"testing"
2020-09-02 01:20:01 +00:00
"github.com/go-acme/lego/v4/platform/tester"
2023-05-05 07:49:38 +00:00
"github.com/go-acme/lego/v4/providers/dns/otc/internal"
"github.com/stretchr/testify/suite"
)
type OTCSuite struct {
suite.Suite
2023-05-05 07:49:38 +00:00
mock *internal.DNSServerMock
envTest *tester.EnvTest
}
func (s *OTCSuite) SetupTest() {
2023-05-05 07:49:38 +00:00
s.mock = internal.NewDNSServerMock(s.T())
s.mock.HandleAuthSuccessfully()
s.envTest = tester.NewEnvTest(
EnvDomainName,
EnvUserName,
EnvPassword,
EnvProjectName,
EnvIdentityEndpoint,
)
}
func (s *OTCSuite) TearDownTest() {
s.envTest.RestoreEnv()
2023-05-05 07:49:38 +00:00
s.mock.ShutdownServer()
}
func TestTestSuite(t *testing.T) {
suite.Run(t, new(OTCSuite))
}
func (s *OTCSuite) createDNSProvider() (*DNSProvider, error) {
config := NewDefaultConfig()
config.UserName = "UserName"
config.Password = "Password"
config.DomainName = "DomainName"
config.ProjectName = "ProjectName"
2023-05-05 07:49:38 +00:00
config.IdentityEndpoint = fmt.Sprintf("%s/v3/auth/token", s.mock.GetServerURL())
return NewDNSProviderConfig(config)
}
func (s *OTCSuite) TestLoginEnv() {
s.envTest.ClearEnv()
s.envTest.Apply(map[string]string{
EnvDomainName: "unittest1",
EnvUserName: "unittest2",
EnvPassword: "unittest3",
EnvProjectName: "unittest4",
EnvIdentityEndpoint: "unittest5",
})
provider, err := NewDNSProvider()
s.Require().NoError(err)
2023-10-31 13:08:50 +00:00
s.Equal("unittest1", provider.config.DomainName)
s.Equal("unittest2", provider.config.UserName)
s.Equal("unittest3", provider.config.Password)
s.Equal("unittest4", provider.config.ProjectName)
s.Equal("unittest5", provider.config.IdentityEndpoint)
os.Setenv(EnvIdentityEndpoint, "")
provider, err = NewDNSProvider()
s.Require().NoError(err)
2023-10-31 13:08:50 +00:00
s.Equal("https://iam.eu-de.otc.t-systems.com:443/v3/auth/tokens", provider.config.IdentityEndpoint)
}
func (s *OTCSuite) TestLoginEnvEmpty() {
s.envTest.ClearEnv()
_, err := NewDNSProvider()
s.EqualError(err, "otc: some credentials information are missing: OTC_DOMAIN_NAME,OTC_USER_NAME,OTC_PASSWORD,OTC_PROJECT_NAME")
}
func (s *OTCSuite) TestDNSProvider_Present() {
2023-05-05 07:49:38 +00:00
s.mock.HandleListZonesSuccessfully()
s.mock.HandleListRecordsetsSuccessfully()
provider, err := s.createDNSProvider()
s.Require().NoError(err)
err = provider.Present("example.com", "", "foobar")
s.Require().NoError(err)
}
func (s *OTCSuite) TestDNSProvider_Present_EmptyZone() {
2023-05-05 07:49:38 +00:00
s.mock.HandleListZonesEmpty()
s.mock.HandleListRecordsetsSuccessfully()
provider, err := s.createDNSProvider()
s.Require().NoError(err)
err = provider.Present("example.com", "", "foobar")
2023-10-31 13:08:50 +00:00
s.Error(err)
}
func (s *OTCSuite) TestDNSProvider_CleanUp() {
2023-05-05 07:49:38 +00:00
s.mock.HandleListZonesSuccessfully()
s.mock.HandleListRecordsetsSuccessfully()
s.mock.HandleDeleteRecordsetsSuccessfully()
provider, err := s.createDNSProvider()
s.Require().NoError(err)
err = provider.CleanUp("example.com", "", "foobar")
s.Require().NoError(err)
}
func (s *OTCSuite) TestDNSProvider_CleanUp_EmptyRecordset() {
2023-05-05 07:49:38 +00:00
s.mock.HandleListZonesSuccessfully()
s.mock.HandleListRecordsetsEmpty()
provider, err := s.createDNSProvider()
s.Require().NoError(err)
err = provider.CleanUp("example.com", "", "foobar")
s.Require().Error(err)
}