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_gpg_keys_test.go 11KB

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