Repository: go-gitea/gitea
File: cmd/admin_user_create_test.go (Go)

Code before:
		reset()
		err = createUser("u", "--random-password --access-token-name new-token-name")
		assert.Equal(t, 0, unittest.GetCount(t, &user_model.User{}))
		assert.Equal(t, 0, unittest.GetCount(t, &auth_model.AccessToken{}))
		assert.ErrorContains(t, err, "access-token-name and access-token-scopes flags are only valid when access-token flag is set")

		// using "--access-token-scopes" without "--access-token"
		reset()
		err = createUser("u", "--random-password --access-token-scopes read:issue")
		assert.Equal(t, 0, unittest.GetCount(t, &user_model.User{}))
		assert.Equal(t, 0, unittest.GetCount(t, &auth_model.AccessToken{}))
		assert.ErrorContains(t, err, "access-token-name and access-token-scopes flags are only valid when access-token flag is set")

		// empty permission
		reset()
		err = createUser("u", "--random-password --access-token --access-token-scopes public-only")
		assert.Equal(t, 0, unittest.GetCount(t, &user_model.User{}))
		assert.Equal(t, 0, unittest.GetCount(t, &auth_model.AccessToken{}))
		assert.ErrorContains(t, err, "access token does not have any permission")
	})

	createUserN := func(name string, args []string) error {
		cmd := []string{"./gitea", "admin", "user", "create", "--username", name, "--email", name + "@gitea.local"}
		cmd = append(cmd, args...)
		return app.Run(cmd)
	}
	t.Run("UserFields", func(t *testing.T) {
		reset()
		assert.NoError(t, createUserN("uA", []string{"--random-password", "--fullname", "First Last"}))
		u := unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "ua"})
		assert.Equal(t, "First Last", u.FullName)
		assert.Equal(t, "uA", u.Name)
		assert.Equal(t, "ua", u.LowerName)
		assert.Equal(t, "uA@gitea.local", u.Email)

		assert.NoError(t, createUserN("uB", []string{"--random-password", "--fullname", "First O'Last"}))
		u = unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "ub"})
		assert.Equal(t, "First O'Last", u.FullName)

		assert.NoError(t, createUserN("uC", []string{"--random-password", "--fullname", "First Middle Last"}))
		u = unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "uc"})
		assert.Equal(t, "First Middle Last", u.FullName)

		assert.NoError(t, createUserN("uD", []string{"--random-password", "--fullname", ""}))
		u = unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "ud"})
		assert.Empty(t, u.FullName)
	})
}


Review comment (suggestion):
It doesn't to test "space" behavior. You could just still use the old `createUser`

The "space" behavior is guaranteed by the shell CLI

Code after:
		reset()
		err = createUser("u", "--random-password", "--access-token-name", "new-token-name")
		assert.Equal(t, 0, unittest.GetCount(t, &user_model.User{}))
		assert.Equal(t, 0, unittest.GetCount(t, &auth_model.AccessToken{}))
		assert.ErrorContains(t, err, "access-token-name and access-token-scopes flags are only valid when access-token flag is set")

		// using "--access-token-scopes" without "--access-token"
		reset()
		err = createUser("u", "--random-password", "--access-token-scopes", "read:issue")
		assert.Equal(t, 0, unittest.GetCount(t, &user_model.User{}))
		assert.Equal(t, 0, unittest.GetCount(t, &auth_model.AccessToken{}))
		assert.ErrorContains(t, err, "access-token-name and access-token-scopes flags are only valid when access-token flag is set")

		// empty permission
		reset()
		err = createUser("u", "--random-password", "--access-token", "--access-token-scopes", "public-only")
		assert.Equal(t, 0, unittest.GetCount(t, &user_model.User{}))
		assert.Equal(t, 0, unittest.GetCount(t, &auth_model.AccessToken{}))
		assert.ErrorContains(t, err, "access token does not have any permission")
	})

	t.Run("UserFields", func(t *testing.T) {
		reset()
		assert.NoError(t, createUser("u-FullNameWithSpace", "--random-password", "--fullname", "First O'Middle Last"))
		unittest.AssertExistsAndLoadBean(t, &user_model.User{
			Name:      "u-FullNameWithSpace",
			LowerName: "u-fullnamewithspace",
			FullName:  "First O'Middle Last",
			Email:     "u-FullNameWithSpace@gitea.local",
		})

		assert.NoError(t, createUser("u-FullNameEmpty", "--random-password", "--fullname", ""))
		u := unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "u-fullnameempty"})
		assert.Empty(t, u.FullName)
	})
}