THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

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.

411 lines
11KB

  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. "database/sql"
  8. "encoding/json"
  9. "fmt"
  10. "io"
  11. "log"
  12. "net/http"
  13. "net/http/cookiejar"
  14. "net/http/httptest"
  15. "net/url"
  16. "os"
  17. "path"
  18. "path/filepath"
  19. "strings"
  20. "testing"
  21. "code.gitea.io/gitea/models"
  22. "code.gitea.io/gitea/modules/base"
  23. "code.gitea.io/gitea/modules/setting"
  24. "code.gitea.io/gitea/routers"
  25. "code.gitea.io/gitea/routers/routes"
  26. "github.com/PuerkitoBio/goquery"
  27. "github.com/Unknwon/com"
  28. "github.com/stretchr/testify/assert"
  29. "gopkg.in/macaron.v1"
  30. "gopkg.in/testfixtures.v2"
  31. )
  32. var mac *macaron.Macaron
  33. type NilResponseRecorder struct {
  34. httptest.ResponseRecorder
  35. Length int
  36. }
  37. func (n *NilResponseRecorder) Write(b []byte) (int, error) {
  38. n.Length += len(b)
  39. return len(b), nil
  40. }
  41. // NewRecorder returns an initialized ResponseRecorder.
  42. func NewNilResponseRecorder() *NilResponseRecorder {
  43. return &NilResponseRecorder{
  44. ResponseRecorder: *httptest.NewRecorder(),
  45. }
  46. }
  47. func TestMain(m *testing.M) {
  48. initIntegrationTest()
  49. mac = routes.NewMacaron()
  50. routes.RegisterRoutes(mac)
  51. var helper testfixtures.Helper
  52. if setting.UseMySQL {
  53. helper = &testfixtures.MySQL{}
  54. } else if setting.UsePostgreSQL {
  55. helper = &testfixtures.PostgreSQL{}
  56. } else if setting.UseSQLite3 {
  57. helper = &testfixtures.SQLite{}
  58. } else if setting.UseMSSQL {
  59. helper = &testfixtures.SQLServer{}
  60. } else {
  61. fmt.Println("Unsupported RDBMS for integration tests")
  62. os.Exit(1)
  63. }
  64. err := models.InitFixtures(
  65. helper,
  66. path.Join(filepath.Dir(setting.AppPath), "models/fixtures/"),
  67. )
  68. if err != nil {
  69. fmt.Printf("Error initializing test database: %v\n", err)
  70. os.Exit(1)
  71. }
  72. exitCode := m.Run()
  73. writerCloser.t = nil
  74. if err = os.RemoveAll(setting.Indexer.IssuePath); err != nil {
  75. fmt.Printf("os.RemoveAll: %v\n", err)
  76. os.Exit(1)
  77. }
  78. if err = os.RemoveAll(setting.Indexer.RepoPath); err != nil {
  79. fmt.Printf("Unable to remove repo indexer: %v\n", err)
  80. os.Exit(1)
  81. }
  82. os.Exit(exitCode)
  83. }
  84. func initIntegrationTest() {
  85. giteaRoot := base.SetupGiteaRoot()
  86. if giteaRoot == "" {
  87. fmt.Println("Environment variable $GITEA_ROOT not set")
  88. os.Exit(1)
  89. }
  90. setting.AppPath = path.Join(giteaRoot, "gitea")
  91. if _, err := os.Stat(setting.AppPath); err != nil {
  92. fmt.Printf("Could not find gitea binary at %s\n", setting.AppPath)
  93. os.Exit(1)
  94. }
  95. giteaConf := os.Getenv("GITEA_CONF")
  96. if giteaConf == "" {
  97. fmt.Println("Environment variable $GITEA_CONF not set")
  98. os.Exit(1)
  99. } else if !path.IsAbs(giteaConf) {
  100. setting.CustomConf = path.Join(giteaRoot, giteaConf)
  101. } else {
  102. setting.CustomConf = giteaConf
  103. }
  104. setting.SetCustomPathAndConf("", "", "")
  105. setting.NewContext()
  106. setting.CheckLFSVersion()
  107. models.LoadConfigs()
  108. switch {
  109. case setting.UseMySQL:
  110. db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/",
  111. models.DbCfg.User, models.DbCfg.Passwd, models.DbCfg.Host))
  112. defer db.Close()
  113. if err != nil {
  114. log.Fatalf("sql.Open: %v", err)
  115. }
  116. if _, err = db.Exec("CREATE DATABASE IF NOT EXISTS testgitea"); err != nil {
  117. log.Fatalf("db.Exec: %v", err)
  118. }
  119. case setting.UsePostgreSQL:
  120. db, err := sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/?sslmode=%s",
  121. models.DbCfg.User, models.DbCfg.Passwd, models.DbCfg.Host, models.DbCfg.SSLMode))
  122. defer db.Close()
  123. if err != nil {
  124. log.Fatalf("sql.Open: %v", err)
  125. }
  126. rows, err := db.Query(fmt.Sprintf("SELECT 1 FROM pg_database WHERE datname = '%s'", models.DbCfg.Name))
  127. if err != nil {
  128. log.Fatalf("db.Query: %v", err)
  129. }
  130. defer rows.Close()
  131. if rows.Next() {
  132. break
  133. }
  134. if _, err = db.Exec("CREATE DATABASE testgitea"); err != nil {
  135. log.Fatalf("db.Exec: %v", err)
  136. }
  137. case setting.UseMSSQL:
  138. host, port := models.ParseMSSQLHostPort(models.DbCfg.Host)
  139. db, err := sql.Open("mssql", fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;",
  140. host, port, "master", models.DbCfg.User, models.DbCfg.Passwd))
  141. if err != nil {
  142. log.Fatalf("sql.Open: %v", err)
  143. }
  144. if _, err := db.Exec("If(db_id(N'gitea') IS NULL) BEGIN CREATE DATABASE gitea; END;"); err != nil {
  145. log.Fatalf("db.Exec: %v", err)
  146. }
  147. defer db.Close()
  148. }
  149. routers.GlobalInit()
  150. }
  151. func prepareTestEnv(t testing.TB, skip ...int) {
  152. t.Helper()
  153. ourSkip := 2
  154. if len(skip) > 0 {
  155. ourSkip += skip[0]
  156. }
  157. PrintCurrentTest(t, ourSkip)
  158. assert.NoError(t, models.LoadFixtures())
  159. assert.NoError(t, os.RemoveAll(setting.RepoRootPath))
  160. assert.NoError(t, os.RemoveAll(models.LocalCopyPath()))
  161. assert.NoError(t, com.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"),
  162. setting.RepoRootPath))
  163. }
  164. type TestSession struct {
  165. jar http.CookieJar
  166. }
  167. func (s *TestSession) GetCookie(name string) *http.Cookie {
  168. baseURL, err := url.Parse(setting.AppURL)
  169. if err != nil {
  170. return nil
  171. }
  172. for _, c := range s.jar.Cookies(baseURL) {
  173. if c.Name == name {
  174. return c
  175. }
  176. }
  177. return nil
  178. }
  179. func (s *TestSession) MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.ResponseRecorder {
  180. t.Helper()
  181. baseURL, err := url.Parse(setting.AppURL)
  182. assert.NoError(t, err)
  183. for _, c := range s.jar.Cookies(baseURL) {
  184. req.AddCookie(c)
  185. }
  186. resp := MakeRequest(t, req, expectedStatus)
  187. ch := http.Header{}
  188. ch.Add("Cookie", strings.Join(resp.Header()["Set-Cookie"], ";"))
  189. cr := http.Request{Header: ch}
  190. s.jar.SetCookies(baseURL, cr.Cookies())
  191. return resp
  192. }
  193. func (s *TestSession) MakeRequestNilResponseRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseRecorder {
  194. t.Helper()
  195. baseURL, err := url.Parse(setting.AppURL)
  196. assert.NoError(t, err)
  197. for _, c := range s.jar.Cookies(baseURL) {
  198. req.AddCookie(c)
  199. }
  200. resp := MakeRequestNilResponseRecorder(t, req, expectedStatus)
  201. ch := http.Header{}
  202. ch.Add("Cookie", strings.Join(resp.Header()["Set-Cookie"], ";"))
  203. cr := http.Request{Header: ch}
  204. s.jar.SetCookies(baseURL, cr.Cookies())
  205. return resp
  206. }
  207. const userPassword = "password"
  208. var loginSessionCache = make(map[string]*TestSession, 10)
  209. func emptyTestSession(t testing.TB) *TestSession {
  210. t.Helper()
  211. jar, err := cookiejar.New(nil)
  212. assert.NoError(t, err)
  213. return &TestSession{jar: jar}
  214. }
  215. func loginUser(t testing.TB, userName string) *TestSession {
  216. t.Helper()
  217. if session, ok := loginSessionCache[userName]; ok {
  218. return session
  219. }
  220. session := loginUserWithPassword(t, userName, userPassword)
  221. loginSessionCache[userName] = session
  222. return session
  223. }
  224. func loginUserWithPassword(t testing.TB, userName, password string) *TestSession {
  225. t.Helper()
  226. req := NewRequest(t, "GET", "/user/login")
  227. resp := MakeRequest(t, req, http.StatusOK)
  228. doc := NewHTMLParser(t, resp.Body)
  229. req = NewRequestWithValues(t, "POST", "/user/login", map[string]string{
  230. "_csrf": doc.GetCSRF(),
  231. "user_name": userName,
  232. "password": password,
  233. })
  234. resp = MakeRequest(t, req, http.StatusFound)
  235. ch := http.Header{}
  236. ch.Add("Cookie", strings.Join(resp.Header()["Set-Cookie"], ";"))
  237. cr := http.Request{Header: ch}
  238. session := emptyTestSession(t)
  239. baseURL, err := url.Parse(setting.AppURL)
  240. assert.NoError(t, err)
  241. session.jar.SetCookies(baseURL, cr.Cookies())
  242. return session
  243. }
  244. func getTokenForLoggedInUser(t testing.TB, session *TestSession) string {
  245. t.Helper()
  246. req := NewRequest(t, "GET", "/user/settings/applications")
  247. resp := session.MakeRequest(t, req, http.StatusOK)
  248. doc := NewHTMLParser(t, resp.Body)
  249. req = NewRequestWithValues(t, "POST", "/user/settings/applications", map[string]string{
  250. "_csrf": doc.GetCSRF(),
  251. "name": "api-testing-token",
  252. })
  253. resp = session.MakeRequest(t, req, http.StatusFound)
  254. req = NewRequest(t, "GET", "/user/settings/applications")
  255. resp = session.MakeRequest(t, req, http.StatusOK)
  256. htmlDoc := NewHTMLParser(t, resp.Body)
  257. token := htmlDoc.doc.Find(".ui.info p").Text()
  258. return token
  259. }
  260. func NewRequest(t testing.TB, method, urlStr string) *http.Request {
  261. t.Helper()
  262. return NewRequestWithBody(t, method, urlStr, nil)
  263. }
  264. func NewRequestf(t testing.TB, method, urlFormat string, args ...interface{}) *http.Request {
  265. t.Helper()
  266. return NewRequest(t, method, fmt.Sprintf(urlFormat, args...))
  267. }
  268. func NewRequestWithValues(t testing.TB, method, urlStr string, values map[string]string) *http.Request {
  269. t.Helper()
  270. urlValues := url.Values{}
  271. for key, value := range values {
  272. urlValues[key] = []string{value}
  273. }
  274. req := NewRequestWithBody(t, method, urlStr, bytes.NewBufferString(urlValues.Encode()))
  275. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  276. return req
  277. }
  278. func NewRequestWithJSON(t testing.TB, method, urlStr string, v interface{}) *http.Request {
  279. t.Helper()
  280. jsonBytes, err := json.Marshal(v)
  281. assert.NoError(t, err)
  282. req := NewRequestWithBody(t, method, urlStr, bytes.NewBuffer(jsonBytes))
  283. req.Header.Add("Content-Type", "application/json")
  284. return req
  285. }
  286. func NewRequestWithBody(t testing.TB, method, urlStr string, body io.Reader) *http.Request {
  287. t.Helper()
  288. request, err := http.NewRequest(method, urlStr, body)
  289. assert.NoError(t, err)
  290. request.RequestURI = urlStr
  291. return request
  292. }
  293. func AddBasicAuthHeader(request *http.Request, username string) *http.Request {
  294. request.SetBasicAuth(username, userPassword)
  295. return request
  296. }
  297. const NoExpectedStatus = -1
  298. func MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.ResponseRecorder {
  299. t.Helper()
  300. recorder := httptest.NewRecorder()
  301. mac.ServeHTTP(recorder, req)
  302. if expectedStatus != NoExpectedStatus {
  303. if !assert.EqualValues(t, expectedStatus, recorder.Code,
  304. "Request: %s %s", req.Method, req.URL.String()) {
  305. logUnexpectedResponse(t, recorder)
  306. }
  307. }
  308. return recorder
  309. }
  310. func MakeRequestNilResponseRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseRecorder {
  311. t.Helper()
  312. recorder := NewNilResponseRecorder()
  313. mac.ServeHTTP(recorder, req)
  314. if expectedStatus != NoExpectedStatus {
  315. if !assert.EqualValues(t, expectedStatus, recorder.Code,
  316. "Request: %s %s", req.Method, req.URL.String()) {
  317. logUnexpectedResponse(t, &recorder.ResponseRecorder)
  318. }
  319. }
  320. return recorder
  321. }
  322. // logUnexpectedResponse logs the contents of an unexpected response.
  323. func logUnexpectedResponse(t testing.TB, recorder *httptest.ResponseRecorder) {
  324. t.Helper()
  325. respBytes := recorder.Body.Bytes()
  326. if len(respBytes) == 0 {
  327. return
  328. } else if len(respBytes) < 500 {
  329. // if body is short, just log the whole thing
  330. t.Log("Response:", string(respBytes))
  331. return
  332. }
  333. // log the "flash" error message, if one exists
  334. // we must create a new buffer, so that we don't "use up" resp.Body
  335. htmlDoc, err := goquery.NewDocumentFromReader(bytes.NewBuffer(respBytes))
  336. if err != nil {
  337. return // probably a non-HTML response
  338. }
  339. errMsg := htmlDoc.Find(".ui.negative.message").Text()
  340. if len(errMsg) > 0 {
  341. t.Log("A flash error message was found:", errMsg)
  342. }
  343. }
  344. func DecodeJSON(t testing.TB, resp *httptest.ResponseRecorder, v interface{}) {
  345. t.Helper()
  346. decoder := json.NewDecoder(resp.Body)
  347. assert.NoError(t, decoder.Decode(v))
  348. }
  349. func GetCSRF(t testing.TB, session *TestSession, urlStr string) string {
  350. t.Helper()
  351. req := NewRequest(t, "GET", urlStr)
  352. resp := session.MakeRequest(t, req, http.StatusOK)
  353. doc := NewHTMLParser(t, resp.Body)
  354. return doc.GetCSRF()
  355. }