| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517 |
- package foodgroup
- import (
- "context"
- "log/slog"
- "testing"
- "github.com/stretchr/testify/assert"
- "github.com/mk6i/open-oscar-server/state"
- "github.com/mk6i/open-oscar-server/wire"
- )
- func TestODirService_KeywordListQuery(t *testing.T) {
- cases := []struct {
- // name is the unit test name
- name string
- // inputSNAC is the SNAC sent by the sender client
- inputSNAC wire.SNACMessage
- // expectSNACFrame is the SNAC frame sent from the server to the recipient
- // client
- expectOutput wire.SNACMessage
- // mockParams is the list of params sent to mocks that satisfy this
- // method's dependencies
- mockParams mockParams
- // expectErr is the expected error returned by the handler
- expectErr error
- }{
- {
- name: "get full list",
- inputSNAC: wire.SNACMessage{
- Frame: wire.SNACFrame{
- RequestID: 1234,
- },
- },
- expectOutput: wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.ODir,
- SubGroup: wire.ODirKeywordListReply,
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0F_0x04_KeywordListReply{
- Status: 0x01,
- Interests: []wire.ODirKeywordListItem{
- {
- ID: 0,
- Name: "Animals",
- Type: wire.ODirKeyword,
- },
- {
- ID: 1,
- Name: "Music",
- Type: wire.ODirKeywordCategory,
- },
- {
- ID: 1,
- Name: "Rock",
- Type: wire.ODirKeyword,
- },
- {
- ID: 1,
- Name: "Jazz",
- Type: wire.ODirKeyword,
- },
- },
- },
- },
- mockParams: mockParams{
- profileManagerParams: profileManagerParams{
- interestListParams: interestListParams{
- {
- result: []wire.ODirKeywordListItem{
- {
- ID: 0,
- Name: "Animals",
- Type: wire.ODirKeyword,
- },
- {
- ID: 1,
- Name: "Music",
- Type: wire.ODirKeywordCategory,
- },
- {
- ID: 1,
- Name: "Rock",
- Type: wire.ODirKeyword,
- },
- {
- ID: 1,
- Name: "Jazz",
- Type: wire.ODirKeyword,
- },
- },
- },
- },
- },
- },
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- profileManager := newMockProfileManager(t)
- for _, params := range tc.mockParams.interestListParams {
- profileManager.EXPECT().
- InterestList(matchContext()).
- Return(params.result, params.err)
- }
- svc := NewODirService(slog.Default(), profileManager)
- actual, err := svc.KeywordListQuery(context.Background(), tc.inputSNAC.Frame)
- assert.NoError(t, err)
- assert.Equal(t, tc.expectOutput, actual)
- })
- }
- }
- func TestODirService_InfoQuery(t *testing.T) {
- cases := []struct {
- // name is the unit test name
- name string
- // inputSNAC is the SNAC sent by the sender client
- inputSNAC wire.SNACMessage
- // expectSNACFrame is the SNAC frame sent from the server to the recipient
- // client
- expectOutput wire.SNACMessage
- // mockParams is the list of params sent to mocks that satisfy this
- // method's dependencies
- mockParams mockParams
- // expectErr is the expected error returned by the handler
- expectErr error
- }{
- {
- name: "search by name and address - results found",
- inputSNAC: wire.SNACMessage{
- Frame: wire.SNACFrame{
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0F_0x02_InfoQuery{
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ODirTLVFirstName, "joe"),
- },
- },
- },
- },
- expectOutput: wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.ODir,
- SubGroup: wire.ODirInfoReply,
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0F_0x03_InfoReply{
- Status: wire.ODirSearchResponseOK,
- Results: struct {
- List []wire.TLVBlock `oscar:"count_prefix=uint16"`
- }{List: []wire.TLVBlock{
- {
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ODirTLVFirstName, "Joe"),
- wire.NewTLVBE(wire.ODirTLVLastName, "Doe"),
- wire.NewTLVBE(wire.ODirTLVState, "California"),
- wire.NewTLVBE(wire.ODirTLVCity, "Los Angeles"),
- wire.NewTLVBE(wire.ODirTLVCountry, "USA"),
- wire.NewTLVBE(wire.ODirTLVScreenName, "joe123"),
- },
- },
- {
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ODirTLVFirstName, "Joe"),
- wire.NewTLVBE(wire.ODirTLVLastName, "Smith"),
- wire.NewTLVBE(wire.ODirTLVState, "New York"),
- wire.NewTLVBE(wire.ODirTLVCity, "New York City"),
- wire.NewTLVBE(wire.ODirTLVCountry, "USA"),
- wire.NewTLVBE(wire.ODirTLVScreenName, "joe321"),
- },
- },
- }},
- },
- },
- mockParams: mockParams{
- profileManagerParams: profileManagerParams{
- findByAIMNameAndAddrParams: findByAIMNameAndAddrParams{
- {
- info: state.AIMNameAndAddr{
- FirstName: "joe",
- },
- result: []state.User{
- {
- DisplayScreenName: "joe123",
- AIMDirectoryInfo: state.AIMNameAndAddr{
- FirstName: "Joe",
- LastName: "Doe",
- Country: "USA",
- State: "California",
- City: "Los Angeles",
- },
- },
- {
- DisplayScreenName: "joe321",
- AIMDirectoryInfo: state.AIMNameAndAddr{
- FirstName: "Joe",
- LastName: "Smith",
- Country: "USA",
- State: "New York",
- City: "New York City",
- },
- },
- },
- },
- },
- },
- },
- },
- {
- name: "search by name and address - no results found",
- inputSNAC: wire.SNACMessage{
- Frame: wire.SNACFrame{
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0F_0x02_InfoQuery{
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ODirTLVFirstName, "joe"),
- },
- },
- },
- },
- expectOutput: wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.ODir,
- SubGroup: wire.ODirInfoReply,
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0F_0x03_InfoReply{
- Status: wire.ODirSearchResponseOK,
- },
- },
- mockParams: mockParams{
- profileManagerParams: profileManagerParams{
- findByAIMNameAndAddrParams: findByAIMNameAndAddrParams{
- {
- info: state.AIMNameAndAddr{
- FirstName: "joe",
- },
- result: []state.User{},
- },
- },
- },
- },
- },
- {
- name: "search by name and address - no first or last name",
- inputSNAC: wire.SNACMessage{
- Frame: wire.SNACFrame{
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0F_0x02_InfoQuery{
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ODirTLVCity, "new york"),
- },
- },
- },
- },
- expectOutput: wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.ODir,
- SubGroup: wire.ODirInfoReply,
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0F_0x03_InfoReply{
- Status: wire.ODirSearchResponseNameMissing,
- },
- },
- mockParams: mockParams{
- profileManagerParams: profileManagerParams{
- findByAIMNameAndAddrParams: findByAIMNameAndAddrParams{},
- },
- },
- },
- {
- name: "search by email - results found",
- inputSNAC: wire.SNACMessage{
- Frame: wire.SNACFrame{
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0F_0x02_InfoQuery{
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ODirTLVEmailAddress, "test@aol.com"),
- },
- },
- },
- },
- expectOutput: wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.ODir,
- SubGroup: wire.ODirInfoReply,
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0F_0x03_InfoReply{
- Status: wire.ODirSearchResponseOK,
- Results: struct {
- List []wire.TLVBlock `oscar:"count_prefix=uint16"`
- }{List: []wire.TLVBlock{
- {
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ODirTLVFirstName, "Joe"),
- wire.NewTLVBE(wire.ODirTLVLastName, "Doe"),
- wire.NewTLVBE(wire.ODirTLVState, "California"),
- wire.NewTLVBE(wire.ODirTLVCity, "Los Angeles"),
- wire.NewTLVBE(wire.ODirTLVCountry, "USA"),
- wire.NewTLVBE(wire.ODirTLVScreenName, "joe123"),
- },
- },
- }},
- },
- },
- mockParams: mockParams{
- profileManagerParams: profileManagerParams{
- findByAIMEmailParams: findByAIMEmailParams{
- {
- email: "test@aol.com",
- result: state.User{
- DisplayScreenName: "joe123",
- AIMDirectoryInfo: state.AIMNameAndAddr{
- FirstName: "Joe",
- LastName: "Doe",
- Country: "USA",
- State: "California",
- City: "Los Angeles",
- },
- },
- },
- },
- },
- },
- },
- {
- name: "search by email - no results found",
- inputSNAC: wire.SNACMessage{
- Frame: wire.SNACFrame{
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0F_0x02_InfoQuery{
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ODirTLVEmailAddress, "test@aol.com"),
- },
- },
- },
- },
- expectOutput: wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.ODir,
- SubGroup: wire.ODirInfoReply,
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0F_0x03_InfoReply{
- Status: wire.ODirSearchResponseOK,
- },
- },
- mockParams: mockParams{
- profileManagerParams: profileManagerParams{
- findByAIMEmailParams: findByAIMEmailParams{
- {
- email: "test@aol.com",
- err: state.ErrNoUser,
- },
- },
- },
- },
- },
- {
- name: "search by interest - results found",
- inputSNAC: wire.SNACMessage{
- Frame: wire.SNACFrame{
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0F_0x02_InfoQuery{
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ODirTLVInterest, "Computers"),
- },
- },
- },
- },
- expectOutput: wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.ODir,
- SubGroup: wire.ODirInfoReply,
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0F_0x03_InfoReply{
- Status: wire.ODirSearchResponseOK,
- Results: struct {
- List []wire.TLVBlock `oscar:"count_prefix=uint16"`
- }{List: []wire.TLVBlock{
- {
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ODirTLVFirstName, "Joe"),
- wire.NewTLVBE(wire.ODirTLVLastName, "Doe"),
- wire.NewTLVBE(wire.ODirTLVState, "California"),
- wire.NewTLVBE(wire.ODirTLVCity, "Los Angeles"),
- wire.NewTLVBE(wire.ODirTLVCountry, "USA"),
- wire.NewTLVBE(wire.ODirTLVScreenName, "joe123"),
- },
- },
- {
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ODirTLVFirstName, "Joe"),
- wire.NewTLVBE(wire.ODirTLVLastName, "Smith"),
- wire.NewTLVBE(wire.ODirTLVState, "New York"),
- wire.NewTLVBE(wire.ODirTLVCity, "New York City"),
- wire.NewTLVBE(wire.ODirTLVCountry, "USA"),
- wire.NewTLVBE(wire.ODirTLVScreenName, "joe321"),
- },
- },
- }},
- },
- },
- mockParams: mockParams{
- profileManagerParams: profileManagerParams{
- findByAIMKeywordParams: findByAIMKeywordParams{
- {
- keyword: "Computers",
- result: []state.User{
- {
- DisplayScreenName: "joe123",
- AIMDirectoryInfo: state.AIMNameAndAddr{
- FirstName: "Joe",
- LastName: "Doe",
- Country: "USA",
- State: "California",
- City: "Los Angeles",
- },
- },
- {
- DisplayScreenName: "joe321",
- AIMDirectoryInfo: state.AIMNameAndAddr{
- FirstName: "Joe",
- LastName: "Smith",
- Country: "USA",
- State: "New York",
- City: "New York City",
- },
- },
- },
- },
- },
- },
- },
- },
- {
- name: "search by interest - no results found",
- inputSNAC: wire.SNACMessage{
- Frame: wire.SNACFrame{
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0F_0x02_InfoQuery{
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ODirTLVInterest, "Computers"),
- },
- },
- },
- },
- expectOutput: wire.SNACMessage{
- Frame: wire.SNACFrame{
- FoodGroup: wire.ODir,
- SubGroup: wire.ODirInfoReply,
- RequestID: 1234,
- },
- Body: wire.SNAC_0x0F_0x03_InfoReply{
- Status: wire.ODirSearchResponseOK,
- },
- },
- mockParams: mockParams{
- profileManagerParams: profileManagerParams{
- findByAIMKeywordParams: findByAIMKeywordParams{
- {
- keyword: "Computers",
- result: []state.User{},
- },
- },
- },
- },
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- profileManager := newMockProfileManager(t)
- for _, params := range tc.mockParams.findByAIMNameAndAddrParams {
- profileManager.EXPECT().
- FindByAIMNameAndAddr(matchContext(), params.info).
- Return(params.result, params.err)
- }
- for _, params := range tc.mockParams.findByAIMEmailParams {
- profileManager.EXPECT().
- FindByAIMEmail(matchContext(), params.email).
- Return(params.result, params.err)
- }
- for _, params := range tc.mockParams.findByAIMKeywordParams {
- profileManager.EXPECT().
- FindByAIMKeyword(matchContext(), params.keyword).
- Return(params.result, params.err)
- }
- svc := NewODirService(slog.Default(), profileManager)
- actual, err := svc.InfoQuery(context.Background(), tc.inputSNAC.Frame, tc.inputSNAC.Body.(wire.SNAC_0x0F_0x02_InfoQuery))
- assert.NoError(t, err)
- assert.Equal(t, tc.expectOutput, actual)
- })
- }
- }
|