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
5package integrations
6
7import (
8	"fmt"
9	"net/http"
10	"net/http/httptest"
11	"testing"
12
13	"code.gitea.io/gitea/models/unittest"
14	user_model "code.gitea.io/gitea/models/user"
15
16	"github.com/stretchr/testify/assert"
17)
18
19func testRepoFork(t *testing.T, session *TestSession, ownerName, repoName, forkOwnerName, forkRepoName string) *httptest.ResponseRecorder {
20	forkOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: forkOwnerName}).(*user_model.User)
21
22	// Step0: check the existence of the to-fork repo
23	req := NewRequestf(t, "GET", "/%s/%s", forkOwnerName, forkRepoName)
24	session.MakeRequest(t, req, http.StatusNotFound)
25
26	// Step1: go to the main page of repo
27	req = NewRequestf(t, "GET", "/%s/%s", ownerName, repoName)
28	resp := session.MakeRequest(t, req, http.StatusOK)
29
30	// Step2: click the fork button
31	htmlDoc := NewHTMLParser(t, resp.Body)
32	link, exists := htmlDoc.doc.Find("a.ui.button[href^=\"/repo/fork/\"]").Attr("href")
33	assert.True(t, exists, "The template has changed")
34	req = NewRequest(t, "GET", link)
35	resp = session.MakeRequest(t, req, http.StatusOK)
36
37	// Step3: fill the form of the forking
38	htmlDoc = NewHTMLParser(t, resp.Body)
39	link, exists = htmlDoc.doc.Find("form.ui.form[action^=\"/repo/fork/\"]").Attr("action")
40	assert.True(t, exists, "The template has changed")
41	_, exists = htmlDoc.doc.Find(fmt.Sprintf(".owner.dropdown .item[data-value=\"%d\"]", forkOwner.ID)).Attr("data-value")
42	assert.True(t, exists, fmt.Sprintf("Fork owner '%s' is not present in select box", forkOwnerName))
43	req = NewRequestWithValues(t, "POST", link, map[string]string{
44		"_csrf":     htmlDoc.GetCSRF(),
45		"uid":       fmt.Sprintf("%d", forkOwner.ID),
46		"repo_name": forkRepoName,
47	})
48	resp = session.MakeRequest(t, req, http.StatusFound)
49
50	// Step4: check the existence of the forked repo
51	req = NewRequestf(t, "GET", "/%s/%s", forkOwnerName, forkRepoName)
52	resp = session.MakeRequest(t, req, http.StatusOK)
53
54	return resp
55}
56
57func TestRepoFork(t *testing.T) {
58	defer prepareTestEnv(t)()
59	session := loginUser(t, "user1")
60	testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
61}
62
63func TestRepoForkToOrg(t *testing.T) {
64	defer prepareTestEnv(t)()
65	session := loginUser(t, "user2")
66	testRepoFork(t, session, "user2", "repo1", "user3", "repo1")
67
68	// Check that no more forking is allowed as user2 owns repository
69	//  and user3 organization that owner user2 is also now has forked this repository
70	req := NewRequest(t, "GET", "/user2/repo1")
71	resp := session.MakeRequest(t, req, http.StatusOK)
72	htmlDoc := NewHTMLParser(t, resp.Body)
73	_, exists := htmlDoc.doc.Find("a.ui.button[href^=\"/repo/fork/\"]").Attr("href")
74	assert.False(t, exists, "Forking should not be allowed anymore")
75}
76