feedbag_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package handler
  2. import (
  3. "bytes"
  4. "log/slog"
  5. "testing"
  6. "github.com/mk6i/retro-aim-server/wire"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/mock"
  9. )
  10. func TestFeedbagHandler_DeleteItem(t *testing.T) {
  11. input := wire.SNACMessage{
  12. Frame: wire.SNACFrame{
  13. FoodGroup: wire.Feedbag,
  14. SubGroup: wire.FeedbagDeleteItem,
  15. },
  16. Body: wire.SNAC_0x13_0x0A_FeedbagDeleteItem{
  17. Items: []wire.FeedbagItem{
  18. {
  19. Name: "my-item",
  20. },
  21. },
  22. },
  23. }
  24. output := wire.SNACMessage{
  25. Frame: wire.SNACFrame{
  26. FoodGroup: wire.Feedbag,
  27. SubGroup: wire.FeedbagStatus,
  28. },
  29. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  30. Results: []uint16{1234},
  31. },
  32. }
  33. svc := newMockFeedbagService(t)
  34. svc.EXPECT().
  35. DeleteItem(mock.Anything, mock.Anything, input.Frame, input.Body).
  36. Return(output, nil)
  37. h := NewFeedbagHandler(slog.Default(), svc)
  38. responseWriter := newMockResponseWriter(t)
  39. responseWriter.EXPECT().
  40. SendSNAC(output.Frame, output.Body).
  41. Return(nil)
  42. buf := &bytes.Buffer{}
  43. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  44. assert.NoError(t, h.DeleteItem(nil, nil, input.Frame, buf, responseWriter))
  45. }
  46. func TestFeedbagHandler_EndCluster(t *testing.T) {
  47. input := wire.SNACMessage{
  48. Frame: wire.SNACFrame{
  49. FoodGroup: wire.Feedbag,
  50. SubGroup: wire.FeedbagEndCluster,
  51. },
  52. Body: struct{}{},
  53. }
  54. svc := newMockFeedbagService(t)
  55. h := NewFeedbagHandler(slog.Default(), svc)
  56. responseWriter := newMockResponseWriter(t)
  57. buf := &bytes.Buffer{}
  58. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  59. assert.NoError(t, h.EndCluster(nil, nil, input.Frame, buf, responseWriter))
  60. }
  61. func TestFeedbagHandler_InsertItem(t *testing.T) {
  62. input := wire.SNACMessage{
  63. Frame: wire.SNACFrame{
  64. FoodGroup: wire.Feedbag,
  65. SubGroup: wire.FeedbagInsertItem,
  66. },
  67. Body: wire.SNAC_0x13_0x08_FeedbagInsertItem{
  68. Items: []wire.FeedbagItem{
  69. {
  70. Name: "my-item",
  71. },
  72. },
  73. },
  74. }
  75. output := wire.SNACMessage{
  76. Frame: wire.SNACFrame{
  77. FoodGroup: wire.Feedbag,
  78. SubGroup: wire.FeedbagStatus,
  79. },
  80. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  81. Results: []uint16{1234},
  82. },
  83. }
  84. svc := newMockFeedbagService(t)
  85. svc.EXPECT().
  86. UpsertItem(mock.Anything, mock.Anything, input.Frame, input.Body.(wire.SNAC_0x13_0x08_FeedbagInsertItem).Items).
  87. Return(output, nil)
  88. h := NewFeedbagHandler(slog.Default(), svc)
  89. responseWriter := newMockResponseWriter(t)
  90. responseWriter.EXPECT().
  91. SendSNAC(output.Frame, output.Body).
  92. Return(nil)
  93. buf := &bytes.Buffer{}
  94. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  95. assert.NoError(t, h.InsertItem(nil, nil, input.Frame, buf, responseWriter))
  96. }
  97. func TestFeedbagHandler_Query(t *testing.T) {
  98. input := wire.SNACMessage{
  99. Frame: wire.SNACFrame{
  100. FoodGroup: wire.Feedbag,
  101. SubGroup: wire.FeedbagQuery,
  102. },
  103. Body: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
  104. TLVRestBlock: wire.TLVRestBlock{
  105. TLVList: wire.TLVList{
  106. {
  107. Tag: 0x01,
  108. Value: []byte{1, 2, 3, 4},
  109. },
  110. },
  111. },
  112. },
  113. }
  114. output := wire.SNACMessage{
  115. Frame: wire.SNACFrame{
  116. FoodGroup: wire.Feedbag,
  117. SubGroup: wire.FeedbagReply,
  118. },
  119. Body: wire.SNAC_0x13_0x06_FeedbagReply{
  120. Version: 4,
  121. },
  122. }
  123. svc := newMockFeedbagService(t)
  124. svc.EXPECT().
  125. Query(mock.Anything, mock.Anything, input.Frame).
  126. Return(output, nil)
  127. h := NewFeedbagHandler(slog.Default(), svc)
  128. responseWriter := newMockResponseWriter(t)
  129. responseWriter.EXPECT().
  130. SendSNAC(output.Frame, output.Body).
  131. Return(nil)
  132. buf := &bytes.Buffer{}
  133. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  134. assert.NoError(t, h.Query(nil, nil, input.Frame, buf, responseWriter))
  135. }
  136. func TestFeedbagHandler_QueryIfModified(t *testing.T) {
  137. input := wire.SNACMessage{
  138. Frame: wire.SNACFrame{
  139. FoodGroup: wire.Feedbag,
  140. SubGroup: wire.FeedbagQueryIfModified,
  141. },
  142. Body: wire.SNAC_0x13_0x05_FeedbagQueryIfModified{
  143. LastUpdate: 1234,
  144. },
  145. }
  146. output := wire.SNACMessage{
  147. Frame: wire.SNACFrame{
  148. FoodGroup: wire.Feedbag,
  149. SubGroup: wire.FeedbagReply,
  150. },
  151. Body: wire.SNAC_0x13_0x06_FeedbagReply{
  152. LastUpdate: 1234,
  153. },
  154. }
  155. svc := newMockFeedbagService(t)
  156. svc.EXPECT().
  157. QueryIfModified(mock.Anything, mock.Anything, input.Frame, input.Body).
  158. Return(output, nil)
  159. h := NewFeedbagHandler(slog.Default(), svc)
  160. responseWriter := newMockResponseWriter(t)
  161. responseWriter.EXPECT().
  162. SendSNAC(output.Frame, output.Body).
  163. Return(nil)
  164. buf := &bytes.Buffer{}
  165. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  166. assert.NoError(t, h.QueryIfModified(nil, nil, input.Frame, buf, responseWriter))
  167. }
  168. func TestFeedbagHandler_RightsQuery(t *testing.T) {
  169. input := wire.SNACMessage{
  170. Frame: wire.SNACFrame{
  171. FoodGroup: wire.Feedbag,
  172. SubGroup: wire.FeedbagRightsQuery,
  173. },
  174. Body: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
  175. TLVRestBlock: wire.TLVRestBlock{
  176. TLVList: wire.TLVList{
  177. {
  178. Tag: 0x01,
  179. Value: []byte{1, 2, 3, 4},
  180. },
  181. },
  182. },
  183. },
  184. }
  185. output := wire.SNACMessage{
  186. Frame: wire.SNACFrame{
  187. FoodGroup: wire.Feedbag,
  188. SubGroup: wire.FeedbagRightsReply,
  189. },
  190. Body: wire.SNAC_0x13_0x03_FeedbagRightsReply{
  191. TLVRestBlock: wire.TLVRestBlock{
  192. TLVList: wire.TLVList{
  193. {
  194. Tag: 0x01,
  195. Value: []byte{1, 2, 3, 4},
  196. },
  197. },
  198. },
  199. },
  200. }
  201. svc := newMockFeedbagService(t)
  202. svc.EXPECT().
  203. RightsQuery(mock.Anything, input.Frame).
  204. Return(output)
  205. h := NewFeedbagHandler(slog.Default(), svc)
  206. responseWriter := newMockResponseWriter(t)
  207. responseWriter.EXPECT().
  208. SendSNAC(output.Frame, output.Body).
  209. Return(nil)
  210. buf := &bytes.Buffer{}
  211. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  212. assert.NoError(t, h.RightsQuery(nil, nil, input.Frame, buf, responseWriter))
  213. }
  214. func TestFeedbagHandler_StartCluster(t *testing.T) {
  215. input := wire.SNACMessage{
  216. Frame: wire.SNACFrame{
  217. FoodGroup: wire.Feedbag,
  218. SubGroup: wire.FeedbagStartCluster,
  219. },
  220. Body: wire.SNAC_0x13_0x11_FeedbagStartCluster{
  221. TLVRestBlock: wire.TLVRestBlock{
  222. TLVList: wire.TLVList{
  223. {
  224. Tag: 0x01,
  225. Value: []byte{1, 2, 3, 4},
  226. },
  227. },
  228. },
  229. },
  230. }
  231. svc := newMockFeedbagService(t)
  232. svc.EXPECT().
  233. StartCluster(mock.Anything, input.Frame, input.Body)
  234. h := NewFeedbagHandler(slog.Default(), svc)
  235. responseWriter := newMockResponseWriter(t)
  236. buf := &bytes.Buffer{}
  237. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  238. assert.NoError(t, h.StartCluster(nil, nil, input.Frame, buf, responseWriter))
  239. }
  240. func TestFeedbagHandler_UpdateItem(t *testing.T) {
  241. input := wire.SNACMessage{
  242. Frame: wire.SNACFrame{
  243. FoodGroup: wire.Feedbag,
  244. SubGroup: wire.FeedbagUpdateItem,
  245. },
  246. Body: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
  247. Items: []wire.FeedbagItem{
  248. {
  249. Name: "my-item",
  250. },
  251. },
  252. },
  253. }
  254. output := wire.SNACMessage{
  255. Frame: wire.SNACFrame{
  256. FoodGroup: wire.Feedbag,
  257. SubGroup: wire.FeedbagStatus,
  258. },
  259. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  260. Results: []uint16{1234},
  261. },
  262. }
  263. svc := newMockFeedbagService(t)
  264. svc.EXPECT().
  265. UpsertItem(mock.Anything, mock.Anything, input.Frame, input.Body.(wire.SNAC_0x13_0x09_FeedbagUpdateItem).Items).
  266. Return(output, nil)
  267. h := NewFeedbagHandler(slog.Default(), svc)
  268. responseWriter := newMockResponseWriter(t)
  269. responseWriter.EXPECT().
  270. SendSNAC(output.Frame, output.Body).
  271. Return(nil)
  272. buf := &bytes.Buffer{}
  273. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  274. assert.NoError(t, h.UpdateItem(nil, nil, input.Frame, buf, responseWriter))
  275. }
  276. func TestFeedbagHandler_Use(t *testing.T) {
  277. input := wire.SNACMessage{
  278. Frame: wire.SNACFrame{
  279. FoodGroup: wire.Feedbag,
  280. SubGroup: wire.FeedbagUse,
  281. },
  282. Body: struct{}{},
  283. }
  284. svc := newMockFeedbagService(t)
  285. svc.EXPECT().
  286. Use(mock.Anything, mock.Anything).
  287. Return(nil)
  288. h := NewFeedbagHandler(slog.Default(), svc)
  289. responseWriter := newMockResponseWriter(t)
  290. buf := &bytes.Buffer{}
  291. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  292. assert.NoError(t, h.Use(nil, nil, input.Frame, buf, responseWriter))
  293. }
  294. func TestFeedbagHandler_RespondAuthorizeToHost(t *testing.T) {
  295. input := wire.SNACMessage{
  296. Frame: wire.SNACFrame{
  297. FoodGroup: wire.Feedbag,
  298. SubGroup: wire.FeedbagRequestAuthorizeToHost,
  299. },
  300. Body: wire.SNAC_0x13_0x1A_FeedbagRespondAuthorizeToHost{
  301. ScreenName: "theScreenName",
  302. },
  303. }
  304. svc := newMockFeedbagService(t)
  305. svc.EXPECT().
  306. RespondAuthorizeToHost(mock.Anything, mock.Anything, input.Frame, input.Body).
  307. Return(nil)
  308. h := NewFeedbagHandler(slog.Default(), svc)
  309. responseWriter := newMockResponseWriter(t)
  310. buf := &bytes.Buffer{}
  311. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  312. assert.NoError(t, h.RespondAuthorizeToHost(nil, nil, input.Frame, buf, responseWriter))
  313. }