From b1ac06c604c023db08bdb30916a87398acd7cb12 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 18 Oct 2019 15:18:00 +0300 Subject: [PATCH] fixes for tests & more tests --- cmd/transport-core-tool/main_test.go | 19 ++++++++++++++ core/engine_test.go | 2 +- core/http_client_builder.go | 18 ++++++------- core/http_client_builder_test.go | 31 ++++++++++++++-------- core/migration_generator_test.go | 39 ++++++++++++++++++++++++++++ core/utils_test.go | 2 +- 6 files changed, 88 insertions(+), 23 deletions(-) create mode 100644 cmd/transport-core-tool/main_test.go create mode 100644 core/migration_generator_test.go diff --git a/cmd/transport-core-tool/main_test.go b/cmd/transport-core-tool/main_test.go new file mode 100644 index 0000000..d14c33a --- /dev/null +++ b/cmd/transport-core-tool/main_test.go @@ -0,0 +1,19 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCoreTool_MigrationCommandExists(t *testing.T) { + found := false + + for _, cmd := range parser.Commands() { + if cmd != nil && cmd.Name == "migration" { + found = true + } + } + + assert.True(t, found) +} diff --git a/core/engine_test.go b/core/engine_test.go index 244b472..0d33b43 100644 --- a/core/engine_test.go +++ b/core/engine_test.go @@ -19,7 +19,7 @@ type EngineTest struct { engine *Engine } -func (e *EngineTest) SetupSuite() { +func (e *EngineTest) SetupTest() { var ( db *sql.DB err error diff --git a/core/http_client_builder.go b/core/http_client_builder.go index fe9f514..d127831 100644 --- a/core/http_client_builder.go +++ b/core/http_client_builder.go @@ -70,14 +70,14 @@ func (b *HTTPClientBuilder) SetMockedDomains(domains []string) *HTTPClientBuilde return b } -// DisableSSLVerification disables SSL certificates verification in client -func (b *HTTPClientBuilder) DisableSSLVerification() *HTTPClientBuilder { - b.logf("WARNING: SSL verification is now disabled, don't use this parameter in production!") - - b.httpTransport.TLSClientConfig = &tls.Config{ - InsecureSkipVerify: true, +// SetSSLVerification enables or disables SSL certificates verification in client +func (b *HTTPClientBuilder) SetSSLVerification(enabled bool) *HTTPClientBuilder { + if b.httpTransport.TLSClientConfig == nil { + b.httpTransport.TLSClientConfig = &tls.Config{} } + b.httpTransport.TLSClientConfig.InsecureSkipVerify = !enabled + return b } @@ -98,14 +98,12 @@ func (b *HTTPClientBuilder) FromConfig(config *HTTPClientConfig) *HTTPClientBuil b.mockedDomains = config.MockedDomains } - if !config.SSLVerification { - b.DisableSSLVerification() - } - if config.Timeout > 0 { b.SetTimeout(config.Timeout) } + b.SetSSLVerification(config.SSLVerification) + return b } diff --git a/core/http_client_builder_test.go b/core/http_client_builder_test.go index 7e5c5cb..efbebc0 100644 --- a/core/http_client_builder_test.go +++ b/core/http_client_builder_test.go @@ -21,12 +21,12 @@ func (t *HTTPClientBuilderTest) SetupSuite() { func (t *HTTPClientBuilderTest) Test_SetTimeout() { t.builder.SetTimeout(90) - assert.Equal(t.T(), 90, t.builder.timeout) - assert.Equal(t.T(), 90, t.builder.httpClient.Timeout) + assert.Equal(t.T(), 90*time.Second, t.builder.timeout) + assert.Equal(t.T(), 90*time.Second, t.builder.httpClient.Timeout) } func (t *HTTPClientBuilderTest) Test_SetMockAddress() { - addr := "http://mock.local:3004" + addr := "mock.local:3004" t.builder.SetMockAddress(addr) assert.Equal(t.T(), addr, t.builder.mockAddress) @@ -48,21 +48,23 @@ func (t *HTTPClientBuilderTest) Test_SetMockedDomains() { assert.Equal(t.T(), domains[0], t.builder.mockedDomains[0]) } -func (t *HTTPClientBuilderTest) Test_DisableSSLVerification() { - t.builder.DisableSSLVerification() +func (t *HTTPClientBuilderTest) Test_SetSSLVerification() { + t.builder.SetSSLVerification(true) + assert.False(t.T(), t.builder.httpTransport.TLSClientConfig.InsecureSkipVerify) + t.builder.SetSSLVerification(false) assert.True(t.T(), t.builder.httpTransport.TLSClientConfig.InsecureSkipVerify) } func (t *HTTPClientBuilderTest) Test_FromConfig() { config := &HTTPClientConfig{ SSLVerification: true, - MockAddress: "http://anothermock.local:3004", + MockAddress: "anothermock.local:3004", MockedDomains: []string{"example.gov"}, + Timeout: 60, } t.builder.FromConfig(config) - assert.Equal(t.T(), !config.SSLVerification, t.builder.httpTransport.TLSClientConfig.InsecureSkipVerify) assert.Equal(t.T(), config.MockAddress, t.builder.mockAddress) assert.Equal(t.T(), config.MockedDomains[0], t.builder.mockedDomains[0]) @@ -75,14 +77,15 @@ func (t *HTTPClientBuilderTest) Test_FromEngine() { Config: Config{ HTTPClientConfig: &HTTPClientConfig{ SSLVerification: true, - MockAddress: "http://anothermock.local:3004", + MockAddress: "anothermock.local:3004", MockedDomains: []string{"example.gov"}, }, Debug: false, }, } - assert.Equal(t.T(), engine, t.builder.engine) + t.builder.FromEngine(engine) + assert.NotNil(t.T(), engine, t.builder.engine) } func (t *HTTPClientBuilderTest) Test_buildDialer() { @@ -108,7 +111,11 @@ func (t *HTTPClientBuilderTest) Test_logf() { } func (t *HTTPClientBuilderTest) Test_Build() { - client, err := t.builder.Build(true) + client, err := t.builder. + SetTimeout(10). + SetMockAddress("api_mock:3004"). + AddMockedDomain("google.com"). + Build(true) assert.NoError(t.T(), err) assert.NotNil(t.T(), client) @@ -116,9 +123,11 @@ func (t *HTTPClientBuilderTest) Test_Build() { } func (t *HTTPClientBuilderTest) Test_RestoreDefault() { + t.builder.ReplaceDefault() t.builder.RestoreDefault() - assert.NotEqual(t.T(), http.DefaultClient, t.builder.httpClient) + assert.Equal(t.T(), http.DefaultClient, DefaultClient) + assert.Equal(t.T(), http.DefaultTransport, DefaultTransport) } func Test_HTTPClientBuilder(t *testing.T) { diff --git a/core/migration_generator_test.go b/core/migration_generator_test.go new file mode 100644 index 0000000..6bc1d29 --- /dev/null +++ b/core/migration_generator_test.go @@ -0,0 +1,39 @@ +package core + +import ( + "io/ioutil" + "log" + "os" + "path" + "strings" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" +) + +type MigrationGeneratorSuite struct { + suite.Suite + command *NewMigrationCommand +} + +func (s *MigrationGeneratorSuite) SetupSuite() { + s.command = &NewMigrationCommand{Directory: "/tmp"} +} + +func (s *MigrationGeneratorSuite) Test_Execute() { + found := false + assert.NoError(s.T(), s.command.Execute([]string{})) + files, err := ioutil.ReadDir(s.command.Directory) + if err != nil { + log.Fatal(err) + } + + for _, f := range files { + if strings.Index(f.Name(), "_app.go") != -1 { + found = true + assert.NoError(s.T(), os.Remove(path.Join(s.command.Directory, f.Name()))) + } + } + + assert.True(s.T(), found) +} diff --git a/core/utils_test.go b/core/utils_test.go index b93dffb..f9fe39b 100644 --- a/core/utils_test.go +++ b/core/utils_test.go @@ -46,7 +46,7 @@ func (u *UtilsTest) SetupSuite() { func (u *UtilsTest) Test_ResetUtils() { assert.Equal(u.T(), "access key id (will be removed)", u.utils.ConfigAWS.AccessKeyID) - assert.Equal(u.T(), uint32(12345), u.utils.TokenCounter) + assert.Equal(u.T(), uint32(12346), u.utils.TokenCounter) assert.False(u.T(), u.utils.IsDebug) awsConfig := u.utils.ConfigAWS