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.

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