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.

62 lines
2.0 KiB

  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 integrations
  5. import (
  6. "bytes"
  7. "net/http"
  8. "net/url"
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func testRepoFork(t *testing.T, session *TestSession) {
  13. // Step0: check the existence of the to-fork repo
  14. req := NewRequest(t, "GET", "/user1/repo1")
  15. resp := session.MakeRequest(t, req)
  16. assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode)
  17. // Step1: go to the main page of repo
  18. req = NewRequest(t, "GET", "/user2/repo1")
  19. resp = session.MakeRequest(t, req)
  20. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  21. // Step2: click the fork button
  22. htmlDoc, err := NewHtmlParser(resp.Body)
  23. assert.NoError(t, err)
  24. link, exists := htmlDoc.doc.Find("a.ui.button[href^=\"/repo/fork/\"]").Attr("href")
  25. assert.True(t, exists, "The template has changed")
  26. req = NewRequest(t, "GET", link)
  27. resp = session.MakeRequest(t, req)
  28. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  29. // Step3: fill the form of the forking
  30. htmlDoc, err = NewHtmlParser(resp.Body)
  31. assert.NoError(t, err)
  32. link, exists = htmlDoc.doc.Find("form.ui.form[action^=\"/repo/fork/\"]").Attr("action")
  33. assert.True(t, exists, "The template has changed")
  34. req = NewRequestBody(t, "POST", link,
  35. bytes.NewBufferString(url.Values{
  36. "_csrf": []string{htmlDoc.GetInputValueByName("_csrf")},
  37. "uid": []string{"1"},
  38. "repo_name": []string{"repo1"},
  39. }.Encode()),
  40. )
  41. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  42. resp = session.MakeRequest(t, req)
  43. assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
  44. // Step4: check the existence of the forked repo
  45. req = NewRequest(t, "GET", "/user1/repo1")
  46. resp = session.MakeRequest(t, req)
  47. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  48. }
  49. func TestRepoFork(t *testing.T) {
  50. prepareTestEnv(t)
  51. session := loginUser(t, "user1", "password")
  52. testRepoFork(t, session)
  53. }