gitea源码

auth_test.go 3.5KB

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