Files
Atay-Makhzan/tests/integration/api_keys_test.go
T

213 lines
9.2 KiB
Go
Raw Normal View History

2017-07-11 21:23:41 -04:00
// Copyright 2017 The Gogs Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2017-07-11 21:23:41 -04:00
2022-09-02 15:18:23 -04:00
package integration
2017-07-11 21:23:41 -04:00
import (
2018-01-07 00:55:53 +02:00
"fmt"
2017-07-11 21:23:41 -04:00
"net/http"
2018-11-01 03:40:49 +00:00
"net/url"
2017-07-11 21:23:41 -04:00
"testing"
2021-12-10 16:14:24 +08:00
asymkey_model "code.gitea.io/gitea/models/asymkey"
2023-01-17 16:46:03 -05:00
auth_model "code.gitea.io/gitea/models/auth"
2021-11-28 19:58:28 +08:00
"code.gitea.io/gitea/models/perm"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
2019-05-11 18:21:34 +08:00
api "code.gitea.io/gitea/modules/structs"
2022-09-02 15:18:23 -04:00
"code.gitea.io/gitea/tests"
2019-08-23 09:40:30 -07:00
"github.com/stretchr/testify/assert"
2017-07-11 21:23:41 -04:00
)
func TestViewDeployKeysNoLogin(t *testing.T) {
2022-09-02 15:18:23 -04:00
defer tests.PrepareTestEnv(t)()
2017-07-11 21:23:41 -04:00
req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/keys")
MakeRequest(t, req, http.StatusUnauthorized)
}
func TestCreateDeployKeyNoLogin(t *testing.T) {
2022-09-02 15:18:23 -04:00
defer tests.PrepareTestEnv(t)()
2017-07-11 21:23:41 -04:00
req := NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/keys", api.CreateKeyOption{
Title: "title",
Key: "key",
})
MakeRequest(t, req, http.StatusUnauthorized)
}
func TestGetDeployKeyNoLogin(t *testing.T) {
2022-09-02 15:18:23 -04:00
defer tests.PrepareTestEnv(t)()
2017-07-11 21:23:41 -04:00
req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/keys/1")
MakeRequest(t, req, http.StatusUnauthorized)
}
func TestDeleteDeployKeyNoLogin(t *testing.T) {
2022-09-02 15:18:23 -04:00
defer tests.PrepareTestEnv(t)()
2017-07-11 21:23:41 -04:00
req := NewRequest(t, "DELETE", "/api/v1/repos/user2/repo1/keys/1")
MakeRequest(t, req, http.StatusUnauthorized)
}
2018-01-07 00:55:53 +02:00
func TestCreateReadOnlyDeployKey(t *testing.T) {
2022-09-02 15:18:23 -04:00
defer tests.PrepareTestEnv(t)()
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: "repo1"})
repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
2018-01-07 00:55:53 +02:00
session := loginUser(t, repoOwner.Name)
2023-06-04 14:57:16 -04:00
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
keysURL := fmt.Sprintf("/api/v1/repos/%s/%s/keys", repoOwner.Name, repo.Name)
2018-01-07 00:55:53 +02:00
rawKeyBody := api.CreateKeyOption{
Title: "read-only",
2020-10-09 07:52:57 +01:00
Key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC4cn+iXnA4KvcQYSV88vGn0Yi91vG47t1P7okprVmhNTkipNRIHWr6WdCO4VDr/cvsRkuVJAsLO2enwjGWWueOO6BodiBgyAOZ/5t5nJNMCNuLGT5UIo/RI1b0WRQwxEZTRjt6mFNw6lH14wRd8ulsr9toSWBPMOGWoYs1PDeDL0JuTjL+tr1SZi/EyxCngpYszKdXllJEHyI79KQgeD0Vt3pTrkbNVTOEcCNqZePSVmUH8X8Vhugz3bnE0/iE9Pb5fkWO9c4AnM1FgI/8Bvp27Fw2ShryIXuR6kKvUqhVMTuOSDHwu6A8jLE5Owt3GAYugDpDYuwTVNGrHLXKpPzrGGPE/jPmaLCMZcsdkec95dYeU3zKODEm8UQZFhmJmDeWVJ36nGrGZHL4J5aTTaeFUJmmXDaJYiJ+K2/ioKgXqnXvltu0A9R8/LGy4nrTJRr4JMLuJFoUXvGm1gXQ70w2LSpk6yl71RNC0hCtsBe8BP8IhYCM0EP5jh7eCMQZNvM= nocomment\n",
2018-01-07 00:55:53 +02:00
ReadOnly: true,
}
req := NewRequestWithJSON(t, "POST", keysURL, rawKeyBody).
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp := MakeRequest(t, req, http.StatusCreated)
2018-01-07 00:55:53 +02:00
var newDeployKey api.DeployKey
DecodeJSON(t, resp, &newDeployKey)
2021-12-10 16:14:24 +08:00
unittest.AssertExistsAndLoadBean(t, &asymkey_model.DeployKey{
2018-01-07 00:55:53 +02:00
ID: newDeployKey.ID,
Name: rawKeyBody.Title,
Content: rawKeyBody.Key,
2021-11-28 19:58:28 +08:00
Mode: perm.AccessModeRead,
2018-01-07 00:55:53 +02:00
})
2023-11-26 01:21:21 +08:00
// Using the ID of a key that does not belong to the repository must fail
{
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/keys/%d", repoOwner.Name, repo.Name, newDeployKey.ID)).
AddTokenAuth(token)
2023-11-26 01:21:21 +08:00
MakeRequest(t, req, http.StatusOK)
session5 := loginUser(t, "user5")
token5 := getTokenForLoggedInUser(t, session5, auth_model.AccessTokenScopeWriteRepository)
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/user5/repo4/keys/%d", newDeployKey.ID)).
AddTokenAuth(token5)
2023-11-26 01:21:21 +08:00
MakeRequest(t, req, http.StatusNotFound)
}
2018-01-07 00:55:53 +02:00
}
func TestCreateReadWriteDeployKey(t *testing.T) {
2022-09-02 15:18:23 -04:00
defer tests.PrepareTestEnv(t)()
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: "repo1"})
repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
2018-01-07 00:55:53 +02:00
session := loginUser(t, repoOwner.Name)
2023-06-04 14:57:16 -04:00
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
keysURL := fmt.Sprintf("/api/v1/repos/%s/%s/keys", repoOwner.Name, repo.Name)
2018-01-07 00:55:53 +02:00
rawKeyBody := api.CreateKeyOption{
Title: "read-write",
2020-10-09 07:52:57 +01:00
Key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC4cn+iXnA4KvcQYSV88vGn0Yi91vG47t1P7okprVmhNTkipNRIHWr6WdCO4VDr/cvsRkuVJAsLO2enwjGWWueOO6BodiBgyAOZ/5t5nJNMCNuLGT5UIo/RI1b0WRQwxEZTRjt6mFNw6lH14wRd8ulsr9toSWBPMOGWoYs1PDeDL0JuTjL+tr1SZi/EyxCngpYszKdXllJEHyI79KQgeD0Vt3pTrkbNVTOEcCNqZePSVmUH8X8Vhugz3bnE0/iE9Pb5fkWO9c4AnM1FgI/8Bvp27Fw2ShryIXuR6kKvUqhVMTuOSDHwu6A8jLE5Owt3GAYugDpDYuwTVNGrHLXKpPzrGGPE/jPmaLCMZcsdkec95dYeU3zKODEm8UQZFhmJmDeWVJ36nGrGZHL4J5aTTaeFUJmmXDaJYiJ+K2/ioKgXqnXvltu0A9R8/LGy4nrTJRr4JMLuJFoUXvGm1gXQ70w2LSpk6yl71RNC0hCtsBe8BP8IhYCM0EP5jh7eCMQZNvM= nocomment\n",
2018-01-07 00:55:53 +02:00
}
req := NewRequestWithJSON(t, "POST", keysURL, rawKeyBody).
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp := MakeRequest(t, req, http.StatusCreated)
2018-01-07 00:55:53 +02:00
var newDeployKey api.DeployKey
DecodeJSON(t, resp, &newDeployKey)
2021-12-10 16:14:24 +08:00
unittest.AssertExistsAndLoadBean(t, &asymkey_model.DeployKey{
2018-01-07 00:55:53 +02:00
ID: newDeployKey.ID,
Name: rawKeyBody.Title,
Content: rawKeyBody.Key,
2021-11-28 19:58:28 +08:00
Mode: perm.AccessModeWrite,
2018-01-07 00:55:53 +02:00
})
}
2018-11-01 03:40:49 +00:00
func TestCreateUserKey(t *testing.T) {
2022-09-02 15:18:23 -04:00
defer tests.PrepareTestEnv(t)()
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user1"})
2018-11-01 03:40:49 +00:00
session := loginUser(t, "user1")
2023-06-04 14:57:16 -04:00
token := url.QueryEscape(getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteUser))
2018-11-01 03:40:49 +00:00
keyType := "ssh-rsa"
2020-10-09 07:52:57 +01:00
keyContent := "AAAAB3NzaC1yc2EAAAADAQABAAABgQC4cn+iXnA4KvcQYSV88vGn0Yi91vG47t1P7okprVmhNTkipNRIHWr6WdCO4VDr/cvsRkuVJAsLO2enwjGWWueOO6BodiBgyAOZ/5t5nJNMCNuLGT5UIo/RI1b0WRQwxEZTRjt6mFNw6lH14wRd8ulsr9toSWBPMOGWoYs1PDeDL0JuTjL+tr1SZi/EyxCngpYszKdXllJEHyI79KQgeD0Vt3pTrkbNVTOEcCNqZePSVmUH8X8Vhugz3bnE0/iE9Pb5fkWO9c4AnM1FgI/8Bvp27Fw2ShryIXuR6kKvUqhVMTuOSDHwu6A8jLE5Owt3GAYugDpDYuwTVNGrHLXKpPzrGGPE/jPmaLCMZcsdkec95dYeU3zKODEm8UQZFhmJmDeWVJ36nGrGZHL4J5aTTaeFUJmmXDaJYiJ+K2/ioKgXqnXvltu0A9R8/LGy4nrTJRr4JMLuJFoUXvGm1gXQ70w2LSpk6yl71RNC0hCtsBe8BP8IhYCM0EP5jh7eCMQZNvM="
2018-11-01 03:40:49 +00:00
rawKeyBody := api.CreateKeyOption{
Title: "test-key",
Key: keyType + " " + keyContent,
}
req := NewRequestWithJSON(t, "POST", "/api/v1/user/keys", rawKeyBody).
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp := MakeRequest(t, req, http.StatusCreated)
2018-11-01 03:40:49 +00:00
var newPublicKey api.PublicKey
DecodeJSON(t, resp, &newPublicKey)
2022-06-05 03:18:50 +08:00
fingerprint, err := asymkey_model.CalcFingerprint(rawKeyBody.Key)
assert.NoError(t, err)
2021-12-10 16:14:24 +08:00
unittest.AssertExistsAndLoadBean(t, &asymkey_model.PublicKey{
2022-06-05 03:18:50 +08:00
ID: newPublicKey.ID,
OwnerID: user.ID,
Name: rawKeyBody.Title,
Fingerprint: fingerprint,
Mode: perm.AccessModeWrite,
2018-11-01 03:40:49 +00:00
})
// Search by fingerprint
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/user/keys?fingerprint=%s", newPublicKey.Fingerprint)).
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp = MakeRequest(t, req, http.StatusOK)
2018-11-01 03:40:49 +00:00
var fingerprintPublicKeys []api.PublicKey
DecodeJSON(t, resp, &fingerprintPublicKeys)
assert.Equal(t, newPublicKey.Fingerprint, fingerprintPublicKeys[0].Fingerprint)
assert.Equal(t, newPublicKey.ID, fingerprintPublicKeys[0].ID)
assert.Equal(t, user.ID, fingerprintPublicKeys[0].Owner.ID)
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s/keys?fingerprint=%s", user.Name, newPublicKey.Fingerprint)).
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp = MakeRequest(t, req, http.StatusOK)
2018-11-01 03:40:49 +00:00
DecodeJSON(t, resp, &fingerprintPublicKeys)
assert.Equal(t, newPublicKey.Fingerprint, fingerprintPublicKeys[0].Fingerprint)
assert.Equal(t, newPublicKey.ID, fingerprintPublicKeys[0].ID)
assert.Equal(t, user.ID, fingerprintPublicKeys[0].Owner.ID)
// Fail search by fingerprint
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/user/keys?fingerprint=%sA", newPublicKey.Fingerprint)).
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp = MakeRequest(t, req, http.StatusOK)
2018-11-01 03:40:49 +00:00
DecodeJSON(t, resp, &fingerprintPublicKeys)
assert.Len(t, fingerprintPublicKeys, 0)
// Fail searching for wrong users key
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s/keys?fingerprint=%s", "user2", newPublicKey.Fingerprint)).
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp = MakeRequest(t, req, http.StatusOK)
2018-11-01 03:40:49 +00:00
DecodeJSON(t, resp, &fingerprintPublicKeys)
assert.Len(t, fingerprintPublicKeys, 0)
// Now login as user 2
session2 := loginUser(t, "user2")
token2 := getTokenForLoggedInUser(t, session2, auth_model.AccessTokenScopeWriteUser)
2018-11-01 03:40:49 +00:00
// Should find key even though not ours, but we shouldn't know whose it is
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/user/keys?fingerprint=%s", newPublicKey.Fingerprint)).
AddTokenAuth(token2)
2022-12-02 11:39:42 +08:00
resp = MakeRequest(t, req, http.StatusOK)
2018-11-01 03:40:49 +00:00
DecodeJSON(t, resp, &fingerprintPublicKeys)
assert.Equal(t, newPublicKey.Fingerprint, fingerprintPublicKeys[0].Fingerprint)
assert.Equal(t, newPublicKey.ID, fingerprintPublicKeys[0].ID)
assert.Nil(t, fingerprintPublicKeys[0].Owner)
// Should find key even though not ours, but we shouldn't know whose it is
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s/keys?fingerprint=%s", user.Name, newPublicKey.Fingerprint)).
AddTokenAuth(token2)
2022-12-02 11:39:42 +08:00
resp = MakeRequest(t, req, http.StatusOK)
2018-11-01 03:40:49 +00:00
DecodeJSON(t, resp, &fingerprintPublicKeys)
assert.Equal(t, newPublicKey.Fingerprint, fingerprintPublicKeys[0].Fingerprint)
assert.Equal(t, newPublicKey.ID, fingerprintPublicKeys[0].ID)
assert.Nil(t, fingerprintPublicKeys[0].Owner)
// Fail when searching for key if it is not ours
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s/keys?fingerprint=%s", "user2", newPublicKey.Fingerprint)).
AddTokenAuth(token2)
2022-12-02 11:39:42 +08:00
resp = MakeRequest(t, req, http.StatusOK)
2018-11-01 03:40:49 +00:00
DecodeJSON(t, resp, &fingerprintPublicKeys)
assert.Len(t, fingerprintPublicKeys, 0)
}