diff options
author | Ethan Koenig <etk39@cornell.edu> | 2017-02-08 01:29:07 -0500 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-02-08 14:29:07 +0800 |
commit | d2329e1c261bb235bfa32692710434936b98533b (patch) | |
tree | 148096febc87256d680a697673e08accb6afa355 /models | |
parent | 23a7527e040a2ea6612db040544cf204b7969d55 (diff) | |
download | gitea-d2329e1c261bb235bfa32692710434936b98533b.tar.gz gitea-d2329e1c261bb235bfa32692710434936b98533b.zip |
Use assert in legacy unit tests (#867)
Diffstat (limited to 'models')
-rw-r--r-- | models/models_test.go | 30 | ||||
-rw-r--r-- | models/repo_test.go | 89 | ||||
-rw-r--r-- | models/ssh_key_test.go | 57 |
3 files changed, 63 insertions, 113 deletions
diff --git a/models/models_test.go b/models/models_test.go index f68590c509..9bf32f81fa 100644 --- a/models/models_test.go +++ b/models/models_test.go @@ -7,27 +7,19 @@ package models import ( "testing" - . "github.com/smartystreets/goconvey/convey" + "github.com/stretchr/testify/assert" ) func Test_parsePostgreSQLHostPort(t *testing.T) { - testSuites := []struct { - input string - host, port string - }{ - {"127.0.0.1:1234", "127.0.0.1", "1234"}, - {"127.0.0.1", "127.0.0.1", "5432"}, - {"[::1]:1234", "[::1]", "1234"}, - {"[::1]", "[::1]", "5432"}, - {"/tmp/pg.sock:1234", "/tmp/pg.sock", "1234"}, - {"/tmp/pg.sock", "/tmp/pg.sock", "5432"}, + test := func (input, expectedHost, expectedPort string) { + host, port := parsePostgreSQLHostPort(input) + assert.Equal(t, expectedHost, host) + assert.Equal(t, expectedPort, port) } - - Convey("Parse PostgreSQL host and port", t, func() { - for _, suite := range testSuites { - host, port := parsePostgreSQLHostPort(suite.input) - So(host, ShouldEqual, suite.host) - So(port, ShouldEqual, suite.port) - } - }) + test("127.0.0.1:1234", "127.0.0.1", "1234") + test("127.0.0.1", "127.0.0.1", "5432") + test("[::1]:1234", "[::1]", "1234") + test("[::1]", "[::1]", "5432") + test("/tmp/pg.sock:1234", "/tmp/pg.sock", "1234") + test("/tmp/pg.sock", "/tmp/pg.sock", "5432") } diff --git a/models/repo_test.go b/models/repo_test.go index 499c8c83eb..db8bd1024e 100644 --- a/models/repo_test.go +++ b/models/repo_test.go @@ -9,69 +9,40 @@ import ( "code.gitea.io/gitea/modules/markdown" - . "github.com/smartystreets/goconvey/convey" "github.com/stretchr/testify/assert" ) func TestRepo(t *testing.T) { - Convey("The metas map", t, func() { - var repo = new(Repository) - repo.Name = "testrepo" - repo.Owner = new(User) - repo.Owner.Name = "testuser" - externalTracker := RepoUnit{ - Type: UnitTypeExternalTracker, - Config: &ExternalTrackerConfig{ - ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}", - }, - } - repo.Units = []*RepoUnit{ - &externalTracker, - } - - Convey("When no external tracker is configured", func() { - Convey("It should be nil", func() { - repo.Units = nil - So(repo.ComposeMetas(), ShouldEqual, map[string]string(nil)) - }) - Convey("It should be nil even if other settings are present", func() { - repo.Units = nil - So(repo.ComposeMetas(), ShouldEqual, map[string]string(nil)) - }) - }) - - Convey("When an external issue tracker is configured", func() { - repo.Units = []*RepoUnit{ - &externalTracker, - } - Convey("It should default to numeric issue style", func() { - metas := repo.ComposeMetas() - So(metas["style"], ShouldEqual, markdown.IssueNameStyleNumeric) - }) - Convey("It should pass through numeric issue style setting", func() { - externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markdown.IssueNameStyleNumeric - metas := repo.ComposeMetas() - So(metas["style"], ShouldEqual, markdown.IssueNameStyleNumeric) - }) - Convey("It should pass through alphanumeric issue style setting", func() { - externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markdown.IssueNameStyleAlphanumeric - metas := repo.ComposeMetas() - So(metas["style"], ShouldEqual, markdown.IssueNameStyleAlphanumeric) - }) - Convey("It should contain the user name", func() { - metas := repo.ComposeMetas() - So(metas["user"], ShouldEqual, "testuser") - }) - Convey("It should contain the repo name", func() { - metas := repo.ComposeMetas() - So(metas["repo"], ShouldEqual, "testrepo") - }) - Convey("It should contain the URL format", func() { - metas := repo.ComposeMetas() - So(metas["format"], ShouldEqual, "https://someurl.com/{user}/{repo}/{issue}") - }) - }) - }) + repo := &Repository{Name: "testRepo"} + repo.Owner = &User{Name: "testOwner"} + + repo.Units = nil + assert.Nil(t, repo.ComposeMetas()) + + externalTracker := RepoUnit{ + Type: UnitTypeExternalTracker, + Config: &ExternalTrackerConfig{ + ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}", + }, + } + + testSuccess := func(expectedStyle string) { + repo.Units = []*RepoUnit{&externalTracker} + repo.ExternalMetas = nil + metas := repo.ComposeMetas() + assert.Equal(t, expectedStyle, metas["style"]) + assert.Equal(t, "testRepo", metas["repo"]) + assert.Equal(t, "testOwner", metas["user"]) + assert.Equal(t, "https://someurl.com/{user}/{repo}/{issue}", metas["format"]) + } + + testSuccess(markdown.IssueNameStyleNumeric) + + externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markdown.IssueNameStyleAlphanumeric + testSuccess(markdown.IssueNameStyleAlphanumeric) + + externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markdown.IssueNameStyleNumeric + testSuccess(markdown.IssueNameStyleNumeric) } func TestGetRepositoryCount(t *testing.T) { diff --git a/models/ssh_key_test.go b/models/ssh_key_test.go index 890951a2b7..beb4556b8b 100644 --- a/models/ssh_key_test.go +++ b/models/ssh_key_test.go @@ -5,13 +5,12 @@ package models import ( - "fmt" "strings" "testing" - . "github.com/smartystreets/goconvey/convey" - "code.gitea.io/gitea/modules/setting" + + "github.com/stretchr/testify/assert" ) func init() { @@ -19,38 +18,26 @@ func init() { } func Test_SSHParsePublicKey(t *testing.T) { - testKeys := map[string]struct { - typeName string - length int - content string - }{ - "dsa-1024": {"dsa", 1024, "ssh-dss AAAAB3NzaC1kc3MAAACBAOChCC7lf6Uo9n7BmZ6M8St19PZf4Tn59NriyboW2x/DZuYAz3ibZ2OkQ3S0SqDIa0HXSEJ1zaExQdmbO+Ux/wsytWZmCczWOVs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8TuBnTiGWYMBNTbOZvPC4e0WI2/yZW/s5F nocomment"}, - "ecdsa-256": {"ecdsa", 256, "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFQacN3PrOll7PXmN5B/ZNVahiUIqI05nbBlZk1KXsO3d06ktAWqbNflv2vEmA38bTFTfJ2sbn2B5ksT52cDDbA= nocomment"}, - "ecdsa-384": {"ecdsa", 384, "ecdsa-sha2-nistp384 AAAAE2VjZHNhLXNoYTItbmlzdHAzODQAAAAIbmlzdHAzODQAAABhBINmioV+XRX1Fm9Qk2ehHXJ2tfVxW30ypUWZw670Zyq5GQfBAH6xjygRsJ5wWsHXBsGYgFUXIHvMKVAG1tpw7s6ax9oA+dJOJ7tj+vhn8joFqT+sg3LYHgZkHrfqryRasQ== nocomment"}, - // "ecdsa-521": {"ecdsa", 521, "ecdsa-sha2-nistp521 AAAAE2VjZHNhLXNoYTItbmlzdHA1MjEAAAAIbmlzdHA1MjEAAACFBACGt3UG3EzRwNOI17QR84l6PgiAcvCE7v6aXPj/SC6UWKg4EL8vW9ZBcdYL9wzs4FZXh4MOV8jAzu3KRWNTwb4k2wFNUpGOt7l28MztFFEtH5BDDrtAJSPENPy8pvPLMfnPg5NhvWycqIBzNcHipem5wSJFN5PdpNOC2xMrPWKNqj+ZjQ== nocomment"}, - } - - Convey("Parse public keys in both native and ssh-keygen", t, func() { - for name, key := range testKeys { - fmt.Println("\nTesting key:", name) - - keyTypeN, lengthN, errN := SSHNativeParsePublicKey(key.content) - So(errN, ShouldBeNil) - So(keyTypeN, ShouldEqual, key.typeName) - So(lengthN, ShouldEqual, key.length) - - keyTypeK, lengthK, errK := SSHKeyGenParsePublicKey(key.content) - if errK != nil { - // Some server just does not support ecdsa format. - if strings.Contains(errK.Error(), "line 1 too long:") { - continue - } - So(errK, ShouldBeNil) + test := func(name, keyType string, length int, content string) { + keyTypeN, lengthN, err := SSHNativeParsePublicKey(content) + assert.NoError(t, err) + assert.Equal(t, keyType, keyTypeN) + assert.EqualValues(t, length, lengthN) + + keyTypeK, lengthK, err := SSHKeyGenParsePublicKey(content) + if err != nil { + // Some servers do not support ecdsa format. + if !strings.Contains(err.Error(), "line 1 too long:") { + assert.Fail(t, "%v", err) } - So(keyTypeK, ShouldEqual, key.typeName) - So(lengthK, ShouldEqual, key.length) } - }) + assert.Equal(t, keyType, keyTypeK) + assert.EqualValues(t, length, lengthK) + } + + test("dsa-1024", "dsa", 1024, "ssh-dss AAAAB3NzaC1kc3MAAACBAOChCC7lf6Uo9n7BmZ6M8St19PZf4Tn59NriyboW2x/DZuYAz3ibZ2OkQ3S0SqDIa0HXSEJ1zaExQdmbO+Ux/wsytWZmCczWOVsaszBZSl90q8UnWlSH6P+/YA+RWJm5SFtuV9PtGIhyZgoNuz5kBQ7K139wuQsecdKktISwTakzAAAAFQCzKsO2JhNKlL+wwwLGOcLffoAmkwAAAIBpK7/3xvduajLBD/9vASqBQIHrgK2J+wiQnIb/Wzy0UsVmvfn8A+udRbBo+csM8xrSnlnlJnjkJS3qiM5g+eTwsLIV1IdKPEwmwB+VcP53Cw6lSyWyJcvhFb0N6s08NZysLzvj0N+ZC/FnhKTLzIyMtkHf/IrPCwlM+pV/M/96YgAAAIEAqQcGn9CKgzgPaguIZooTAOQdvBLMI5y0bQjOW6734XOpqQGf/Kra90wpoasLKZjSYKNPjE+FRUOrStLrxcNs4BeVKhy2PYTRnybfYVk1/dmKgH6P1YSRONsGKvTsH6c5IyCRG0ncCgYeF8tXppyd642982daopE7zQ/NPAnJfag= nocomment") + test("rsa-1024", "rsa", 1024, "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAu7tvIvX6ZHrRXuZNfkR3XLHSsuCK9Zn3X58lxBcQzuo5xZgB6vRwwm/QtJuF+zZPtY5hsQILBLmF+BZ5WpKZp1jBeSjH2G7lxet9kbcH+kIVj0tPFEoyKI9wvWqIwC4prx/WVk2wLTJjzBAhyNxfEq7C9CeiX9pQEbEqJfkKCQ== nocomment\n") + test("rsa-2048", "rsa", 2048, "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDMZXh+1OBUwSH9D45wTaxErQIN9IoC9xl7MKJkqvTvv6O5RR9YW/IK9FbfjXgXsppYGhsCZo1hFOOsXHMnfOORqu/xMDx4yPuyvKpw4LePEcg4TDipaDFuxbWOqc/BUZRZcXu41QAWfDLrInwsltWZHSeG7hjhpacl4FrVv9V1pS6Oc5Q1NxxEzTzuNLS/8diZrTm/YAQQ/+B+mzWI3zEtF4miZjjAljWd1LTBPvU23d29DcBmmFahcZ441XZsTeAwGxG/Q6j8NgNXj9WxMeWwxXV2jeAX/EBSpZrCVlCQ1yJswT6xCp8TuBnTiGWYMBNTbOZvPC4e0WI2/yZW/s5F nocomment") + test("ecdsa-256", "ecdsa", 256, "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFQacN3PrOll7PXmN5B/ZNVahiUIqI05nbBlZk1KXsO3d06ktAWqbNflv2vEmA38bTFTfJ2sbn2B5ksT52cDDbA= nocomment") + test("ecdsa-384", "ecdsa", 384, "ecdsa-sha2-nistp384 AAAAE2VjZHNhLXNoYTItbmlzdHAzODQAAAAIbmlzdHAzODQAAABhBINmioV+XRX1Fm9Qk2ehHXJ2tfVxW30ypUWZw670Zyq5GQfBAH6xjygRsJ5wWsHXBsGYgFUXIHvMKVAG1tpw7s6ax9oA+dJOJ7tj+vhn8joFqT+sg3LYHgZkHrfqryRasQ== nocomment") } |