server_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log/slog"
  6. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "testing"
  12. "time"
  13. "git.andreafazzi.eu/andrea/probo/pkg/models"
  14. "github.com/lmittmann/tint"
  15. "github.com/remogatto/prettytest"
  16. )
  17. var examPayload = `
  18. {
  19. "ID": "fe0a7ee0-f31a-413d-f123-ab5068bcaaaa",
  20. "Name": "Test session",
  21. "Exams": {
  22. "111222": {
  23. "id": "fe0a7ee0-f31a-413d-ba81-ab5068bc4c73",
  24. "created_at": "0001-01-01T00:00:00Z",
  25. "updated_at": "0001-01-01T00:00:00Z",
  26. "Participant": {
  27. "ID": "1234",
  28. "Firstname": "John",
  29. "Lastname": "Smith",
  30. "Token": "111222",
  31. "Attributes": {
  32. "class": "1 D LIN"
  33. }
  34. },
  35. "Quizzes": [
  36. {
  37. "id": "0610939b-a1a3-4d0e-bbc4-2aae0e8ee4b9",
  38. "created_at": "2023-11-27T17:51:53.910642221+01:00",
  39. "updated_at": "0001-01-01T00:00:00Z",
  40. "hash": "",
  41. "question": {
  42. "id": "98c0eec9-677f-464e-9e3e-864a859f29a3",
  43. "created_at": "0001-01-01T00:00:00Z",
  44. "updated_at": "0001-01-01T00:00:00Z",
  45. "text": "Question text with #tag1."
  46. },
  47. "answers": [
  48. {
  49. "id": "1ab5ff1f-74c8-4efc-abdc-d03a3640f3bc",
  50. "text": "Answer 1"
  51. },
  52. {
  53. "id": "74547724-b905-476f-8cfc-6ee633f92ef3",
  54. "text": "Answer 2"
  55. },
  56. {
  57. "id": "96c1a8ee-c50c-4ebc-89e4-9f3ca356adbd",
  58. "text": "Answer 3"
  59. },
  60. {
  61. "id": "16c4b95e-64ce-4666-8cbe-b66fa59eb23b",
  62. "text": "Answer 4"
  63. }
  64. ],
  65. "tags": [
  66. {
  67. "CreatedAt": "0001-01-01T00:00:00Z",
  68. "UpdatedAt": "0001-01-01T00:00:00Z",
  69. "DeletedAt": null,
  70. "name": "#tag1"
  71. }
  72. ],
  73. "correct": {
  74. "id": "1ab5ff1f-74c8-4efc-abdc-d03a3640f3bc",
  75. "text": "Answer 1"
  76. },
  77. "CorrectPos": 0,
  78. "type": 0
  79. },
  80. {
  81. "id": "915818c4-b0ce-4efc-8def-7fc8d5fa7454",
  82. "created_at": "2023-11-27T17:51:53.91077796+01:00",
  83. "updated_at": "0001-01-01T00:00:00Z",
  84. "hash": "",
  85. "question": {
  86. "id": "70793f0d-2855-4140-814e-40167464424b",
  87. "created_at": "0001-01-01T00:00:00Z",
  88. "updated_at": "0001-01-01T00:00:00Z",
  89. "text": "Another question text with #tag1."
  90. },
  91. "answers": [
  92. {
  93. "id": "1ab5ff1f-74c8-4efc-abdc-d03a3640f3bc",
  94. "text": "Answer 1"
  95. },
  96. {
  97. "id": "74547724-b905-476f-8cfc-6ee633f92ef3",
  98. "text": "Answer 2"
  99. },
  100. {
  101. "id": "96c1a8ee-c50c-4ebc-89e4-9f3ca356adbd",
  102. "text": "Answer 3"
  103. },
  104. {
  105. "id": "16c4b95e-64ce-4666-8cbe-b66fa59eb23b",
  106. "text": "Answer 4"
  107. }
  108. ],
  109. "tags": [
  110. {
  111. "CreatedAt": "0001-01-01T00:00:00Z",
  112. "UpdatedAt": "0001-01-01T00:00:00Z",
  113. "DeletedAt": null,
  114. "name": "#tag1"
  115. }
  116. ],
  117. "correct": {
  118. "id": "1ab5ff1f-74c8-4efc-abdc-d03a3640f3bc",
  119. "text": "Answer 1"
  120. },
  121. "CorrectPos": 0,
  122. "type": 0
  123. }
  124. ]
  125. }
  126. },
  127. "333444": {
  128. "id": "12345678-abcd-efgh-ijkl-9876543210ef",
  129. "created_at": "2023-12-01T12:00:00Z",
  130. "updated_at": "2023-12-01T12:00:00Z",
  131. "Participant": {
  132. "ID": "5678",
  133. "Firstname": "Jane",
  134. "Lastname": "Doe",
  135. "Token": "333444",
  136. "Attributes": {
  137. "class": "2 A SCI"
  138. }
  139. },
  140. "Quizzes": [
  141. {
  142. "id": "22222222-abcd-efgh-ijkl-9876543210ef",
  143. "created_at": "2023-12-01T12:00:00Z",
  144. "updated_at": "2023-12-01T12:00:00Z",
  145. "hash": "",
  146. "question": {
  147. "id": "33333333-abcd-efgh-ijkl-9876543210ef",
  148. "created_at": "2023-12-01T12:00:00Z",
  149. "updated_at": "2023-12-01T12:00:00Z",
  150. "text": "Sample question text."
  151. },
  152. "answers": [
  153. {
  154. "id": "44444444-abcd-efgh-ijkl-9876543210ef",
  155. "text": "Option 1"
  156. },
  157. {
  158. "id": "55555555-abcd-efgh-ijkl-9876543210ef",
  159. "text": "Option 2"
  160. },
  161. {
  162. "id": "66666666-abcd-efgh-ijkl-9876543210ef",
  163. "text": "Option 3"
  164. },
  165. {
  166. "id": "77777777-abcd-efgh-ijkl-9876543210ef",
  167. "text": "Option 4"
  168. }
  169. ],
  170. "tags": [
  171. {
  172. "CreatedAt": "2023-12-01T12:00:00Z",
  173. "UpdatedAt": "2023-12-01T12:00:00Z",
  174. "DeletedAt": null,
  175. "name": "#tag2"
  176. }
  177. ],
  178. "correct": {
  179. "id": "44444444-abcd-efgh-ijkl-9876543210ef",
  180. "text": "Option 1"
  181. },
  182. "CorrectPos": 0,
  183. "type": 0
  184. }
  185. ]
  186. }
  187. }
  188. `
  189. type serverTestSuite struct {
  190. prettytest.Suite
  191. }
  192. func TestRunner(t *testing.T) {
  193. slog.SetDefault(slog.New(
  194. tint.NewHandler(os.Stderr, &tint.Options{
  195. Level: slog.LevelError,
  196. TimeFormat: time.Kitchen,
  197. }),
  198. ))
  199. prettytest.Run(
  200. t,
  201. new(serverTestSuite),
  202. )
  203. }
  204. func (t *serverTestSuite) TestCreate() {
  205. DefaultDataDir = "testdata"
  206. s, err := NewDefaultServer()
  207. t.Nil(err)
  208. if !t.Failed() {
  209. request, _ := http.NewRequest(http.MethodPost, "/create", strings.NewReader(examPayload))
  210. response := httptest.NewRecorder()
  211. handler := http.HandlerFunc(s.createExamSessionHandler)
  212. handler.ServeHTTP(response, request)
  213. t.Equal(http.StatusOK, response.Code)
  214. if !t.Failed() {
  215. var session *models.Session
  216. err := json.Unmarshal(response.Body.Bytes(), &session)
  217. t.Nil(err)
  218. path := filepath.Join(GetDefaultSessionDir(), "session_fe0a7ee0-f31a-413d-f123-ab5068bcaaaa.json")
  219. _, err = os.Stat(path)
  220. t.Nil(err)
  221. defer os.Remove(path)
  222. t.Equal("fe0a7ee0-f31a-413d-f123-ab5068bcaaaa", session.ID)
  223. }
  224. }
  225. }
  226. func (t *serverTestSuite) TestRead() {
  227. DefaultDataDir = "testdata"
  228. s, err := NewDefaultServer()
  229. t.Nil(err)
  230. if !t.Failed() {
  231. request, _ := http.NewRequest(http.MethodPost, "/create", strings.NewReader(examPayload))
  232. response := httptest.NewRecorder()
  233. handler := http.HandlerFunc(s.createExamSessionHandler)
  234. handler.ServeHTTP(response, request)
  235. t.Equal(http.StatusOK, response.Code)
  236. if !t.Failed() {
  237. var session *models.Session
  238. err := json.Unmarshal(response.Body.Bytes(), &session)
  239. t.Nil(err)
  240. path := filepath.Join(GetDefaultSessionDir(), "session_fe0a7ee0-f31a-413d-f123-ab5068bcaaaa.json")
  241. _, err = os.Stat(path)
  242. t.Nil(err)
  243. if !t.Failed() {
  244. defer os.RemoveAll(path)
  245. request, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("/%s/%s", session.ID, "111222"), nil)
  246. response := httptest.NewRecorder()
  247. handler := http.HandlerFunc(s.getExamHandler)
  248. handler.ServeHTTP(response, request)
  249. t.Equal(http.StatusOK, response.Code)
  250. }
  251. }
  252. }
  253. }