server_integration_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/http/httptest"
  7. "reflect"
  8. "strings"
  9. "git.andreafazzi.eu/andrea/probo/client"
  10. "git.andreafazzi.eu/andrea/probo/hasher/sha256"
  11. "git.andreafazzi.eu/andrea/probo/store/memory"
  12. "github.com/remogatto/prettytest"
  13. )
  14. type integrationTestSuite struct {
  15. prettytest.Suite
  16. }
  17. func (t *integrationTestSuite) TestQuizCreateAndReadAll() {
  18. server := NewProboCollectorServer(
  19. memory.NewMemoryProboCollectorStore(
  20. sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
  21. ),
  22. )
  23. // POST a new question using a JSON payload
  24. payload := `
  25. {
  26. "question": {"text": "Question 1"},
  27. "answers": [
  28. {"text": "Text of the answer 1", "correct": true},
  29. {"text": "Text of the answer 2"},
  30. {"text": "Text of the answer 3"},
  31. {"text": "Text of the answer 4"}
  32. ]
  33. }
  34. `
  35. createQuizResponse, err := t.createQuiz(server, payload)
  36. t.True(err == nil, "Response should be decoded properly")
  37. if !t.Failed() {
  38. t.Equal("success", createQuizResponse.Status)
  39. t.Equal("Question 1", createQuizResponse.Content.Question.Text)
  40. t.Equal("Text of the answer 1", createQuizResponse.Content.Answers[0].Text)
  41. t.Equal("Text of the answer 1", createQuizResponse.Content.Correct.Text)
  42. t.True(createQuizResponse.Content.ID != "", "Test ID should not be empty")
  43. t.True(createQuizResponse.Content.Question.ID != "", "Question ID should not be empty")
  44. t.True(createQuizResponse.Content.Answers[0].ID != "", "Answer ID should not be empty")
  45. }
  46. readAllQuizResponse, err := t.readAllQuiz(server)
  47. t.True(err == nil, "Response should be decoded properly")
  48. if !t.Failed() {
  49. t.True(len(readAllQuizResponse.Content) == 1, "Length of returned tests should be 1")
  50. t.Equal("Question 1", readAllQuizResponse.Content[0].Question.Text)
  51. }
  52. }
  53. func (t *integrationTestSuite) TestQuizCreateAndUpdate() {
  54. server := NewProboCollectorServer(
  55. memory.NewMemoryProboCollectorStore(
  56. sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
  57. ),
  58. )
  59. // POST a new question using a JSON payload
  60. payload := `
  61. {
  62. "question": {"text": "Question 1"},
  63. "answers": [
  64. {"text": "Text of the answer 1", "correct": true},
  65. {"text": "Text of the answer 2"},
  66. {"text": "Text of the answer 3"},
  67. {"text": "Text of the answer 4"}
  68. ]
  69. }
  70. `
  71. createQuizResponse, err := t.createQuiz(server, payload)
  72. t.True(err == nil, "Response should be decoded properly")
  73. if !t.Failed() {
  74. payload = `
  75. {
  76. "question": {"text": "Updated Question 1"},
  77. "answers": [
  78. {"text": "Text of the answer 1"},
  79. {"text": "Text of the answer 2"},
  80. {"text": "Text of the answer 3", "correct": true},
  81. {"text": "Text of the answer 4"}
  82. ]
  83. }
  84. `
  85. updateQuizResponse, err := t.updateQuiz(server, payload, createQuizResponse.Content.ID)
  86. t.True(err == nil, "Response should be decoded properly")
  87. if !t.Failed() {
  88. t.Equal("Updated Question 1", updateQuizResponse.Content.Question.Text)
  89. t.Equal("Text of the answer 3", updateQuizResponse.Content.Correct.Text)
  90. }
  91. }
  92. }
  93. func (t *integrationTestSuite) TestUpdateNotExistentQuiz() {
  94. server := NewProboCollectorServer(
  95. memory.NewMemoryProboCollectorStore(
  96. sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
  97. ),
  98. )
  99. payload := ""
  100. r, err := t.updateQuiz(server, payload, "1234")
  101. t.True(err == nil, fmt.Sprintf("The operation should not return an error: %v", err))
  102. t.Equal("error", r.Status)
  103. }
  104. func (t *integrationTestSuite) TestCatchDuplicateQuiz() {
  105. server := NewProboCollectorServer(
  106. memory.NewMemoryProboCollectorStore(
  107. sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
  108. ),
  109. )
  110. // POST a new question using a JSON payload
  111. payload := `
  112. {
  113. "question": {"text": "Question 1"},
  114. "answers": [
  115. {"text": "Text of the answer 1", "correct": true},
  116. {"text": "Text of the answer 2"},
  117. {"text": "Text of the answer 3"},
  118. {"text": "Text of the answer 4"}
  119. ]
  120. }
  121. `
  122. quiz1, err := t.createQuiz(server, payload)
  123. t.True(err == nil, fmt.Sprintf("Create quiz should not raise an error: %v", err))
  124. quiz2, err := t.createQuiz(server, payload)
  125. t.True(err == nil, "Quizzes are duplicated, but the API should not return an error")
  126. t.True(reflect.DeepEqual(quiz1, quiz2), "Quizzes shold be exactly the same")
  127. }
  128. func (t *integrationTestSuite) createQuiz(server *ProboCollectorServer, payload string) (*client.CreateQuizResponse, error) {
  129. request, _ := http.NewRequest(http.MethodPost, "/quizzes/create", strings.NewReader(payload))
  130. response := httptest.NewRecorder()
  131. server.ServeHTTP(response, request)
  132. decodedResponse := new(client.CreateQuizResponse)
  133. err := json.Unmarshal(response.Body.Bytes(), decodedResponse)
  134. if err != nil {
  135. return nil, err
  136. }
  137. return decodedResponse, err
  138. }
  139. func (t *integrationTestSuite) updateQuiz(server *ProboCollectorServer, payload string, id string) (*client.UpdateQuizResponse, error) {
  140. request, _ := http.NewRequest(http.MethodPut, "/quizzes/update/"+id, strings.NewReader(payload))
  141. response := httptest.NewRecorder()
  142. server.ServeHTTP(response, request)
  143. decodedResponse := new(client.UpdateQuizResponse)
  144. err := json.Unmarshal(response.Body.Bytes(), decodedResponse)
  145. if err != nil {
  146. return decodedResponse, err
  147. }
  148. return decodedResponse, err
  149. }
  150. func (t *integrationTestSuite) readAllQuiz(server *ProboCollectorServer) (*client.ReadAllQuizResponse, error) {
  151. request, _ := http.NewRequest(http.MethodGet, "/quizzes", nil)
  152. response := httptest.NewRecorder()
  153. server.ServeHTTP(response, request)
  154. decodedResponse := new(client.ReadAllQuizResponse)
  155. err := json.Unmarshal(response.Body.Bytes(), decodedResponse)
  156. if err != nil {
  157. return nil, err
  158. }
  159. return decodedResponse, err
  160. }