code.gitea.io/gitea@v1.21.7/services/auth/auth_test.go (about)

     1  // Copyright 2014 The Gogs Authors. All rights reserved.
     2  // Copyright 2019 The Gitea Authors. All rights reserved.
     3  // SPDX-License-Identifier: MIT
     4  
     5  package auth
     6  
     7  import (
     8  	"net/http"
     9  	"testing"
    10  
    11  	"code.gitea.io/gitea/modules/setting"
    12  )
    13  
    14  func Test_isGitRawOrLFSPath(t *testing.T) {
    15  	tests := []struct {
    16  		path string
    17  
    18  		want bool
    19  	}{
    20  		{
    21  			"/owner/repo/git-upload-pack",
    22  			true,
    23  		},
    24  		{
    25  			"/owner/repo/git-receive-pack",
    26  			true,
    27  		},
    28  		{
    29  			"/owner/repo/info/refs",
    30  			true,
    31  		},
    32  		{
    33  			"/owner/repo/HEAD",
    34  			true,
    35  		},
    36  		{
    37  			"/owner/repo/objects/info/alternates",
    38  			true,
    39  		},
    40  		{
    41  			"/owner/repo/objects/info/http-alternates",
    42  			true,
    43  		},
    44  		{
    45  			"/owner/repo/objects/info/packs",
    46  			true,
    47  		},
    48  		{
    49  			"/owner/repo/objects/info/blahahsdhsdkla",
    50  			true,
    51  		},
    52  		{
    53  			"/owner/repo/objects/01/23456789abcdef0123456789abcdef01234567",
    54  			true,
    55  		},
    56  		{
    57  			"/owner/repo/objects/pack/pack-123456789012345678921234567893124567894.pack",
    58  			true,
    59  		},
    60  		{
    61  			"/owner/repo/objects/pack/pack-0123456789abcdef0123456789abcdef0123456.idx",
    62  			true,
    63  		},
    64  		{
    65  			"/owner/repo/raw/branch/foo/fanaso",
    66  			true,
    67  		},
    68  		{
    69  			"/owner/repo/stars",
    70  			false,
    71  		},
    72  		{
    73  			"/notowner",
    74  			false,
    75  		},
    76  		{
    77  			"/owner/repo",
    78  			false,
    79  		},
    80  		{
    81  			"/owner/repo/commit/123456789012345678921234567893124567894",
    82  			false,
    83  		},
    84  		{
    85  			"/owner/repo/releases/download/tag/repo.tar.gz",
    86  			true,
    87  		},
    88  		{
    89  			"/owner/repo/attachments/6d92a9ee-5d8b-4993-97c9-6181bdaa8955",
    90  			true,
    91  		},
    92  	}
    93  	lfsTests := []string{
    94  		"/owner/repo/info/lfs/",
    95  		"/owner/repo/info/lfs/objects/batch",
    96  		"/owner/repo/info/lfs/objects/oid/filename",
    97  		"/owner/repo/info/lfs/objects/oid",
    98  		"/owner/repo/info/lfs/objects",
    99  		"/owner/repo/info/lfs/verify",
   100  		"/owner/repo/info/lfs/locks",
   101  		"/owner/repo/info/lfs/locks/verify",
   102  		"/owner/repo/info/lfs/locks/123/unlock",
   103  	}
   104  
   105  	origLFSStartServer := setting.LFS.StartServer
   106  
   107  	for _, tt := range tests {
   108  		t.Run(tt.path, func(t *testing.T) {
   109  			req, _ := http.NewRequest("POST", "http://localhost"+tt.path, nil)
   110  			setting.LFS.StartServer = false
   111  			if got := isGitRawOrAttachOrLFSPath(req); got != tt.want {
   112  				t.Errorf("isGitOrLFSPath() = %v, want %v", got, tt.want)
   113  			}
   114  			setting.LFS.StartServer = true
   115  			if got := isGitRawOrAttachOrLFSPath(req); got != tt.want {
   116  				t.Errorf("isGitOrLFSPath() = %v, want %v", got, tt.want)
   117  			}
   118  		})
   119  	}
   120  	for _, tt := range lfsTests {
   121  		t.Run(tt, func(t *testing.T) {
   122  			req, _ := http.NewRequest("POST", tt, nil)
   123  			setting.LFS.StartServer = false
   124  			if got := isGitRawOrAttachOrLFSPath(req); got != setting.LFS.StartServer {
   125  				t.Errorf("isGitOrLFSPath(%q) = %v, want %v, %v", tt, got, setting.LFS.StartServer, gitRawOrAttachPathRe.MatchString(tt))
   126  			}
   127  			setting.LFS.StartServer = true
   128  			if got := isGitRawOrAttachOrLFSPath(req); got != setting.LFS.StartServer {
   129  				t.Errorf("isGitOrLFSPath(%q) = %v, want %v", tt, got, setting.LFS.StartServer)
   130  			}
   131  		})
   132  	}
   133  	setting.LFS.StartServer = origLFSStartServer
   134  }