From 3ef9318e40590efcb93476172a5c78ffbd9b9ff0 Mon Sep 17 00:00:00 2001 From: Mura Li Date: Tue, 7 Mar 2017 14:55:04 +0800 Subject: [PATCH 1/6] Add integration test for signup --- integrations/signup_test.go | 64 +++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 integrations/signup_test.go diff --git a/integrations/signup_test.go b/integrations/signup_test.go new file mode 100644 index 0000000000000..3364bd5058f4b --- /dev/null +++ b/integrations/signup_test.go @@ -0,0 +1,64 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package integration + +import ( + "fmt" + "net/http" + "os" + "testing" + + "code.gitea.io/gitea/integrations/internal/utils" +) + +var signupFormSample map[string][]string = map[string][]string{ + "Name": []string{"tester"}, + "Email": []string{"user1@example.com"}, + "Passwd": []string{"12345678"}, +} + +func signup(t *utils.T) error { + var err error + var r *http.Response + + r, err = http.Get("http://:" + ServerHTTPPort + "/user/sign_up") + if err != nil { + return err + } + defer r.Body.Close() + + if r.StatusCode != http.StatusOK { + return fmt.Errorf("GET '/user/signup': %s", r.Status) + } + + r, err = http.PostForm("http://:"+ServerHTTPPort+"/user/sign_up", signupFormSample) + if err != nil { + return err + } + defer r.Body.Close() + + if r.StatusCode != http.StatusOK { + return fmt.Errorf("POST '/user/signup': %s", r.Status) + } + + return nil +} + +func signin(t *utils.T) error { + return nil +} + +func TestSignup(t *testing.T) { + conf := utils.Config{ + Program: "../gitea", + WorkDir: "", + Args: []string{"web", "--port", ServerHTTPPort}, + LogFile: os.Stderr, + } + + if err := utils.New(t, &conf).RunTest(install, signup); err != nil { + t.Fatal(err) + } +} From 3797cbe82840925d3b54556631bfa7da2c8a6579 Mon Sep 17 00:00:00 2001 From: Mura Li Date: Tue, 7 Mar 2017 15:36:46 +0800 Subject: [PATCH 2/6] Remove unused functions --- integrations/signup_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/integrations/signup_test.go b/integrations/signup_test.go index 3364bd5058f4b..26ff2c7c9f59b 100644 --- a/integrations/signup_test.go +++ b/integrations/signup_test.go @@ -46,10 +46,6 @@ func signup(t *utils.T) error { return nil } -func signin(t *utils.T) error { - return nil -} - func TestSignup(t *testing.T) { conf := utils.Config{ Program: "../gitea", From cbfd523ffb23cadc98a23a14b135c1e10efa22fc Mon Sep 17 00:00:00 2001 From: Mura Li Date: Thu, 9 Mar 2017 09:44:24 +0800 Subject: [PATCH 3/6] Refactoring --- integrations/internal/utils/utils.go | 29 ++++++++++++++++++++++++++++ integrations/signup_test.go | 26 +------------------------ 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/integrations/internal/utils/utils.go b/integrations/internal/utils/utils.go index 511fa478b946f..22429c7fdadd0 100644 --- a/integrations/internal/utils/utils.go +++ b/integrations/internal/utils/utils.go @@ -6,9 +6,11 @@ package utils import ( "errors" + "fmt" "io" "io/ioutil" "log" + "net/http" "os" "os/exec" "path/filepath" @@ -123,3 +125,30 @@ func (t *T) RunTest(tests ...func(*T) error) (err error) { // Note that the return value 'err' may be updated by the 'defer' statement before despite it's returning nil here. return nil } + +func GetAndPost(url string, form map[string][]string, expectedStatus int) error { + var err error + var r *http.Response + + r, err = http.Get(url) + if err != nil { + return err + } + defer r.Body.Close() + + if r.StatusCode != http.StatusOK { + return fmt.Errorf("GET '%s': %s", url, r.Status) + } + + r, err = http.PostForm(url, form) + if err != nil { + return err + } + defer r.Body.Close() + + if r.StatusCode != expectedStatus { + return fmt.Errorf("POST '%s': %s", url, r.Status) + } + + return nil +} diff --git a/integrations/signup_test.go b/integrations/signup_test.go index 26ff2c7c9f59b..cf2fd37472194 100644 --- a/integrations/signup_test.go +++ b/integrations/signup_test.go @@ -5,7 +5,6 @@ package integration import ( - "fmt" "net/http" "os" "testing" @@ -20,30 +19,7 @@ var signupFormSample map[string][]string = map[string][]string{ } func signup(t *utils.T) error { - var err error - var r *http.Response - - r, err = http.Get("http://:" + ServerHTTPPort + "/user/sign_up") - if err != nil { - return err - } - defer r.Body.Close() - - if r.StatusCode != http.StatusOK { - return fmt.Errorf("GET '/user/signup': %s", r.Status) - } - - r, err = http.PostForm("http://:"+ServerHTTPPort+"/user/sign_up", signupFormSample) - if err != nil { - return err - } - defer r.Body.Close() - - if r.StatusCode != http.StatusOK { - return fmt.Errorf("POST '/user/signup': %s", r.Status) - } - - return nil + return utils.GetAndPost("http://:"+ServerHTTPPort+"/user/sign_up", signupFormSample, http.StatusOK) } func TestSignup(t *testing.T) { From 21613fc427f8b4477fc0cdb373fd352ce080503f Mon Sep 17 00:00:00 2001 From: Mura Li Date: Thu, 9 Mar 2017 09:50:48 +0800 Subject: [PATCH 4/6] Add repo_create_test.go --- integrations/repo_create_test.go | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 integrations/repo_create_test.go diff --git a/integrations/repo_create_test.go b/integrations/repo_create_test.go new file mode 100644 index 0000000000000..04decd3ea66f4 --- /dev/null +++ b/integrations/repo_create_test.go @@ -0,0 +1,41 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package integration + +import ( + "net/http" + "os" + "testing" + + "code.gitea.io/gitea/integrations/internal/utils" +) + +var createRepoFormSample map[string][]string = map[string][]string{ + "UID": []string{}, + "RepoName": []string{}, + "Private": []string{}, + "Description": []string{}, + "AutoInit": []string{}, + "Gitignores": []string{}, + "License": []string{}, + "Readme": []string{}, +} + +func repoCreate(t *utils.T) error { + return utils.GetAndPost("http://:"+ServerHTTPPort+"/repo/create", createRepoFormSample, http.StatusOK) +} + +func TestRepoCreate(t *testing.T) { + conf := utils.Config{ + Program: "../gitea", + WorkDir: "", + Args: []string{"web", "--port", ServerHTTPPort}, + LogFile: os.Stderr, + } + + if err := utils.New(t, &conf).RunTest(install, signup, repoCreate); err != nil { + t.Fatal(err) + } +} From d377d1616270c0910e300c133fed18a594446d31 Mon Sep 17 00:00:00 2001 From: Mura Li Date: Thu, 9 Mar 2017 10:31:24 +0800 Subject: [PATCH 5/6] Rollback the incomplete repo create test --- integrations/repo_create_test.go | 41 -------------------------------- 1 file changed, 41 deletions(-) delete mode 100644 integrations/repo_create_test.go diff --git a/integrations/repo_create_test.go b/integrations/repo_create_test.go deleted file mode 100644 index 04decd3ea66f4..0000000000000 --- a/integrations/repo_create_test.go +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2017 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package integration - -import ( - "net/http" - "os" - "testing" - - "code.gitea.io/gitea/integrations/internal/utils" -) - -var createRepoFormSample map[string][]string = map[string][]string{ - "UID": []string{}, - "RepoName": []string{}, - "Private": []string{}, - "Description": []string{}, - "AutoInit": []string{}, - "Gitignores": []string{}, - "License": []string{}, - "Readme": []string{}, -} - -func repoCreate(t *utils.T) error { - return utils.GetAndPost("http://:"+ServerHTTPPort+"/repo/create", createRepoFormSample, http.StatusOK) -} - -func TestRepoCreate(t *testing.T) { - conf := utils.Config{ - Program: "../gitea", - WorkDir: "", - Args: []string{"web", "--port", ServerHTTPPort}, - LogFile: os.Stderr, - } - - if err := utils.New(t, &conf).RunTest(install, signup, repoCreate); err != nil { - t.Fatal(err) - } -} From c3883a035390575c69b67e2a585c57448eeaf844 Mon Sep 17 00:00:00 2001 From: Mura Li Date: Thu, 9 Mar 2017 10:44:10 +0800 Subject: [PATCH 6/6] Comply with linter requirements and simplify the code a little bit --- integrations/internal/utils/utils.go | 6 ++++-- integrations/signup_test.go | 3 +-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/integrations/internal/utils/utils.go b/integrations/internal/utils/utils.go index 22429c7fdadd0..e8380df39ea68 100644 --- a/integrations/internal/utils/utils.go +++ b/integrations/internal/utils/utils.go @@ -126,7 +126,9 @@ func (t *T) RunTest(tests ...func(*T) error) (err error) { return nil } -func GetAndPost(url string, form map[string][]string, expectedStatus int) error { +// GetAndPost provides a convenient helper function for testing an HTTP endpoint with GET and POST method. +// The function sends GET first and then POST with the given form. +func GetAndPost(url string, form map[string][]string) error { var err error var r *http.Response @@ -146,7 +148,7 @@ func GetAndPost(url string, form map[string][]string, expectedStatus int) error } defer r.Body.Close() - if r.StatusCode != expectedStatus { + if r.StatusCode != http.StatusOK { return fmt.Errorf("POST '%s': %s", url, r.Status) } diff --git a/integrations/signup_test.go b/integrations/signup_test.go index cf2fd37472194..c317b88d10c05 100644 --- a/integrations/signup_test.go +++ b/integrations/signup_test.go @@ -5,7 +5,6 @@ package integration import ( - "net/http" "os" "testing" @@ -19,7 +18,7 @@ var signupFormSample map[string][]string = map[string][]string{ } func signup(t *utils.T) error { - return utils.GetAndPost("http://:"+ServerHTTPPort+"/user/sign_up", signupFormSample, http.StatusOK) + return utils.GetAndPost("http://:"+ServerHTTPPort+"/user/sign_up", signupFormSample) } func TestSignup(t *testing.T) {