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.

settings_test.go 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. // Copyright 2017 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 repo
  5. import (
  6. "io/ioutil"
  7. "net/http"
  8. "testing"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/test"
  13. "code.gitea.io/gitea/modules/util"
  14. "code.gitea.io/gitea/modules/web"
  15. "code.gitea.io/gitea/services/forms"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func createSSHAuthorizedKeysTmpPath(t *testing.T) func() {
  19. tmpDir, err := ioutil.TempDir("", "tmp-ssh")
  20. if err != nil {
  21. assert.Fail(t, "Unable to create temporary directory: %v", err)
  22. return nil
  23. }
  24. oldPath := setting.SSH.RootPath
  25. setting.SSH.RootPath = tmpDir
  26. return func() {
  27. setting.SSH.RootPath = oldPath
  28. util.RemoveAll(tmpDir)
  29. }
  30. }
  31. func TestAddReadOnlyDeployKey(t *testing.T) {
  32. if deferable := createSSHAuthorizedKeysTmpPath(t); deferable != nil {
  33. defer deferable()
  34. } else {
  35. return
  36. }
  37. models.PrepareTestEnv(t)
  38. ctx := test.MockContext(t, "user2/repo1/settings/keys")
  39. test.LoadUser(t, ctx, 2)
  40. test.LoadRepo(t, ctx, 2)
  41. addKeyForm := forms.AddKeyForm{
  42. Title: "read-only",
  43. Content: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC4cn+iXnA4KvcQYSV88vGn0Yi91vG47t1P7okprVmhNTkipNRIHWr6WdCO4VDr/cvsRkuVJAsLO2enwjGWWueOO6BodiBgyAOZ/5t5nJNMCNuLGT5UIo/RI1b0WRQwxEZTRjt6mFNw6lH14wRd8ulsr9toSWBPMOGWoYs1PDeDL0JuTjL+tr1SZi/EyxCngpYszKdXllJEHyI79KQgeD0Vt3pTrkbNVTOEcCNqZePSVmUH8X8Vhugz3bnE0/iE9Pb5fkWO9c4AnM1FgI/8Bvp27Fw2ShryIXuR6kKvUqhVMTuOSDHwu6A8jLE5Owt3GAYugDpDYuwTVNGrHLXKpPzrGGPE/jPmaLCMZcsdkec95dYeU3zKODEm8UQZFhmJmDeWVJ36nGrGZHL4J5aTTaeFUJmmXDaJYiJ+K2/ioKgXqnXvltu0A9R8/LGy4nrTJRr4JMLuJFoUXvGm1gXQ70w2LSpk6yl71RNC0hCtsBe8BP8IhYCM0EP5jh7eCMQZNvM= nocomment\n",
  44. }
  45. web.SetForm(ctx, &addKeyForm)
  46. DeployKeysPost(ctx)
  47. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  48. models.AssertExistsAndLoadBean(t, &models.DeployKey{
  49. Name: addKeyForm.Title,
  50. Content: addKeyForm.Content,
  51. Mode: models.AccessModeRead,
  52. })
  53. }
  54. func TestAddReadWriteOnlyDeployKey(t *testing.T) {
  55. if deferable := createSSHAuthorizedKeysTmpPath(t); deferable != nil {
  56. defer deferable()
  57. } else {
  58. return
  59. }
  60. models.PrepareTestEnv(t)
  61. ctx := test.MockContext(t, "user2/repo1/settings/keys")
  62. test.LoadUser(t, ctx, 2)
  63. test.LoadRepo(t, ctx, 2)
  64. addKeyForm := forms.AddKeyForm{
  65. Title: "read-write",
  66. Content: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC4cn+iXnA4KvcQYSV88vGn0Yi91vG47t1P7okprVmhNTkipNRIHWr6WdCO4VDr/cvsRkuVJAsLO2enwjGWWueOO6BodiBgyAOZ/5t5nJNMCNuLGT5UIo/RI1b0WRQwxEZTRjt6mFNw6lH14wRd8ulsr9toSWBPMOGWoYs1PDeDL0JuTjL+tr1SZi/EyxCngpYszKdXllJEHyI79KQgeD0Vt3pTrkbNVTOEcCNqZePSVmUH8X8Vhugz3bnE0/iE9Pb5fkWO9c4AnM1FgI/8Bvp27Fw2ShryIXuR6kKvUqhVMTuOSDHwu6A8jLE5Owt3GAYugDpDYuwTVNGrHLXKpPzrGGPE/jPmaLCMZcsdkec95dYeU3zKODEm8UQZFhmJmDeWVJ36nGrGZHL4J5aTTaeFUJmmXDaJYiJ+K2/ioKgXqnXvltu0A9R8/LGy4nrTJRr4JMLuJFoUXvGm1gXQ70w2LSpk6yl71RNC0hCtsBe8BP8IhYCM0EP5jh7eCMQZNvM= nocomment\n",
  67. IsWritable: true,
  68. }
  69. web.SetForm(ctx, &addKeyForm)
  70. DeployKeysPost(ctx)
  71. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  72. models.AssertExistsAndLoadBean(t, &models.DeployKey{
  73. Name: addKeyForm.Title,
  74. Content: addKeyForm.Content,
  75. Mode: models.AccessModeWrite,
  76. })
  77. }
  78. func TestCollaborationPost(t *testing.T) {
  79. models.PrepareTestEnv(t)
  80. ctx := test.MockContext(t, "user2/repo1/issues/labels")
  81. test.LoadUser(t, ctx, 2)
  82. test.LoadUser(t, ctx, 4)
  83. test.LoadRepo(t, ctx, 1)
  84. ctx.Req.Form.Set("collaborator", "user4")
  85. u := &models.User{
  86. LowerName: "user2",
  87. Type: models.UserTypeIndividual,
  88. }
  89. re := &models.Repository{
  90. ID: 2,
  91. Owner: u,
  92. }
  93. repo := &context.Repository{
  94. Owner: u,
  95. Repository: re,
  96. }
  97. ctx.Repo = repo
  98. CollaborationPost(ctx)
  99. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  100. exists, err := re.IsCollaborator(4)
  101. assert.NoError(t, err)
  102. assert.True(t, exists)
  103. }
  104. func TestCollaborationPost_InactiveUser(t *testing.T) {
  105. models.PrepareTestEnv(t)
  106. ctx := test.MockContext(t, "user2/repo1/issues/labels")
  107. test.LoadUser(t, ctx, 2)
  108. test.LoadUser(t, ctx, 9)
  109. test.LoadRepo(t, ctx, 1)
  110. ctx.Req.Form.Set("collaborator", "user9")
  111. repo := &context.Repository{
  112. Owner: &models.User{
  113. LowerName: "user2",
  114. },
  115. }
  116. ctx.Repo = repo
  117. CollaborationPost(ctx)
  118. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  119. assert.NotEmpty(t, ctx.Flash.ErrorMsg)
  120. }
  121. func TestCollaborationPost_AddCollaboratorTwice(t *testing.T) {
  122. models.PrepareTestEnv(t)
  123. ctx := test.MockContext(t, "user2/repo1/issues/labels")
  124. test.LoadUser(t, ctx, 2)
  125. test.LoadUser(t, ctx, 4)
  126. test.LoadRepo(t, ctx, 1)
  127. ctx.Req.Form.Set("collaborator", "user4")
  128. u := &models.User{
  129. LowerName: "user2",
  130. Type: models.UserTypeIndividual,
  131. }
  132. re := &models.Repository{
  133. ID: 2,
  134. Owner: u,
  135. }
  136. repo := &context.Repository{
  137. Owner: u,
  138. Repository: re,
  139. }
  140. ctx.Repo = repo
  141. CollaborationPost(ctx)
  142. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  143. exists, err := re.IsCollaborator(4)
  144. assert.NoError(t, err)
  145. assert.True(t, exists)
  146. // Try adding the same collaborator again
  147. CollaborationPost(ctx)
  148. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  149. assert.NotEmpty(t, ctx.Flash.ErrorMsg)
  150. }
  151. func TestCollaborationPost_NonExistentUser(t *testing.T) {
  152. models.PrepareTestEnv(t)
  153. ctx := test.MockContext(t, "user2/repo1/issues/labels")
  154. test.LoadUser(t, ctx, 2)
  155. test.LoadRepo(t, ctx, 1)
  156. ctx.Req.Form.Set("collaborator", "user34")
  157. repo := &context.Repository{
  158. Owner: &models.User{
  159. LowerName: "user2",
  160. },
  161. }
  162. ctx.Repo = repo
  163. CollaborationPost(ctx)
  164. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  165. assert.NotEmpty(t, ctx.Flash.ErrorMsg)
  166. }
  167. func TestAddTeamPost(t *testing.T) {
  168. models.PrepareTestEnv(t)
  169. ctx := test.MockContext(t, "org26/repo43")
  170. ctx.Req.Form.Set("team", "team11")
  171. org := &models.User{
  172. LowerName: "org26",
  173. Type: models.UserTypeOrganization,
  174. }
  175. team := &models.Team{
  176. ID: 11,
  177. OrgID: 26,
  178. }
  179. re := &models.Repository{
  180. ID: 43,
  181. Owner: org,
  182. OwnerID: 26,
  183. }
  184. repo := &context.Repository{
  185. Owner: &models.User{
  186. ID: 26,
  187. LowerName: "org26",
  188. RepoAdminChangeTeamAccess: true,
  189. },
  190. Repository: re,
  191. }
  192. ctx.Repo = repo
  193. AddTeamPost(ctx)
  194. assert.True(t, team.HasRepository(re.ID))
  195. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  196. assert.Empty(t, ctx.Flash.ErrorMsg)
  197. }
  198. func TestAddTeamPost_NotAllowed(t *testing.T) {
  199. models.PrepareTestEnv(t)
  200. ctx := test.MockContext(t, "org26/repo43")
  201. ctx.Req.Form.Set("team", "team11")
  202. org := &models.User{
  203. LowerName: "org26",
  204. Type: models.UserTypeOrganization,
  205. }
  206. team := &models.Team{
  207. ID: 11,
  208. OrgID: 26,
  209. }
  210. re := &models.Repository{
  211. ID: 43,
  212. Owner: org,
  213. OwnerID: 26,
  214. }
  215. repo := &context.Repository{
  216. Owner: &models.User{
  217. ID: 26,
  218. LowerName: "org26",
  219. RepoAdminChangeTeamAccess: false,
  220. },
  221. Repository: re,
  222. }
  223. ctx.Repo = repo
  224. AddTeamPost(ctx)
  225. assert.False(t, team.HasRepository(re.ID))
  226. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  227. assert.NotEmpty(t, ctx.Flash.ErrorMsg)
  228. }
  229. func TestAddTeamPost_AddTeamTwice(t *testing.T) {
  230. models.PrepareTestEnv(t)
  231. ctx := test.MockContext(t, "org26/repo43")
  232. ctx.Req.Form.Set("team", "team11")
  233. org := &models.User{
  234. LowerName: "org26",
  235. Type: models.UserTypeOrganization,
  236. }
  237. team := &models.Team{
  238. ID: 11,
  239. OrgID: 26,
  240. }
  241. re := &models.Repository{
  242. ID: 43,
  243. Owner: org,
  244. OwnerID: 26,
  245. }
  246. repo := &context.Repository{
  247. Owner: &models.User{
  248. ID: 26,
  249. LowerName: "org26",
  250. RepoAdminChangeTeamAccess: true,
  251. },
  252. Repository: re,
  253. }
  254. ctx.Repo = repo
  255. AddTeamPost(ctx)
  256. AddTeamPost(ctx)
  257. assert.True(t, team.HasRepository(re.ID))
  258. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  259. assert.NotEmpty(t, ctx.Flash.ErrorMsg)
  260. }
  261. func TestAddTeamPost_NonExistentTeam(t *testing.T) {
  262. models.PrepareTestEnv(t)
  263. ctx := test.MockContext(t, "org26/repo43")
  264. ctx.Req.Form.Set("team", "team-non-existent")
  265. org := &models.User{
  266. LowerName: "org26",
  267. Type: models.UserTypeOrganization,
  268. }
  269. re := &models.Repository{
  270. ID: 43,
  271. Owner: org,
  272. OwnerID: 26,
  273. }
  274. repo := &context.Repository{
  275. Owner: &models.User{
  276. ID: 26,
  277. LowerName: "org26",
  278. RepoAdminChangeTeamAccess: true,
  279. },
  280. Repository: re,
  281. }
  282. ctx.Repo = repo
  283. AddTeamPost(ctx)
  284. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  285. assert.NotEmpty(t, ctx.Flash.ErrorMsg)
  286. }
  287. func TestDeleteTeam(t *testing.T) {
  288. models.PrepareTestEnv(t)
  289. ctx := test.MockContext(t, "org3/team1/repo3")
  290. ctx.Req.Form.Set("id", "2")
  291. org := &models.User{
  292. LowerName: "org3",
  293. Type: models.UserTypeOrganization,
  294. }
  295. team := &models.Team{
  296. ID: 2,
  297. OrgID: 3,
  298. }
  299. re := &models.Repository{
  300. ID: 3,
  301. Owner: org,
  302. OwnerID: 3,
  303. }
  304. repo := &context.Repository{
  305. Owner: &models.User{
  306. ID: 3,
  307. LowerName: "org3",
  308. RepoAdminChangeTeamAccess: true,
  309. },
  310. Repository: re,
  311. }
  312. ctx.Repo = repo
  313. DeleteTeam(ctx)
  314. assert.False(t, team.HasRepository(re.ID))
  315. }