Skip to content

Commit c4e0ac6

Browse files
committed
implement SNAC(0x02,0x0C) - LocateGetDirReply
This make directory info lookup for a single user work.
1 parent cef0bfd commit c4e0ac6

File tree

10 files changed

+322
-7
lines changed

10 files changed

+322
-7
lines changed

foodgroup/auth_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ func TestAuthService_BUCPLoginRequest(t *testing.T) {
418418
for _, tc := range cases {
419419
t.Run(tc.name, func(t *testing.T) {
420420
userManager := newMockUserManager(t)
421-
for _, params := range tc.mockParams.getUserParams {
421+
for _, params := range tc.mockParams.userManagerParams.getUserParams {
422422
userManager.EXPECT().
423423
User(params.screenName).
424424
Return(params.result, params.err)
@@ -800,7 +800,7 @@ func TestAuthService_FLAPLoginResponse(t *testing.T) {
800800
for _, tc := range cases {
801801
t.Run(tc.name, func(t *testing.T) {
802802
userManager := newMockUserManager(t)
803-
for _, params := range tc.mockParams.getUserParams {
803+
for _, params := range tc.mockParams.userManagerParams.getUserParams {
804804
userManager.EXPECT().
805805
User(params.screenName).
806806
Return(params.result, params.err)
@@ -980,7 +980,7 @@ func TestAuthService_BUCPChallengeRequest(t *testing.T) {
980980
for _, tc := range cases {
981981
t.Run(tc.name, func(t *testing.T) {
982982
userManager := newMockUserManager(t)
983-
for _, params := range tc.mockParams.getUserParams {
983+
for _, params := range tc.mockParams.userManagerParams.getUserParams {
984984
userManager.EXPECT().
985985
User(params.screenName).
986986
Return(params.result, params.err)
@@ -1165,7 +1165,7 @@ func TestAuthService_RegisterBOSSession(t *testing.T) {
11651165
Return(params.dataOut, nil)
11661166
}
11671167
userManager := newMockUserManager(t)
1168-
for _, params := range tc.mockParams.getUserParams {
1168+
for _, params := range tc.mockParams.userManagerParams.getUserParams {
11691169
userManager.EXPECT().
11701170
User(params.screenName).
11711171
Return(params.result, nil)

foodgroup/locate.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,40 @@ func (s LocateService) SetKeywordInfo(ctx context.Context, sess *state.Session,
234234
},
235235
}, nil
236236
}
237+
238+
// DirInfo returns directory information for a user.
239+
func (s LocateService) DirInfo(ctx context.Context, inFrame wire.SNACFrame, body wire.SNAC_0x02_0x0B_LocateGetDirInfo) (wire.SNACMessage, error) {
240+
reply := wire.SNAC_0x02_0x0C_LocateGetDirReply{
241+
Status: wire.LocateGetDirReplyOK,
242+
TLVBlock: wire.TLVBlock{
243+
TLVList: wire.TLVList{},
244+
},
245+
}
246+
247+
user, err := s.profileManager.User(state.NewIdentScreenName(body.WatcherScreenNames))
248+
if err != nil {
249+
return wire.SNACMessage{}, fmt.Errorf("User: %w", err)
250+
}
251+
252+
if user != nil {
253+
reply.Append(wire.NewTLVBE(wire.ODirTLVFirstName, user.AIMDirectoryInfo.FirstName))
254+
reply.Append(wire.NewTLVBE(wire.ODirTLVLastName, user.AIMDirectoryInfo.LastName))
255+
reply.Append(wire.NewTLVBE(wire.ODirTLVMiddleName, user.AIMDirectoryInfo.MiddleName))
256+
reply.Append(wire.NewTLVBE(wire.ODirTLVMaidenName, user.AIMDirectoryInfo.MaidenName))
257+
reply.Append(wire.NewTLVBE(wire.ODirTLVCountry, user.AIMDirectoryInfo.Country))
258+
reply.Append(wire.NewTLVBE(wire.ODirTLVState, user.AIMDirectoryInfo.State))
259+
reply.Append(wire.NewTLVBE(wire.ODirTLVCity, user.AIMDirectoryInfo.City))
260+
reply.Append(wire.NewTLVBE(wire.ODirTLVNickName, user.AIMDirectoryInfo.NickName))
261+
reply.Append(wire.NewTLVBE(wire.ODirTLVZIP, user.AIMDirectoryInfo.ZIPCode))
262+
reply.Append(wire.NewTLVBE(wire.ODirTLVAddress, user.AIMDirectoryInfo.Address))
263+
}
264+
265+
return wire.SNACMessage{
266+
Frame: wire.SNACFrame{
267+
FoodGroup: wire.Locate,
268+
SubGroup: wire.LocateGetDirReply,
269+
RequestID: inFrame.RequestID,
270+
},
271+
Body: reply,
272+
}, nil
273+
}

foodgroup/locate_test.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,3 +764,130 @@ func TestLocateService_RightsQuery(t *testing.T) {
764764

765765
assert.Equal(t, expectSNAC, outputSNAC)
766766
}
767+
768+
func TestLocateService_DirInfo(t *testing.T) {
769+
tests := []struct {
770+
// name is the unit test name
771+
name string
772+
// userSession is the session of the user setting info
773+
userSession *state.Session
774+
// inputSNAC is the SNAC sent from client to server
775+
inputSNAC wire.SNACMessage
776+
// expectOutput is the SNAC sent from the server to client
777+
expectOutput wire.SNACMessage
778+
// mockParams is the list of params sent to mocks that satisfy this
779+
// method's dependencies
780+
mockParams mockParams
781+
// wantErr is the expected error
782+
wantErr error
783+
}{
784+
{
785+
name: "happy path",
786+
userSession: newTestSession("test-user"),
787+
inputSNAC: wire.SNACMessage{
788+
Frame: wire.SNACFrame{
789+
RequestID: 1234,
790+
},
791+
Body: wire.SNAC_0x02_0x0B_LocateGetDirInfo{
792+
WatcherScreenNames: "test-user",
793+
},
794+
},
795+
expectOutput: wire.SNACMessage{
796+
Frame: wire.SNACFrame{
797+
FoodGroup: wire.Locate,
798+
SubGroup: wire.LocateGetDirReply,
799+
RequestID: 1234,
800+
},
801+
Body: wire.SNAC_0x02_0x0C_LocateGetDirReply{
802+
Status: wire.LocateGetDirReplyOK,
803+
TLVBlock: wire.TLVBlock{
804+
TLVList: wire.TLVList{
805+
wire.NewTLVBE(wire.ODirTLVFirstName, "John"),
806+
wire.NewTLVBE(wire.ODirTLVLastName, "Doe"),
807+
wire.NewTLVBE(wire.ODirTLVMiddleName, "A"),
808+
wire.NewTLVBE(wire.ODirTLVMaidenName, "Smith"),
809+
wire.NewTLVBE(wire.ODirTLVCountry, "USA"),
810+
wire.NewTLVBE(wire.ODirTLVState, "CA"),
811+
wire.NewTLVBE(wire.ODirTLVCity, "San Francisco"),
812+
wire.NewTLVBE(wire.ODirTLVNickName, "Johnny"),
813+
wire.NewTLVBE(wire.ODirTLVZIP, "94107"),
814+
wire.NewTLVBE(wire.ODirTLVAddress, "123 Main St"),
815+
},
816+
},
817+
},
818+
},
819+
mockParams: mockParams{
820+
profileManagerParams: profileManagerParams{
821+
getUserParams: getUserParams{
822+
{
823+
screenName: state.NewIdentScreenName("test-user"),
824+
result: &state.User{
825+
AIMDirectoryInfo: state.AIMNameAndAddr{
826+
FirstName: "John",
827+
LastName: "Doe",
828+
MiddleName: "A",
829+
MaidenName: "Smith",
830+
Country: "USA",
831+
State: "CA",
832+
City: "San Francisco",
833+
NickName: "Johnny",
834+
ZIPCode: "94107",
835+
Address: "123 Main St",
836+
},
837+
},
838+
},
839+
},
840+
},
841+
},
842+
},
843+
{
844+
name: "user not found",
845+
userSession: newTestSession("test-user"),
846+
inputSNAC: wire.SNACMessage{
847+
Frame: wire.SNACFrame{
848+
RequestID: 1234,
849+
},
850+
Body: wire.SNAC_0x02_0x0B_LocateGetDirInfo{
851+
WatcherScreenNames: "test-user",
852+
},
853+
},
854+
expectOutput: wire.SNACMessage{
855+
Frame: wire.SNACFrame{
856+
FoodGroup: wire.Locate,
857+
SubGroup: wire.LocateGetDirReply,
858+
RequestID: 1234,
859+
},
860+
Body: wire.SNAC_0x02_0x0C_LocateGetDirReply{
861+
Status: wire.LocateGetDirReplyOK,
862+
TLVBlock: wire.TLVBlock{
863+
TLVList: wire.TLVList{},
864+
},
865+
},
866+
},
867+
mockParams: mockParams{
868+
profileManagerParams: profileManagerParams{
869+
getUserParams: getUserParams{
870+
{
871+
screenName: state.NewIdentScreenName("test-user"),
872+
result: nil,
873+
},
874+
},
875+
},
876+
},
877+
},
878+
}
879+
for _, tt := range tests {
880+
t.Run(tt.name, func(t *testing.T) {
881+
profileManager := newMockProfileManager(t)
882+
for _, params := range tt.mockParams.profileManagerParams.getUserParams {
883+
profileManager.EXPECT().
884+
User(params.screenName).
885+
Return(params.result, params.err)
886+
}
887+
svc := NewLocateService(nil, nil, profileManager, nil)
888+
outputSNAC, err := svc.DirInfo(nil, tt.inputSNAC.Frame, tt.inputSNAC.Body.(wire.SNAC_0x02_0x0B_LocateGetDirInfo))
889+
assert.NoError(t, err)
890+
assert.Equal(t, tt.expectOutput, outputSNAC)
891+
})
892+
}
893+
}

foodgroup/mock_profile_manager_test.go

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

foodgroup/test_helpers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ type profileManagerParams struct {
364364
findByAIMEmailParams
365365
findByAIMKeywordParams
366366
findByAIMNameAndAddrParams
367+
getUserParams
367368
interestListParams
368369
retrieveProfileParams
369370
setDirectoryInfoParams

foodgroup/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ type ProfileManager interface {
9696
SetDirectoryInfo(name state.IdentScreenName, info state.AIMNameAndAddr) error
9797
SetKeywords(name state.IdentScreenName, keywords [5]string) error
9898
SetProfile(screenName state.IdentScreenName, body string) error
99+
User(screenName state.IdentScreenName) (*state.User, error)
99100
}
100101

101102
type MessageRelayer interface {

server/oscar/handler/locate.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
)
1414

1515
type LocateService interface {
16+
DirInfo(ctx context.Context, frame wire.SNACFrame, body wire.SNAC_0x02_0x0B_LocateGetDirInfo) (wire.SNACMessage, error)
1617
RightsQuery(ctx context.Context, inFrame wire.SNACFrame) wire.SNACMessage
1718
SetDirInfo(ctx context.Context, sess *state.Session, inFrame wire.SNACFrame, inBody wire.SNAC_0x02_0x09_LocateSetDirInfo) (wire.SNACMessage, error)
1819
SetInfo(ctx context.Context, sess *state.Session, inBody wire.SNAC_0x02_0x04_LocateSetInfo) error
@@ -62,10 +63,17 @@ func (h LocateHandler) SetDirInfo(ctx context.Context, sess *state.Session, inFr
6263
return rw.SendSNAC(outSNAC.Frame, outSNAC.Body)
6364
}
6465

65-
func (h LocateHandler) GetDirInfo(ctx context.Context, _ *state.Session, inFrame wire.SNACFrame, r io.Reader, _ oscar.ResponseWriter) error {
66+
func (h LocateHandler) GetDirInfo(ctx context.Context, _ *state.Session, inFrame wire.SNACFrame, r io.Reader, rw oscar.ResponseWriter) error {
6667
inBody := wire.SNAC_0x02_0x0B_LocateGetDirInfo{}
67-
h.LogRequest(ctx, inFrame, inBody)
68-
return wire.UnmarshalBE(&inBody, r)
68+
if err := wire.UnmarshalBE(&inBody, r); err != nil {
69+
return err
70+
}
71+
outSNAC, err := h.LocateService.DirInfo(ctx, inFrame, inBody)
72+
if err != nil {
73+
return err
74+
}
75+
h.LogRequestAndResponse(ctx, inFrame, inBody, outSNAC.Frame, outSNAC.Body)
76+
return rw.SendSNAC(outSNAC.Frame, outSNAC.Body)
6977
}
7078

7179
func (h LocateHandler) SetKeywordInfo(ctx context.Context, sess *state.Session, inFrame wire.SNACFrame, r io.Reader, rw oscar.ResponseWriter) error {

server/oscar/handler/locate_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,27 @@ func TestLocateHandler_GetDirInfo(t *testing.T) {
2121
WatcherScreenNames: "screen-name",
2222
},
2323
}
24+
output := wire.SNACMessage{
25+
Frame: wire.SNACFrame{
26+
FoodGroup: wire.Locate,
27+
SubGroup: wire.LocateGetDirReply,
28+
},
29+
Body: wire.SNAC_0x02_0x0C_LocateGetDirReply{
30+
Status: 1,
31+
},
32+
}
2433

2534
svc := newMockLocateService(t)
35+
svc.EXPECT().
36+
DirInfo(mock.Anything, input.Frame, input.Body).
37+
Return(output, nil)
38+
2639
h := NewLocateHandler(svc, slog.Default())
40+
2741
responseWriter := newMockResponseWriter(t)
42+
responseWriter.EXPECT().
43+
SendSNAC(output.Frame, output.Body).
44+
Return(nil)
2845

2946
buf := &bytes.Buffer{}
3047
assert.NoError(t, wire.MarshalBE(input.Body, buf))

0 commit comments

Comments
 (0)