user_lookup_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package foodgroup
  2. import (
  3. "context"
  4. "io"
  5. "testing"
  6. "github.com/mk6i/open-oscar-server/state"
  7. "github.com/mk6i/open-oscar-server/wire"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestUserLookupService_FindByEmail(t *testing.T) {
  11. cases := []struct {
  12. // name is the unit test name
  13. name string
  14. // inputSNAC is the SNAC sent by the sender client
  15. inputSNAC wire.SNACMessage
  16. // expectSNACFrame is the SNAC frame sent from the server to the recipient
  17. // client
  18. expectOutput wire.SNACMessage
  19. // mockParams is the list of params sent to mocks that satisfy this
  20. // method's dependencies
  21. mockParams mockParams
  22. // expectErr is the expected error returned by the handler
  23. expectErr error
  24. }{
  25. {
  26. name: "search by email address - results found",
  27. inputSNAC: wire.SNACMessage{
  28. Frame: wire.SNACFrame{
  29. RequestID: 1234,
  30. },
  31. Body: wire.SNAC_0x0A_0x02_UserLookupFindByEmail{
  32. Email: []byte("user@aol.com"),
  33. },
  34. },
  35. expectOutput: wire.SNACMessage{
  36. Frame: wire.SNACFrame{
  37. FoodGroup: wire.UserLookup,
  38. SubGroup: wire.UserLookupFindReply,
  39. RequestID: 1234,
  40. },
  41. Body: wire.SNAC_0x0A_0x03_UserLookupFindReply{
  42. TLVRestBlock: wire.TLVRestBlock{
  43. TLVList: wire.TLVList{
  44. wire.NewTLVBE(wire.UserLookupTLVEmailAddress, "ChattingChuck"),
  45. },
  46. },
  47. },
  48. },
  49. mockParams: mockParams{
  50. profileManagerParams: profileManagerParams{
  51. findByAIMEmailParams: findByAIMEmailParams{
  52. {
  53. email: "user@aol.com",
  54. result: state.User{
  55. DisplayScreenName: "ChattingChuck",
  56. },
  57. },
  58. },
  59. },
  60. },
  61. },
  62. {
  63. name: "search by email address - no results found",
  64. inputSNAC: wire.SNACMessage{
  65. Frame: wire.SNACFrame{
  66. RequestID: 1234,
  67. },
  68. Body: wire.SNAC_0x0A_0x02_UserLookupFindByEmail{
  69. Email: []byte("user@aol.com"),
  70. },
  71. },
  72. expectOutput: wire.SNACMessage{
  73. Frame: wire.SNACFrame{
  74. FoodGroup: wire.UserLookup,
  75. SubGroup: wire.UserLookupErr,
  76. RequestID: 1234,
  77. },
  78. Body: wire.UserLookupErrNoUserFound,
  79. },
  80. mockParams: mockParams{
  81. profileManagerParams: profileManagerParams{
  82. findByAIMEmailParams: findByAIMEmailParams{
  83. {
  84. email: "user@aol.com",
  85. err: state.ErrNoUser,
  86. },
  87. },
  88. },
  89. },
  90. },
  91. {
  92. name: "search by email address - search error",
  93. inputSNAC: wire.SNACMessage{
  94. Frame: wire.SNACFrame{
  95. RequestID: 1234,
  96. },
  97. Body: wire.SNAC_0x0A_0x02_UserLookupFindByEmail{
  98. Email: []byte("user@aol.com"),
  99. },
  100. },
  101. expectOutput: wire.SNACMessage{},
  102. expectErr: io.EOF,
  103. mockParams: mockParams{
  104. profileManagerParams: profileManagerParams{
  105. findByAIMEmailParams: findByAIMEmailParams{
  106. {
  107. email: "user@aol.com",
  108. err: io.EOF,
  109. },
  110. },
  111. },
  112. },
  113. },
  114. }
  115. for _, tc := range cases {
  116. t.Run(tc.name, func(t *testing.T) {
  117. profileManager := newMockProfileManager(t)
  118. for _, params := range tc.mockParams.findByAIMEmailParams {
  119. profileManager.EXPECT().
  120. FindByAIMEmail(matchContext(), params.email).
  121. Return(params.result, params.err)
  122. }
  123. svc := NewUserLookupService(profileManager)
  124. actual, err := svc.FindByEmail(context.Background(), tc.inputSNAC.Frame, tc.inputSNAC.Body.(wire.SNAC_0x0A_0x02_UserLookupFindByEmail))
  125. assert.ErrorIs(t, err, tc.expectErr)
  126. assert.Equal(t, tc.expectOutput, actual)
  127. })
  128. }
  129. }