Skip to content

Add integration test for signup #1135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions integrations/internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ package utils

import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -123,3 +125,32 @@ 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
}

// 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

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 != http.StatusOK {
return fmt.Errorf("POST '%s': %s", url, r.Status)
}

return nil
}
35 changes: 35 additions & 0 deletions integrations/signup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should move this file to routers/user/ folder and rename to auth_test.go ?

Copy link
Contributor Author

@typeless typeless Mar 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The motivation that led me to this way is that it depends on /install and some housekeeping works (creating tempdir, launching the program ... etc.). Imaging that we are testing 'merging a pull-request', We have to

  1. Set up a Gitea server from /install
  2. Signup a user
  3. Create a repo
  4. Push a prepared repo to Gitea
  5. Signup another user
  6. Fork the repo on Gitea
  7. Clone it
  8. Make some changes to the cloned code, commit them and push them back
  9. Create a pull request
  10. Push the 'merge' button

That's what I had to do manually to test it reliably without depending on certain kept states.
Otherwise, I'd also go for the idiomatic unit test scheme.

Edit: Another approach is to prepare set-up Gitea environments and just relying on them. That could be a good idea as well. I will think about it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can have an hibrid approach: we'd have an "integration" package that would have the setup and cleanup logic, but the tests itself would be on the router packages.

There's one more thing I'd change: Go routers like Macaron have an ServeHTTP method, which can be used to simulate HTTP requests created in tests. So no need to run Gitea in a port and do a real request through the network.

These are ideas, we're totally open to discussion. =)


import (
"os"
"testing"

"code.gitea.io/gitea/integrations/internal/utils"
)

var signupFormSample map[string][]string = map[string][]string{
"Name": []string{"tester"},
"Email": []string{"[email protected]"},
"Passwd": []string{"12345678"},
}

func signup(t *utils.T) error {
return utils.GetAndPost("http://:"+ServerHTTPPort+"/user/sign_up", signupFormSample)
}

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)
}
}