Skip to content

Commit bdcc1a2

Browse files
typelesslunny
authored andcommitted
Add integration test for signup (#1135)
* Add integration test for signup * Remove unused functions * Refactoring * Add repo_create_test.go * Rollback the incomplete repo create test * Comply with linter requirements and simplify the code a little bit
1 parent 8a98a25 commit bdcc1a2

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

integrations/internal/utils/utils.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ package utils
66

77
import (
88
"errors"
9+
"fmt"
910
"io"
1011
"io/ioutil"
1112
"log"
13+
"net/http"
1214
"os"
1315
"os/exec"
1416
"path/filepath"
@@ -123,3 +125,32 @@ func (t *T) RunTest(tests ...func(*T) error) (err error) {
123125
// Note that the return value 'err' may be updated by the 'defer' statement before despite it's returning nil here.
124126
return nil
125127
}
128+
129+
// GetAndPost provides a convenient helper function for testing an HTTP endpoint with GET and POST method.
130+
// The function sends GET first and then POST with the given form.
131+
func GetAndPost(url string, form map[string][]string) error {
132+
var err error
133+
var r *http.Response
134+
135+
r, err = http.Get(url)
136+
if err != nil {
137+
return err
138+
}
139+
defer r.Body.Close()
140+
141+
if r.StatusCode != http.StatusOK {
142+
return fmt.Errorf("GET '%s': %s", url, r.Status)
143+
}
144+
145+
r, err = http.PostForm(url, form)
146+
if err != nil {
147+
return err
148+
}
149+
defer r.Body.Close()
150+
151+
if r.StatusCode != http.StatusOK {
152+
return fmt.Errorf("POST '%s': %s", url, r.Status)
153+
}
154+
155+
return nil
156+
}

integrations/signup_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integration
6+
7+
import (
8+
"os"
9+
"testing"
10+
11+
"code.gitea.io/gitea/integrations/internal/utils"
12+
)
13+
14+
var signupFormSample map[string][]string = map[string][]string{
15+
"Name": []string{"tester"},
16+
"Email": []string{"[email protected]"},
17+
"Passwd": []string{"12345678"},
18+
}
19+
20+
func signup(t *utils.T) error {
21+
return utils.GetAndPost("http://:"+ServerHTTPPort+"/user/sign_up", signupFormSample)
22+
}
23+
24+
func TestSignup(t *testing.T) {
25+
conf := utils.Config{
26+
Program: "../gitea",
27+
WorkDir: "",
28+
Args: []string{"web", "--port", ServerHTTPPort},
29+
LogFile: os.Stderr,
30+
}
31+
32+
if err := utils.New(t, &conf).RunTest(install, signup); err != nil {
33+
t.Fatal(err)
34+
}
35+
}

0 commit comments

Comments
 (0)