You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.2 KiB
67 lines
2.2 KiB
7 years ago
|
// 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.
|
||
|
|
||
2 years ago
|
package integration
|
||
7 years ago
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"testing"
|
||
|
|
||
2 years ago
|
auth_model "code.gitea.io/gitea/models/auth"
|
||
3 years ago
|
"code.gitea.io/gitea/models/unittest"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
6 years ago
|
api "code.gitea.io/gitea/modules/structs"
|
||
2 years ago
|
"code.gitea.io/gitea/tests"
|
||
7 years ago
|
)
|
||
|
|
||
|
// TestAPICreateAndDeleteToken tests that token that was just created can be deleted
|
||
|
func TestAPICreateAndDeleteToken(t *testing.T) {
|
||
2 years ago
|
defer tests.PrepareTestEnv(t)()
|
||
2 years ago
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
||
7 years ago
|
|
||
|
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)
|
||
2 years ago
|
unittest.AssertExistsAndLoadBean(t, &auth_model.AccessToken{
|
||
6 years ago
|
ID: newAccessToken.ID,
|
||
|
Name: newAccessToken.Name,
|
||
|
Token: newAccessToken.Token,
|
||
|
UID: user.ID,
|
||
7 years ago
|
})
|
||
|
|
||
|
req = NewRequestf(t, "DELETE", "/api/v1/users/user1/tokens/%d", newAccessToken.ID)
|
||
|
req = AddBasicAuthHeader(req, user.Name)
|
||
|
MakeRequest(t, req, http.StatusNoContent)
|
||
|
|
||
2 years ago
|
unittest.AssertNotExistsBean(t, &auth_model.AccessToken{ID: newAccessToken.ID})
|
||
4 years ago
|
|
||
|
req = NewRequestWithJSON(t, "POST", "/api/v1/users/user1/tokens", map[string]string{
|
||
|
"name": "test-key-2",
|
||
|
})
|
||
|
req = AddBasicAuthHeader(req, user.Name)
|
||
|
resp = MakeRequest(t, req, http.StatusCreated)
|
||
|
DecodeJSON(t, resp, &newAccessToken)
|
||
|
|
||
|
req = NewRequestf(t, "DELETE", "/api/v1/users/user1/tokens/%s", newAccessToken.Name)
|
||
|
req = AddBasicAuthHeader(req, user.Name)
|
||
|
MakeRequest(t, req, http.StatusNoContent)
|
||
|
|
||
2 years ago
|
unittest.AssertNotExistsBean(t, &auth_model.AccessToken{ID: newAccessToken.ID})
|
||
7 years ago
|
}
|
||
|
|
||
|
// TestAPIDeleteMissingToken ensures that error is thrown when token not found
|
||
|
func TestAPIDeleteMissingToken(t *testing.T) {
|
||
2 years ago
|
defer tests.PrepareTestEnv(t)()
|
||
2 years ago
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
||
7 years ago
|
|
||
3 years ago
|
req := NewRequestf(t, "DELETE", "/api/v1/users/user1/tokens/%d", unittest.NonexistentID)
|
||
7 years ago
|
req = AddBasicAuthHeader(req, user.Name)
|
||
|
MakeRequest(t, req, http.StatusNotFound)
|
||
|
}
|