| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- package handler
- import (
- "bytes"
- "log/slog"
- "testing"
- "github.com/mk6i/retro-aim-server/wire"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/mock"
- )
- func TestFeedbagHandler_DeleteItem(t *testing.T) {
- input := wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Feedbag,
- SubGroup: wire.FeedbagDeleteItem,
- },
- Body: wire.SNAC_0x13_0x0A_FeedbagDeleteItem{
- Items: []wire.FeedbagItem{
- {
- Name: "my-item",
- },
- },
- },
- }
- output := wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Feedbag,
- SubGroup: wire.FeedbagStatus,
- },
- Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
- Results: []uint16{1234},
- },
- }
- svc := newMockFeedbagService(t)
- svc.EXPECT().
- DeleteItem(mock.Anything, mock.Anything, input.Frame, input.Body).
- Return(output, nil)
- h := NewFeedbagHandler(slog.Default(), svc)
- responseWriter := newMockResponseWriter(t)
- responseWriter.EXPECT().
- SendSNAC(output.Frame, output.Body).
- Return(nil)
- buf := &bytes.Buffer{}
- assert.NoError(t, wire.MarshalBE(input.Body, buf))
- assert.NoError(t, h.DeleteItem(nil, nil, input.Frame, buf, responseWriter))
- }
- func TestFeedbagHandler_EndCluster(t *testing.T) {
- input := wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Feedbag,
- SubGroup: wire.FeedbagEndCluster,
- },
- Body: struct{}{},
- }
- svc := newMockFeedbagService(t)
- h := NewFeedbagHandler(slog.Default(), svc)
- responseWriter := newMockResponseWriter(t)
- buf := &bytes.Buffer{}
- assert.NoError(t, wire.MarshalBE(input.Body, buf))
- assert.NoError(t, h.EndCluster(nil, nil, input.Frame, buf, responseWriter))
- }
- func TestFeedbagHandler_InsertItem(t *testing.T) {
- input := wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Feedbag,
- SubGroup: wire.FeedbagInsertItem,
- },
- Body: wire.SNAC_0x13_0x08_FeedbagInsertItem{
- Items: []wire.FeedbagItem{
- {
- Name: "my-item",
- },
- },
- },
- }
- output := wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Feedbag,
- SubGroup: wire.FeedbagStatus,
- },
- Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
- Results: []uint16{1234},
- },
- }
- svc := newMockFeedbagService(t)
- svc.EXPECT().
- UpsertItem(mock.Anything, mock.Anything, input.Frame, input.Body.(wire.SNAC_0x13_0x08_FeedbagInsertItem).Items).
- Return(output, nil)
- h := NewFeedbagHandler(slog.Default(), svc)
- responseWriter := newMockResponseWriter(t)
- responseWriter.EXPECT().
- SendSNAC(output.Frame, output.Body).
- Return(nil)
- buf := &bytes.Buffer{}
- assert.NoError(t, wire.MarshalBE(input.Body, buf))
- assert.NoError(t, h.InsertItem(nil, nil, input.Frame, buf, responseWriter))
- }
- func TestFeedbagHandler_Query(t *testing.T) {
- input := wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Feedbag,
- SubGroup: wire.FeedbagQuery,
- },
- Body: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- {
- Tag: 0x01,
- Value: []byte{1, 2, 3, 4},
- },
- },
- },
- },
- }
- output := wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Feedbag,
- SubGroup: wire.FeedbagReply,
- },
- Body: wire.SNAC_0x13_0x06_FeedbagReply{
- Version: 4,
- },
- }
- svc := newMockFeedbagService(t)
- svc.EXPECT().
- Query(mock.Anything, mock.Anything, input.Frame).
- Return(output, nil)
- h := NewFeedbagHandler(slog.Default(), svc)
- responseWriter := newMockResponseWriter(t)
- responseWriter.EXPECT().
- SendSNAC(output.Frame, output.Body).
- Return(nil)
- buf := &bytes.Buffer{}
- assert.NoError(t, wire.MarshalBE(input.Body, buf))
- assert.NoError(t, h.Query(nil, nil, input.Frame, buf, responseWriter))
- }
- func TestFeedbagHandler_QueryIfModified(t *testing.T) {
- input := wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Feedbag,
- SubGroup: wire.FeedbagQueryIfModified,
- },
- Body: wire.SNAC_0x13_0x05_FeedbagQueryIfModified{
- LastUpdate: 1234,
- },
- }
- output := wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Feedbag,
- SubGroup: wire.FeedbagReply,
- },
- Body: wire.SNAC_0x13_0x06_FeedbagReply{
- LastUpdate: 1234,
- },
- }
- svc := newMockFeedbagService(t)
- svc.EXPECT().
- QueryIfModified(mock.Anything, mock.Anything, input.Frame, input.Body).
- Return(output, nil)
- h := NewFeedbagHandler(slog.Default(), svc)
- responseWriter := newMockResponseWriter(t)
- responseWriter.EXPECT().
- SendSNAC(output.Frame, output.Body).
- Return(nil)
- buf := &bytes.Buffer{}
- assert.NoError(t, wire.MarshalBE(input.Body, buf))
- assert.NoError(t, h.QueryIfModified(nil, nil, input.Frame, buf, responseWriter))
- }
- func TestFeedbagHandler_RightsQuery(t *testing.T) {
- input := wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Feedbag,
- SubGroup: wire.FeedbagRightsQuery,
- },
- Body: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- {
- Tag: 0x01,
- Value: []byte{1, 2, 3, 4},
- },
- },
- },
- },
- }
- output := wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Feedbag,
- SubGroup: wire.FeedbagRightsReply,
- },
- Body: wire.SNAC_0x13_0x03_FeedbagRightsReply{
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- {
- Tag: 0x01,
- Value: []byte{1, 2, 3, 4},
- },
- },
- },
- },
- }
- svc := newMockFeedbagService(t)
- svc.EXPECT().
- RightsQuery(mock.Anything, input.Frame).
- Return(output)
- h := NewFeedbagHandler(slog.Default(), svc)
- responseWriter := newMockResponseWriter(t)
- responseWriter.EXPECT().
- SendSNAC(output.Frame, output.Body).
- Return(nil)
- buf := &bytes.Buffer{}
- assert.NoError(t, wire.MarshalBE(input.Body, buf))
- assert.NoError(t, h.RightsQuery(nil, nil, input.Frame, buf, responseWriter))
- }
- func TestFeedbagHandler_StartCluster(t *testing.T) {
- input := wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Feedbag,
- SubGroup: wire.FeedbagStartCluster,
- },
- Body: wire.SNAC_0x13_0x11_FeedbagStartCluster{
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- {
- Tag: 0x01,
- Value: []byte{1, 2, 3, 4},
- },
- },
- },
- },
- }
- svc := newMockFeedbagService(t)
- svc.EXPECT().
- StartCluster(mock.Anything, input.Frame, input.Body)
- h := NewFeedbagHandler(slog.Default(), svc)
- responseWriter := newMockResponseWriter(t)
- buf := &bytes.Buffer{}
- assert.NoError(t, wire.MarshalBE(input.Body, buf))
- assert.NoError(t, h.StartCluster(nil, nil, input.Frame, buf, responseWriter))
- }
- func TestFeedbagHandler_UpdateItem(t *testing.T) {
- input := wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Feedbag,
- SubGroup: wire.FeedbagUpdateItem,
- },
- Body: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
- Items: []wire.FeedbagItem{
- {
- Name: "my-item",
- },
- },
- },
- }
- output := wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Feedbag,
- SubGroup: wire.FeedbagStatus,
- },
- Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
- Results: []uint16{1234},
- },
- }
- svc := newMockFeedbagService(t)
- svc.EXPECT().
- UpsertItem(mock.Anything, mock.Anything, input.Frame, input.Body.(wire.SNAC_0x13_0x09_FeedbagUpdateItem).Items).
- Return(output, nil)
- h := NewFeedbagHandler(slog.Default(), svc)
- responseWriter := newMockResponseWriter(t)
- responseWriter.EXPECT().
- SendSNAC(output.Frame, output.Body).
- Return(nil)
- buf := &bytes.Buffer{}
- assert.NoError(t, wire.MarshalBE(input.Body, buf))
- assert.NoError(t, h.UpdateItem(nil, nil, input.Frame, buf, responseWriter))
- }
- func TestFeedbagHandler_Use(t *testing.T) {
- input := wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Feedbag,
- SubGroup: wire.FeedbagUse,
- },
- Body: struct{}{},
- }
- svc := newMockFeedbagService(t)
- svc.EXPECT().
- Use(mock.Anything, mock.Anything).
- Return(nil)
- h := NewFeedbagHandler(slog.Default(), svc)
- responseWriter := newMockResponseWriter(t)
- buf := &bytes.Buffer{}
- assert.NoError(t, wire.MarshalBE(input.Body, buf))
- assert.NoError(t, h.Use(nil, nil, input.Frame, buf, responseWriter))
- }
- func TestFeedbagHandler_RespondAuthorizeToHost(t *testing.T) {
- input := wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.Feedbag,
- SubGroup: wire.FeedbagRequestAuthorizeToHost,
- },
- Body: wire.SNAC_0x13_0x1A_FeedbagRespondAuthorizeToHost{
- ScreenName: "theScreenName",
- },
- }
- svc := newMockFeedbagService(t)
- svc.EXPECT().
- RespondAuthorizeToHost(mock.Anything, mock.Anything, input.Frame, input.Body).
- Return(nil)
- h := NewFeedbagHandler(slog.Default(), svc)
- responseWriter := newMockResponseWriter(t)
- buf := &bytes.Buffer{}
- assert.NoError(t, wire.MarshalBE(input.Body, buf))
- assert.NoError(t, h.RespondAuthorizeToHost(nil, nil, input.Frame, buf, responseWriter))
- }
|