code.gitea.io/gitea@v1.21.7/tests/integration/links_test.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"path"
    10  	"testing"
    11  
    12  	"code.gitea.io/gitea/modules/setting"
    13  	api "code.gitea.io/gitea/modules/structs"
    14  	"code.gitea.io/gitea/modules/test"
    15  	"code.gitea.io/gitea/tests"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func TestLinksNoLogin(t *testing.T) {
    21  	defer tests.PrepareTestEnv(t)()
    22  
    23  	links := []string{
    24  		"/explore/repos",
    25  		"/explore/repos?q=test",
    26  		"/explore/users",
    27  		"/explore/users?q=test",
    28  		"/explore/organizations",
    29  		"/explore/organizations?q=test",
    30  		"/",
    31  		"/user/sign_up",
    32  		"/user/login",
    33  		"/user/forgot_password",
    34  		"/api/swagger",
    35  		"/user2/repo1",
    36  		"/user2/repo1/",
    37  		"/user2/repo1/projects",
    38  		"/user2/repo1/projects/1",
    39  		"/assets/img/404.png",
    40  		"/assets/img/500.png",
    41  		"/.well-known/security.txt",
    42  	}
    43  
    44  	for _, link := range links {
    45  		req := NewRequest(t, "GET", link)
    46  		MakeRequest(t, req, http.StatusOK)
    47  	}
    48  }
    49  
    50  func TestRedirectsNoLogin(t *testing.T) {
    51  	defer tests.PrepareTestEnv(t)()
    52  
    53  	redirects := map[string]string{
    54  		"/user2/repo1/commits/master":                "/user2/repo1/commits/branch/master",
    55  		"/user2/repo1/src/master":                    "/user2/repo1/src/branch/master",
    56  		"/user2/repo1/src/master/file.txt":           "/user2/repo1/src/branch/master/file.txt",
    57  		"/user2/repo1/src/master/directory/file.txt": "/user2/repo1/src/branch/master/directory/file.txt",
    58  		"/user/avatar/Ghost/-1":                      "/assets/img/avatar_default.png",
    59  		"/api/v1/swagger":                            "/api/swagger",
    60  	}
    61  	for link, redirectLink := range redirects {
    62  		req := NewRequest(t, "GET", link)
    63  		resp := MakeRequest(t, req, http.StatusSeeOther)
    64  		assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), test.RedirectURL(resp))
    65  	}
    66  }
    67  
    68  func TestNoLoginNotExist(t *testing.T) {
    69  	defer tests.PrepareTestEnv(t)()
    70  
    71  	links := []string{
    72  		"/user5/repo4/projects",
    73  		"/user5/repo4/projects/3",
    74  	}
    75  
    76  	for _, link := range links {
    77  		req := NewRequest(t, "GET", link)
    78  		MakeRequest(t, req, http.StatusNotFound)
    79  	}
    80  }
    81  
    82  func testLinksAsUser(userName string, t *testing.T) {
    83  	links := []string{
    84  		"/explore/repos",
    85  		"/explore/repos?q=test",
    86  		"/explore/users",
    87  		"/explore/users?q=test",
    88  		"/explore/organizations",
    89  		"/explore/organizations?q=test",
    90  		"/",
    91  		"/user/forgot_password",
    92  		"/api/swagger",
    93  		"/issues",
    94  		"/issues?type=your_repositories&repos=[0]&sort=&state=open",
    95  		"/issues?type=assigned&repos=[0]&sort=&state=open",
    96  		"/issues?type=your_repositories&repos=[0]&sort=&state=closed",
    97  		"/issues?type=assigned&repos=[]&sort=&state=closed",
    98  		"/issues?type=assigned&sort=&state=open",
    99  		"/issues?type=created_by&repos=[1,2]&sort=&state=closed",
   100  		"/issues?type=created_by&repos=[1,2]&sort=&state=open",
   101  		"/pulls",
   102  		"/pulls?type=your_repositories&repos=[2]&sort=&state=open",
   103  		"/pulls?type=assigned&repos=[]&sort=&state=open",
   104  		"/pulls?type=created_by&repos=[0]&sort=&state=open",
   105  		"/pulls?type=your_repositories&repos=[0]&sort=&state=closed",
   106  		"/pulls?type=assigned&repos=[0]&sort=&state=closed",
   107  		"/pulls?type=created_by&repos=[0]&sort=&state=closed",
   108  		"/milestones",
   109  		"/milestones?sort=mostcomplete&state=closed",
   110  		"/milestones?type=your_repositories&sort=mostcomplete&state=closed",
   111  		"/milestones?sort=&repos=[1]&state=closed",
   112  		"/milestones?sort=&repos=[1]&state=open",
   113  		"/milestones?repos=[0]&sort=mostissues&state=open",
   114  		"/notifications",
   115  		"/repo/create",
   116  		"/repo/migrate",
   117  		"/org/create",
   118  		"/user2",
   119  		"/user2?tab=stars",
   120  		"/user2?tab=activity",
   121  		"/user/settings",
   122  		"/user/settings/account",
   123  		"/user/settings/security",
   124  		"/user/settings/security/two_factor/enroll",
   125  		"/user/settings/keys",
   126  		"/user/settings/organization",
   127  		"/user/settings/repos",
   128  	}
   129  
   130  	session := loginUser(t, userName)
   131  	for _, link := range links {
   132  		req := NewRequest(t, "GET", link)
   133  		session.MakeRequest(t, req, http.StatusOK)
   134  	}
   135  
   136  	reqAPI := NewRequestf(t, "GET", "/api/v1/users/%s/repos", userName)
   137  	respAPI := MakeRequest(t, reqAPI, http.StatusOK)
   138  
   139  	var apiRepos []*api.Repository
   140  	DecodeJSON(t, respAPI, &apiRepos)
   141  
   142  	repoLinks := []string{
   143  		"",
   144  		"/issues",
   145  		"/pulls",
   146  		"/commits/branch/master",
   147  		"/graph",
   148  		"/settings",
   149  		"/settings/collaboration",
   150  		"/settings/branches",
   151  		"/settings/hooks",
   152  		// FIXME: below links should return 200 but 404 ??
   153  		//"/settings/hooks/git",
   154  		//"/settings/hooks/git/pre-receive",
   155  		//"/settings/hooks/git/update",
   156  		//"/settings/hooks/git/post-receive",
   157  		"/settings/keys",
   158  		"/releases",
   159  		"/releases/new",
   160  		//"/wiki/_pages",
   161  		"/wiki/?action=_new",
   162  	}
   163  
   164  	for _, repo := range apiRepos {
   165  		for _, link := range repoLinks {
   166  			req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s%s", userName, repo.Name, link))
   167  			session.MakeRequest(t, req, http.StatusOK)
   168  		}
   169  	}
   170  }
   171  
   172  func TestLinksLogin(t *testing.T) {
   173  	defer tests.PrepareTestEnv(t)()
   174  
   175  	testLinksAsUser("user2", t)
   176  }