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.

327 lines
6.4 KiB

  1. package Api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/http/httptest"
  7. "os"
  8. "path"
  9. "runtime"
  10. "strings"
  11. "testing"
  12. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database"
  13. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models"
  14. "github.com/gorilla/mux"
  15. )
  16. var (
  17. r *mux.Router
  18. )
  19. func init() {
  20. // Fix working directory for tests
  21. _, filename, _, _ := runtime.Caller(0)
  22. dir := path.Join(path.Dir(filename), "..")
  23. err := os.Chdir(dir)
  24. if err != nil {
  25. panic(err)
  26. }
  27. Database.InitTest()
  28. r = mux.NewRouter()
  29. }
  30. func createTestPost() (Models.Post, error) {
  31. userData, err := createTestUser(true)
  32. postData := Models.Post{
  33. UserID: userData.ID,
  34. Title: "Test post",
  35. Content: "Test content",
  36. FrontPage: true,
  37. Order: 1,
  38. PostLinks: []Models.PostLink{
  39. {
  40. Type: "Facebook",
  41. Link: "http://facebook.com/",
  42. },
  43. },
  44. }
  45. err = Database.CreatePost(&postData)
  46. return postData, err
  47. }
  48. func Test_getPosts(t *testing.T) {
  49. t.Log("Testing getPosts...")
  50. r.HandleFunc("/post", getPosts).Methods("GET")
  51. ts := httptest.NewServer(r)
  52. defer ts.Close()
  53. var err error
  54. for i := 0; i < 20; i++ {
  55. createTestPost()
  56. }
  57. res, err := http.Get(ts.URL + "/post?page=1&pageSize=10")
  58. if err != nil {
  59. t.Errorf("Expected nil, recieved %s", err.Error())
  60. }
  61. if res.StatusCode != http.StatusOK {
  62. t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode)
  63. }
  64. getPostsData := new([]Models.Post)
  65. err = json.NewDecoder(res.Body).Decode(getPostsData)
  66. if err != nil {
  67. t.Errorf("Expected nil, recieved %s", err.Error())
  68. }
  69. if len(*getPostsData) != 10 {
  70. t.Errorf("Expected 10, recieved %d", len(*getPostsData))
  71. }
  72. }
  73. func Test_getPost(t *testing.T) {
  74. t.Log("Testing getPost...")
  75. r.HandleFunc("/post/{postID}", getPost).Methods("GET")
  76. ts := httptest.NewServer(r)
  77. defer ts.Close()
  78. postData, err := createTestPost()
  79. if err != nil {
  80. t.Errorf("Expected nil, recieved %s", err.Error())
  81. t.FailNow()
  82. }
  83. res, err := http.Get(fmt.Sprintf(
  84. "%s/post/%s",
  85. ts.URL,
  86. postData.ID,
  87. ))
  88. if err != nil {
  89. t.Errorf("Expected nil, recieved %s", err.Error())
  90. t.FailNow()
  91. }
  92. if res.StatusCode != http.StatusOK {
  93. t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode)
  94. t.FailNow()
  95. }
  96. getPostData := new(Models.Post)
  97. err = json.NewDecoder(res.Body).Decode(getPostData)
  98. if err != nil {
  99. t.Errorf("Expected nil, recieved %s", err.Error())
  100. t.FailNow()
  101. }
  102. if getPostData.Title != "Test post" {
  103. t.Errorf("Expected title \"Test post\", recieved %s", getPostData.Title)
  104. t.FailNow()
  105. }
  106. if getPostData.Content != "Test content" {
  107. t.Errorf("Expected content \"Test content\", recieved %s", getPostData.Content)
  108. t.FailNow()
  109. }
  110. if len(getPostData.PostLinks) != 1 {
  111. t.Errorf("Expected len(PostLinks) == 1, recieved %d", len(getPostData.PostLinks))
  112. t.FailNow()
  113. }
  114. }
  115. func Test_createPost(t *testing.T) {
  116. t.Log("Testing createPost...")
  117. r.HandleFunc("/post", createPost).Methods("POST")
  118. ts := httptest.NewServer(r)
  119. defer ts.Close()
  120. c, u, err := login()
  121. if err != nil {
  122. t.Errorf("Expected nil, recieved %s", err.Error())
  123. return
  124. }
  125. postJson := `
  126. {
  127. "user_id": "%s",
  128. "title": "Test post",
  129. "content": "Test content",
  130. "front_page": true,
  131. "order": 1,
  132. "links": [{
  133. "type": "Facebook",
  134. "link": "http://facebook.com/"
  135. }]
  136. }
  137. `
  138. postJson = fmt.Sprintf(postJson, u.ID.String())
  139. req, err := http.NewRequest("POST", ts.URL+"/post", strings.NewReader(postJson))
  140. if err != nil {
  141. t.Errorf("Expected nil, recieved %s", err.Error())
  142. return
  143. }
  144. req.AddCookie(c)
  145. res, err := http.DefaultClient.Do(req)
  146. if err != nil {
  147. t.Errorf("Expected nil, recieved %s", err.Error())
  148. return
  149. }
  150. if res.StatusCode != http.StatusOK {
  151. t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode)
  152. return
  153. }
  154. postData := new(Models.Post)
  155. err = json.NewDecoder(res.Body).Decode(postData)
  156. if err != nil {
  157. t.Errorf("Expected nil, recieved %s", err.Error())
  158. }
  159. if postData.Title != "Test post" {
  160. t.Errorf("Expected title \"Test post\", recieved \"%s\"", postData.Title)
  161. }
  162. if postData.Content != "Test content" {
  163. t.Errorf("Expected content \"Test content\", recieved \"%s\"", postData.Content)
  164. }
  165. }
  166. func Test_deletePost(t *testing.T) {
  167. t.Log("Testing deletePost...")
  168. r.HandleFunc("/post/{postID}", deletePost).Methods("DELETE")
  169. ts := httptest.NewServer(r)
  170. defer ts.Close()
  171. c, _, err := login()
  172. if err != nil {
  173. t.Errorf("Expected nil, recieved %s", err.Error())
  174. return
  175. }
  176. postData, err := createTestPost()
  177. if err != nil {
  178. t.Errorf("Expected nil, recieved %s", err.Error())
  179. t.FailNow()
  180. }
  181. req, err := http.NewRequest("DELETE", fmt.Sprintf(
  182. "%s/post/%s",
  183. ts.URL,
  184. postData.ID,
  185. ), nil)
  186. if err != nil {
  187. t.Errorf("Expected nil, recieved %s", err.Error())
  188. }
  189. req.AddCookie(c)
  190. // Fetch Request
  191. res, err := http.DefaultClient.Do(req)
  192. if err != nil {
  193. t.Errorf("Expected nil, recieved %s", err.Error())
  194. return
  195. }
  196. defer res.Body.Close()
  197. if res.StatusCode != http.StatusOK {
  198. t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode)
  199. }
  200. }
  201. func Test_updatePost(t *testing.T) {
  202. t.Log("Testing updatePost...")
  203. r.HandleFunc("/post/{postID}", updatePost).Methods("PUT")
  204. ts := httptest.NewServer(r)
  205. defer ts.Close()
  206. c, _, err := login()
  207. if err != nil {
  208. t.Errorf("Expected nil, recieved %s", err.Error())
  209. return
  210. }
  211. postData, err := createTestPost()
  212. if err != nil {
  213. t.Errorf("Expected nil, recieved %s", err.Error())
  214. }
  215. postJson := `
  216. {
  217. "content": "New test content",
  218. "front_page": false,
  219. "order": 2
  220. }
  221. `
  222. req, err := http.NewRequest("PUT", fmt.Sprintf(
  223. "%s/post/%s",
  224. ts.URL,
  225. postData.ID,
  226. ), strings.NewReader(postJson))
  227. if err != nil {
  228. t.Errorf("Expected nil, recieved %s", err.Error())
  229. }
  230. req.AddCookie(c)
  231. // Fetch Request
  232. res, err := http.DefaultClient.Do(req)
  233. if err != nil {
  234. t.Errorf("Expected nil, recieved %s", err.Error())
  235. }
  236. defer res.Body.Close()
  237. if res.StatusCode != http.StatusOK {
  238. t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode)
  239. }
  240. updatePostData := new(Models.Post)
  241. err = json.NewDecoder(res.Body).Decode(updatePostData)
  242. if err != nil {
  243. t.Errorf("Expected nil, recieved %s", err.Error())
  244. }
  245. if updatePostData.Content != "New test content" {
  246. t.Errorf("Expected \"New test content\", recieved %s", updatePostData.Content)
  247. }
  248. if updatePostData.FrontPage != false {
  249. t.Errorf("Expected false, recieved %t", updatePostData.FrontPage)
  250. }
  251. if updatePostData.Order != 2 {
  252. t.Errorf("Expected 2, recieved %d", updatePostData.Order)
  253. }
  254. }