buddy_icon_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/mk6i/open-oscar-server/state"
  10. "github.com/mk6i/open-oscar-server/wire"
  11. )
  12. // iconGIF stands in for buddy icon image bytes.
  13. var iconGIF = []byte{0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00}
  14. func bartID(hash []byte) *wire.BARTID {
  15. return &wire.BARTID{
  16. Type: wire.BARTTypesBuddyIcon,
  17. BARTInfo: wire.BARTInfo{Flags: wire.BARTFlagsCustom, Hash: hash},
  18. }
  19. }
  20. func TestBuddyIconSource_URL(t *testing.T) {
  21. tests := []struct {
  22. name string
  23. baseURL string
  24. id *wire.BARTID
  25. idErr error
  26. want string
  27. }{
  28. {
  29. name: "user with an icon gets a URL carrying the icon hash",
  30. baseURL: "http://api.example.com",
  31. id: bartID([]byte{0xde, 0xad, 0xbe, 0xef}),
  32. want: "http://api.example.com/expressions/get?t=mikekelly&type=buddyIcon&bartId=deadbeef",
  33. },
  34. {
  35. name: "user without an icon gets no URL",
  36. baseURL: "http://api.example.com",
  37. id: nil,
  38. want: "",
  39. },
  40. {
  41. name: "a cleared icon is not published",
  42. baseURL: "http://api.example.com",
  43. id: bartID(wire.GetClearIconHash()),
  44. want: "",
  45. },
  46. {
  47. name: "a lookup failure is not fatal, it just yields no icon",
  48. baseURL: "http://api.example.com",
  49. idErr: errors.New("db exploded"),
  50. want: "",
  51. },
  52. }
  53. for _, tt := range tests {
  54. t.Run(tt.name, func(t *testing.T) {
  55. iconRetriever := &MockBuddyIconRetriever{}
  56. iconRetriever.On("BuddyIconMetadata", mock.Anything, state.NewIdentScreenName("mikekelly")).
  57. Return(tt.id, tt.idErr).Once()
  58. s := BuddyIconSource{IconRetriever: iconRetriever, Logger: slog.Default()}
  59. got := s.URL(context.Background(), tt.baseURL, state.NewIdentScreenName("mikekelly"))
  60. assert.Equal(t, tt.want, got)
  61. iconRetriever.AssertExpectations(t)
  62. })
  63. }
  64. }
  65. func TestBuddyIconSource_URL_NoBaseURLSkipsLookup(t *testing.T) {
  66. // Callers with no origin to build an absolute URL against opt out by passing
  67. // an empty baseURL. That must not cost a lookup.
  68. iconRetriever := &MockBuddyIconRetriever{}
  69. s := BuddyIconSource{IconRetriever: iconRetriever, Logger: slog.Default()}
  70. got := s.URL(context.Background(), "", state.NewIdentScreenName("mikekelly"))
  71. assert.Empty(t, got)
  72. iconRetriever.AssertNotCalled(t, "BuddyIconMetadata", mock.Anything, mock.Anything)
  73. }
  74. func TestBuddyIconSource_URL_UsesNormalizedScreenName(t *testing.T) {
  75. // The URL targets the normalized screen name, which is what the endpoint
  76. // resolves against and what the client keys users by.
  77. iconRetriever := &MockBuddyIconRetriever{}
  78. iconRetriever.On("BuddyIconMetadata", mock.Anything, mock.Anything).
  79. Return(bartID([]byte{0x01}), nil).Once()
  80. s := BuddyIconSource{IconRetriever: iconRetriever, Logger: slog.Default()}
  81. got := s.URL(context.Background(), "http://api.example.com", state.NewIdentScreenName("Mike Kelly"))
  82. assert.Equal(t, "http://api.example.com/expressions/get?t=mikekelly&type=buddyIcon&bartId=01", got)
  83. }
  84. func TestBuddyIconSource_Image(t *testing.T) {
  85. hash := []byte{0xde, 0xad}
  86. iconRetriever := &MockBuddyIconRetriever{}
  87. iconRetriever.On("BuddyIconMetadata", mock.Anything, state.NewIdentScreenName("mikekelly")).
  88. Return(bartID(hash), nil).Once()
  89. // Image resolves the current hash from metadata, then downloads that exact
  90. // hash. The download query is keyed by hash; flags are irrelevant to the
  91. // lookup, so it carries only the type and hash.
  92. bartService := &MockBARTService{}
  93. bartService.On("RetrieveItem", mock.Anything, wire.SNACFrame{}, wire.SNAC_0x10_0x04_BARTDownloadQuery{
  94. ScreenName: "mikekelly",
  95. BARTID: wire.BARTID{Type: wire.BARTTypesBuddyIcon, BARTInfo: wire.BARTInfo{Hash: hash}},
  96. }).Return(wire.SNACMessage{
  97. Body: wire.SNAC_0x10_0x05_BARTDownloadReply{Data: iconGIF},
  98. }, nil).Once()
  99. s := BuddyIconSource{IconRetriever: iconRetriever, BARTService: bartService, Logger: slog.Default()}
  100. got, err := s.Image(context.Background(), state.NewIdentScreenName("mikekelly"))
  101. assert.NoError(t, err)
  102. assert.Equal(t, iconGIF, got)
  103. iconRetriever.AssertExpectations(t)
  104. bartService.AssertExpectations(t)
  105. }
  106. func TestBuddyIconSource_ImageForHash(t *testing.T) {
  107. hash := []byte{0xca, 0xfe}
  108. // ImageForHash downloads the requested hash directly, without consulting the
  109. // user's current icon metadata.
  110. bartService := &MockBARTService{}
  111. bartService.On("RetrieveItem", mock.Anything, wire.SNACFrame{}, wire.SNAC_0x10_0x04_BARTDownloadQuery{
  112. ScreenName: "mikekelly",
  113. BARTID: wire.BARTID{Type: wire.BARTTypesBuddyIcon, BARTInfo: wire.BARTInfo{Hash: hash}},
  114. }).Return(wire.SNACMessage{
  115. Body: wire.SNAC_0x10_0x05_BARTDownloadReply{Data: iconGIF},
  116. }, nil).Once()
  117. s := BuddyIconSource{BARTService: bartService, Logger: slog.Default()}
  118. got, err := s.ImageForHash(context.Background(), state.NewIdentScreenName("mikekelly"), hash)
  119. assert.NoError(t, err)
  120. assert.Equal(t, iconGIF, got)
  121. bartService.AssertExpectations(t)
  122. }
  123. func TestBuddyIconSource_ImageForHash_NotFound(t *testing.T) {
  124. bartService := &MockBARTService{}
  125. bartService.On("RetrieveItem", mock.Anything, mock.Anything, mock.Anything).
  126. Return(wire.SNACMessage{Body: wire.SNAC_0x10_0x05_BARTDownloadReply{}}, nil).Once()
  127. s := BuddyIconSource{BARTService: bartService, Logger: slog.Default()}
  128. _, err := s.ImageForHash(context.Background(), state.NewIdentScreenName("mikekelly"), []byte{0x01})
  129. assert.ErrorIs(t, err, ErrNoBuddyIcon)
  130. }
  131. func TestBuddyIconSource_PublishedURL(t *testing.T) {
  132. tests := []struct {
  133. name string
  134. baseURL string
  135. id *wire.BARTID
  136. idErr error
  137. want string
  138. }{
  139. {
  140. name: "an icon yields a content-addressed URL",
  141. baseURL: "http://api.example.com",
  142. id: bartID([]byte{0xde, 0xad, 0xbe, 0xef}),
  143. want: "http://api.example.com/expressions/get?t=mikekelly&type=buddyIcon&bartId=deadbeef",
  144. },
  145. {
  146. name: "no icon still yields a hash-less placeholder URL",
  147. baseURL: "http://api.example.com",
  148. id: nil,
  149. want: "http://api.example.com/expressions/get?t=mikekelly&type=buddyIcon",
  150. },
  151. {
  152. name: "a cleared icon yields the placeholder URL",
  153. baseURL: "http://api.example.com",
  154. id: bartID(wire.GetClearIconHash()),
  155. want: "http://api.example.com/expressions/get?t=mikekelly&type=buddyIcon",
  156. },
  157. {
  158. name: "a lookup failure yields no URL",
  159. baseURL: "http://api.example.com",
  160. idErr: errors.New("db exploded"),
  161. want: "",
  162. },
  163. {
  164. name: "no base URL yields no URL",
  165. baseURL: "",
  166. id: bartID([]byte{0x01}),
  167. want: "",
  168. },
  169. }
  170. for _, tt := range tests {
  171. t.Run(tt.name, func(t *testing.T) {
  172. iconRetriever := &MockBuddyIconRetriever{}
  173. iconRetriever.On("BuddyIconMetadata", mock.Anything, state.NewIdentScreenName("mikekelly")).
  174. Return(tt.id, tt.idErr).Maybe()
  175. s := BuddyIconSource{IconRetriever: iconRetriever, Logger: slog.Default()}
  176. got := s.PublishedURL(context.Background(), tt.baseURL, state.NewIdentScreenName("mikekelly"))
  177. assert.Equal(t, tt.want, got)
  178. })
  179. }
  180. }
  181. func TestBuddyIconSource_Image_NoIcon(t *testing.T) {
  182. iconRetriever := &MockBuddyIconRetriever{}
  183. iconRetriever.On("BuddyIconMetadata", mock.Anything, mock.Anything).Return(nil, nil).Once()
  184. bartService := &MockBARTService{}
  185. s := BuddyIconSource{IconRetriever: iconRetriever, BARTService: bartService, Logger: slog.Default()}
  186. _, err := s.Image(context.Background(), state.NewIdentScreenName("mikekelly"))
  187. assert.ErrorIs(t, err, ErrNoBuddyIcon)
  188. // No icon means there is nothing to ask BART for.
  189. bartService.AssertNotCalled(t, "RetrieveItem", mock.Anything, mock.Anything, mock.Anything)
  190. }
  191. func TestBuddyIconSource_URLForHash(t *testing.T) {
  192. // URLForHash never touches the retriever: the hash is supplied by the caller.
  193. s := BuddyIconSource{Logger: slog.Default()}
  194. sn := state.NewIdentScreenName("Mike Kelly")
  195. t.Run("hash yields the content-addressed URL", func(t *testing.T) {
  196. assert.Equal(t,
  197. "http://api.example.com/expressions/get?t=mikekelly&type=buddyIcon&bartId=deadbeef",
  198. s.URLForHash("http://api.example.com", sn, []byte{0xde, 0xad, 0xbe, 0xef}))
  199. })
  200. t.Run("no hash yields the placeholder URL", func(t *testing.T) {
  201. assert.Equal(t,
  202. "http://api.example.com/expressions/get?t=mikekelly&type=buddyIcon",
  203. s.URLForHash("http://api.example.com", sn, nil))
  204. })
  205. t.Run("empty baseURL opts out", func(t *testing.T) {
  206. assert.Empty(t, s.URLForHash("", sn, []byte{0x01}))
  207. })
  208. }
  209. func TestBuddyIconSource_Image_RetrieveFails(t *testing.T) {
  210. iconRetriever := &MockBuddyIconRetriever{}
  211. iconRetriever.On("BuddyIconMetadata", mock.Anything, mock.Anything).
  212. Return(bartID([]byte{0x01}), nil).Once()
  213. bartService := &MockBARTService{}
  214. bartService.On("RetrieveItem", mock.Anything, mock.Anything, mock.Anything).
  215. Return(wire.SNACMessage{}, errors.New("item missing")).Once()
  216. s := BuddyIconSource{IconRetriever: iconRetriever, BARTService: bartService, Logger: slog.Default()}
  217. _, err := s.Image(context.Background(), state.NewIdentScreenName("mikekelly"))
  218. assert.ErrorContains(t, err, "item missing")
  219. assert.NotErrorIs(t, err, ErrNoBuddyIcon)
  220. }