aboutsummaryrefslogtreecommitdiffstats
path: root/integrations/user_test.go
blob: 4b7d81cfb591e5d4b1ec4a883dbcb77d87e37a2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// 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 integrations

import (
	"net/http"
	"testing"

	"code.gitea.io/gitea/models"

	"github.com/Unknwon/i18n"
	"github.com/stretchr/testify/assert"
)

func TestViewUser(t *testing.T) {
	prepareTestEnv(t)

	req := NewRequest(t, "GET", "/user2")
	resp := MakeRequest(req)
	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
}

func TestRenameUsername(t *testing.T) {
	prepareTestEnv(t)

	session := loginUser(t, "user2")

	req := NewRequest(t, "GET", "/user/settings")
	resp := session.MakeRequest(t, req)
	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)

	htmlDoc := NewHTMLParser(t, resp.Body)
	req = NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
		"_csrf": htmlDoc.GetCSRF(),
		"name":  "newUsername",
		"email": "user2@example.com",
	})
	resp = session.MakeRequest(t, req)
	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)

	models.AssertExistsAndLoadBean(t, &models.User{Name: "newUsername"})
	models.AssertNotExistsBean(t, &models.User{Name: "user2"})
}

func TestRenameInvalidUsername(t *testing.T) {
	prepareTestEnv(t)

	invalidUsernames := []string{
		"%2f*",
		"%2f.",
		"%2f..",
		"%00",
		"thisHas ASpace",
	}

	session := loginUser(t, "user2")
	for _, invalidUsername := range invalidUsernames {
		t.Logf("Testing username %s", invalidUsername)
		req := NewRequest(t, "GET", "/user/settings")
		resp := session.MakeRequest(t, req)
		assert.EqualValues(t, http.StatusOK, resp.HeaderCode)

		htmlDoc := NewHTMLParser(t, resp.Body)
		req = NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
			"_csrf": htmlDoc.GetCSRF(),
			"name":  invalidUsername,
			"email": "user2@example.com",
		})
		resp = session.MakeRequest(t, req)
		assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
		htmlDoc = NewHTMLParser(t, resp.Body)
		assert.Contains(t,
			htmlDoc.doc.Find(".ui.negative.message").Text(),
			i18n.Tr("en", "form.alpha_dash_dot_error"),
		)

		models.AssertNotExistsBean(t, &models.User{Name: invalidUsername})
	}
}

func TestRenameReservedUsername(t *testing.T) {
	prepareTestEnv(t)

	reservedUsernames := []string{
		"help",
		"user",
		"template",
	}

	session := loginUser(t, "user2")
	for _, reservedUsername := range reservedUsernames {
		t.Logf("Testing username %s", reservedUsername)
		req := NewRequest(t, "GET", "/user/settings")
		resp := session.MakeRequest(t, req)
		assert.EqualValues(t, http.StatusOK, resp.HeaderCode)

		htmlDoc := NewHTMLParser(t, resp.Body)
		req = NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
			"_csrf": htmlDoc.GetCSRF(),
			"name":  reservedUsername,
			"email": "user2@example.com",
		})
		resp = session.MakeRequest(t, req)
		assert.EqualValues(t, http.StatusFound, resp.HeaderCode)

		req = NewRequest(t, "GET", "/user/settings")
		resp = session.MakeRequest(t, req)
		assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
		htmlDoc = NewHTMLParser(t, resp.Body)
		assert.Contains(t,
			htmlDoc.doc.Find(".ui.negative.message").Text(),
			i18n.Tr("en", "user.newName_reserved"),
		)

		models.AssertNotExistsBean(t, &models.User{Name: reservedUsername})
	}
}