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.

260 lines
11 KiB

  1. // Copyright 2017 The Gogs 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. "net/http"
  7. "strconv"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. api "code.gitea.io/sdk/gitea"
  11. )
  12. func TestGPGKeys(t *testing.T) {
  13. prepareTestEnv(t)
  14. session := loginUser(t, "user2")
  15. tt := []struct {
  16. name string
  17. reqBuilder func(testing.TB, *http.Request, int) *TestResponse
  18. results []int
  19. }{
  20. {name: "NoLogin", reqBuilder: MakeRequest,
  21. results: []int{http.StatusUnauthorized, http.StatusUnauthorized, http.StatusUnauthorized, http.StatusUnauthorized, http.StatusUnauthorized, http.StatusUnauthorized, http.StatusUnauthorized, http.StatusUnauthorized},
  22. },
  23. {name: "LoggedAsUser2", reqBuilder: session.MakeRequest,
  24. results: []int{http.StatusOK, http.StatusOK, http.StatusNotFound, http.StatusNoContent, http.StatusInternalServerError, http.StatusInternalServerError, http.StatusCreated, http.StatusCreated}},
  25. }
  26. for _, tc := range tt {
  27. //Basic test on result code
  28. t.Run(tc.name, func(t *testing.T) {
  29. t.Run("ViewOwnGPGKeys", func(t *testing.T) {
  30. testViewOwnGPGKeys(t, tc.reqBuilder, tc.results[0])
  31. })
  32. t.Run("ViewGPGKeys", func(t *testing.T) {
  33. testViewGPGKeys(t, tc.reqBuilder, tc.results[1])
  34. })
  35. t.Run("GetGPGKey", func(t *testing.T) {
  36. testGetGPGKey(t, tc.reqBuilder, tc.results[2])
  37. })
  38. t.Run("DeleteGPGKey", func(t *testing.T) {
  39. testDeleteGPGKey(t, tc.reqBuilder, tc.results[3])
  40. })
  41. t.Run("CreateInvalidGPGKey", func(t *testing.T) {
  42. testCreateInvalidGPGKey(t, tc.reqBuilder, tc.results[4])
  43. })
  44. t.Run("CreateNoneRegistredEmailGPGKey", func(t *testing.T) {
  45. testCreateNoneRegistredEmailGPGKey(t, tc.reqBuilder, tc.results[5])
  46. })
  47. t.Run("CreateValidGPGKey", func(t *testing.T) {
  48. testCreateValidGPGKey(t, tc.reqBuilder, tc.results[6])
  49. })
  50. t.Run("CreateValidSecondaryEmailGPGKey", func(t *testing.T) {
  51. testCreateValidSecondaryEmailGPGKey(t, tc.reqBuilder, tc.results[7])
  52. })
  53. })
  54. }
  55. //Check state after basic add
  56. t.Run("CheckState", func(t *testing.T) {
  57. var keys []*api.GPGKey
  58. req := NewRequest(t, "GET", "/api/v1/user/gpg_keys") //GET all keys
  59. resp := session.MakeRequest(t, req, http.StatusOK)
  60. DecodeJSON(t, resp, &keys)
  61. primaryKey1 := keys[0] //Primary key 1
  62. assert.EqualValues(t, "38EA3BCED732982C", primaryKey1.KeyID)
  63. assert.EqualValues(t, 1, len(primaryKey1.Emails))
  64. assert.EqualValues(t, "user2@example.com", primaryKey1.Emails[0].Email)
  65. assert.EqualValues(t, true, primaryKey1.Emails[0].Verified)
  66. subKey := primaryKey1.SubsKey[0] //Subkey of 38EA3BCED732982C
  67. assert.EqualValues(t, "70D7C694D17D03AD", subKey.KeyID)
  68. assert.EqualValues(t, 0, len(subKey.Emails))
  69. primaryKey2 := keys[1] //Primary key 2
  70. assert.EqualValues(t, "FABF39739FE1E927", primaryKey2.KeyID)
  71. assert.EqualValues(t, 1, len(primaryKey2.Emails))
  72. assert.EqualValues(t, "user21@example.com", primaryKey2.Emails[0].Email)
  73. assert.EqualValues(t, false, primaryKey2.Emails[0].Verified)
  74. var key api.GPGKey
  75. req = NewRequest(t, "GET", "/api/v1/user/gpg_keys/"+strconv.FormatInt(primaryKey1.ID, 10)) //Primary key 1
  76. resp = session.MakeRequest(t, req, http.StatusOK)
  77. DecodeJSON(t, resp, &key)
  78. assert.EqualValues(t, "38EA3BCED732982C", key.KeyID)
  79. assert.EqualValues(t, 1, len(key.Emails))
  80. assert.EqualValues(t, "user2@example.com", key.Emails[0].Email)
  81. assert.EqualValues(t, true, key.Emails[0].Verified)
  82. req = NewRequest(t, "GET", "/api/v1/user/gpg_keys/"+strconv.FormatInt(subKey.ID, 10)) //Subkey of 38EA3BCED732982C
  83. resp = session.MakeRequest(t, req, http.StatusOK)
  84. DecodeJSON(t, resp, &key)
  85. assert.EqualValues(t, "70D7C694D17D03AD", key.KeyID)
  86. assert.EqualValues(t, 0, len(key.Emails))
  87. req = NewRequest(t, "GET", "/api/v1/user/gpg_keys/"+strconv.FormatInt(primaryKey2.ID, 10)) //Primary key 2
  88. resp = session.MakeRequest(t, req, http.StatusOK)
  89. DecodeJSON(t, resp, &key)
  90. assert.EqualValues(t, "FABF39739FE1E927", key.KeyID)
  91. assert.EqualValues(t, 1, len(key.Emails))
  92. assert.EqualValues(t, "user21@example.com", key.Emails[0].Email)
  93. assert.EqualValues(t, false, key.Emails[0].Verified)
  94. })
  95. //Check state after basic add
  96. t.Run("CheckCommits", func(t *testing.T) {
  97. t.Run("NotSigned", func(t *testing.T) {
  98. var branch api.Branch
  99. req := NewRequest(t, "GET", "/api/v1/repos/user2/repo16/branches/not-signed")
  100. resp := session.MakeRequest(t, req, http.StatusOK)
  101. DecodeJSON(t, resp, &branch)
  102. assert.EqualValues(t, false, branch.Commit.Verification.Verified)
  103. })
  104. t.Run("SignedWithNotValidatedEmail", func(t *testing.T) {
  105. var branch api.Branch
  106. req := NewRequest(t, "GET", "/api/v1/repos/user2/repo16/branches/good-sign-not-yet-validated")
  107. resp := session.MakeRequest(t, req, http.StatusOK)
  108. DecodeJSON(t, resp, &branch)
  109. assert.EqualValues(t, false, branch.Commit.Verification.Verified)
  110. })
  111. t.Run("SignedWithValidEmail", func(t *testing.T) {
  112. var branch api.Branch
  113. req := NewRequest(t, "GET", "/api/v1/repos/user2/repo16/branches/good-sign")
  114. resp := session.MakeRequest(t, req, http.StatusOK)
  115. DecodeJSON(t, resp, &branch)
  116. assert.EqualValues(t, true, branch.Commit.Verification.Verified)
  117. })
  118. })
  119. }
  120. func testViewOwnGPGKeys(t *testing.T, reqBuilder func(testing.TB, *http.Request, int) *TestResponse, expected int) {
  121. req := NewRequest(t, "GET", "/api/v1/user/gpg_keys")
  122. reqBuilder(t, req, expected)
  123. }
  124. func testViewGPGKeys(t *testing.T, reqBuilder func(testing.TB, *http.Request, int) *TestResponse, expected int) {
  125. req := NewRequest(t, "GET", "/api/v1/users/user2/gpg_keys")
  126. reqBuilder(t, req, expected)
  127. }
  128. func testGetGPGKey(t *testing.T, reqBuilder func(testing.TB, *http.Request, int) *TestResponse, expected int) {
  129. req := NewRequest(t, "GET", "/api/v1/user/gpg_keys/1")
  130. reqBuilder(t, req, expected)
  131. }
  132. func testDeleteGPGKey(t *testing.T, reqBuilder func(testing.TB, *http.Request, int) *TestResponse, expected int) {
  133. req := NewRequest(t, "DELETE", "/api/v1/user/gpg_keys/1")
  134. reqBuilder(t, req, expected)
  135. }
  136. func testCreateGPGKey(t *testing.T, reqBuilder func(testing.TB, *http.Request, int) *TestResponse, expected int, publicKey string) {
  137. req := NewRequestWithJSON(t, "POST", "/api/v1/user/gpg_keys", api.CreateGPGKeyOption{
  138. ArmoredKey: publicKey,
  139. })
  140. reqBuilder(t, req, expected)
  141. }
  142. func testCreateInvalidGPGKey(t *testing.T, reqBuilder func(testing.TB, *http.Request, int) *TestResponse, expected int) {
  143. testCreateGPGKey(t, reqBuilder, expected, "invalid_key")
  144. }
  145. func testCreateNoneRegistredEmailGPGKey(t *testing.T, reqBuilder func(testing.TB, *http.Request, int) *TestResponse, expected int) {
  146. testCreateGPGKey(t, reqBuilder, expected, `-----BEGIN PGP PUBLIC KEY BLOCK-----
  147. mQENBFmGUygBCACjCNbKvMGgp0fd5vyFW9olE1CLCSyyF9gQN2hSuzmZLuAZF2Kh
  148. dCMCG2T1UwzUB/yWUFWJ2BtCwSjuaRv+cGohqEy6bhEBV90peGA33lHfjx7wP25O
  149. 7moAphDOTZtDj1AZfCh/PTcJut8Lc0eRDMhNyp/bYtO7SHNT1Hr6rrCV/xEtSAvR
  150. 3b148/tmIBiSadaLwc558KU3ucjnW5RVGins3AjBZ+TuT4XXVH/oeLSeXPSJ5rt1
  151. rHwaseslMqZ4AbvwFLx5qn1OC9rEQv/F548QsA8m0IntLjoPon+6wcubA9Gra21c
  152. Fp6aRYl9x7fiqXDLg8i3s2nKdV7+e6as6Tp9ABEBAAG0FG5vdGtub3duQGV4YW1w
  153. bGUuY29tiQEcBBABAgAGBQJZhlMoAAoJEC8+pvYULDtte/wH/2JNrhmHwDY+hMj0
  154. batIK4HICnkKxjIgbha80P2Ao08NkzSge58fsxiKDFYAQjHui+ZAw4dq79Ax9AOO
  155. Iv2GS9+DUfWhrb6RF+vNuJldFzcI0rTW/z2q+XGKrUCwN3khJY5XngHfQQrdBtMK
  156. qsoUXz/5B8g422RTbo/SdPsyYAV6HeLLeV3rdgjI1fpaW0seZKHeTXQb/HvNeuPg
  157. qz+XV1g6Gdqa1RjDOaX7A8elVKxrYq3LBtc93FW+grBde8n7JL0zPM3DY+vJ0IJZ
  158. INx/MmBfmtCq05FqNclvU+sj2R3N1JJOtBOjZrJHQbJhzoILou8AkxeX1A+q9OAz
  159. 1geiY5E=
  160. =TkP3
  161. -----END PGP PUBLIC KEY BLOCK-----`)
  162. }
  163. func testCreateValidGPGKey(t *testing.T, reqBuilder func(testing.TB, *http.Request, int) *TestResponse, expected int) {
  164. //User2 <user2@example.com> //primary & activated
  165. testCreateGPGKey(t, reqBuilder, expected, `-----BEGIN PGP PUBLIC KEY BLOCK-----
  166. mQENBFmGVsMBCACuxgZ7W7rI9xN08Y4M7B8yx/6/I4Slm94+wXf8YNRvAyqj30dW
  167. VJhyBcnfNRDLKSQp5o/hhfDkCgdqBjLa1PnHlGS3PXJc0hP/FyYPD2BFvNMPpCYS
  168. eu3T1qKSNXm6X0XOWD2LIrdiDC8HaI9FqZVMI/srMK2CF8XCL2m67W1FuoPlWzod
  169. 5ORy0IZB7spoF0xihmcgnEGElRmdo5w/vkGH8U7Zyn9Eb57UVFeafgeskf4wqB23
  170. BjbMdW2YaB+yzMRwYgOnD5lnBD4uqSmvjaV9C0kxn7x+oJkkiRV8/z1cNcO+BaeQ
  171. Akh/yTTeTzYGSc/ZOqCX1O+NOPgSeixVlqenABEBAAG0GVVzZXIyIDx1c2VyMkBl
  172. eGFtcGxlLmNvbT6JAVQEEwEIAD4WIQRXgbSh0TtGbgRd7XI46jvO1zKYLAUCWYZW
  173. wwIbAwUJA8JnAAULCQgHAgYVCAkKCwIEFgIDAQIeAQIXgAAKCRA46jvO1zKYLF/e
  174. B/91wm2KLMIQBZBA9WA2/+9rQWTo9EqgYrXN60rEzX3cYJWXZiE4DrKR1oWDGNLi
  175. KXOCW62snvJldolBqq0ZqaKvPKzl0Y5TRqbYEc9AjUSqgRin1b+G2DevLGT4ibq+
  176. 7ocQvz0XkASEUAgHahp0Ubiiib1521WwT/duL+AG8Gg0+DK09RfV3eX5/EOkQCKv
  177. 8cutqgsd2Smz40A8wXuJkRcipZBtrB/GkUaZ/eJdwEeSYZjEA9GWF61LJT2stvRN
  178. HCk7C3z3pVEek1PluiFs/4VN8BG8yDzW4c0tLty4Fj3VwPqwIbB5AJbquVfhQCb4
  179. Eep2lm3Lc9b1OwO5N3coPJkouQENBFmGVsMBCADAGba2L6NCOE1i3WIP6CPzbdOo
  180. N3gdTfTgccAx9fNeon9jor+3tgEjlo9/6cXiRoksOV6W4wFab/ZwWgwN6JO4CGvZ
  181. Wi7EQwMMMp1E36YTojKQJrcA9UvMnTHulqQQ88F5E845DhzFQM3erv42QZZMBAX3
  182. kXCgy1GNFocl6tLUvJdEqs+VcJGGANMpmzE4WLa8KhSYnxipwuQ62JBy9R+cHyKT
  183. OARk8znRqSu5bT3LtlrZ/HXu+6Oy4+2uCdNzZIh5J5tPS7CPA6ptl88iGVBte/CJ
  184. 7cjgJWSQqeYp2Y5QvsWAivkQ4Ww9plHbbwV0A2eaHsjjWzlUl3HoJ/snMOhBABEB
  185. AAGJATwEGAEIACYWIQRXgbSh0TtGbgRd7XI46jvO1zKYLAUCWYZWwwIbDAUJA8Jn
  186. AAAKCRA46jvO1zKYLBwLCACQOpeRVrwIKVaWcPMYjVHHJsGscaLKpgpARAUgbiG6
  187. Cbc2WI8Sm3fRwrY0VAfN+u9QwrtvxANcyB3vTgTzw7FimfhOimxiTSO8HQCfjDZF
  188. Xly8rq+Fua7+ClWUpy21IekW41VvZYjH2sL6EVP+UcEOaGAyN53XfhaRVZPhNtZN
  189. NKAE9N5EG3rbsZ33LzJj40rEKlzFSseAAPft8qA3IXjzFBx+PQXHMpNCagL79he6
  190. lqockTJ+oPmta4CF/J0U5LUr1tOZXheL3TP6m8d08gDrtn0YuGOPk87i9sJz+jR9
  191. uy6MA3VSB99SK9ducGmE1Jv8mcziREroz2TEGr0zPs6h
  192. =J59D
  193. -----END PGP PUBLIC KEY BLOCK-----`)
  194. }
  195. func testCreateValidSecondaryEmailGPGKey(t *testing.T, reqBuilder func(testing.TB, *http.Request, int) *TestResponse, expected int) {
  196. //User2 <user21@example.com> //secondary and not activated
  197. testCreateGPGKey(t, reqBuilder, expected, `-----BEGIN PGP PUBLIC KEY BLOCK-----
  198. mQENBFmGWN4BCAC18V4tVGO65VLCV7p14FuXJlUtZ5CuYMvgEkcOqrvRaBSW9ao4
  199. PGESOhJpfWpnW3QgJniYndLzPpsmdHEclEER6aZjiNgReWPOjHD5tykWocZAJqXD
  200. eY1ym59gvVMLcfbV2yQsyR2hbJlc+dJsl16tigSEe3nwxZSw2IsW92pgEzT9JNUr
  201. Q+mC8dw4dqY0tYmFazYUGNxufUc/twgQT/Or1aNs0az5Q6Jft4rrTRsh/S7We0VB
  202. COKGkdcQyYgAls7HJBuPjQRi6DM9VhgBSHLAgSLyaUcZvhZBJr8Qe/q4PP3/kYDJ
  203. wm4RMnjOLz2pFZPgtRqgcAwpmFtLrACbEB3JABEBAAG0GlVzZXIyIDx1c2VyMjFA
  204. ZXhhbXBsZS5jb20+iQFUBBMBCAA+FiEEPOLHOjPSO42DWM57+r85c5/h6ScFAlmG
  205. WN4CGwMFCQPCZwAFCwkIBwIGFQgJCgsCBBYCAwECHgECF4AACgkQ+r85c5/h6Sfx
  206. Lgf/dq64NBV8+X9an3seaLxePRviva48e4K67/wV/JxtXNO5Z/DhMGz5kHXCsG9D
  207. CXuWYO8ehlTjEnMZ6qqdDnY+H6bQsb2OS5oPn4RwpPXslAjEKtojPAr0dDsMS2DB
  208. dUuIm1AoOnewOVO0OFRf1EqX1bivxnN0FVMcO0m8AczfnKDaGb0y/qg/Y9JAsKqp
  209. j5pZNMWUkntRtGySeJ4CVJMmkVKJAHsa1Qj6MKdFeid4h4y94cBJ4ZdyBxNdpQOx
  210. ydf0doicovfeqGNO4oWzsGP4RBK2CqGPCUT+EFl20jPvMkKwOjxgqc8p0z3b2UT9
  211. +9bnmCGHgF/fW1HJ3iKmfFPqnLkBDQRZhljeAQgA5AirU/NJGgm19ZJYFOiHftjS
  212. azbrPxGeD3cSqmvDPIMc1DNZGfQV5D4EVumnVbQBtL6xHFoGKz9KisUMbe4a/X2J
  213. S8JmIphQWG0vMJX1DaZIzr2gT71MnPD7JMGsSUCh5dIKpTNTZX4w+oGPGOu0/UlL
  214. x0448AryKwp30J2p6D4GeI0nb03n35S2lTOpnHDn1wj7Jl/8LS2fdFOdNaNHXSZe
  215. twdSwJKhyBEiScgeHBDyKqo8zWkYoSb9eA2HiYlbVaiNtp24KP1mIEpiUdrRjWno
  216. zauYSZGHZlOFMgF4dKWuetPiuH9m7UYZGKyMLfQ9vYFb+xcPh2bLCQHJ1OEmMQAR
  217. AQABiQE8BBgBCAAmFiEEPOLHOjPSO42DWM57+r85c5/h6ScFAlmGWN4CGwwFCQPC
  218. ZwAACgkQ+r85c5/h6Sfjfwf+O4WEjRdvPJLxNy7mfAGoAqDMHIwyH/tVzYgyVhnG
  219. h/+cfRxJbGc3rpjYdr8dmvghzjEAout8uibPWaIqs63RCAPGPqgWLfxNO5c8+y8V
  220. LZMVOTV26l2olkkdBWAuhLqKTNh6TiQva03yhOgHWj4XDvFfxICWPFXVd6t5ELpD
  221. iApGu1OAj8JfhmzbG03Yzx+Ku7bWDxMonx3V/IDEu5LS5zrboHYDKCA53bXXghoi
  222. Aceqql+PKrDwEjoY4bptwMHLmcjGjdCQ//Qx1neho7nZcS7xjTucY8gQuulwCyXF
  223. y6wM+wMz8dunIG9gw4+Re6c4Rz9tX1kzxLrU7Pl21tMqfg==
  224. =0N/9
  225. -----END PGP PUBLIC KEY BLOCK-----`)
  226. }