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
|
|
|
|
2019-07-30 19:19:32 +00:00
|
|
|
"github.com/go-acme/lego/v3/platform/tester"
|
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
|
2018-10-16 15:52:57 +00:00
|
|
|
Mock *DNSServerMock
|
|
|
|
envTest *tester.EnvTest
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
func (s *OTCSuite) SetupTest() {
|
|
|
|
s.Mock = NewDNSServerMock(s.T())
|
|
|
|
s.Mock.HandleAuthSuccessfully()
|
|
|
|
s.envTest = tester.NewEnvTest(
|
|
|
|
"OTC_DOMAIN_NAME",
|
|
|
|
"OTC_USER_NAME",
|
|
|
|
"OTC_PASSWORD",
|
|
|
|
"OTC_PROJECT_NAME",
|
|
|
|
"OTC_IDENTITY_ENDPOINT",
|
|
|
|
)
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
func (s *OTCSuite) TearDownTest() {
|
|
|
|
s.envTest.RestoreEnv()
|
|
|
|
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"
|
|
|
|
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) TestLogin() {
|
2018-10-12 17:29:18 +00:00
|
|
|
provider, err := s.createDNSProvider()
|
2018-10-16 15:52:57 +00:00
|
|
|
s.Require().NoError(err)
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
err = provider.loginRequest()
|
2018-10-16 15:52:57 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.Equal(provider.baseURL, fmt.Sprintf("%s/v2", s.Mock.GetServerURL()))
|
|
|
|
s.Equal(fakeOTCToken, provider.token)
|
2018-10-12 17:29:18 +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{
|
|
|
|
"OTC_DOMAIN_NAME": "unittest1",
|
|
|
|
"OTC_USER_NAME": "unittest2",
|
|
|
|
"OTC_PASSWORD": "unittest3",
|
|
|
|
"OTC_PROJECT_NAME": "unittest4",
|
|
|
|
"OTC_IDENTITY_ENDPOINT": "unittest5",
|
|
|
|
})
|
2017-07-19 22:54:35 +00:00
|
|
|
|
|
|
|
provider, err := NewDNSProvider()
|
2018-10-16 15:52:57 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.Equal(provider.config.DomainName, "unittest1")
|
|
|
|
s.Equal(provider.config.UserName, "unittest2")
|
|
|
|
s.Equal(provider.config.Password, "unittest3")
|
|
|
|
s.Equal(provider.config.ProjectName, "unittest4")
|
|
|
|
s.Equal(provider.config.IdentityEndpoint, "unittest5")
|
2017-07-19 22:54:35 +00:00
|
|
|
|
|
|
|
os.Setenv("OTC_IDENTITY_ENDPOINT", "")
|
|
|
|
|
|
|
|
provider, err = NewDNSProvider()
|
2018-10-16 15:52:57 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.Equal(provider.config.IdentityEndpoint, "https://iam.eu-de.otc.t-systems.com:443/v3/auth/tokens")
|
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() {
|
2018-10-12 17:29:18 +00:00
|
|
|
s.Mock.HandleListZonesSuccessfully()
|
|
|
|
s.Mock.HandleListRecordsetsSuccessfully()
|
|
|
|
|
|
|
|
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() {
|
2017-07-19 22:54:35 +00:00
|
|
|
s.Mock.HandleListZonesEmpty()
|
|
|
|
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.NotNil(err)
|
2017-07-19 22:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
func (s *OTCSuite) TestDNSProvider_CleanUp() {
|
2017-07-19 22:54:35 +00:00
|
|
|
s.Mock.HandleListZonesSuccessfully()
|
|
|
|
s.Mock.HandleListRecordsetsSuccessfully()
|
2018-10-12 17:29:18 +00:00
|
|
|
s.Mock.HandleDeleteRecordsetsSuccessfully()
|
|
|
|
|
|
|
|
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() {
|
2017-07-19 22:54:35 +00:00
|
|
|
s.Mock.HandleListZonesSuccessfully()
|
2018-10-12 17:29:18 +00:00
|
|
|
s.Mock.HandleListRecordsetsEmpty()
|
|
|
|
|
|
|
|
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
|
|
|
}
|