mgmt_api_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. package http
  2. import (
  3. "io"
  4. "log/slog"
  5. "net/http"
  6. "net/http/httptest"
  7. "strings"
  8. "testing"
  9. "time"
  10. "github.com/google/uuid"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/mock"
  13. "github.com/mk6i/retro-aim-server/state"
  14. "github.com/mk6i/retro-aim-server/wire"
  15. )
  16. func TestSessionHandler_GET(t *testing.T) {
  17. fnNewSess := func(screenName string) *state.Session {
  18. sess := state.NewSession()
  19. sess.SetIdentScreenName(state.NewIdentScreenName(screenName))
  20. sess.SetDisplayScreenName(state.DisplayScreenName(screenName))
  21. return sess
  22. }
  23. tt := []struct {
  24. name string
  25. sessions []*state.Session
  26. userHandlerErr error
  27. want string
  28. statusCode int
  29. }{
  30. {
  31. name: "without sessions",
  32. sessions: []*state.Session{},
  33. want: `{"count":0,"sessions":[]}`,
  34. statusCode: http.StatusOK,
  35. },
  36. {
  37. name: "with sessions",
  38. sessions: []*state.Session{
  39. fnNewSess("userA"),
  40. fnNewSess("userB"),
  41. },
  42. want: `{"count":2,"sessions":[{"id":"usera","screen_name":"userA"},{"id":"userb","screen_name":"userB"}]}`,
  43. statusCode: http.StatusOK,
  44. },
  45. }
  46. for _, tc := range tt {
  47. t.Run(tc.name, func(t *testing.T) {
  48. request := httptest.NewRequest(http.MethodGet, "/session", nil)
  49. responseRecorder := httptest.NewRecorder()
  50. sessionRetriever := newMockSessionRetriever(t)
  51. sessionRetriever.EXPECT().
  52. AllSessions().
  53. Return(tc.sessions)
  54. sessionHandler(responseRecorder, request, sessionRetriever)
  55. if responseRecorder.Code != tc.statusCode {
  56. t.Errorf("Want status '%d', got '%d'", tc.statusCode, responseRecorder.Code)
  57. }
  58. if strings.TrimSpace(responseRecorder.Body.String()) != tc.want {
  59. t.Errorf("Want '%s', got '%s'", tc.want, responseRecorder.Body)
  60. }
  61. })
  62. }
  63. }
  64. func TestSessionHandler_DisallowedMethod(t *testing.T) {
  65. request := httptest.NewRequest(http.MethodPut, "/session", nil)
  66. responseRecorder := httptest.NewRecorder()
  67. sessionHandler(responseRecorder, request, nil)
  68. wantCode := http.StatusMethodNotAllowed
  69. if responseRecorder.Code != wantCode {
  70. t.Errorf("want status '%d', got '%d'", http.StatusMethodNotAllowed, responseRecorder.Code)
  71. }
  72. wantBody := `method not allowed`
  73. if strings.TrimSpace(responseRecorder.Body.String()) != wantBody {
  74. t.Errorf("want '%s', got '%s'", wantBody, responseRecorder.Body)
  75. }
  76. }
  77. func TestUserHandler_GET(t *testing.T) {
  78. tt := []struct {
  79. name string
  80. users []state.User
  81. userHandlerErr error
  82. want string
  83. statusCode int
  84. }{
  85. {
  86. name: "empty user store",
  87. users: []state.User{},
  88. want: `[]`,
  89. statusCode: http.StatusOK,
  90. },
  91. {
  92. name: "user store containing 2 users",
  93. users: []state.User{
  94. {
  95. DisplayScreenName: "userA",
  96. IdentScreenName: state.NewIdentScreenName("userA"),
  97. },
  98. {
  99. DisplayScreenName: "userB",
  100. IdentScreenName: state.NewIdentScreenName("userB"),
  101. },
  102. },
  103. want: `[{"id":"usera","screen_name":"userA"},{"id":"userb","screen_name":"userB"}]`,
  104. statusCode: http.StatusOK,
  105. },
  106. {
  107. name: "user handler error",
  108. users: []state.User{},
  109. userHandlerErr: io.EOF,
  110. want: `internal server error`,
  111. statusCode: http.StatusInternalServerError,
  112. },
  113. }
  114. for _, tc := range tt {
  115. t.Run(tc.name, func(t *testing.T) {
  116. request := httptest.NewRequest(http.MethodGet, "/user", nil)
  117. responseRecorder := httptest.NewRecorder()
  118. userManager := newMockUserManager(t)
  119. userManager.EXPECT().
  120. AllUsers().
  121. Return(tc.users, tc.userHandlerErr)
  122. userHandler(responseRecorder, request, userManager, nil, slog.Default())
  123. if responseRecorder.Code != tc.statusCode {
  124. t.Errorf("Want status '%d', got '%d'", tc.statusCode, responseRecorder.Code)
  125. }
  126. if strings.TrimSpace(responseRecorder.Body.String()) != tc.want {
  127. t.Errorf("Want '%s', got '%s'", tc.want, responseRecorder.Body)
  128. }
  129. })
  130. }
  131. }
  132. func TestUserHandler_POST(t *testing.T) {
  133. tt := []struct {
  134. name string
  135. body string
  136. UUID uuid.UUID
  137. user state.User
  138. userHandlerErr error
  139. want string
  140. statusCode int
  141. }{
  142. {
  143. name: "with valid user",
  144. body: `{"screen_name":"userA", "password":"thepassword"}`,
  145. UUID: uuid.MustParse("07c70701-ba68-49a9-9f9b-67a53816e37b"),
  146. user: func() state.User {
  147. user := state.User{
  148. AuthKey: uuid.MustParse("07c70701-ba68-49a9-9f9b-67a53816e37b").String(),
  149. DisplayScreenName: "userA",
  150. IdentScreenName: state.NewIdentScreenName("userA"),
  151. }
  152. assert.NoError(t, user.HashPassword("thepassword"))
  153. return user
  154. }(),
  155. want: `User account created successfully.`,
  156. statusCode: http.StatusCreated,
  157. },
  158. {
  159. name: "with malformed body",
  160. body: `{"screen_name":"userA", "password":"thepassword"`,
  161. user: state.User{},
  162. want: `malformed input`,
  163. statusCode: http.StatusBadRequest,
  164. },
  165. {
  166. name: "user handler error",
  167. body: `{"screen_name":"userA", "password":"thepassword"}`,
  168. UUID: uuid.MustParse("07c70701-ba68-49a9-9f9b-67a53816e37b"),
  169. user: func() state.User {
  170. user := state.User{
  171. AuthKey: uuid.MustParse("07c70701-ba68-49a9-9f9b-67a53816e37b").String(),
  172. DisplayScreenName: "userA",
  173. IdentScreenName: state.NewIdentScreenName("userA"),
  174. }
  175. assert.NoError(t, user.HashPassword("thepassword"))
  176. return user
  177. }(),
  178. userHandlerErr: io.EOF,
  179. want: `internal server error`,
  180. statusCode: http.StatusInternalServerError,
  181. },
  182. {
  183. name: "duplicate user",
  184. body: `{"screen_name":"userA", "password":"thepassword"}`,
  185. UUID: uuid.MustParse("07c70701-ba68-49a9-9f9b-67a53816e37b"),
  186. user: func() state.User {
  187. user := state.User{
  188. AuthKey: uuid.MustParse("07c70701-ba68-49a9-9f9b-67a53816e37b").String(),
  189. DisplayScreenName: "userA",
  190. IdentScreenName: state.NewIdentScreenName("userA"),
  191. }
  192. assert.NoError(t, user.HashPassword("thepassword"))
  193. return user
  194. }(),
  195. userHandlerErr: state.ErrDupUser,
  196. want: `user already exists`,
  197. statusCode: http.StatusConflict,
  198. },
  199. }
  200. for _, tc := range tt {
  201. t.Run(tc.name, func(t *testing.T) {
  202. request := httptest.NewRequest(http.MethodPost, "/user", strings.NewReader(tc.body))
  203. responseRecorder := httptest.NewRecorder()
  204. userManager := newMockUserManager(t)
  205. if tc.user.IdentScreenName.String() != "" {
  206. userManager.EXPECT().
  207. InsertUser(tc.user).
  208. Return(tc.userHandlerErr)
  209. }
  210. newUUID := func() uuid.UUID { return tc.UUID }
  211. userHandler(responseRecorder, request, userManager, newUUID, slog.Default())
  212. if responseRecorder.Code != tc.statusCode {
  213. t.Errorf("want status '%d', got '%d'", tc.statusCode, responseRecorder.Code)
  214. }
  215. if strings.TrimSpace(responseRecorder.Body.String()) != tc.want {
  216. t.Errorf("want '%s', got '%s'", tc.want, responseRecorder.Body)
  217. }
  218. })
  219. }
  220. }
  221. func TestUserHandler_DELETE(t *testing.T) {
  222. tt := []struct {
  223. name string
  224. body string
  225. user state.User
  226. userHandlerErr error
  227. want string
  228. statusCode int
  229. }{
  230. {
  231. name: "with valid user",
  232. body: `{"screen_name":"userA"}`,
  233. user: state.User{
  234. IdentScreenName: state.NewIdentScreenName("userA"),
  235. },
  236. want: `User account successfully deleted.`,
  237. statusCode: http.StatusNoContent,
  238. },
  239. {
  240. name: "with non-existent user",
  241. body: `{"screen_name":"userA"}`,
  242. user: state.User{
  243. IdentScreenName: state.NewIdentScreenName("userA"),
  244. },
  245. userHandlerErr: state.ErrNoUser,
  246. want: `user does not exist`,
  247. statusCode: http.StatusNotFound,
  248. },
  249. {
  250. name: "with malformed body",
  251. body: `{"screen_name":"userA"`,
  252. user: state.User{},
  253. want: `malformed input`,
  254. statusCode: http.StatusBadRequest,
  255. },
  256. {
  257. name: "user handler error",
  258. body: `{"screen_name":"userA"}`,
  259. user: state.User{
  260. IdentScreenName: state.NewIdentScreenName("userA"),
  261. },
  262. userHandlerErr: io.EOF,
  263. want: `internal server error`,
  264. statusCode: http.StatusInternalServerError,
  265. },
  266. }
  267. for _, tc := range tt {
  268. t.Run(tc.name, func(t *testing.T) {
  269. request := httptest.NewRequest(http.MethodDelete, "/user", strings.NewReader(tc.body))
  270. responseRecorder := httptest.NewRecorder()
  271. userManager := newMockUserManager(t)
  272. if tc.user.IdentScreenName.String() != "" {
  273. userManager.EXPECT().
  274. DeleteUser(tc.user.IdentScreenName).
  275. Return(tc.userHandlerErr)
  276. }
  277. userHandler(responseRecorder, request, userManager, nil, slog.Default())
  278. if responseRecorder.Code != tc.statusCode {
  279. t.Errorf("want status '%d', got '%d'", tc.statusCode, responseRecorder.Code)
  280. }
  281. if strings.TrimSpace(responseRecorder.Body.String()) != tc.want {
  282. t.Errorf("want '%s', got '%s'", tc.want, responseRecorder.Body)
  283. }
  284. })
  285. }
  286. }
  287. func TestUserPasswordHandler_PUT(t *testing.T) {
  288. tt := []struct {
  289. name string
  290. body string
  291. user state.User
  292. UUID uuid.UUID
  293. userHandlerErr error
  294. want string
  295. statusCode int
  296. }{
  297. {
  298. name: "with valid password",
  299. body: `{"screen_name":"userA", "password":"thepassword"}`,
  300. UUID: uuid.MustParse("07c70701-ba68-49a9-9f9b-67a53816e37b"),
  301. user: func() state.User {
  302. user := state.User{
  303. AuthKey: uuid.MustParse("07c70701-ba68-49a9-9f9b-67a53816e37b").String(),
  304. IdentScreenName: state.NewIdentScreenName("userA"),
  305. }
  306. assert.NoError(t, user.HashPassword("thepassword"))
  307. return user
  308. }(),
  309. want: ``,
  310. statusCode: http.StatusNoContent,
  311. },
  312. {
  313. name: "with malformed body",
  314. body: `{"screen_name":"userA", "password":"thepassword"`,
  315. user: state.User{},
  316. want: `malformed input`,
  317. statusCode: http.StatusBadRequest,
  318. },
  319. {
  320. name: "user password handler error",
  321. body: `{"screen_name":"userA", "password":"thepassword"}`,
  322. UUID: uuid.MustParse("07c70701-ba68-49a9-9f9b-67a53816e37b"),
  323. user: func() state.User {
  324. user := state.User{
  325. AuthKey: uuid.MustParse("07c70701-ba68-49a9-9f9b-67a53816e37b").String(),
  326. IdentScreenName: state.NewIdentScreenName("userA"),
  327. }
  328. assert.NoError(t, user.HashPassword("thepassword"))
  329. return user
  330. }(),
  331. userHandlerErr: io.EOF,
  332. want: `internal server error`,
  333. statusCode: http.StatusInternalServerError,
  334. },
  335. {
  336. name: "user doesn't exist",
  337. body: `{"screen_name":"userA", "password":"thepassword"}`,
  338. UUID: uuid.MustParse("07c70701-ba68-49a9-9f9b-67a53816e37b"),
  339. user: func() state.User {
  340. user := state.User{
  341. AuthKey: uuid.MustParse("07c70701-ba68-49a9-9f9b-67a53816e37b").String(),
  342. IdentScreenName: state.NewIdentScreenName("userA"),
  343. }
  344. assert.NoError(t, user.HashPassword("thepassword"))
  345. return user
  346. }(),
  347. userHandlerErr: state.ErrNoUser,
  348. want: `user does not exist`,
  349. statusCode: http.StatusNotFound,
  350. },
  351. }
  352. for _, tc := range tt {
  353. t.Run(tc.name, func(t *testing.T) {
  354. request := httptest.NewRequest(http.MethodPut, "/user", strings.NewReader(tc.body))
  355. responseRecorder := httptest.NewRecorder()
  356. userManager := newMockUserManager(t)
  357. if tc.user.IdentScreenName.String() != "" {
  358. userManager.EXPECT().
  359. SetUserPassword(tc.user).
  360. Return(tc.userHandlerErr)
  361. }
  362. newUUID := func() uuid.UUID { return tc.UUID }
  363. userPasswordHandler(responseRecorder, request, userManager, newUUID, slog.Default())
  364. if responseRecorder.Code != tc.statusCode {
  365. t.Errorf("want status '%d', got '%d'", tc.statusCode, responseRecorder.Code)
  366. }
  367. if strings.TrimSpace(responseRecorder.Body.String()) != tc.want {
  368. t.Errorf("want '%s', got '%s'", tc.want, responseRecorder.Body)
  369. }
  370. })
  371. }
  372. }
  373. func TestUserHandler_DisallowedMethod(t *testing.T) {
  374. request := httptest.NewRequest(http.MethodPut, "/user", nil)
  375. responseRecorder := httptest.NewRecorder()
  376. userHandler(responseRecorder, request, nil, nil, nil)
  377. wantCode := http.StatusMethodNotAllowed
  378. if responseRecorder.Code != wantCode {
  379. t.Errorf("want status '%d', got '%d'", http.StatusMethodNotAllowed, responseRecorder.Code)
  380. }
  381. wantBody := `method not allowed`
  382. if strings.TrimSpace(responseRecorder.Body.String()) != wantBody {
  383. t.Errorf("want '%s', got '%s'", wantBody, responseRecorder.Body)
  384. }
  385. }
  386. func TestPublicChatHandler_GET(t *testing.T) {
  387. fnNewSess := func(screenName string) *state.Session {
  388. sess := state.NewSession()
  389. sess.SetIdentScreenName(state.NewIdentScreenName(screenName))
  390. sess.SetDisplayScreenName(state.DisplayScreenName(screenName))
  391. return sess
  392. }
  393. type allChatRoomsParams struct {
  394. exchange uint16
  395. result []state.ChatRoom
  396. err error
  397. }
  398. type allSessionsParams struct {
  399. cookie string
  400. result []*state.Session
  401. }
  402. tt := []struct {
  403. name string
  404. allChatRoomsParams allChatRoomsParams
  405. allSessionsParams []allSessionsParams
  406. userHandlerErr error
  407. want string
  408. statusCode int
  409. }{
  410. {
  411. name: "multiple chat rooms with participants",
  412. allChatRoomsParams: allChatRoomsParams{
  413. exchange: state.PublicExchange,
  414. result: []state.ChatRoom{
  415. {
  416. Cookie: "chat-room-1-cookie",
  417. Creator: state.NewIdentScreenName("chat-room-1-creator"),
  418. Name: "chat-room-1-name",
  419. CreateTime: time.Date(2024, 06, 01, 1, 2, 3, 4, time.UTC),
  420. },
  421. {
  422. Cookie: "chat-room-2-cookie",
  423. Creator: state.NewIdentScreenName("chat-room-2-creator"),
  424. Name: "chat-room-2-name",
  425. CreateTime: time.Date(2022, 01, 04, 6, 8, 1, 2, time.UTC),
  426. },
  427. },
  428. },
  429. allSessionsParams: []allSessionsParams{
  430. {
  431. cookie: "chat-room-1-cookie",
  432. result: []*state.Session{
  433. fnNewSess("userA"),
  434. fnNewSess("userB"),
  435. },
  436. },
  437. {
  438. cookie: "chat-room-2-cookie",
  439. result: []*state.Session{
  440. fnNewSess("userC"),
  441. fnNewSess("userD"),
  442. },
  443. },
  444. },
  445. want: `[{"name":"chat-room-1-name","create_time":"2024-06-01T01:02:03.000000004Z","url":"aim:gochat?exchange=0\u0026roomname=chat-room-1-name","participants":[{"id":"usera","screen_name":"userA"},{"id":"userb","screen_name":"userB"}]},{"name":"chat-room-2-name","create_time":"2022-01-04T06:08:01.000000002Z","url":"aim:gochat?exchange=0\u0026roomname=chat-room-2-name","participants":[{"id":"userc","screen_name":"userC"},{"id":"userd","screen_name":"userD"}]}]`,
  446. statusCode: http.StatusOK,
  447. },
  448. {
  449. name: "chat room without participants",
  450. allChatRoomsParams: allChatRoomsParams{
  451. exchange: state.PublicExchange,
  452. result: []state.ChatRoom{
  453. {
  454. Cookie: "chat-room-1-cookie",
  455. Creator: state.NewIdentScreenName("chat-room-1-creator"),
  456. Name: "chat-room-1-name",
  457. CreateTime: time.Date(2024, 06, 01, 1, 2, 3, 4, time.UTC),
  458. },
  459. },
  460. },
  461. allSessionsParams: []allSessionsParams{
  462. {
  463. cookie: "chat-room-1-cookie",
  464. result: []*state.Session{},
  465. },
  466. },
  467. want: `[{"name":"chat-room-1-name","create_time":"2024-06-01T01:02:03.000000004Z","url":"aim:gochat?exchange=0\u0026roomname=chat-room-1-name","participants":[]}]`,
  468. statusCode: http.StatusOK,
  469. },
  470. {
  471. name: "no chat rooms",
  472. allChatRoomsParams: allChatRoomsParams{
  473. exchange: state.PublicExchange,
  474. result: []state.ChatRoom{},
  475. },
  476. allSessionsParams: []allSessionsParams{},
  477. want: `[]`,
  478. statusCode: http.StatusOK,
  479. },
  480. }
  481. for _, tc := range tt {
  482. t.Run(tc.name, func(t *testing.T) {
  483. request := httptest.NewRequest(http.MethodGet, "/chat/room/public", nil)
  484. responseRecorder := httptest.NewRecorder()
  485. chatRoomRetriever := newMockChatRoomRetriever(t)
  486. chatRoomRetriever.EXPECT().
  487. AllChatRooms(tc.allChatRoomsParams.exchange).
  488. Return(tc.allChatRoomsParams.result, tc.allChatRoomsParams.err)
  489. chatSessionRetriever := newMockChatSessionRetriever(t)
  490. for _, params := range tc.allSessionsParams {
  491. chatSessionRetriever.EXPECT().
  492. AllSessions(params.cookie).
  493. Return(params.result)
  494. }
  495. getPublicChatHandler(responseRecorder, request, chatRoomRetriever, chatSessionRetriever, slog.Default())
  496. if responseRecorder.Code != tc.statusCode {
  497. t.Errorf("Want status '%d', got '%d'", tc.statusCode, responseRecorder.Code)
  498. }
  499. if strings.TrimSpace(responseRecorder.Body.String()) != tc.want {
  500. t.Errorf("Want '%s', got '%s'", tc.want, responseRecorder.Body)
  501. }
  502. })
  503. }
  504. }
  505. func TestPrivateChatHandler_GET(t *testing.T) {
  506. fnNewSess := func(screenName string) *state.Session {
  507. sess := state.NewSession()
  508. sess.SetIdentScreenName(state.NewIdentScreenName(screenName))
  509. sess.SetDisplayScreenName(state.DisplayScreenName(screenName))
  510. return sess
  511. }
  512. type allChatRoomsParams struct {
  513. exchange uint16
  514. result []state.ChatRoom
  515. err error
  516. }
  517. type allSessionsParams struct {
  518. cookie string
  519. result []*state.Session
  520. }
  521. tt := []struct {
  522. name string
  523. allChatRoomsParams allChatRoomsParams
  524. allSessionsParams []allSessionsParams
  525. userHandlerErr error
  526. want string
  527. statusCode int
  528. }{
  529. {
  530. name: "multiple chat rooms with participants",
  531. allChatRoomsParams: allChatRoomsParams{
  532. exchange: state.PrivateExchange,
  533. result: []state.ChatRoom{
  534. {
  535. Cookie: "chat-room-1-cookie",
  536. Creator: state.NewIdentScreenName("chat-room-1-creator"),
  537. Name: "chat-room-1-name",
  538. CreateTime: time.Date(2024, 06, 01, 1, 2, 3, 4, time.UTC),
  539. },
  540. {
  541. Cookie: "chat-room-2-cookie",
  542. Creator: state.NewIdentScreenName("chat-room-2-creator"),
  543. Name: "chat-room-2-name",
  544. CreateTime: time.Date(2022, 01, 04, 6, 8, 1, 2, time.UTC),
  545. },
  546. },
  547. },
  548. allSessionsParams: []allSessionsParams{
  549. {
  550. cookie: "chat-room-1-cookie",
  551. result: []*state.Session{
  552. fnNewSess("userA"),
  553. fnNewSess("userB"),
  554. },
  555. },
  556. {
  557. cookie: "chat-room-2-cookie",
  558. result: []*state.Session{
  559. fnNewSess("userC"),
  560. fnNewSess("userD"),
  561. },
  562. },
  563. },
  564. want: `[{"name":"chat-room-1-name","create_time":"2024-06-01T01:02:03.000000004Z","creator_id":"chat-room-1-creator","url":"aim:gochat?exchange=0\u0026roomname=chat-room-1-name","participants":[{"id":"usera","screen_name":"userA"},{"id":"userb","screen_name":"userB"}]},{"name":"chat-room-2-name","create_time":"2022-01-04T06:08:01.000000002Z","creator_id":"chat-room-2-creator","url":"aim:gochat?exchange=0\u0026roomname=chat-room-2-name","participants":[{"id":"userc","screen_name":"userC"},{"id":"userd","screen_name":"userD"}]}]`,
  565. statusCode: http.StatusOK,
  566. },
  567. {
  568. name: "chat room without participants",
  569. allChatRoomsParams: allChatRoomsParams{
  570. exchange: state.PrivateExchange,
  571. result: []state.ChatRoom{
  572. {
  573. Cookie: "chat-room-1-cookie",
  574. Creator: state.NewIdentScreenName("chat-room-1-creator"),
  575. Name: "chat-room-1-name",
  576. CreateTime: time.Date(2024, 06, 01, 1, 2, 3, 4, time.UTC),
  577. },
  578. },
  579. },
  580. allSessionsParams: []allSessionsParams{
  581. {
  582. cookie: "chat-room-1-cookie",
  583. result: []*state.Session{},
  584. },
  585. },
  586. want: `[{"name":"chat-room-1-name","create_time":"2024-06-01T01:02:03.000000004Z","creator_id":"chat-room-1-creator","url":"aim:gochat?exchange=0\u0026roomname=chat-room-1-name","participants":[]}]`,
  587. statusCode: http.StatusOK,
  588. },
  589. {
  590. name: "no chat rooms",
  591. allChatRoomsParams: allChatRoomsParams{
  592. exchange: state.PrivateExchange,
  593. result: []state.ChatRoom{},
  594. },
  595. allSessionsParams: []allSessionsParams{},
  596. want: `[]`,
  597. statusCode: http.StatusOK,
  598. },
  599. }
  600. for _, tc := range tt {
  601. t.Run(tc.name, func(t *testing.T) {
  602. request := httptest.NewRequest(http.MethodGet, "/chat/room/private", nil)
  603. responseRecorder := httptest.NewRecorder()
  604. chatRoomRetriever := newMockChatRoomRetriever(t)
  605. chatRoomRetriever.EXPECT().
  606. AllChatRooms(tc.allChatRoomsParams.exchange).
  607. Return(tc.allChatRoomsParams.result, tc.allChatRoomsParams.err)
  608. chatSessionRetriever := newMockChatSessionRetriever(t)
  609. for _, params := range tc.allSessionsParams {
  610. chatSessionRetriever.EXPECT().
  611. AllSessions(params.cookie).
  612. Return(params.result)
  613. }
  614. getPrivateChatHandler(responseRecorder, request, chatRoomRetriever, chatSessionRetriever, slog.Default())
  615. if responseRecorder.Code != tc.statusCode {
  616. t.Errorf("Want status '%d', got '%d'", tc.statusCode, responseRecorder.Code)
  617. }
  618. if strings.TrimSpace(responseRecorder.Body.String()) != tc.want {
  619. t.Errorf("Want '%s', got '%s'", tc.want, responseRecorder.Body)
  620. }
  621. })
  622. }
  623. }
  624. func TestInstantMessageHandler_POST(t *testing.T) {
  625. type relayToScreenNameParams struct {
  626. sender state.IdentScreenName
  627. recipient state.IdentScreenName
  628. msg string
  629. }
  630. tt := []struct {
  631. name string
  632. relayToScreenNameParams []relayToScreenNameParams
  633. body string
  634. want string
  635. statusCode int
  636. }{
  637. {
  638. name: "send an instant message",
  639. relayToScreenNameParams: []relayToScreenNameParams{
  640. {
  641. sender: state.NewIdentScreenName("sender_sn"),
  642. recipient: state.NewIdentScreenName("recip_sn"),
  643. msg: "hello world!",
  644. },
  645. },
  646. body: `{"from":"sender_sn","to":"recip_sn","text":"hello world!"}`,
  647. want: `Message sent successfully.`,
  648. statusCode: http.StatusOK,
  649. },
  650. {
  651. name: "with malformed body",
  652. body: `{"screen_name":"userA", "password":"thepassword"`,
  653. want: `malformed input`,
  654. statusCode: http.StatusBadRequest,
  655. },
  656. }
  657. for _, tc := range tt {
  658. t.Run(tc.name, func(t *testing.T) {
  659. request := httptest.NewRequest(http.MethodPost, "/user", strings.NewReader(tc.body))
  660. responseRecorder := httptest.NewRecorder()
  661. messageRelayer := newMockMessageRelayer(t)
  662. for _, params := range tc.relayToScreenNameParams {
  663. validateSNAC := func(msg wire.SNACMessage) bool {
  664. body := msg.Body.(wire.SNAC_0x04_0x07_ICBMChannelMsgToClient)
  665. assert.Equal(t, params.sender.String(), body.TLVUserInfo.ScreenName)
  666. b, ok := body.Slice(wire.ICBMTLVAOLIMData)
  667. assert.True(t, ok)
  668. txt, err := wire.UnmarshalICBMMessageText(b)
  669. assert.NoError(t, err)
  670. assert.Equal(t, params.msg, txt)
  671. return true
  672. }
  673. messageRelayer.EXPECT().
  674. RelayToScreenName(mock.Anything, params.recipient, mock.MatchedBy(validateSNAC))
  675. }
  676. postInstantMessageHandler(responseRecorder, request, messageRelayer, slog.Default())
  677. if responseRecorder.Code != tc.statusCode {
  678. t.Errorf("want status '%d', got '%d'", tc.statusCode, responseRecorder.Code)
  679. }
  680. if strings.TrimSpace(responseRecorder.Body.String()) != tc.want {
  681. t.Errorf("want '%s', got '%s'", tc.want, responseRecorder.Body)
  682. }
  683. })
  684. }
  685. }