| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643 |
- package handler
- import (
- "testing"
- "time"
- "github.com/mkaminski/goaim/oscar"
- "github.com/mkaminski/goaim/state"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/mock"
- )
- func TestQueryHandler(t *testing.T) {
- cases := []struct {
- // name is the unit test name
- name string
- // screenName is the buddy list owner
- screenName string
- // feedbagItems is the list of items in user's buddy list
- feedbagItems []oscar.FeedbagItem
- // lastModified is the time the buddy list was last changed
- lastModified time.Time
- // inputSNAC is the SNAC sent by the sender client
- inputSNAC oscar.SNACMessage
- // expectOutput is the SNAC payload sent from the server to the
- // recipient client
- expectOutput oscar.SNACMessage
- }{
- {
- name: "retrieve empty feedbag",
- screenName: "user_screen_name",
- feedbagItems: []oscar.FeedbagItem{},
- lastModified: time.UnixMilli(0),
- inputSNAC: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- RequestID: 1234,
- },
- },
- expectOutput: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Feedbag,
- SubGroup: oscar.FeedbagReply,
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x06_FeedbagReply{
- Items: []oscar.FeedbagItem{},
- },
- },
- },
- {
- name: "retrieve feedbag with items",
- screenName: "user_screen_name",
- feedbagItems: []oscar.FeedbagItem{
- {
- Name: "buddy_1",
- },
- {
- Name: "buddy_2",
- },
- },
- lastModified: time.UnixMilli(1696472198082),
- inputSNAC: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- RequestID: 1234,
- },
- },
- expectOutput: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Feedbag,
- SubGroup: oscar.FeedbagReply,
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x06_FeedbagReply{
- Version: 0,
- Items: []oscar.FeedbagItem{
- {
- Name: "buddy_1",
- },
- {
- Name: "buddy_2",
- },
- },
- LastUpdate: uint32(time.UnixMilli(1696472198082).Unix()),
- },
- },
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- //
- // initialize dependencies
- //
- feedbagManager := newMockFeedbagManager(t)
- feedbagManager.EXPECT().
- Retrieve(tc.screenName).
- Return(tc.feedbagItems, nil).
- Maybe()
- feedbagManager.EXPECT().
- LastModified(tc.screenName).
- Return(tc.lastModified, nil).
- Maybe()
- //
- // send input SNAC
- //
- senderSession := newTestSession(tc.screenName)
- svc := FeedbagService{
- feedbagManager: feedbagManager,
- }
- outputSNAC, err := svc.QueryHandler(nil, senderSession, tc.inputSNAC.Frame)
- assert.NoError(t, err)
- assert.Equal(t, tc.expectOutput, outputSNAC)
- })
- }
- }
- func TestQueryIfModifiedHandler(t *testing.T) {
- cases := []struct {
- // name is the unit test name
- name string
- // screenName is the buddy list owner
- screenName string
- // feedbagItems is the list of items in user's buddy list
- feedbagItems []oscar.FeedbagItem
- // lastModified is the time the buddy list was last changed
- lastModified time.Time
- // inputSNAC is the SNAC sent by the sender client
- inputSNAC oscar.SNACMessage
- // expectOutput is the SNAC payload sent from the server to the
- // recipient client
- expectOutput oscar.SNACMessage
- }{
- {
- name: "retrieve empty feedbag",
- screenName: "user_screen_name",
- feedbagItems: []oscar.FeedbagItem{},
- lastModified: time.UnixMilli(0),
- inputSNAC: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x05_FeedbagQueryIfModified{
- LastUpdate: uint32(time.UnixMilli(100000).Unix()),
- },
- },
- expectOutput: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Feedbag,
- SubGroup: oscar.FeedbagReply,
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x06_FeedbagReply{
- Items: []oscar.FeedbagItem{},
- },
- },
- },
- {
- name: "retrieve feedbag with items",
- screenName: "user_screen_name",
- feedbagItems: []oscar.FeedbagItem{
- {
- Name: "buddy_1",
- },
- {
- Name: "buddy_2",
- },
- },
- lastModified: time.UnixMilli(200000),
- inputSNAC: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x05_FeedbagQueryIfModified{
- LastUpdate: uint32(time.UnixMilli(100000).Unix()),
- },
- },
- expectOutput: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Feedbag,
- SubGroup: oscar.FeedbagReply,
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x06_FeedbagReply{
- Version: 0,
- Items: []oscar.FeedbagItem{
- {
- Name: "buddy_1",
- },
- {
- Name: "buddy_2",
- },
- },
- LastUpdate: uint32(time.UnixMilli(200000).Unix()),
- },
- },
- },
- {
- name: "retrieve not-modified response",
- screenName: "user_screen_name",
- feedbagItems: []oscar.FeedbagItem{
- {
- Name: "buddy_1",
- },
- {
- Name: "buddy_2",
- },
- },
- lastModified: time.UnixMilli(100000),
- inputSNAC: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x05_FeedbagQueryIfModified{
- LastUpdate: uint32(time.UnixMilli(200000).Unix()),
- },
- },
- expectOutput: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Feedbag,
- SubGroup: oscar.FeedbagReplyNotModified,
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x05_FeedbagQueryIfModified{
- LastUpdate: uint32(time.UnixMilli(100000).Unix()),
- Count: 2,
- },
- },
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- //
- // initialize dependencies
- //
- feedbagManager := newMockFeedbagManager(t)
- feedbagManager.EXPECT().
- Retrieve(tc.screenName).
- Return(tc.feedbagItems, nil).
- Maybe()
- feedbagManager.EXPECT().
- LastModified(tc.screenName).
- Return(tc.lastModified, nil).
- Maybe()
- //
- // send input SNAC
- //
- senderSession := newTestSession(tc.screenName)
- svc := FeedbagService{
- feedbagManager: feedbagManager,
- }
- outputSNAC, err := svc.QueryIfModifiedHandler(nil, senderSession, tc.inputSNAC.Frame,
- tc.inputSNAC.Body.(oscar.SNAC_0x13_0x05_FeedbagQueryIfModified))
- assert.NoError(t, err)
- //
- // verify output
- //
- assert.Equal(t, tc.expectOutput, outputSNAC)
- })
- }
- }
- func TestInsertItemHandler(t *testing.T) {
- cases := []struct {
- // name is the unit test name
- name string
- // userSession is the session of the user managing buddy list
- userSession *state.Session
- // feedbagItems is the list of items in user's buddy list
- feedbagItems []oscar.FeedbagItem
- // inputSNAC is the SNAC sent by the sender client
- inputSNAC oscar.SNACMessage
- // screenNameLookups is the list of user's online buddies
- screenNameLookups map[string]struct {
- sess *state.Session
- err error
- }
- // clientResponse is the message returned to the client
- clientResponse oscar.SNACMessage
- // buddyMessages are events forwarded to buddy clients
- buddyMessages []struct {
- user string
- msg oscar.SNACMessage
- }
- }{
- {
- name: "user adds 2 online buddies, expect OK response",
- userSession: newTestSession("user_screen_name"),
- inputSNAC: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x08_FeedbagInsertItem{
- Items: []oscar.FeedbagItem{
- {
- ClassID: 2,
- Name: "buddy_1_online",
- },
- {
- ClassID: 2,
- Name: "buddy_2_online",
- },
- },
- },
- },
- screenNameLookups: map[string]struct {
- sess *state.Session
- err error
- }{
- "user_screen_name": {
- sess: newTestSession("user_screen_name", sessOptCannedSignonTime),
- },
- "buddy_1_online": {
- sess: newTestSession("buddy_1_online", sessOptCannedSignonTime),
- },
- "buddy_2_online": {
- sess: newTestSession("buddy_2_online", sessOptCannedSignonTime),
- },
- },
- clientResponse: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Feedbag,
- SubGroup: oscar.FeedbagStatus,
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x0E_FeedbagStatus{
- Results: []uint16{0x0000, 0x0000},
- },
- },
- buddyMessages: []struct {
- user string
- msg oscar.SNACMessage
- }{
- {
- user: "user_screen_name",
- msg: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Buddy,
- SubGroup: oscar.BuddyArrived,
- },
- Body: oscar.SNAC_0x03_0x0B_BuddyArrived{
- TLVUserInfo: oscar.TLVUserInfo{
- ScreenName: "buddy_1_online",
- TLVBlock: oscar.TLVBlock{
- TLVList: newTestSession("", sessOptCannedSignonTime).UserInfo(),
- },
- },
- },
- },
- },
- {
- user: "user_screen_name",
- msg: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Buddy,
- SubGroup: oscar.BuddyArrived,
- },
- Body: oscar.SNAC_0x03_0x0B_BuddyArrived{
- TLVUserInfo: oscar.TLVUserInfo{
- ScreenName: "buddy_2_online",
- TLVBlock: oscar.TLVBlock{
- TLVList: newTestSession("", sessOptCannedSignonTime).UserInfo(),
- },
- },
- },
- },
- },
- },
- },
- {
- name: "user adds an offline buddy, expect OK response and 0 buddy arrived events",
- userSession: newTestSession("user_screen_name"),
- inputSNAC: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x08_FeedbagInsertItem{
- Items: []oscar.FeedbagItem{
- {
- ClassID: 2,
- Name: "buddy_offline",
- },
- },
- },
- },
- screenNameLookups: map[string]struct {
- sess *state.Session
- err error
- }{
- "buddy_offline": {
- sess: nil,
- },
- },
- clientResponse: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Feedbag,
- SubGroup: oscar.FeedbagStatus,
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x0E_FeedbagStatus{
- Results: []uint16{0x0000},
- },
- },
- },
- {
- name: "users adds an invisible buddy, expect OK response and 0 buddy arrived events",
- userSession: newTestSession("user_screen_name"),
- inputSNAC: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x08_FeedbagInsertItem{
- Items: []oscar.FeedbagItem{
- {
- ClassID: 2,
- Name: "invisible_buddy_online",
- },
- },
- },
- },
- screenNameLookups: map[string]struct {
- sess *state.Session
- err error
- }{
- "invisible_buddy_online": {
- sess: newTestSession("invisible_buddy_online", sessOptInvisible),
- },
- },
- clientResponse: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Feedbag,
- SubGroup: oscar.FeedbagStatus,
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x0E_FeedbagStatus{
- Results: []uint16{0x0000},
- },
- },
- },
- {
- name: "user blocks buddy currently online, expect OK response, buddy departed event client, 1 buddy " +
- "departed event sent to buddy",
- userSession: newTestSession("user_screen_name"),
- inputSNAC: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x08_FeedbagInsertItem{
- Items: []oscar.FeedbagItem{
- {
- ClassID: 3,
- Name: "buddy_1",
- },
- },
- },
- },
- screenNameLookups: map[string]struct {
- sess *state.Session
- err error
- }{
- "user_screen_name": {
- sess: newTestSession("user_screen_name"),
- },
- "buddy_1": {
- sess: newTestSession("buddy_1"),
- },
- },
- buddyMessages: []struct {
- user string
- msg oscar.SNACMessage
- }{
- {
- user: "buddy_1",
- msg: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Buddy,
- SubGroup: oscar.BuddyDeparted,
- },
- Body: oscar.SNAC_0x03_0x0C_BuddyDeparted{
- TLVUserInfo: oscar.TLVUserInfo{
- ScreenName: "user_screen_name",
- WarningLevel: 0,
- },
- },
- },
- },
- {
- user: "user_screen_name",
- msg: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Buddy,
- SubGroup: oscar.BuddyDeparted,
- },
- Body: oscar.SNAC_0x03_0x0C_BuddyDeparted{
- TLVUserInfo: oscar.TLVUserInfo{
- ScreenName: "buddy_1",
- WarningLevel: 0,
- },
- },
- },
- },
- },
- clientResponse: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Feedbag,
- SubGroup: oscar.FeedbagStatus,
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x0E_FeedbagStatus{
- Results: []uint16{0x0000},
- },
- },
- },
- {
- name: "user blocks buddy currently offline, expect OK response and a superfluous buddy departed events",
- userSession: newTestSession("user_screen_name"),
- inputSNAC: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x08_FeedbagInsertItem{
- Items: []oscar.FeedbagItem{
- {
- ClassID: 3,
- Name: "buddy_1",
- },
- },
- },
- },
- screenNameLookups: map[string]struct {
- sess *state.Session
- err error
- }{
- "user_screen_name": {
- sess: newTestSession("user_screen_name"),
- },
- "buddy_1": {
- sess: nil,
- },
- },
- clientResponse: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Feedbag,
- SubGroup: oscar.FeedbagStatus,
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x0E_FeedbagStatus{
- Results: []uint16{0x0000},
- },
- },
- buddyMessages: []struct {
- user string
- msg oscar.SNACMessage
- }{
- {
- user: "buddy_1",
- msg: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Buddy,
- SubGroup: oscar.BuddyDeparted,
- },
- Body: oscar.SNAC_0x03_0x0C_BuddyDeparted{
- TLVUserInfo: oscar.TLVUserInfo{
- ScreenName: "user_screen_name",
- WarningLevel: 0,
- },
- },
- },
- },
- },
- },
- {
- name: "user tries to block themselves, expect feedback error",
- userSession: newTestSession("user_screen_name"),
- inputSNAC: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x13_0x08_FeedbagInsertItem{
- Items: []oscar.FeedbagItem{
- {
- ClassID: 3,
- Name: "user_screen_name",
- },
- },
- },
- },
- clientResponse: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Feedbag,
- SubGroup: oscar.FeedbagErr,
- RequestID: 1234,
- },
- Body: oscar.SNACError{
- Code: oscar.ErrorCodeNotSupportedByHost,
- },
- },
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- //
- // initialize dependencies
- //
- feedbagManager := newMockFeedbagManager(t)
- feedbagManager.EXPECT().
- Upsert(tc.userSession.ScreenName(), tc.inputSNAC.Body.(oscar.SNAC_0x13_0x08_FeedbagInsertItem).Items).
- Return(nil).
- Maybe()
- feedbagManager.EXPECT().
- Buddies(tc.userSession.ScreenName()).
- Return([]string{}, nil).
- Maybe()
- messageRelayer := newMockMessageRelayer(t)
- for screenName, val := range tc.screenNameLookups {
- messageRelayer.EXPECT().
- RetrieveByScreenName(screenName).
- Return(val.sess).
- Maybe()
- }
- for _, n := range tc.buddyMessages {
- messageRelayer.EXPECT().
- SendToScreenName(mock.Anything, n.user, n.msg).
- Maybe()
- }
- //
- // send input SNAC
- //
- svc := FeedbagService{
- feedbagManager: feedbagManager,
- messageRelayer: messageRelayer,
- }
- output, err := svc.InsertItemHandler(nil, tc.userSession, tc.inputSNAC.Frame,
- tc.inputSNAC.Body.(oscar.SNAC_0x13_0x08_FeedbagInsertItem))
- assert.NoError(t, err)
- //
- // verify response
- //
- assert.Equal(t, output, tc.clientResponse)
- })
- }
- }
|