-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Add ability to delete a token #4235
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9ca5c60
Add ability to delete a token
techknowlogick 4ec4dc8
add swagger file
techknowlogick 8654ad7
resolve swagger issues
techknowlogick e211c28
correct api description
techknowlogick 112ec99
add ID as an output for list API
techknowlogick b91a969
update code.gitea.io/sdk with changes
techknowlogick 9cae3ea
Merge branch 'master' into issue-4234
techknowlogick f426f13
ID first
techknowlogick b6891c1
Merge branch 'master' into issue-4234
techknowlogick 0d8a446
Merge branch 'master' into issue-4234
techknowlogick 071ad8d
Merge branch 'master' into issue-4234
techknowlogick 255ccc9
add some tests
techknowlogick ae4f22a
fix syntax
techknowlogick b47947a
make fmt
techknowlogick 52f139a
missing import
techknowlogick 1afda06
remove import
techknowlogick be1687f
tokens api can only auth via basic auth
techknowlogick 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2018 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" | ||
api "code.gitea.io/sdk/gitea" | ||
) | ||
|
||
// TestAPICreateAndDeleteToken tests that token that was just created can be deleted | ||
func TestAPICreateAndDeleteToken(t *testing.T) { | ||
prepareTestEnv(t) | ||
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) | ||
|
||
req := NewRequestWithJSON(t, "POST", "/api/v1/users/user1/tokens", map[string]string{ | ||
"name": "test-key-1", | ||
}) | ||
req = AddBasicAuthHeader(req, user.Name) | ||
resp := MakeRequest(t, req, http.StatusCreated) | ||
|
||
var newAccessToken api.AccessToken | ||
DecodeJSON(t, resp, &newAccessToken) | ||
models.AssertExistsAndLoadBean(t, &models.AccessToken{ | ||
ID: newAccessToken.ID, | ||
Name: newAccessToken.Name, | ||
Sha1: newAccessToken.Sha1, | ||
UID: user.ID, | ||
}) | ||
|
||
req = NewRequestf(t, "DELETE", "/api/v1/users/user1/tokens/%d", newAccessToken.ID) | ||
req = AddBasicAuthHeader(req, user.Name) | ||
MakeRequest(t, req, http.StatusNoContent) | ||
|
||
models.AssertNotExistsBean(t, &models.AccessToken{ID: newAccessToken.ID}) | ||
} | ||
|
||
// TestAPIDeleteMissingToken ensures that error is thrown when token not found | ||
func TestAPIDeleteMissingToken(t *testing.T) { | ||
prepareTestEnv(t) | ||
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) | ||
|
||
req := NewRequestf(t, "DELETE", "/api/v1/users/user1/tokens/%d", models.NonexistentID) | ||
req = AddBasicAuthHeader(req, user.Name) | ||
MakeRequest(t, req, http.StatusNotFound) | ||
} |
This file contains 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 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 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question for reviewers: This deletes based on ID (int), however we only return the name (string) of the token above. So this works, but users need to know what the ID (int) is before using this. My question is: Should this be changed to delete based on token name (string)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does GitHub API works?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lafriks Their docs say they use the integer ID of the token: https://developer.github.com/v3/oauth_authorizations/#delete-an-authorization
They also list the ID in the response when listing the tokens, so I will keep this PR the same, and I'll update this PR to include the ID in the List endpoint.