buddy_list_manager_test.go 19 KB

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