odir_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. package foodgroup
  2. import (
  3. "context"
  4. "log/slog"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/mk6i/open-oscar-server/state"
  8. "github.com/mk6i/open-oscar-server/wire"
  9. )
  10. func TestODirService_KeywordListQuery(t *testing.T) {
  11. cases := []struct {
  12. // name is the unit test name
  13. name string
  14. // inputSNAC is the SNAC sent by the sender client
  15. inputSNAC wire.SNACMessage
  16. // expectSNACFrame is the SNAC frame sent from the server to the recipient
  17. // client
  18. expectOutput wire.SNACMessage
  19. // mockParams is the list of params sent to mocks that satisfy this
  20. // method's dependencies
  21. mockParams mockParams
  22. // expectErr is the expected error returned by the handler
  23. expectErr error
  24. }{
  25. {
  26. name: "get full list",
  27. inputSNAC: wire.SNACMessage{
  28. Frame: wire.SNACFrame{
  29. RequestID: 1234,
  30. },
  31. },
  32. expectOutput: wire.SNACMessage{
  33. Frame: wire.SNACFrame{
  34. FoodGroup: wire.ODir,
  35. SubGroup: wire.ODirKeywordListReply,
  36. RequestID: 1234,
  37. },
  38. Body: wire.SNAC_0x0F_0x04_KeywordListReply{
  39. Status: 0x01,
  40. Interests: []wire.ODirKeywordListItem{
  41. {
  42. ID: 0,
  43. Name: "Animals",
  44. Type: wire.ODirKeyword,
  45. },
  46. {
  47. ID: 1,
  48. Name: "Music",
  49. Type: wire.ODirKeywordCategory,
  50. },
  51. {
  52. ID: 1,
  53. Name: "Rock",
  54. Type: wire.ODirKeyword,
  55. },
  56. {
  57. ID: 1,
  58. Name: "Jazz",
  59. Type: wire.ODirKeyword,
  60. },
  61. },
  62. },
  63. },
  64. mockParams: mockParams{
  65. profileManagerParams: profileManagerParams{
  66. interestListParams: interestListParams{
  67. {
  68. result: []wire.ODirKeywordListItem{
  69. {
  70. ID: 0,
  71. Name: "Animals",
  72. Type: wire.ODirKeyword,
  73. },
  74. {
  75. ID: 1,
  76. Name: "Music",
  77. Type: wire.ODirKeywordCategory,
  78. },
  79. {
  80. ID: 1,
  81. Name: "Rock",
  82. Type: wire.ODirKeyword,
  83. },
  84. {
  85. ID: 1,
  86. Name: "Jazz",
  87. Type: wire.ODirKeyword,
  88. },
  89. },
  90. },
  91. },
  92. },
  93. },
  94. },
  95. }
  96. for _, tc := range cases {
  97. t.Run(tc.name, func(t *testing.T) {
  98. profileManager := newMockProfileManager(t)
  99. for _, params := range tc.mockParams.interestListParams {
  100. profileManager.EXPECT().
  101. InterestList(matchContext()).
  102. Return(params.result, params.err)
  103. }
  104. svc := NewODirService(slog.Default(), profileManager)
  105. actual, err := svc.KeywordListQuery(context.Background(), tc.inputSNAC.Frame)
  106. assert.NoError(t, err)
  107. assert.Equal(t, tc.expectOutput, actual)
  108. })
  109. }
  110. }
  111. func TestODirService_InfoQuery(t *testing.T) {
  112. cases := []struct {
  113. // name is the unit test name
  114. name string
  115. // inputSNAC is the SNAC sent by the sender client
  116. inputSNAC wire.SNACMessage
  117. // expectSNACFrame is the SNAC frame sent from the server to the recipient
  118. // client
  119. expectOutput wire.SNACMessage
  120. // mockParams is the list of params sent to mocks that satisfy this
  121. // method's dependencies
  122. mockParams mockParams
  123. // expectErr is the expected error returned by the handler
  124. expectErr error
  125. }{
  126. {
  127. name: "search by name and address - results found",
  128. inputSNAC: wire.SNACMessage{
  129. Frame: wire.SNACFrame{
  130. RequestID: 1234,
  131. },
  132. Body: wire.SNAC_0x0F_0x02_InfoQuery{
  133. TLVRestBlock: wire.TLVRestBlock{
  134. TLVList: wire.TLVList{
  135. wire.NewTLVBE(wire.ODirTLVFirstName, "joe"),
  136. },
  137. },
  138. },
  139. },
  140. expectOutput: wire.SNACMessage{
  141. Frame: wire.SNACFrame{
  142. FoodGroup: wire.ODir,
  143. SubGroup: wire.ODirInfoReply,
  144. RequestID: 1234,
  145. },
  146. Body: wire.SNAC_0x0F_0x03_InfoReply{
  147. Status: wire.ODirSearchResponseOK,
  148. Results: struct {
  149. List []wire.TLVBlock `oscar:"count_prefix=uint16"`
  150. }{List: []wire.TLVBlock{
  151. {
  152. TLVList: wire.TLVList{
  153. wire.NewTLVBE(wire.ODirTLVFirstName, "Joe"),
  154. wire.NewTLVBE(wire.ODirTLVLastName, "Doe"),
  155. wire.NewTLVBE(wire.ODirTLVState, "California"),
  156. wire.NewTLVBE(wire.ODirTLVCity, "Los Angeles"),
  157. wire.NewTLVBE(wire.ODirTLVCountry, "USA"),
  158. wire.NewTLVBE(wire.ODirTLVScreenName, "joe123"),
  159. },
  160. },
  161. {
  162. TLVList: wire.TLVList{
  163. wire.NewTLVBE(wire.ODirTLVFirstName, "Joe"),
  164. wire.NewTLVBE(wire.ODirTLVLastName, "Smith"),
  165. wire.NewTLVBE(wire.ODirTLVState, "New York"),
  166. wire.NewTLVBE(wire.ODirTLVCity, "New York City"),
  167. wire.NewTLVBE(wire.ODirTLVCountry, "USA"),
  168. wire.NewTLVBE(wire.ODirTLVScreenName, "joe321"),
  169. },
  170. },
  171. }},
  172. },
  173. },
  174. mockParams: mockParams{
  175. profileManagerParams: profileManagerParams{
  176. findByAIMNameAndAddrParams: findByAIMNameAndAddrParams{
  177. {
  178. info: state.AIMNameAndAddr{
  179. FirstName: "joe",
  180. },
  181. result: []state.User{
  182. {
  183. DisplayScreenName: "joe123",
  184. AIMDirectoryInfo: state.AIMNameAndAddr{
  185. FirstName: "Joe",
  186. LastName: "Doe",
  187. Country: "USA",
  188. State: "California",
  189. City: "Los Angeles",
  190. },
  191. },
  192. {
  193. DisplayScreenName: "joe321",
  194. AIMDirectoryInfo: state.AIMNameAndAddr{
  195. FirstName: "Joe",
  196. LastName: "Smith",
  197. Country: "USA",
  198. State: "New York",
  199. City: "New York City",
  200. },
  201. },
  202. },
  203. },
  204. },
  205. },
  206. },
  207. },
  208. {
  209. name: "search by name and address - no results found",
  210. inputSNAC: wire.SNACMessage{
  211. Frame: wire.SNACFrame{
  212. RequestID: 1234,
  213. },
  214. Body: wire.SNAC_0x0F_0x02_InfoQuery{
  215. TLVRestBlock: wire.TLVRestBlock{
  216. TLVList: wire.TLVList{
  217. wire.NewTLVBE(wire.ODirTLVFirstName, "joe"),
  218. },
  219. },
  220. },
  221. },
  222. expectOutput: wire.SNACMessage{
  223. Frame: wire.SNACFrame{
  224. FoodGroup: wire.ODir,
  225. SubGroup: wire.ODirInfoReply,
  226. RequestID: 1234,
  227. },
  228. Body: wire.SNAC_0x0F_0x03_InfoReply{
  229. Status: wire.ODirSearchResponseOK,
  230. },
  231. },
  232. mockParams: mockParams{
  233. profileManagerParams: profileManagerParams{
  234. findByAIMNameAndAddrParams: findByAIMNameAndAddrParams{
  235. {
  236. info: state.AIMNameAndAddr{
  237. FirstName: "joe",
  238. },
  239. result: []state.User{},
  240. },
  241. },
  242. },
  243. },
  244. },
  245. {
  246. name: "search by name and address - no first or last name",
  247. inputSNAC: wire.SNACMessage{
  248. Frame: wire.SNACFrame{
  249. RequestID: 1234,
  250. },
  251. Body: wire.SNAC_0x0F_0x02_InfoQuery{
  252. TLVRestBlock: wire.TLVRestBlock{
  253. TLVList: wire.TLVList{
  254. wire.NewTLVBE(wire.ODirTLVCity, "new york"),
  255. },
  256. },
  257. },
  258. },
  259. expectOutput: wire.SNACMessage{
  260. Frame: wire.SNACFrame{
  261. FoodGroup: wire.ODir,
  262. SubGroup: wire.ODirInfoReply,
  263. RequestID: 1234,
  264. },
  265. Body: wire.SNAC_0x0F_0x03_InfoReply{
  266. Status: wire.ODirSearchResponseNameMissing,
  267. },
  268. },
  269. mockParams: mockParams{
  270. profileManagerParams: profileManagerParams{
  271. findByAIMNameAndAddrParams: findByAIMNameAndAddrParams{},
  272. },
  273. },
  274. },
  275. {
  276. name: "search by email - results found",
  277. inputSNAC: wire.SNACMessage{
  278. Frame: wire.SNACFrame{
  279. RequestID: 1234,
  280. },
  281. Body: wire.SNAC_0x0F_0x02_InfoQuery{
  282. TLVRestBlock: wire.TLVRestBlock{
  283. TLVList: wire.TLVList{
  284. wire.NewTLVBE(wire.ODirTLVEmailAddress, "test@aol.com"),
  285. },
  286. },
  287. },
  288. },
  289. expectOutput: wire.SNACMessage{
  290. Frame: wire.SNACFrame{
  291. FoodGroup: wire.ODir,
  292. SubGroup: wire.ODirInfoReply,
  293. RequestID: 1234,
  294. },
  295. Body: wire.SNAC_0x0F_0x03_InfoReply{
  296. Status: wire.ODirSearchResponseOK,
  297. Results: struct {
  298. List []wire.TLVBlock `oscar:"count_prefix=uint16"`
  299. }{List: []wire.TLVBlock{
  300. {
  301. TLVList: wire.TLVList{
  302. wire.NewTLVBE(wire.ODirTLVFirstName, "Joe"),
  303. wire.NewTLVBE(wire.ODirTLVLastName, "Doe"),
  304. wire.NewTLVBE(wire.ODirTLVState, "California"),
  305. wire.NewTLVBE(wire.ODirTLVCity, "Los Angeles"),
  306. wire.NewTLVBE(wire.ODirTLVCountry, "USA"),
  307. wire.NewTLVBE(wire.ODirTLVScreenName, "joe123"),
  308. },
  309. },
  310. }},
  311. },
  312. },
  313. mockParams: mockParams{
  314. profileManagerParams: profileManagerParams{
  315. findByAIMEmailParams: findByAIMEmailParams{
  316. {
  317. email: "test@aol.com",
  318. result: state.User{
  319. DisplayScreenName: "joe123",
  320. AIMDirectoryInfo: state.AIMNameAndAddr{
  321. FirstName: "Joe",
  322. LastName: "Doe",
  323. Country: "USA",
  324. State: "California",
  325. City: "Los Angeles",
  326. },
  327. },
  328. },
  329. },
  330. },
  331. },
  332. },
  333. {
  334. name: "search by email - no results found",
  335. inputSNAC: wire.SNACMessage{
  336. Frame: wire.SNACFrame{
  337. RequestID: 1234,
  338. },
  339. Body: wire.SNAC_0x0F_0x02_InfoQuery{
  340. TLVRestBlock: wire.TLVRestBlock{
  341. TLVList: wire.TLVList{
  342. wire.NewTLVBE(wire.ODirTLVEmailAddress, "test@aol.com"),
  343. },
  344. },
  345. },
  346. },
  347. expectOutput: wire.SNACMessage{
  348. Frame: wire.SNACFrame{
  349. FoodGroup: wire.ODir,
  350. SubGroup: wire.ODirInfoReply,
  351. RequestID: 1234,
  352. },
  353. Body: wire.SNAC_0x0F_0x03_InfoReply{
  354. Status: wire.ODirSearchResponseOK,
  355. },
  356. },
  357. mockParams: mockParams{
  358. profileManagerParams: profileManagerParams{
  359. findByAIMEmailParams: findByAIMEmailParams{
  360. {
  361. email: "test@aol.com",
  362. err: state.ErrNoUser,
  363. },
  364. },
  365. },
  366. },
  367. },
  368. {
  369. name: "search by interest - results found",
  370. inputSNAC: wire.SNACMessage{
  371. Frame: wire.SNACFrame{
  372. RequestID: 1234,
  373. },
  374. Body: wire.SNAC_0x0F_0x02_InfoQuery{
  375. TLVRestBlock: wire.TLVRestBlock{
  376. TLVList: wire.TLVList{
  377. wire.NewTLVBE(wire.ODirTLVInterest, "Computers"),
  378. },
  379. },
  380. },
  381. },
  382. expectOutput: wire.SNACMessage{
  383. Frame: wire.SNACFrame{
  384. FoodGroup: wire.ODir,
  385. SubGroup: wire.ODirInfoReply,
  386. RequestID: 1234,
  387. },
  388. Body: wire.SNAC_0x0F_0x03_InfoReply{
  389. Status: wire.ODirSearchResponseOK,
  390. Results: struct {
  391. List []wire.TLVBlock `oscar:"count_prefix=uint16"`
  392. }{List: []wire.TLVBlock{
  393. {
  394. TLVList: wire.TLVList{
  395. wire.NewTLVBE(wire.ODirTLVFirstName, "Joe"),
  396. wire.NewTLVBE(wire.ODirTLVLastName, "Doe"),
  397. wire.NewTLVBE(wire.ODirTLVState, "California"),
  398. wire.NewTLVBE(wire.ODirTLVCity, "Los Angeles"),
  399. wire.NewTLVBE(wire.ODirTLVCountry, "USA"),
  400. wire.NewTLVBE(wire.ODirTLVScreenName, "joe123"),
  401. },
  402. },
  403. {
  404. TLVList: wire.TLVList{
  405. wire.NewTLVBE(wire.ODirTLVFirstName, "Joe"),
  406. wire.NewTLVBE(wire.ODirTLVLastName, "Smith"),
  407. wire.NewTLVBE(wire.ODirTLVState, "New York"),
  408. wire.NewTLVBE(wire.ODirTLVCity, "New York City"),
  409. wire.NewTLVBE(wire.ODirTLVCountry, "USA"),
  410. wire.NewTLVBE(wire.ODirTLVScreenName, "joe321"),
  411. },
  412. },
  413. }},
  414. },
  415. },
  416. mockParams: mockParams{
  417. profileManagerParams: profileManagerParams{
  418. findByAIMKeywordParams: findByAIMKeywordParams{
  419. {
  420. keyword: "Computers",
  421. result: []state.User{
  422. {
  423. DisplayScreenName: "joe123",
  424. AIMDirectoryInfo: state.AIMNameAndAddr{
  425. FirstName: "Joe",
  426. LastName: "Doe",
  427. Country: "USA",
  428. State: "California",
  429. City: "Los Angeles",
  430. },
  431. },
  432. {
  433. DisplayScreenName: "joe321",
  434. AIMDirectoryInfo: state.AIMNameAndAddr{
  435. FirstName: "Joe",
  436. LastName: "Smith",
  437. Country: "USA",
  438. State: "New York",
  439. City: "New York City",
  440. },
  441. },
  442. },
  443. },
  444. },
  445. },
  446. },
  447. },
  448. {
  449. name: "search by interest - no results found",
  450. inputSNAC: wire.SNACMessage{
  451. Frame: wire.SNACFrame{
  452. RequestID: 1234,
  453. },
  454. Body: wire.SNAC_0x0F_0x02_InfoQuery{
  455. TLVRestBlock: wire.TLVRestBlock{
  456. TLVList: wire.TLVList{
  457. wire.NewTLVBE(wire.ODirTLVInterest, "Computers"),
  458. },
  459. },
  460. },
  461. },
  462. expectOutput: wire.SNACMessage{
  463. Frame: wire.SNACFrame{
  464. FoodGroup: wire.ODir,
  465. SubGroup: wire.ODirInfoReply,
  466. RequestID: 1234,
  467. },
  468. Body: wire.SNAC_0x0F_0x03_InfoReply{
  469. Status: wire.ODirSearchResponseOK,
  470. },
  471. },
  472. mockParams: mockParams{
  473. profileManagerParams: profileManagerParams{
  474. findByAIMKeywordParams: findByAIMKeywordParams{
  475. {
  476. keyword: "Computers",
  477. result: []state.User{},
  478. },
  479. },
  480. },
  481. },
  482. },
  483. }
  484. for _, tc := range cases {
  485. t.Run(tc.name, func(t *testing.T) {
  486. profileManager := newMockProfileManager(t)
  487. for _, params := range tc.mockParams.findByAIMNameAndAddrParams {
  488. profileManager.EXPECT().
  489. FindByAIMNameAndAddr(matchContext(), params.info).
  490. Return(params.result, params.err)
  491. }
  492. for _, params := range tc.mockParams.findByAIMEmailParams {
  493. profileManager.EXPECT().
  494. FindByAIMEmail(matchContext(), params.email).
  495. Return(params.result, params.err)
  496. }
  497. for _, params := range tc.mockParams.findByAIMKeywordParams {
  498. profileManager.EXPECT().
  499. FindByAIMKeyword(matchContext(), params.keyword).
  500. Return(params.result, params.err)
  501. }
  502. svc := NewODirService(slog.Default(), profileManager)
  503. actual, err := svc.InfoQuery(context.Background(), tc.inputSNAC.Frame, tc.inputSNAC.Body.(wire.SNAC_0x0F_0x02_InfoQuery))
  504. assert.NoError(t, err)
  505. assert.Equal(t, tc.expectOutput, actual)
  506. })
  507. }
  508. }