Contains the Concourse pipeline definition for building a line-server container
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.

700 lines
15 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "mime/multipart"
  6. "net/http"
  7. "net/http/httptest"
  8. "net/url"
  9. "os"
  10. "path"
  11. "strconv"
  12. "strings"
  13. "testing"
  14. "time"
  15. "github.com/zenazn/goji"
  16. )
  17. type RespOkJSON struct {
  18. Filename string
  19. Url string
  20. Delete_Key string
  21. Expiry string
  22. Size string
  23. }
  24. type RespErrJSON struct {
  25. Error string
  26. }
  27. func TestSetup(t *testing.T) {
  28. Config.siteURL = "http://linx.example.org/"
  29. Config.filesDir = path.Join(os.TempDir(), generateBarename())
  30. Config.metaDir = Config.filesDir + "_meta"
  31. Config.maxSize = 1024 * 1024 * 1024
  32. Config.noLogs = true
  33. Config.siteName = "linx"
  34. setup()
  35. }
  36. func TestIndex(t *testing.T) {
  37. w := httptest.NewRecorder()
  38. req, err := http.NewRequest("GET", "/", nil)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. goji.DefaultMux.ServeHTTP(w, req)
  43. if !strings.Contains(w.Body.String(), "Click or Drop file") {
  44. t.Fatal("String 'Click or Drop file' not found in index response")
  45. }
  46. }
  47. func TestNotFound(t *testing.T) {
  48. w := httptest.NewRecorder()
  49. req, err := http.NewRequest("GET", "/url/should/not/exist", nil)
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. goji.DefaultMux.ServeHTTP(w, req)
  54. if w.Code != 404 {
  55. t.Fatalf("Expected 404, got %d", w.Code)
  56. }
  57. }
  58. func TestFileNotFound(t *testing.T) {
  59. w := httptest.NewRecorder()
  60. filename := generateBarename()
  61. req, err := http.NewRequest("GET", "/selif/"+filename, nil)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. goji.DefaultMux.ServeHTTP(w, req)
  66. if w.Code != 404 {
  67. t.Fatalf("Expected 404, got %d", w.Code)
  68. }
  69. }
  70. func TestDisplayNotFound(t *testing.T) {
  71. w := httptest.NewRecorder()
  72. filename := generateBarename()
  73. req, err := http.NewRequest("GET", "/"+filename, nil)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. goji.DefaultMux.ServeHTTP(w, req)
  78. if w.Code != 404 {
  79. t.Fatalf("Expected 404, got %d", w.Code)
  80. }
  81. }
  82. func TestPostCodeUpload(t *testing.T) {
  83. w := httptest.NewRecorder()
  84. filename := generateBarename()
  85. extension := "txt"
  86. form := url.Values{}
  87. form.Add("content", "File content")
  88. form.Add("filename", filename)
  89. form.Add("extension", extension)
  90. req, err := http.NewRequest("POST", "/upload/", nil)
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. req.PostForm = form
  95. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  96. goji.DefaultMux.ServeHTTP(w, req)
  97. if w.Code != 301 {
  98. t.Fatalf("Status code is not 301, but %d", w.Code)
  99. }
  100. if w.Header().Get("Location") != "/"+filename+"."+extension {
  101. t.Fatalf("Was redirected to %s instead of /%s", w.Header().Get("Location"), filename)
  102. }
  103. }
  104. func TestPostCodeExpiryJSONUpload(t *testing.T) {
  105. w := httptest.NewRecorder()
  106. form := url.Values{}
  107. form.Add("content", "File content")
  108. form.Add("filename", "")
  109. form.Add("expires", "60")
  110. req, err := http.NewRequest("POST", "/upload/", nil)
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. req.PostForm = form
  115. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  116. req.Header.Set("Accept", "application/json")
  117. goji.DefaultMux.ServeHTTP(w, req)
  118. if w.Code != 200 {
  119. t.Log(w.Body.String())
  120. t.Fatalf("Status code is not 200, but %d", w.Code)
  121. }
  122. var myjson RespOkJSON
  123. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. myExp, err := strconv.ParseInt(myjson.Expiry, 10, 64)
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. curTime := time.Now().Unix()
  132. if myExp < curTime {
  133. t.Fatalf("File expiry (%d) is smaller than current time (%d)", myExp, curTime)
  134. }
  135. if myjson.Size != "12" {
  136. t.Fatalf("File size was not 12 but %s", myjson.Size)
  137. }
  138. }
  139. func TestPostUpload(t *testing.T) {
  140. w := httptest.NewRecorder()
  141. filename := generateBarename() + ".txt"
  142. var b bytes.Buffer
  143. mw := multipart.NewWriter(&b)
  144. fw, err := mw.CreateFormFile("file", filename)
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. fw.Write([]byte("File content"))
  149. mw.Close()
  150. req, err := http.NewRequest("POST", "/upload/", &b)
  151. req.Header.Set("Content-Type", mw.FormDataContentType())
  152. if err != nil {
  153. t.Fatal(err)
  154. }
  155. goji.DefaultMux.ServeHTTP(w, req)
  156. if w.Code != 301 {
  157. t.Fatalf("Status code is not 301, but %d", w.Code)
  158. }
  159. if w.Header().Get("Location") != "/"+filename {
  160. t.Fatalf("Was redirected to %s instead of /%s", w.Header().Get("Location"), filename)
  161. }
  162. }
  163. func TestPostJSONUpload(t *testing.T) {
  164. w := httptest.NewRecorder()
  165. filename := generateBarename() + ".txt"
  166. var b bytes.Buffer
  167. mw := multipart.NewWriter(&b)
  168. fw, err := mw.CreateFormFile("file", filename)
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. fw.Write([]byte("File content"))
  173. mw.Close()
  174. req, err := http.NewRequest("POST", "/upload/", &b)
  175. req.Header.Set("Content-Type", mw.FormDataContentType())
  176. req.Header.Set("Accept", "application/json")
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. goji.DefaultMux.ServeHTTP(w, req)
  181. if w.Code != 200 {
  182. t.Log(w.Body.String())
  183. t.Fatalf("Status code is not 200, but %d", w.Code)
  184. }
  185. var myjson RespOkJSON
  186. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  187. if err != nil {
  188. t.Fatal(err)
  189. }
  190. if myjson.Filename != filename {
  191. t.Fatalf("Filename is not '%s' but '%s' ", filename, myjson.Filename)
  192. }
  193. if myjson.Expiry != "0" {
  194. t.Fatalf("File expiry is not 0 but %s", myjson.Expiry)
  195. }
  196. if myjson.Size != "12" {
  197. t.Fatalf("File size was not 12 but %s", myjson.Size)
  198. }
  199. }
  200. func TestPostExpiresJSONUpload(t *testing.T) {
  201. w := httptest.NewRecorder()
  202. filename := generateBarename() + ".txt"
  203. var b bytes.Buffer
  204. mw := multipart.NewWriter(&b)
  205. fw, err := mw.CreateFormFile("file", filename)
  206. if err != nil {
  207. t.Fatal(err)
  208. }
  209. fw.Write([]byte("File content"))
  210. exp, err := mw.CreateFormField("expires")
  211. if err != nil {
  212. t.Fatal(err)
  213. }
  214. exp.Write([]byte("60"))
  215. mw.Close()
  216. req, err := http.NewRequest("POST", "/upload/", &b)
  217. req.Header.Set("Content-Type", mw.FormDataContentType())
  218. req.Header.Set("Accept", "application/json")
  219. if err != nil {
  220. t.Fatal(err)
  221. }
  222. goji.DefaultMux.ServeHTTP(w, req)
  223. if w.Code != 200 {
  224. t.Log(w.Body.String())
  225. t.Fatalf("Status code is not 200, but %d", w.Code)
  226. }
  227. var myjson RespOkJSON
  228. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  229. if err != nil {
  230. t.Fatal(err)
  231. }
  232. if myjson.Filename != filename {
  233. t.Fatalf("Filename is not '%s' but '%s' ", filename, myjson.Filename)
  234. }
  235. myExp, err := strconv.ParseInt(myjson.Expiry, 10, 64)
  236. if err != nil {
  237. t.Fatal(err)
  238. }
  239. curTime := time.Now().Unix()
  240. if myExp < curTime {
  241. t.Fatalf("File expiry (%d) is smaller than current time (%d)", myExp, curTime)
  242. }
  243. if myjson.Size != "12" {
  244. t.Fatalf("File size was not 12 but %s", myjson.Size)
  245. }
  246. }
  247. func TestPostRandomizeJSONUpload(t *testing.T) {
  248. w := httptest.NewRecorder()
  249. filename := generateBarename() + ".txt"
  250. var b bytes.Buffer
  251. mw := multipart.NewWriter(&b)
  252. fw, err := mw.CreateFormFile("file", filename)
  253. if err != nil {
  254. t.Fatal(err)
  255. }
  256. fw.Write([]byte("File content"))
  257. rnd, err := mw.CreateFormField("randomize")
  258. if err != nil {
  259. t.Fatal(err)
  260. }
  261. rnd.Write([]byte("true"))
  262. mw.Close()
  263. req, err := http.NewRequest("POST", "/upload/", &b)
  264. req.Header.Set("Content-Type", mw.FormDataContentType())
  265. req.Header.Set("Accept", "application/json")
  266. if err != nil {
  267. t.Fatal(err)
  268. }
  269. goji.DefaultMux.ServeHTTP(w, req)
  270. if w.Code != 200 {
  271. t.Log(w.Body.String())
  272. t.Fatalf("Status code is not 200, but %d", w.Code)
  273. }
  274. var myjson RespOkJSON
  275. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  276. if err != nil {
  277. t.Fatal(err)
  278. }
  279. if myjson.Filename == filename {
  280. t.Fatalf("Filename (%s) is not random (%s)", filename, myjson.Filename)
  281. }
  282. if myjson.Size != "12" {
  283. t.Fatalf("File size was not 12 but %s", myjson.Size)
  284. }
  285. }
  286. func TestPostEmptyUpload(t *testing.T) {
  287. w := httptest.NewRecorder()
  288. filename := generateBarename() + ".txt"
  289. var b bytes.Buffer
  290. mw := multipart.NewWriter(&b)
  291. fw, err := mw.CreateFormFile("file", filename)
  292. if err != nil {
  293. t.Fatal(err)
  294. }
  295. fw.Write([]byte(""))
  296. mw.Close()
  297. req, err := http.NewRequest("POST", "/upload/", &b)
  298. req.Header.Set("Content-Type", mw.FormDataContentType())
  299. if err != nil {
  300. t.Fatal(err)
  301. }
  302. goji.DefaultMux.ServeHTTP(w, req)
  303. if w.Code != 500 {
  304. t.Log(w.Body.String())
  305. t.Fatalf("Status code is not 500, but %d", w.Code)
  306. }
  307. if !strings.Contains(w.Body.String(), "Empty file") {
  308. t.Fatal("Response did not contain 'Empty file'")
  309. }
  310. }
  311. func TestPostEmptyJSONUpload(t *testing.T) {
  312. w := httptest.NewRecorder()
  313. filename := generateBarename() + ".txt"
  314. var b bytes.Buffer
  315. mw := multipart.NewWriter(&b)
  316. fw, err := mw.CreateFormFile("file", filename)
  317. if err != nil {
  318. t.Fatal(err)
  319. }
  320. fw.Write([]byte(""))
  321. mw.Close()
  322. req, err := http.NewRequest("POST", "/upload/", &b)
  323. req.Header.Set("Content-Type", mw.FormDataContentType())
  324. req.Header.Set("Accept", "application/json")
  325. if err != nil {
  326. t.Fatal(err)
  327. }
  328. goji.DefaultMux.ServeHTTP(w, req)
  329. if w.Code != 500 {
  330. t.Log(w.Body.String())
  331. t.Fatalf("Status code is not 500, but %d", w.Code)
  332. }
  333. var myjson RespErrJSON
  334. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  335. if err != nil {
  336. t.Fatal(err)
  337. }
  338. if myjson.Error != "Could not upload file: Empty file" {
  339. t.Fatal("Json 'error' was not 'Empty file' but " + myjson.Error)
  340. }
  341. }
  342. func TestPutUpload(t *testing.T) {
  343. w := httptest.NewRecorder()
  344. filename := generateBarename() + ".ext"
  345. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  346. if err != nil {
  347. t.Fatal(err)
  348. }
  349. goji.DefaultMux.ServeHTTP(w, req)
  350. if w.Body.String() != Config.siteURL+filename {
  351. t.Fatal("Response was not expected URL")
  352. }
  353. }
  354. func TestPutRandomizedUpload(t *testing.T) {
  355. w := httptest.NewRecorder()
  356. filename := generateBarename() + ".ext"
  357. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  358. if err != nil {
  359. t.Fatal(err)
  360. }
  361. req.Header.Set("Linx-Randomize", "yes")
  362. goji.DefaultMux.ServeHTTP(w, req)
  363. if w.Body.String() == Config.siteURL+filename {
  364. t.Fatal("Filename was not random")
  365. }
  366. }
  367. func TestPutNoExtensionUpload(t *testing.T) {
  368. w := httptest.NewRecorder()
  369. filename := generateBarename()
  370. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  371. if err != nil {
  372. t.Fatal(err)
  373. }
  374. req.Header.Set("Linx-Randomize", "yes")
  375. goji.DefaultMux.ServeHTTP(w, req)
  376. if w.Body.String() == Config.siteURL+filename {
  377. t.Fatal("Filename was not random")
  378. }
  379. }
  380. func TestPutEmptyUpload(t *testing.T) {
  381. w := httptest.NewRecorder()
  382. filename := generateBarename() + ".ext"
  383. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader(""))
  384. if err != nil {
  385. t.Fatal(err)
  386. }
  387. req.Header.Set("Linx-Randomize", "yes")
  388. goji.DefaultMux.ServeHTTP(w, req)
  389. if !strings.Contains(w.Body.String(), "Empty file") {
  390. t.Fatal("Response doesn't contain'Empty file'")
  391. }
  392. }
  393. func TestPutJSONUpload(t *testing.T) {
  394. var myjson RespOkJSON
  395. w := httptest.NewRecorder()
  396. filename := generateBarename() + ".ext"
  397. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  398. if err != nil {
  399. t.Fatal(err)
  400. }
  401. req.Header.Set("Accept", "application/json")
  402. goji.DefaultMux.ServeHTTP(w, req)
  403. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  404. if err != nil {
  405. t.Fatal(err)
  406. }
  407. if myjson.Filename != filename {
  408. t.Fatal("Filename was not provided one but " + myjson.Filename)
  409. }
  410. }
  411. func TestPutRandomizedJSONUpload(t *testing.T) {
  412. var myjson RespOkJSON
  413. w := httptest.NewRecorder()
  414. filename := generateBarename() + ".ext"
  415. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  416. if err != nil {
  417. t.Fatal(err)
  418. }
  419. req.Header.Set("Accept", "application/json")
  420. req.Header.Set("Linx-Randomize", "yes")
  421. goji.DefaultMux.ServeHTTP(w, req)
  422. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  423. if err != nil {
  424. t.Fatal(err)
  425. }
  426. if myjson.Filename == filename {
  427. t.Fatal("Filename was not random ")
  428. }
  429. }
  430. func TestPutExpireJSONUpload(t *testing.T) {
  431. var myjson RespOkJSON
  432. w := httptest.NewRecorder()
  433. filename := generateBarename() + ".ext"
  434. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  435. if err != nil {
  436. t.Fatal(err)
  437. }
  438. req.Header.Set("Accept", "application/json")
  439. req.Header.Set("Linx-Expiry", "600")
  440. goji.DefaultMux.ServeHTTP(w, req)
  441. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  442. if err != nil {
  443. t.Fatal(err)
  444. }
  445. expiry, err := strconv.Atoi(myjson.Expiry)
  446. if err != nil {
  447. t.Fatal("Expiry was not an integer")
  448. }
  449. if expiry < 1 {
  450. t.Fatal("Expiry was not set")
  451. }
  452. }
  453. func TestPutAndDelete(t *testing.T) {
  454. var myjson RespOkJSON
  455. w := httptest.NewRecorder()
  456. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  457. if err != nil {
  458. t.Fatal(err)
  459. }
  460. req.Header.Set("Accept", "application/json")
  461. goji.DefaultMux.ServeHTTP(w, req)
  462. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  463. if err != nil {
  464. t.Fatal(err)
  465. }
  466. // Delete it
  467. w = httptest.NewRecorder()
  468. req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
  469. req.Header.Set("Linx-Delete-Key", myjson.Delete_Key)
  470. goji.DefaultMux.ServeHTTP(w, req)
  471. if w.Code != 200 {
  472. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  473. }
  474. // Make sure it's actually gone
  475. w = httptest.NewRecorder()
  476. req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
  477. goji.DefaultMux.ServeHTTP(w, req)
  478. if w.Code != 404 {
  479. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  480. }
  481. // Make sure torrent is also gone
  482. w = httptest.NewRecorder()
  483. req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
  484. goji.DefaultMux.ServeHTTP(w, req)
  485. if w.Code != 404 {
  486. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  487. }
  488. }
  489. func TestPutAndSpecificDelete(t *testing.T) {
  490. var myjson RespOkJSON
  491. w := httptest.NewRecorder()
  492. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  493. if err != nil {
  494. t.Fatal(err)
  495. }
  496. req.Header.Set("Accept", "application/json")
  497. req.Header.Set("Linx-Delete-Key", "supersecret")
  498. goji.DefaultMux.ServeHTTP(w, req)
  499. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  500. if err != nil {
  501. t.Fatal(err)
  502. }
  503. // Delete it
  504. w = httptest.NewRecorder()
  505. req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
  506. req.Header.Set("Linx-Delete-Key", "supersecret")
  507. goji.DefaultMux.ServeHTTP(w, req)
  508. if w.Code != 200 {
  509. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  510. }
  511. // Make sure it's actually gone
  512. w = httptest.NewRecorder()
  513. req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
  514. goji.DefaultMux.ServeHTTP(w, req)
  515. if w.Code != 404 {
  516. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  517. }
  518. // Make sure torrent is gone too
  519. w = httptest.NewRecorder()
  520. req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
  521. goji.DefaultMux.ServeHTTP(w, req)
  522. if w.Code != 404 {
  523. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  524. }
  525. }
  526. func TestShutdown(t *testing.T) {
  527. os.RemoveAll(Config.filesDir)
  528. os.RemoveAll(Config.metaDir)
  529. }