buddy_list_manager_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. package webapi
  2. import (
  3. "context"
  4. "errors"
  5. "log/slog"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/mock"
  9. "github.com/stretchr/testify/require"
  10. "github.com/mk6i/open-oscar-server/state"
  11. "github.com/mk6i/open-oscar-server/wire"
  12. )
  13. func offlineWebAPIBuddy(aimID, displayID string) BuddyInfo {
  14. return BuddyInfo{
  15. AimID: aimID,
  16. DisplayID: displayID,
  17. State: "offline",
  18. UserType: "aim",
  19. Bot: false,
  20. Service: "AIM",
  21. }
  22. }
  23. // withAlias sets the viewer's private name for a buddy. It travels in friendly, not
  24. // displayId, which keeps carrying the buddy's own screen name.
  25. func withAlias(b BuddyInfo, alias string) BuddyInfo {
  26. b.Friendly = alias
  27. return b
  28. }
  29. func TestBuddyListManager_GetBuddyListForUser(t *testing.T) {
  30. ctx := context.Background()
  31. owner := state.NewIdentScreenName("listowner")
  32. tests := []struct {
  33. name string
  34. fb []wire.FeedbagItem
  35. fbErr error
  36. want []BuddyGroup
  37. wantErr string
  38. }{
  39. {
  40. name: "retrieve feedbag error",
  41. fbErr: errors.New("db unavailable"),
  42. wantErr: "failed to retrieve feedbag",
  43. },
  44. {
  45. name: "root group missing order attribute yields no groups",
  46. fb: []wire.FeedbagItem{
  47. {Name: "", GroupID: 0, ItemID: 0, ClassID: wire.FeedbagClassIdGroup, TLVLBlock: wire.TLVLBlock{}},
  48. },
  49. want: nil,
  50. },
  51. {
  52. name: "empty buddylist yields no groups",
  53. fb: []wire.FeedbagItem{
  54. {
  55. Name: "", GroupID: 0, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  56. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{})}},
  57. },
  58. },
  59. want: nil,
  60. },
  61. {
  62. name: "single group with buddies",
  63. fb: []wire.FeedbagItem{
  64. {
  65. Name: "", GroupID: 0, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  66. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{100})}},
  67. },
  68. {Name: "Buddies", GroupID: 100, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  69. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{1, 2})}}},
  70. {ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "user1", TLVLBlock: wire.TLVLBlock{}},
  71. {ItemID: 2, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "user2", TLVLBlock: wire.TLVLBlock{}},
  72. },
  73. want: []BuddyGroup{
  74. {
  75. Name: "Buddies",
  76. ID: 100,
  77. Buddies: []BuddyInfo{
  78. offlineWebAPIBuddy("user1", "user1"),
  79. offlineWebAPIBuddy("user2", "user2"),
  80. },
  81. },
  82. },
  83. },
  84. {
  85. name: "deny permit and pdinfo items do not produce groups",
  86. fb: []wire.FeedbagItem{
  87. {
  88. Name: "", GroupID: 0, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  89. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{})}},
  90. },
  91. {ClassID: wire.FeedbagClassIDDeny, Name: "blockeduser"},
  92. {ClassID: wire.FeedbagClassIDPermit, Name: "allowuser"},
  93. {
  94. ClassID: wire.FeedbagClassIdPdinfo,
  95. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesPdMode, uint8(3))}},
  96. },
  97. },
  98. want: nil,
  99. },
  100. {
  101. name: "buddy with alias",
  102. fb: []wire.FeedbagItem{
  103. {
  104. Name: "", GroupID: 0, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  105. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{100})}},
  106. },
  107. {Name: "Buddies", GroupID: 100, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  108. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{1})}}},
  109. {
  110. ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "bob",
  111. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesAlias, "Bob Smith")}},
  112. },
  113. },
  114. want: []BuddyGroup{
  115. {
  116. Name: "Buddies",
  117. ID: 100,
  118. // The buddy is offline, so no locate reply supplies a display
  119. // name and displayId falls back to the normalized feedbag name.
  120. Buddies: []BuddyInfo{withAlias(offlineWebAPIBuddy("bob", "bob"), "Bob Smith")},
  121. },
  122. },
  123. },
  124. {
  125. name: "unnormalized feedbag buddy name still yields a normalized aimId",
  126. fb: []wire.FeedbagItem{
  127. {
  128. Name: "", GroupID: 0, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  129. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{100})}},
  130. },
  131. {Name: "Buddies", GroupID: 100, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  132. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{1})}}},
  133. {ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "Mike Kelly"},
  134. },
  135. want: []BuddyGroup{
  136. {
  137. Name: "Buddies",
  138. ID: 100,
  139. Buddies: []BuddyInfo{offlineWebAPIBuddy("mikekelly", "Mike Kelly")},
  140. },
  141. },
  142. },
  143. {
  144. name: "buddy with note still listed note not exposed in WebAPI",
  145. fb: []wire.FeedbagItem{
  146. {
  147. Name: "", GroupID: 0, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  148. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{100})}},
  149. },
  150. {Name: "Buddies", GroupID: 100, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  151. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{1})}}},
  152. {
  153. ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "alice",
  154. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesNote, "Friend from work")}},
  155. },
  156. },
  157. want: []BuddyGroup{
  158. {
  159. Name: "Buddies",
  160. ID: 100,
  161. Buddies: []BuddyInfo{offlineWebAPIBuddy("alice", "alice")},
  162. },
  163. },
  164. },
  165. {
  166. name: "multiple groups in root order",
  167. fb: []wire.FeedbagItem{
  168. {
  169. Name: "", GroupID: 0, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  170. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{100, 200})}},
  171. },
  172. {Name: "Buddies", GroupID: 100, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  173. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{1})}}},
  174. {Name: "Family", GroupID: 200, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  175. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{2})}}},
  176. {ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "friend1", TLVLBlock: wire.TLVLBlock{}},
  177. {ItemID: 2, ClassID: wire.FeedbagClassIdBuddy, GroupID: 200, Name: "mom", TLVLBlock: wire.TLVLBlock{}},
  178. },
  179. want: []BuddyGroup{
  180. {
  181. Name: "Buddies",
  182. ID: 100,
  183. Buddies: []BuddyInfo{offlineWebAPIBuddy("friend1", "friend1")},
  184. },
  185. {
  186. Name: "Family",
  187. ID: 200,
  188. Buddies: []BuddyInfo{offlineWebAPIBuddy("mom", "mom")},
  189. },
  190. },
  191. },
  192. {
  193. name: "buddy order follows group order TLV not feedbag slice order",
  194. fb: []wire.FeedbagItem{
  195. {
  196. Name: "", GroupID: 0, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  197. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{100})}},
  198. },
  199. {Name: "Buddies", GroupID: 100, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  200. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{2, 1})}}},
  201. {ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "firstInSlice", TLVLBlock: wire.TLVLBlock{}},
  202. {ItemID: 2, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "secondInSlice", TLVLBlock: wire.TLVLBlock{}},
  203. },
  204. want: []BuddyGroup{
  205. {
  206. Name: "Buddies",
  207. ID: 100,
  208. Buddies: []BuddyInfo{
  209. offlineWebAPIBuddy("secondinslice", "secondInSlice"),
  210. offlineWebAPIBuddy("firstinslice", "firstInSlice"),
  211. },
  212. },
  213. },
  214. },
  215. {
  216. name: "group order follows root order TLV not feedbag slice order",
  217. fb: []wire.FeedbagItem{
  218. {
  219. Name: "", GroupID: 0, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  220. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{200, 100})}},
  221. },
  222. {Name: "Family", GroupID: 200, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  223. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{2})}}},
  224. {Name: "Buddies", GroupID: 100, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  225. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{1})}}},
  226. {ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "inBuddies", TLVLBlock: wire.TLVLBlock{}},
  227. {ItemID: 2, ClassID: wire.FeedbagClassIdBuddy, GroupID: 200, Name: "inFamily", TLVLBlock: wire.TLVLBlock{}},
  228. },
  229. want: []BuddyGroup{
  230. {
  231. Name: "Family",
  232. ID: 200,
  233. Buddies: []BuddyInfo{offlineWebAPIBuddy("infamily", "inFamily")},
  234. },
  235. {
  236. Name: "Buddies",
  237. ID: 100,
  238. Buddies: []BuddyInfo{offlineWebAPIBuddy("inbuddies", "inBuddies")},
  239. },
  240. },
  241. },
  242. {
  243. name: "unnamed group becomes Buddies",
  244. fb: []wire.FeedbagItem{
  245. {
  246. Name: "", GroupID: 0, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  247. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{100})}},
  248. },
  249. {Name: "", GroupID: 100, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  250. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{1})}}},
  251. {ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "solo", TLVLBlock: wire.TLVLBlock{}},
  252. },
  253. want: []BuddyGroup{
  254. {
  255. Name: "Buddies",
  256. ID: 100,
  257. Buddies: []BuddyInfo{offlineWebAPIBuddy("solo", "solo")},
  258. },
  259. },
  260. },
  261. }
  262. for _, tt := range tests {
  263. t.Run(tt.name, func(t *testing.T) {
  264. fs := newMockFeedbagService(t)
  265. // The locate query returns an error, so every buddy resolves to
  266. // offline. This keeps the focus on feedbag -> group conversion.
  267. ls := newMockLocateService(t)
  268. ls.EXPECT().UserInfoQuery(mock.Anything, mock.Anything, mock.Anything, mock.Anything).
  269. Return(wire.SNACMessage{}, errors.New("offline")).Maybe()
  270. if tt.fbErr != nil {
  271. fs.EXPECT().Query(mock.Anything, mock.Anything, mock.Anything).Return(wire.SNACMessage{}, tt.fbErr).Once()
  272. } else {
  273. fs.EXPECT().Query(mock.Anything, mock.Anything, mock.Anything).Return(
  274. wire.SNACMessage{Body: wire.SNAC_0x13_0x06_FeedbagReply{Items: tt.fb}}, nil,
  275. ).Once()
  276. }
  277. m := NewBuddyListManager(fs, ls, newTestIconSource(t), slog.Default())
  278. sess := &Session{
  279. ScreenName: state.DisplayScreenName(owner.String()),
  280. OSCARSession: state.NewSession().AddInstance(),
  281. }
  282. got, err := m.GetBuddyListForUser(ctx, sess)
  283. if tt.wantErr != "" {
  284. assert.ErrorContains(t, err, tt.wantErr)
  285. assert.Nil(t, got)
  286. return
  287. }
  288. assert.NoError(t, err)
  289. assert.Equal(t, tt.want, got)
  290. })
  291. }
  292. }
  293. func TestBuddyListManager_GetBuddyListForUser_DisplayIDFromLocateReply(t *testing.T) {
  294. // Feedbag buddy names are stored normalized, so an online buddy's display
  295. // name can only come from the locate reply's user info.
  296. ctx := context.Background()
  297. fb := []wire.FeedbagItem{
  298. {
  299. Name: "", GroupID: 0, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  300. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{100})}},
  301. },
  302. {Name: "Buddies", GroupID: 100, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  303. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{1})}}},
  304. {ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "mikekelly"},
  305. }
  306. fs := newMockFeedbagService(t)
  307. fs.EXPECT().Query(mock.Anything, mock.Anything, mock.Anything).Return(
  308. wire.SNACMessage{Body: wire.SNAC_0x13_0x06_FeedbagReply{Items: fb}}, nil,
  309. ).Once()
  310. ls := newMockLocateService(t)
  311. ls.EXPECT().UserInfoQuery(mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(
  312. wire.SNACMessage{Body: wire.SNAC_0x02_0x06_LocateUserInfoReply{
  313. TLVUserInfo: wire.TLVUserInfo{ScreenName: "Mike Kelly"},
  314. }}, nil,
  315. ).Once()
  316. m := NewBuddyListManager(fs, ls, newTestIconSource(t), slog.Default())
  317. sess := &Session{
  318. ScreenName: state.DisplayScreenName("listowner"),
  319. OSCARSession: state.NewSession().AddInstance(),
  320. }
  321. got, err := m.GetBuddyListForUser(ctx, sess)
  322. require.NoError(t, err)
  323. require.Len(t, got, 1)
  324. require.Len(t, got[0].Buddies, 1)
  325. assert.Equal(t, "mikekelly", got[0].Buddies[0].AimID)
  326. assert.Equal(t, "Mike Kelly", got[0].Buddies[0].DisplayID)
  327. assert.Equal(t, "online", got[0].Buddies[0].State)
  328. }
  329. // Icons are published only for online, non-blocking buddies: an online buddy with
  330. // an icon gets a content-addressed URL, an online buddy without one gets the
  331. // hash-less placeholder URL, and an offline (or blocking) buddy gets no icon and
  332. // is never even looked up, so neither their icon nor its hash leaks.
  333. func TestBuddyListManager_GetBuddyListForUser_PublishesBuddyIcons(t *testing.T) {
  334. ctx := context.Background()
  335. fb := []wire.FeedbagItem{
  336. {Name: "", GroupID: 0, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  337. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{100})}}},
  338. {Name: "Buddies", GroupID: 100, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  339. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{1, 2, 3})}}},
  340. {ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "onlineicon"},
  341. {ItemID: 2, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "offlinebud"},
  342. {ItemID: 3, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "onlinenoicon"},
  343. }
  344. fs := newMockFeedbagService(t)
  345. fs.EXPECT().Query(mock.Anything, mock.Anything, mock.Anything).Return(
  346. wire.SNACMessage{Body: wire.SNAC_0x13_0x06_FeedbagReply{Items: fb}}, nil,
  347. ).Once()
  348. online := func(name string) wire.SNACMessage {
  349. return wire.SNACMessage{Body: wire.SNAC_0x02_0x06_LocateUserInfoReply{
  350. TLVUserInfo: wire.TLVUserInfo{ScreenName: name},
  351. }}
  352. }
  353. locateFor := func(name string) any {
  354. return mock.MatchedBy(func(q wire.SNAC_0x02_0x05_LocateUserInfoQuery) bool { return q.ScreenName == name })
  355. }
  356. ls := newMockLocateService(t)
  357. ls.EXPECT().UserInfoQuery(mock.Anything, mock.Anything, mock.Anything, locateFor("onlineicon")).
  358. Return(online("onlineicon"), nil).Once()
  359. ls.EXPECT().UserInfoQuery(mock.Anything, mock.Anything, mock.Anything, locateFor("onlinenoicon")).
  360. Return(online("onlinenoicon"), nil).Once()
  361. ls.EXPECT().UserInfoQuery(mock.Anything, mock.Anything, mock.Anything, locateFor("offlinebud")).
  362. Return(wire.SNACMessage{}, errors.New("offline")).Once()
  363. iconRetriever := newMockBuddyIconRetriever(t)
  364. iconRetriever.EXPECT().BuddyIconMetadata(mock.Anything, state.NewIdentScreenName("onlineicon")).
  365. Return(bartID([]byte{0xab, 0xcd}), nil).Once()
  366. iconRetriever.EXPECT().BuddyIconMetadata(mock.Anything, state.NewIdentScreenName("onlinenoicon")).
  367. Return(nil, nil).Once()
  368. m := NewBuddyListManager(fs, ls, BuddyIconSource{
  369. IconRetriever: iconRetriever,
  370. Logger: slog.Default(),
  371. }, slog.Default())
  372. sess := &Session{
  373. ScreenName: state.DisplayScreenName("listowner"),
  374. OSCARSession: state.NewSession().AddInstance(),
  375. BaseURL: "http://api.example.com",
  376. }
  377. got, err := m.GetBuddyListForUser(ctx, sess)
  378. require.NoError(t, err)
  379. require.Len(t, got, 1)
  380. require.Len(t, got[0].Buddies, 3)
  381. // onlineicon: content-addressed URL carrying the icon hash.
  382. assert.Equal(t, "online", got[0].Buddies[0].State)
  383. assert.Equal(t,
  384. "http://api.example.com/expressions/get?t=onlineicon&type=buddyIcon&bartId=abcd",
  385. got[0].Buddies[0].BuddyIcon)
  386. // offlinebud: no icon, and its metadata is never queried.
  387. assert.Equal(t, "offline", got[0].Buddies[1].State)
  388. assert.Empty(t, got[0].Buddies[1].BuddyIcon)
  389. iconRetriever.AssertNotCalled(t, "BuddyIconMetadata", mock.Anything, state.NewIdentScreenName("offlinebud"))
  390. // onlinenoicon: hash-less placeholder URL so a cleared icon still propagates.
  391. assert.Equal(t, "online", got[0].Buddies[2].State)
  392. assert.Equal(t,
  393. "http://api.example.com/expressions/get?t=onlinenoicon&type=buddyIcon",
  394. got[0].Buddies[2].BuddyIcon)
  395. }
  396. // The feedbag service relays a session's own writes only to the owner's other
  397. // instances, so renaming a buddy from the web client produces no SNAC for that
  398. // session. Without an explicit invalidation, its cached aliases would keep serving
  399. // the old name and the next presence or IM event would rename the buddy back.
  400. func TestBuddyListManager_SetBuddyAttributeInFeedbag_InvalidatesAliasCache(t *testing.T) {
  401. ctx := context.Background()
  402. feedbag := func(alias string) []wire.FeedbagItem {
  403. buddy := wire.FeedbagItem{ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "mikekelly"}
  404. buddy.TLVLBlock = wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesAlias, alias)}}
  405. return []wire.FeedbagItem{
  406. {Name: "", GroupID: 0, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  407. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{100})}}},
  408. {Name: "Buddies", GroupID: 100, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  409. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{1})}}},
  410. buddy,
  411. }
  412. }
  413. fs := newMockFeedbagService(t)
  414. // Query 1: the alias cache loads. Query 2: SetBuddyAttributeInFeedbag reads the
  415. // feedbag it is about to rewrite. Query 3: the cache reloads post-invalidation,
  416. // now seeing the stored rename.
  417. fs.EXPECT().Query(mock.Anything, mock.Anything, mock.Anything).
  418. Return(wire.SNACMessage{Body: wire.SNAC_0x13_0x06_FeedbagReply{Items: feedbag("MICHAELKELLY")}}, nil).Twice()
  419. fs.EXPECT().UpsertItem(mock.Anything, mock.Anything, mock.Anything, mock.Anything).
  420. Return(&wire.SNACMessage{}, nil).Once()
  421. fs.EXPECT().Query(mock.Anything, mock.Anything, mock.Anything).
  422. Return(wire.SNACMessage{Body: wire.SNAC_0x13_0x06_FeedbagReply{Items: feedbag("MIKE")}}, nil).Once()
  423. m := NewBuddyListManager(fs, newMockLocateService(t), newTestIconSource(t), slog.Default())
  424. sess := &Session{
  425. ScreenName: state.DisplayScreenName("listowner"),
  426. OSCARSession: state.NewSession().AddInstance(),
  427. }
  428. sess.BuddyAliasLoader = func(ctx context.Context) (map[string]string, error) {
  429. return LookupBuddyAliases(ctx, fs, sess.OSCARSession)
  430. }
  431. require.Equal(t, "MICHAELKELLY", sess.Aliases(ctx)["mikekelly"])
  432. resultCode, err := m.SetBuddyAttributeInFeedbag(ctx, sess, "mikekelly", "MIKE")
  433. require.NoError(t, err)
  434. require.Equal(t, "success", resultCode)
  435. assert.Equal(t, "MIKE", sess.Aliases(ctx)["mikekelly"])
  436. }
  437. func TestFeedbagGroupMatchesRequested(t *testing.T) {
  438. assert.True(t, feedbagGroupMatchesRequested("Buddies", "Buddies"))
  439. assert.True(t, feedbagGroupMatchesRequested("", "Buddies"))
  440. assert.True(t, feedbagGroupMatchesRequested(" ", "Buddies"))
  441. assert.True(t, feedbagGroupMatchesRequested("Friends", "friends"))
  442. assert.False(t, feedbagGroupMatchesRequested("", "Friends"))
  443. }
  444. func TestStoredGroupNameForRequest(t *testing.T) {
  445. items := []wire.FeedbagItem{
  446. {ItemID: 1, ClassID: wire.FeedbagClassIdGroup, Name: "", GroupID: 1},
  447. {ItemID: 2, ClassID: wire.FeedbagClassIdBuddy, Name: "jon", GroupID: 1},
  448. }
  449. st, ok := storedGroupNameForRequest(items, "Buddies")
  450. assert.True(t, ok)
  451. assert.Equal(t, "", st)
  452. items2 := []wire.FeedbagItem{
  453. {ItemID: 1, ClassID: wire.FeedbagClassIdGroup, Name: "Friends", GroupID: 2},
  454. }
  455. st2, ok2 := storedGroupNameForRequest(items2, "Friends")
  456. assert.True(t, ok2)
  457. assert.Equal(t, "Friends", st2)
  458. }