-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add new CLI flags to set name and scopes when creating a user with access token #34080
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
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
20e27a9
Add --with-scopes CLI flag when creating a user with access token
kemzeb 87eec64
Fix typo in test function
kemzeb fda7e9b
Reimplement --access-token to accept scopes
kemzeb 3e06100
improve usage
wxiaoguang 61d0a2f
use separate cli flags
wxiaoguang 010962c
fix tests
wxiaoguang c02b06e
Add additional tests
kemzeb ae5f92c
Add missing reset()
kemzeb 8d893a6
Merge branch 'main' into feat/admin-cli
wxiaoguang 0387ff2
handle empty scope
wxiaoguang ac86171
arguments should be prepared before creating the user & access token,…
wxiaoguang 2c768f5
add more test asserts
wxiaoguang 336d188
Merge branch 'main' into feat/admin-cli
wxiaoguang 6f5c97c
fix prompt order
wxiaoguang 2a5375c
Merge branch 'main' into feat/admin-cli
wxiaoguang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"strings" | ||
"testing" | ||
|
||
auth_model "code.gitea.io/gitea/models/auth" | ||
"code.gitea.io/gitea/models/db" | ||
"code.gitea.io/gitea/models/unittest" | ||
user_model "code.gitea.io/gitea/models/user" | ||
|
@@ -22,6 +23,7 @@ func TestAdminUserCreate(t *testing.T) { | |
reset := func() { | ||
require.NoError(t, db.TruncateBeans(db.DefaultContext, &user_model.User{})) | ||
require.NoError(t, db.TruncateBeans(db.DefaultContext, &user_model.EmailAddress{})) | ||
require.NoError(t, db.TruncateBeans(db.DefaultContext, &auth_model.AccessToken{})) | ||
} | ||
|
||
t.Run("MustChangePassword", func(t *testing.T) { | ||
|
@@ -48,11 +50,11 @@ func TestAdminUserCreate(t *testing.T) { | |
assert.Equal(t, check{IsAdmin: false, MustChangePassword: false}, createCheck("u5", "--must-change-password=false")) | ||
}) | ||
|
||
t.Run("UserType", func(t *testing.T) { | ||
createUser := func(name, args string) error { | ||
return app.Run(strings.Fields(fmt.Sprintf("./gitea admin user create --username %s --email %[email protected] %s", name, name, args))) | ||
} | ||
createUser := func(name, args string) error { | ||
return app.Run(strings.Fields(fmt.Sprintf("./gitea admin user create --username %s --email %[email protected] %s", name, name, args))) | ||
} | ||
|
||
t.Run("UserType", func(t *testing.T) { | ||
reset() | ||
assert.ErrorContains(t, createUser("u", "--user-type invalid"), "invalid user type") | ||
assert.ErrorContains(t, createUser("u", "--user-type bot --password 123"), "can only be set for individual users") | ||
|
@@ -63,4 +65,56 @@ func TestAdminUserCreate(t *testing.T) { | |
assert.Equal(t, user_model.UserTypeBot, u.Type) | ||
assert.Empty(t, u.Passwd) | ||
}) | ||
|
||
t.Run("AccessToken", func(t *testing.T) { | ||
// no generated access token | ||
reset() | ||
assert.NoError(t, createUser("u", "--random-password")) | ||
assert.Equal(t, 1, unittest.GetCount(t, &user_model.User{})) | ||
assert.Equal(t, 0, unittest.GetCount(t, &auth_model.AccessToken{})) | ||
|
||
// using "--access-token" only means "all" access | ||
reset() | ||
assert.NoError(t, createUser("u", "--random-password --access-token")) | ||
assert.Equal(t, 1, unittest.GetCount(t, &user_model.User{})) | ||
assert.Equal(t, 1, unittest.GetCount(t, &auth_model.AccessToken{})) | ||
accessToken := unittest.AssertExistsAndLoadBean(t, &auth_model.AccessToken{Name: "gitea-admin"}) | ||
hasScopes, err := accessToken.Scope.HasScope(auth_model.AccessTokenScopeWriteAdmin, auth_model.AccessTokenScopeWriteRepository) | ||
assert.NoError(t, err) | ||
assert.True(t, hasScopes) | ||
|
||
// using "--access-token" with name & scopes | ||
reset() | ||
assert.NoError(t, createUser("u", "--random-password --access-token --access-token-name new-token-name --access-token-scopes read:issue,read:user")) | ||
assert.Equal(t, 1, unittest.GetCount(t, &user_model.User{})) | ||
assert.Equal(t, 1, unittest.GetCount(t, &auth_model.AccessToken{})) | ||
accessToken = unittest.AssertExistsAndLoadBean(t, &auth_model.AccessToken{Name: "new-token-name"}) | ||
hasScopes, err = accessToken.Scope.HasScope(auth_model.AccessTokenScopeReadIssue, auth_model.AccessTokenScopeReadUser) | ||
assert.NoError(t, err) | ||
assert.True(t, hasScopes) | ||
hasScopes, err = accessToken.Scope.HasScope(auth_model.AccessTokenScopeWriteAdmin, auth_model.AccessTokenScopeWriteRepository) | ||
assert.NoError(t, err) | ||
assert.False(t, hasScopes) | ||
|
||
// using "--access-token-name" without "--access-token" | ||
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") | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.