code.gitea.io/gitea@v1.21.7/tests/integration/signup_test.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"strings"
    10  	"testing"
    11  
    12  	"code.gitea.io/gitea/models/unittest"
    13  	user_model "code.gitea.io/gitea/models/user"
    14  	"code.gitea.io/gitea/modules/setting"
    15  	"code.gitea.io/gitea/modules/translation"
    16  	"code.gitea.io/gitea/tests"
    17  
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  func TestSignup(t *testing.T) {
    22  	defer tests.PrepareTestEnv(t)()
    23  
    24  	setting.Service.EnableCaptcha = false
    25  
    26  	req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{
    27  		"user_name": "exampleUser",
    28  		"email":     "exampleUser@example.com",
    29  		"password":  "examplePassword!1",
    30  		"retype":    "examplePassword!1",
    31  	})
    32  	MakeRequest(t, req, http.StatusSeeOther)
    33  
    34  	// should be able to view new user's page
    35  	req = NewRequest(t, "GET", "/exampleUser")
    36  	MakeRequest(t, req, http.StatusOK)
    37  }
    38  
    39  func TestSignupAsRestricted(t *testing.T) {
    40  	defer tests.PrepareTestEnv(t)()
    41  
    42  	setting.Service.EnableCaptcha = false
    43  	setting.Service.DefaultUserIsRestricted = true
    44  
    45  	req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{
    46  		"user_name": "restrictedUser",
    47  		"email":     "restrictedUser@example.com",
    48  		"password":  "examplePassword!1",
    49  		"retype":    "examplePassword!1",
    50  	})
    51  	MakeRequest(t, req, http.StatusSeeOther)
    52  
    53  	// should be able to view new user's page
    54  	req = NewRequest(t, "GET", "/restrictedUser")
    55  	MakeRequest(t, req, http.StatusOK)
    56  
    57  	user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "restrictedUser"})
    58  	assert.True(t, user2.IsRestricted)
    59  }
    60  
    61  func TestSignupEmail(t *testing.T) {
    62  	defer tests.PrepareTestEnv(t)()
    63  
    64  	setting.Service.EnableCaptcha = false
    65  
    66  	tests := []struct {
    67  		email      string
    68  		wantStatus int
    69  		wantMsg    string
    70  	}{
    71  		{"exampleUser@example.com\r\n", http.StatusOK, translation.NewLocale("en-US").Tr("form.email_invalid")},
    72  		{"exampleUser@example.com\r", http.StatusOK, translation.NewLocale("en-US").Tr("form.email_invalid")},
    73  		{"exampleUser@example.com\n", http.StatusOK, translation.NewLocale("en-US").Tr("form.email_invalid")},
    74  		{"exampleUser@example.com", http.StatusSeeOther, ""},
    75  	}
    76  
    77  	for i, test := range tests {
    78  		req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{
    79  			"user_name": fmt.Sprintf("exampleUser%d", i),
    80  			"email":     test.email,
    81  			"password":  "examplePassword!1",
    82  			"retype":    "examplePassword!1",
    83  		})
    84  		resp := MakeRequest(t, req, test.wantStatus)
    85  		if test.wantMsg != "" {
    86  			htmlDoc := NewHTMLParser(t, resp.Body)
    87  			assert.Equal(t,
    88  				test.wantMsg,
    89  				strings.TrimSpace(htmlDoc.doc.Find(".ui.message").Text()),
    90  			)
    91  		}
    92  	}
    93  }