buddy_list_manager_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. Buddies: []BuddyInfo{
  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: []BuddyGroup{
  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: []BuddyInfo{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: []BuddyGroup{
  134. {
  135. Name: "Buddies",
  136. Buddies: []BuddyInfo{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: []BuddyGroup{
  155. {
  156. Name: "Buddies",
  157. Buddies: []BuddyInfo{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: []BuddyGroup{
  176. {
  177. Name: "Buddies",
  178. Buddies: []BuddyInfo{offlineWebAPIBuddy("friend1", "friend1")},
  179. },
  180. {
  181. Name: "Family",
  182. Buddies: []BuddyInfo{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: []BuddyGroup{
  199. {
  200. Name: "Buddies",
  201. Buddies: []BuddyInfo{
  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: []BuddyGroup{
  223. {
  224. Name: "Family",
  225. Buddies: []BuddyInfo{offlineWebAPIBuddy("infamily", "inFamily")},
  226. },
  227. {
  228. Name: "Buddies",
  229. Buddies: []BuddyInfo{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: []BuddyGroup{
  245. {
  246. Name: "Buddies",
  247. Buddies: []BuddyInfo{offlineWebAPIBuddy("solo", "solo")},
  248. },
  249. },
  250. },
  251. }
  252. for _, tt := range tests {
  253. t.Run(tt.name, func(t *testing.T) {
  254. fs := newMockFeedbagService(t)
  255. // The locate query returns an error, so every buddy resolves to
  256. // offline. This keeps the focus on feedbag -> group conversion.
  257. ls := newMockLocateService(t)
  258. ls.EXPECT().UserInfoQuery(mock.Anything, mock.Anything, mock.Anything, mock.Anything).
  259. Return(wire.SNACMessage{}, errors.New("offline")).Maybe()
  260. if tt.fbErr != nil {
  261. fs.EXPECT().Query(mock.Anything, mock.Anything, mock.Anything).Return(wire.SNACMessage{}, tt.fbErr).Once()
  262. } else {
  263. fs.EXPECT().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(t), slog.Default())
  268. sess := &Session{
  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. return
  277. }
  278. assert.NoError(t, err)
  279. assert.Equal(t, tt.want, got)
  280. })
  281. }
  282. }
  283. func TestBuddyListManager_GetBuddyListForUser_DisplayIDFromLocateReply(t *testing.T) {
  284. // Feedbag buddy names are stored normalized, so an online buddy's display
  285. // name can only come from the locate reply's user info.
  286. ctx := context.Background()
  287. fb := []wire.FeedbagItem{
  288. {
  289. Name: "", GroupID: 0, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  290. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{100})}},
  291. },
  292. {Name: "Buddies", GroupID: 100, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  293. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{1})}}},
  294. {ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "mikekelly"},
  295. }
  296. fs := newMockFeedbagService(t)
  297. fs.EXPECT().Query(mock.Anything, mock.Anything, mock.Anything).Return(
  298. wire.SNACMessage{Body: wire.SNAC_0x13_0x06_FeedbagReply{Items: fb}}, nil,
  299. ).Once()
  300. ls := newMockLocateService(t)
  301. ls.EXPECT().UserInfoQuery(mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(
  302. wire.SNACMessage{Body: wire.SNAC_0x02_0x06_LocateUserInfoReply{
  303. TLVUserInfo: wire.TLVUserInfo{ScreenName: "Mike Kelly"},
  304. }}, nil,
  305. ).Once()
  306. m := NewBuddyListManager(fs, ls, newTestIconSource(t), slog.Default())
  307. sess := &Session{
  308. ScreenName: state.DisplayScreenName("listowner"),
  309. OSCARSession: state.NewSession().AddInstance(),
  310. }
  311. got, err := m.GetBuddyListForUser(ctx, sess)
  312. require.NoError(t, err)
  313. require.Len(t, got, 1)
  314. require.Len(t, got[0].Buddies, 1)
  315. assert.Equal(t, "mikekelly", got[0].Buddies[0].AimID)
  316. assert.Equal(t, "Mike Kelly", got[0].Buddies[0].DisplayID)
  317. assert.Equal(t, "online", got[0].Buddies[0].State)
  318. }
  319. // Icons are published only for online, non-blocking buddies: an online buddy with
  320. // an icon gets a content-addressed URL, an online buddy without one gets the
  321. // hash-less placeholder URL, and an offline (or blocking) buddy gets no icon and
  322. // is never even looked up, so neither their icon nor its hash leaks.
  323. func TestBuddyListManager_GetBuddyListForUser_PublishesBuddyIcons(t *testing.T) {
  324. ctx := context.Background()
  325. fb := []wire.FeedbagItem{
  326. {Name: "", GroupID: 0, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  327. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{100})}}},
  328. {Name: "Buddies", GroupID: 100, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  329. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{1, 2, 3})}}},
  330. {ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "onlineicon"},
  331. {ItemID: 2, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "offlinebud"},
  332. {ItemID: 3, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "onlinenoicon"},
  333. }
  334. fs := newMockFeedbagService(t)
  335. fs.EXPECT().Query(mock.Anything, mock.Anything, mock.Anything).Return(
  336. wire.SNACMessage{Body: wire.SNAC_0x13_0x06_FeedbagReply{Items: fb}}, nil,
  337. ).Once()
  338. online := func(name string) wire.SNACMessage {
  339. return wire.SNACMessage{Body: wire.SNAC_0x02_0x06_LocateUserInfoReply{
  340. TLVUserInfo: wire.TLVUserInfo{ScreenName: name},
  341. }}
  342. }
  343. locateFor := func(name string) any {
  344. return mock.MatchedBy(func(q wire.SNAC_0x02_0x05_LocateUserInfoQuery) bool { return q.ScreenName == name })
  345. }
  346. ls := newMockLocateService(t)
  347. ls.EXPECT().UserInfoQuery(mock.Anything, mock.Anything, mock.Anything, locateFor("onlineicon")).
  348. Return(online("onlineicon"), nil).Once()
  349. ls.EXPECT().UserInfoQuery(mock.Anything, mock.Anything, mock.Anything, locateFor("onlinenoicon")).
  350. Return(online("onlinenoicon"), nil).Once()
  351. ls.EXPECT().UserInfoQuery(mock.Anything, mock.Anything, mock.Anything, locateFor("offlinebud")).
  352. Return(wire.SNACMessage{}, errors.New("offline")).Once()
  353. iconRetriever := newMockBuddyIconRetriever(t)
  354. iconRetriever.EXPECT().BuddyIconMetadata(mock.Anything, state.NewIdentScreenName("onlineicon")).
  355. Return(bartID([]byte{0xab, 0xcd}), nil).Once()
  356. iconRetriever.EXPECT().BuddyIconMetadata(mock.Anything, state.NewIdentScreenName("onlinenoicon")).
  357. Return(nil, nil).Once()
  358. m := NewBuddyListManager(fs, ls, BuddyIconSource{
  359. IconRetriever: iconRetriever,
  360. Logger: slog.Default(),
  361. }, slog.Default())
  362. sess := &Session{
  363. ScreenName: state.DisplayScreenName("listowner"),
  364. OSCARSession: state.NewSession().AddInstance(),
  365. BaseURL: "http://api.example.com",
  366. }
  367. got, err := m.GetBuddyListForUser(ctx, sess)
  368. require.NoError(t, err)
  369. require.Len(t, got, 1)
  370. require.Len(t, got[0].Buddies, 3)
  371. // onlineicon: content-addressed URL carrying the icon hash.
  372. assert.Equal(t, "online", got[0].Buddies[0].State)
  373. assert.Equal(t,
  374. "http://api.example.com/expressions/get?t=onlineicon&type=buddyIcon&bartId=abcd",
  375. got[0].Buddies[0].BuddyIcon)
  376. // offlinebud: no icon, and its metadata is never queried.
  377. assert.Equal(t, "offline", got[0].Buddies[1].State)
  378. assert.Empty(t, got[0].Buddies[1].BuddyIcon)
  379. iconRetriever.AssertNotCalled(t, "BuddyIconMetadata", mock.Anything, state.NewIdentScreenName("offlinebud"))
  380. // onlinenoicon: hash-less placeholder URL so a cleared icon still propagates.
  381. assert.Equal(t, "online", got[0].Buddies[2].State)
  382. assert.Equal(t,
  383. "http://api.example.com/expressions/get?t=onlinenoicon&type=buddyIcon",
  384. got[0].Buddies[2].BuddyIcon)
  385. }
  386. // The feedbag service relays a session's own writes only to the owner's other
  387. // instances, so renaming a buddy from the web client produces no SNAC for that
  388. // session. Without an explicit invalidation, its cached aliases would keep serving
  389. // the old name and the next presence or IM event would rename the buddy back.
  390. func TestBuddyListManager_SetBuddyAttributeInFeedbag_InvalidatesAliasCache(t *testing.T) {
  391. ctx := context.Background()
  392. feedbag := func(alias string) []wire.FeedbagItem {
  393. buddy := wire.FeedbagItem{ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "mikekelly"}
  394. buddy.TLVLBlock = wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesAlias, alias)}}
  395. return []wire.FeedbagItem{
  396. {Name: "", GroupID: 0, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  397. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{100})}}},
  398. {Name: "Buddies", GroupID: 100, ItemID: 0, ClassID: wire.FeedbagClassIdGroup,
  399. TLVLBlock: wire.TLVLBlock{TLVList: wire.TLVList{wire.NewTLVBE(wire.FeedbagAttributesOrder, []uint16{1})}}},
  400. buddy,
  401. }
  402. }
  403. fs := newMockFeedbagService(t)
  404. // Query 1: the alias cache loads. Query 2: SetBuddyAttributeInFeedbag reads the
  405. // feedbag it is about to rewrite. Query 3: the cache reloads post-invalidation,
  406. // now seeing the stored rename.
  407. fs.EXPECT().Query(mock.Anything, mock.Anything, mock.Anything).
  408. Return(wire.SNACMessage{Body: wire.SNAC_0x13_0x06_FeedbagReply{Items: feedbag("MICHAELKELLY")}}, nil).Twice()
  409. fs.EXPECT().UpsertItem(mock.Anything, mock.Anything, mock.Anything, mock.Anything).
  410. Return(&wire.SNACMessage{}, nil).Once()
  411. fs.EXPECT().Query(mock.Anything, mock.Anything, mock.Anything).
  412. Return(wire.SNACMessage{Body: wire.SNAC_0x13_0x06_FeedbagReply{Items: feedbag("MIKE")}}, nil).Once()
  413. m := NewBuddyListManager(fs, newMockLocateService(t), newTestIconSource(t), slog.Default())
  414. sess := &Session{
  415. ScreenName: state.DisplayScreenName("listowner"),
  416. OSCARSession: state.NewSession().AddInstance(),
  417. }
  418. sess.BuddyAliasLoader = func(ctx context.Context) (map[string]string, error) {
  419. return LookupBuddyAliases(ctx, fs, sess.OSCARSession)
  420. }
  421. require.Equal(t, "MICHAELKELLY", sess.Aliases(ctx)["mikekelly"])
  422. resultCode, err := m.SetBuddyAttributeInFeedbag(ctx, sess, "mikekelly", "MIKE")
  423. require.NoError(t, err)
  424. require.Equal(t, "success", resultCode)
  425. assert.Equal(t, "MIKE", sess.Aliases(ctx)["mikekelly"])
  426. }
  427. func TestFeedbagGroupMatchesRequested(t *testing.T) {
  428. assert.True(t, feedbagGroupMatchesRequested("Buddies", "Buddies"))
  429. assert.True(t, feedbagGroupMatchesRequested("", "Buddies"))
  430. assert.True(t, feedbagGroupMatchesRequested(" ", "Buddies"))
  431. assert.True(t, feedbagGroupMatchesRequested("Friends", "friends"))
  432. assert.False(t, feedbagGroupMatchesRequested("", "Friends"))
  433. }
  434. func TestStoredGroupNameForRequest(t *testing.T) {
  435. items := []wire.FeedbagItem{
  436. {ItemID: 1, ClassID: wire.FeedbagClassIdGroup, Name: "", GroupID: 1},
  437. {ItemID: 2, ClassID: wire.FeedbagClassIdBuddy, Name: "jon", GroupID: 1},
  438. }
  439. st, ok := storedGroupNameForRequest(items, "Buddies")
  440. assert.True(t, ok)
  441. assert.Equal(t, "", st)
  442. items2 := []wire.FeedbagItem{
  443. {ItemID: 1, ClassID: wire.FeedbagClassIdGroup, Name: "Friends", GroupID: 2},
  444. }
  445. st2, ok2 := storedGroupNameForRequest(items2, "Friends")
  446. assert.True(t, ok2)
  447. assert.Equal(t, "Friends", st2)
  448. }