mgmt_api_icq_profile_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package http
  2. import (
  3. "context"
  4. "encoding/json"
  5. "log/slog"
  6. "net/http"
  7. "net/http/httptest"
  8. "strings"
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/mk6i/open-oscar-server/state"
  12. )
  13. // icqProfileStore is an in-memory ICQProfileManager holding one ICQ user.
  14. type icqProfileStore struct {
  15. user *state.User
  16. basic *state.ICQBasicInfo
  17. }
  18. func (s *icqProfileStore) User(_ context.Context, sn state.IdentScreenName) (*state.User, error) {
  19. if s.user.IdentScreenName != sn {
  20. return nil, nil
  21. }
  22. return s.user, nil
  23. }
  24. func (s *icqProfileStore) SetBasicInfo(_ context.Context, _ state.IdentScreenName, data state.ICQBasicInfo) error {
  25. s.basic = &data
  26. return nil
  27. }
  28. func (s *icqProfileStore) SetMoreInfo(context.Context, state.IdentScreenName, state.ICQMoreInfo) error {
  29. return nil
  30. }
  31. func (s *icqProfileStore) SetWorkInfo(context.Context, state.IdentScreenName, state.ICQWorkInfo) error {
  32. return nil
  33. }
  34. func (s *icqProfileStore) SetUserNotes(context.Context, state.IdentScreenName, state.ICQUserNotes) error {
  35. return nil
  36. }
  37. func (s *icqProfileStore) SetInterests(context.Context, state.IdentScreenName, state.ICQInterests) error {
  38. return nil
  39. }
  40. func (s *icqProfileStore) SetAffiliations(context.Context, state.IdentScreenName, state.ICQAffiliations) error {
  41. return nil
  42. }
  43. func (s *icqProfileStore) SetPermissions(context.Context, state.IdentScreenName, state.ICQPermissions) error {
  44. return nil
  45. }
  46. func (s *icqProfileStore) SetICQInfo(context.Context, state.IdentScreenName, state.ICQInfo) error {
  47. return nil
  48. }
  49. func TestPutICQProfileHandler_NicknameLength(t *testing.T) {
  50. cases := []struct {
  51. name string
  52. nickname string
  53. }{
  54. {name: "cyrillic, 18 characters but 32 bytes", nickname: "<Деффачка в кедах>"},
  55. {name: "emoji", nickname: "Jimm 😎🔥"},
  56. {name: "longer than a client UI allows", nickname: strings.Repeat("a", 40)},
  57. }
  58. for _, tc := range cases {
  59. t.Run(tc.name, func(t *testing.T) {
  60. sn := state.NewIdentScreenName("100003")
  61. store := &icqProfileStore{user: &state.User{IdentScreenName: sn, DisplayScreenName: "100003", IsICQ: true}}
  62. body, err := json.Marshal(map[string]any{"basic_info": map[string]any{"nickname": tc.nickname}})
  63. assert.NoError(t, err)
  64. r := httptest.NewRequest(http.MethodPut, "/user/100003/icq", strings.NewReader(string(body)))
  65. r.SetPathValue("screenname", "100003")
  66. w := httptest.NewRecorder()
  67. putICQProfileHandler(w, r, store, slog.Default())
  68. assert.Equal(t, http.StatusNoContent, w.Code, w.Body.String())
  69. if assert.NotNil(t, store.basic) {
  70. assert.Equal(t, tc.nickname, store.basic.Nickname)
  71. }
  72. })
  73. }
  74. }
  75. func TestPutICQProfileHandler_OtherFieldsStillLimited(t *testing.T) {
  76. sn := state.NewIdentScreenName("100003")
  77. store := &icqProfileStore{user: &state.User{IdentScreenName: sn, DisplayScreenName: "100003", IsICQ: true}}
  78. body := `{"basic_info":{"first_name":"` + strings.Repeat("a", 65) + `"}}`
  79. r := httptest.NewRequest(http.MethodPut, "/user/100003/icq", strings.NewReader(body))
  80. r.SetPathValue("screenname", "100003")
  81. w := httptest.NewRecorder()
  82. putICQProfileHandler(w, r, store, slog.Default())
  83. assert.Equal(t, http.StatusBadRequest, w.Code)
  84. assert.Contains(t, w.Body.String(), "basic_info.first_name")
  85. assert.Nil(t, store.basic)
  86. }