2019-03-11 16:56:48 +00:00
|
|
|
package otc
|
2017-07-19 22:54:35 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2018-06-11 15:32:50 +00:00
|
|
|
|
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"
|
2018-06-11 15:32:50 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2017-07-19 22:54:35 +00:00
|
|
|
)
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
type OTCSuite struct {
|
2017-07-19 22:54:35 +00:00
|
|
|
suite.Suite
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
mock *internal.DNSServerMock
|
2018-10-16 15:52:57 +00:00
|
|
|
envTest *tester.EnvTest
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
func (s *OTCSuite) SetupTest() {
|
2023-05-05 07:49:38 +00:00
|
|
|
s.mock = internal.NewDNSServerMock(s.T())
|
|
|
|
s.mock.HandleAuthSuccessfully()
|
2018-10-16 15:52:57 +00:00
|
|
|
s.envTest = tester.NewEnvTest(
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvDomainName,
|
|
|
|
EnvUserName,
|
|
|
|
EnvPassword,
|
|
|
|
EnvProjectName,
|
|
|
|
EnvIdentityEndpoint,
|
2018-10-16 15:52:57 +00:00
|
|
|
)
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
func (s *OTCSuite) TearDownTest() {
|
|
|
|
s.envTest.RestoreEnv()
|
2023-05-05 07:49:38 +00:00
|
|
|
s.mock.ShutdownServer()
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestTestSuite(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
suite.Run(t, new(OTCSuite))
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
func (s *OTCSuite) createDNSProvider() (*DNSProvider, error) {
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
2018-10-16 15:52:57 +00:00
|
|
|
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())
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
func (s *OTCSuite) TestLoginEnv() {
|
|
|
|
s.envTest.ClearEnv()
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
s.envTest.Apply(map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvDomainName: "unittest1",
|
|
|
|
EnvUserName: "unittest2",
|
|
|
|
EnvPassword: "unittest3",
|
|
|
|
EnvProjectName: "unittest4",
|
|
|
|
EnvIdentityEndpoint: "unittest5",
|
2018-10-16 15:52:57 +00:00
|
|
|
})
|
2017-07-19 22:54:35 +00:00
|
|
|
|
|
|
|
provider, err := NewDNSProvider()
|
2018-10-16 15:52:57 +00:00
|
|
|
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)
|
2017-07-19 22:54:35 +00:00
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
os.Setenv(EnvIdentityEndpoint, "")
|
2017-07-19 22:54:35 +00:00
|
|
|
|
|
|
|
provider, err = NewDNSProvider()
|
2018-10-16 15:52:57 +00:00
|
|
|
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)
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
func (s *OTCSuite) TestLoginEnvEmpty() {
|
|
|
|
s.envTest.ClearEnv()
|
2017-07-19 22:54:35 +00:00
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
_, err := NewDNSProvider()
|
2018-10-16 15:52:57 +00:00
|
|
|
s.EqualError(err, "otc: some credentials information are missing: OTC_DOMAIN_NAME,OTC_USER_NAME,OTC_PASSWORD,OTC_PROJECT_NAME")
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
func (s *OTCSuite) TestDNSProvider_Present() {
|
2023-05-05 07:49:38 +00:00
|
|
|
s.mock.HandleListZonesSuccessfully()
|
|
|
|
s.mock.HandleListRecordsetsSuccessfully()
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
provider, err := s.createDNSProvider()
|
2018-10-16 15:52:57 +00:00
|
|
|
s.Require().NoError(err)
|
2017-07-19 22:54:35 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
err = provider.Present("example.com", "", "foobar")
|
2018-10-16 15:52:57 +00:00
|
|
|
s.Require().NoError(err)
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
func (s *OTCSuite) TestDNSProvider_Present_EmptyZone() {
|
2023-05-05 07:49:38 +00:00
|
|
|
s.mock.HandleListZonesEmpty()
|
|
|
|
s.mock.HandleListRecordsetsSuccessfully()
|
2017-07-19 22:54:35 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
provider, err := s.createDNSProvider()
|
2018-10-16 15:52:57 +00:00
|
|
|
s.Require().NoError(err)
|
2017-07-19 22:54:35 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
err = provider.Present("example.com", "", "foobar")
|
2023-10-31 13:08:50 +00:00
|
|
|
s.Error(err)
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
func (s *OTCSuite) TestDNSProvider_CleanUp() {
|
2023-05-05 07:49:38 +00:00
|
|
|
s.mock.HandleListZonesSuccessfully()
|
|
|
|
s.mock.HandleListRecordsetsSuccessfully()
|
|
|
|
s.mock.HandleDeleteRecordsetsSuccessfully()
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
provider, err := s.createDNSProvider()
|
2018-10-16 15:52:57 +00:00
|
|
|
s.Require().NoError(err)
|
2017-07-19 22:54:35 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
err = provider.CleanUp("example.com", "", "foobar")
|
2018-10-16 15:52:57 +00:00
|
|
|
s.Require().NoError(err)
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
func (s *OTCSuite) TestDNSProvider_CleanUp_EmptyRecordset() {
|
2023-05-05 07:49:38 +00:00
|
|
|
s.mock.HandleListZonesSuccessfully()
|
|
|
|
s.mock.HandleListRecordsetsEmpty()
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
provider, err := s.createDNSProvider()
|
2018-10-16 15:52:57 +00:00
|
|
|
s.Require().NoError(err)
|
2017-07-19 22:54:35 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
err = provider.CleanUp("example.com", "", "foobar")
|
2018-10-16 15:52:57 +00:00
|
|
|
s.Require().Error(err)
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|