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.

335 lines
6.7 KiB

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