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.

api_private_serv_test.go 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "context"
  7. "net/url"
  8. "testing"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/models/perm"
  11. "code.gitea.io/gitea/modules/private"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestAPIPrivateNoServ(t *testing.T) {
  15. onGiteaRun(t, func(*testing.T, *url.URL) {
  16. ctx, cancel := context.WithCancel(context.Background())
  17. defer cancel()
  18. key, user, err := private.ServNoCommand(ctx, 1)
  19. assert.NoError(t, err)
  20. assert.Equal(t, int64(2), user.ID)
  21. assert.Equal(t, "user2", user.Name)
  22. assert.Equal(t, int64(1), key.ID)
  23. assert.Equal(t, "user2@localhost", key.Name)
  24. deployKey, err := models.AddDeployKey(1, "test-deploy", "sk-ecdsa-sha2-nistp256@openssh.com AAAAInNrLWVjZHNhLXNoYTItbmlzdHAyNTZAb3BlbnNzaC5jb20AAAAIbmlzdHAyNTYAAABBBGXEEzWmm1dxb+57RoK5KVCL0w2eNv9cqJX2AGGVlkFsVDhOXHzsadS3LTK4VlEbbrDMJdoti9yM8vclA8IeRacAAAAEc3NoOg== nocomment", false)
  25. assert.NoError(t, err)
  26. key, user, err = private.ServNoCommand(ctx, deployKey.KeyID)
  27. assert.NoError(t, err)
  28. assert.Empty(t, user)
  29. assert.Equal(t, deployKey.KeyID, key.ID)
  30. assert.Equal(t, "test-deploy", key.Name)
  31. })
  32. }
  33. func TestAPIPrivateServ(t *testing.T) {
  34. onGiteaRun(t, func(*testing.T, *url.URL) {
  35. ctx, cancel := context.WithCancel(context.Background())
  36. defer cancel()
  37. // Can push to a repo we own
  38. results, err := private.ServCommand(ctx, 1, "user2", "repo1", perm.AccessModeWrite, "git-upload-pack", "")
  39. assert.NoError(t, err)
  40. assert.False(t, results.IsWiki)
  41. assert.False(t, results.IsDeployKey)
  42. assert.Equal(t, int64(1), results.KeyID)
  43. assert.Equal(t, "user2@localhost", results.KeyName)
  44. assert.Equal(t, "user2", results.UserName)
  45. assert.Equal(t, int64(2), results.UserID)
  46. assert.Equal(t, "user2", results.OwnerName)
  47. assert.Equal(t, "repo1", results.RepoName)
  48. assert.Equal(t, int64(1), results.RepoID)
  49. // Cannot push to a private repo we're not associated with
  50. results, err = private.ServCommand(ctx, 1, "user15", "big_test_private_1", perm.AccessModeWrite, "git-upload-pack", "")
  51. assert.Error(t, err)
  52. assert.Empty(t, results)
  53. // Cannot pull from a private repo we're not associated with
  54. results, err = private.ServCommand(ctx, 1, "user15", "big_test_private_1", perm.AccessModeRead, "git-upload-pack", "")
  55. assert.Error(t, err)
  56. assert.Empty(t, results)
  57. // Can pull from a public repo we're not associated with
  58. results, err = private.ServCommand(ctx, 1, "user15", "big_test_public_1", perm.AccessModeRead, "git-upload-pack", "")
  59. assert.NoError(t, err)
  60. assert.False(t, results.IsWiki)
  61. assert.False(t, results.IsDeployKey)
  62. assert.Equal(t, int64(1), results.KeyID)
  63. assert.Equal(t, "user2@localhost", results.KeyName)
  64. assert.Equal(t, "user2", results.UserName)
  65. assert.Equal(t, int64(2), results.UserID)
  66. assert.Equal(t, "user15", results.OwnerName)
  67. assert.Equal(t, "big_test_public_1", results.RepoName)
  68. assert.Equal(t, int64(17), results.RepoID)
  69. // Cannot push to a public repo we're not associated with
  70. results, err = private.ServCommand(ctx, 1, "user15", "big_test_public_1", perm.AccessModeWrite, "git-upload-pack", "")
  71. assert.Error(t, err)
  72. assert.Empty(t, results)
  73. // Add reading deploy key
  74. deployKey, err := models.AddDeployKey(19, "test-deploy", "sk-ecdsa-sha2-nistp256@openssh.com AAAAInNrLWVjZHNhLXNoYTItbmlzdHAyNTZAb3BlbnNzaC5jb20AAAAIbmlzdHAyNTYAAABBBGXEEzWmm1dxb+57RoK5KVCL0w2eNv9cqJX2AGGVlkFsVDhOXHzsadS3LTK4VlEbbrDMJdoti9yM8vclA8IeRacAAAAEc3NoOg== nocomment", true)
  75. assert.NoError(t, err)
  76. // Can pull from repo we're a deploy key for
  77. results, err = private.ServCommand(ctx, deployKey.KeyID, "user15", "big_test_private_1", perm.AccessModeRead, "git-upload-pack", "")
  78. assert.NoError(t, err)
  79. assert.False(t, results.IsWiki)
  80. assert.True(t, results.IsDeployKey)
  81. assert.Equal(t, deployKey.KeyID, results.KeyID)
  82. assert.Equal(t, "test-deploy", results.KeyName)
  83. assert.Equal(t, "user15", results.UserName)
  84. assert.Equal(t, int64(15), results.UserID)
  85. assert.Equal(t, "user15", results.OwnerName)
  86. assert.Equal(t, "big_test_private_1", results.RepoName)
  87. assert.Equal(t, int64(19), results.RepoID)
  88. // Cannot push to a private repo with reading key
  89. results, err = private.ServCommand(ctx, deployKey.KeyID, "user15", "big_test_private_1", perm.AccessModeWrite, "git-upload-pack", "")
  90. assert.Error(t, err)
  91. assert.Empty(t, results)
  92. // Cannot pull from a private repo we're not associated with
  93. results, err = private.ServCommand(ctx, deployKey.ID, "user15", "big_test_private_2", perm.AccessModeRead, "git-upload-pack", "")
  94. assert.Error(t, err)
  95. assert.Empty(t, results)
  96. // Cannot pull from a public repo we're not associated with
  97. results, err = private.ServCommand(ctx, deployKey.ID, "user15", "big_test_public_1", perm.AccessModeRead, "git-upload-pack", "")
  98. assert.Error(t, err)
  99. assert.Empty(t, results)
  100. // Add writing deploy key
  101. deployKey, err = models.AddDeployKey(20, "test-deploy", "sk-ecdsa-sha2-nistp256@openssh.com AAAAInNrLWVjZHNhLXNoYTItbmlzdHAyNTZAb3BlbnNzaC5jb20AAAAIbmlzdHAyNTYAAABBBGXEEzWmm1dxb+57RoK5KVCL0w2eNv9cqJX2AGGVlkFsVDhOXHzsadS3LTK4VlEbbrDMJdoti9yM8vclA8IeRacAAAAEc3NoOg== nocomment", false)
  102. assert.NoError(t, err)
  103. // Cannot push to a private repo with reading key
  104. results, err = private.ServCommand(ctx, deployKey.KeyID, "user15", "big_test_private_1", perm.AccessModeWrite, "git-upload-pack", "")
  105. assert.Error(t, err)
  106. assert.Empty(t, results)
  107. // Can pull from repo we're a writing deploy key for
  108. results, err = private.ServCommand(ctx, deployKey.KeyID, "user15", "big_test_private_2", perm.AccessModeRead, "git-upload-pack", "")
  109. assert.NoError(t, err)
  110. assert.False(t, results.IsWiki)
  111. assert.True(t, results.IsDeployKey)
  112. assert.Equal(t, deployKey.KeyID, results.KeyID)
  113. assert.Equal(t, "test-deploy", results.KeyName)
  114. assert.Equal(t, "user15", results.UserName)
  115. assert.Equal(t, int64(15), results.UserID)
  116. assert.Equal(t, "user15", results.OwnerName)
  117. assert.Equal(t, "big_test_private_2", results.RepoName)
  118. assert.Equal(t, int64(20), results.RepoID)
  119. // Can push to repo we're a writing deploy key for
  120. results, err = private.ServCommand(ctx, deployKey.KeyID, "user15", "big_test_private_2", perm.AccessModeWrite, "git-upload-pack", "")
  121. assert.NoError(t, err)
  122. assert.False(t, results.IsWiki)
  123. assert.True(t, results.IsDeployKey)
  124. assert.Equal(t, deployKey.KeyID, results.KeyID)
  125. assert.Equal(t, "test-deploy", results.KeyName)
  126. assert.Equal(t, "user15", results.UserName)
  127. assert.Equal(t, int64(15), results.UserID)
  128. assert.Equal(t, "user15", results.OwnerName)
  129. assert.Equal(t, "big_test_private_2", results.RepoName)
  130. assert.Equal(t, int64(20), results.RepoID)
  131. })
  132. }