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.

296 lines
6.0 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. userData, err := createTestUser(true)
  121. postJson := `
  122. {
  123. "user_id": "%s",
  124. "title": "Test post",
  125. "content": "Test content",
  126. "front_page": true,
  127. "order": 1,
  128. "links": [{
  129. "type": "Facebook",
  130. "link": "http://facebook.com/"
  131. }]
  132. }
  133. `
  134. postJson = fmt.Sprintf(postJson, userData.ID.String())
  135. res, err := http.Post(ts.URL+"/post", "application/json", strings.NewReader(postJson))
  136. if err != nil {
  137. t.Errorf("Expected nil, recieved %s", err.Error())
  138. }
  139. if res.StatusCode != http.StatusOK {
  140. t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode)
  141. }
  142. postData := new(Models.Post)
  143. err = json.NewDecoder(res.Body).Decode(postData)
  144. if err != nil {
  145. t.Errorf("Expected nil, recieved %s", err.Error())
  146. }
  147. if postData.Title != "Test post" {
  148. t.Errorf("Expected title \"Test post\", recieved \"%s\"", postData.Title)
  149. }
  150. if postData.Content != "Test content" {
  151. t.Errorf("Expected content \"Test content\", recieved \"%s\"", postData.Content)
  152. }
  153. }
  154. func Test_deletePost(t *testing.T) {
  155. t.Log("Testing deletePost...")
  156. r.HandleFunc("/post/{postID}", deletePost).Methods("DELETE")
  157. ts := httptest.NewServer(r)
  158. defer ts.Close()
  159. postData, err := createTestPost()
  160. if err != nil {
  161. t.Errorf("Expected nil, recieved %s", err.Error())
  162. t.FailNow()
  163. }
  164. req, err := http.NewRequest("DELETE", fmt.Sprintf(
  165. "%s/post/%s",
  166. ts.URL,
  167. postData.ID,
  168. ), nil)
  169. if err != nil {
  170. t.Errorf("Expected nil, recieved %s", err.Error())
  171. }
  172. // Fetch Request
  173. res, err := http.DefaultClient.Do(req)
  174. if err != nil {
  175. t.Errorf("Expected nil, recieved %s", err.Error())
  176. return
  177. }
  178. defer res.Body.Close()
  179. if res.StatusCode != http.StatusOK {
  180. t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode)
  181. }
  182. }
  183. func Test_updatePost(t *testing.T) {
  184. t.Log("Testing updatePost...")
  185. r.HandleFunc("/post/{postID}", updatePost).Methods("PUT")
  186. ts := httptest.NewServer(r)
  187. defer ts.Close()
  188. postData, err := createTestPost()
  189. if err != nil {
  190. t.Errorf("Expected nil, recieved %s", err.Error())
  191. }
  192. postJson := `
  193. {
  194. "content": "New test content",
  195. "front_page": false,
  196. "order": 2
  197. }
  198. `
  199. req, err := http.NewRequest("PUT", fmt.Sprintf(
  200. "%s/post/%s",
  201. ts.URL,
  202. postData.ID,
  203. ), strings.NewReader(postJson))
  204. if err != nil {
  205. t.Errorf("Expected nil, recieved %s", err.Error())
  206. }
  207. // Fetch Request
  208. res, err := http.DefaultClient.Do(req)
  209. if err != nil {
  210. t.Errorf("Expected nil, recieved %s", err.Error())
  211. }
  212. defer res.Body.Close()
  213. if res.StatusCode != http.StatusOK {
  214. t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode)
  215. }
  216. updatePostData := new(Models.Post)
  217. err = json.NewDecoder(res.Body).Decode(updatePostData)
  218. if err != nil {
  219. t.Errorf("Expected nil, recieved %s", err.Error())
  220. }
  221. if updatePostData.Content != "New test content" {
  222. t.Errorf("Expected \"New test content\", recieved %s", updatePostData.Content)
  223. }
  224. if updatePostData.FrontPage != false {
  225. t.Errorf("Expected false, recieved %t", updatePostData.FrontPage)
  226. }
  227. if updatePostData.Order != 2 {
  228. t.Errorf("Expected 2, recieved %d", updatePostData.Order)
  229. }
  230. }