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.

289 lines
5.8 KiB

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