Increase unit test coverage of api/user.go (#4541)
* Add test to CheckUserDomain * Add unit test to IsUsernameTaken
Этот коммит содержится в:
коммит произвёл
enahum
родитель
ef080a0a10
Коммит
602f85d2ef
@@ -192,6 +192,7 @@ func createUser(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check that a user's email domain matches a list of space-delimited domains as a string.
|
||||||
func CheckUserDomain(user *model.User, domains string) bool {
|
func CheckUserDomain(user *model.User, domains string) bool {
|
||||||
if len(domains) == 0 {
|
if len(domains) == 0 {
|
||||||
return true
|
return true
|
||||||
@@ -1957,6 +1958,7 @@ func updateUserNotify(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if the username is already used by another user. Return false if the username is invalid.
|
||||||
func IsUsernameTaken(name string) bool {
|
func IsUsernameTaken(name string) bool {
|
||||||
|
|
||||||
if !model.IsValidUsername(name) {
|
if !model.IsValidUsername(name) {
|
||||||
|
|||||||
@@ -80,6 +80,51 @@ func TestCreateUser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCheckUserDomain(t *testing.T) {
|
||||||
|
th := Setup().InitBasic()
|
||||||
|
user := th.BasicUser
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
domains string
|
||||||
|
matched bool
|
||||||
|
}{
|
||||||
|
{"simulator.amazonses.com", true},
|
||||||
|
{"gmail.com", false},
|
||||||
|
{"", true},
|
||||||
|
{"gmail.com simulator.amazonses.com", true},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
matched := CheckUserDomain(user, c.domains)
|
||||||
|
if matched != c.matched {
|
||||||
|
if c.matched {
|
||||||
|
t.Logf("'%v' should have matched '%v'", user.Email, c.domains)
|
||||||
|
} else {
|
||||||
|
t.Logf("'%v' should not have matched '%v'", user.Email, c.domains)
|
||||||
|
}
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsUsernameTaken(t *testing.T) {
|
||||||
|
th := Setup().InitBasic()
|
||||||
|
user := th.BasicUser
|
||||||
|
taken := IsUsernameTaken(user.Username)
|
||||||
|
|
||||||
|
if !taken {
|
||||||
|
t.Logf("the username '%v' should be taken", user.Username)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
newUsername := "randomUsername"
|
||||||
|
taken = IsUsernameTaken(newUsername)
|
||||||
|
|
||||||
|
if taken {
|
||||||
|
t.Logf("the username '%v' should not be taken", newUsername)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestLogin(t *testing.T) {
|
func TestLogin(t *testing.T) {
|
||||||
th := Setup()
|
th := Setup()
|
||||||
Client := th.CreateClient()
|
Client := th.CreateClient()
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user