X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/d4c152a4dba0eec7c8ecd13732900909f51b1c97..424ef66f0fe1ea21b4bae765dc40c97c98ed363e:/hotline/transaction_handlers_test.go?ds=inline diff --git a/hotline/transaction_handlers_test.go b/hotline/transaction_handlers_test.go index d7ec098..01ae862 100644 --- a/hotline/transaction_handlers_test.go +++ b/hotline/transaction_handlers_test.go @@ -689,15 +689,13 @@ func TestHandleNewFolder(t *testing.T) { t *Transaction } tests := []struct { - setup func() name string args args wantRes []Transaction wantErr bool }{ { - name: "without required permission", - setup: func() {}, + name: "without required permission", args: args{ cc: &ClientConn{ Account: &Account{ @@ -744,6 +742,12 @@ func TestHandleNewFolder(t *testing.T) { Config: &Config{ FileRoot: "/Files/", }, + FS: func() *MockFileStore { + mfs := &MockFileStore{} + mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil) + mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist) + return mfs + }(), }, }, t: NewTransaction( @@ -757,12 +761,6 @@ func TestHandleNewFolder(t *testing.T) { }), ), }, - setup: func() { - mfs := &MockFileStore{} - mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil) - mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist) - FS = mfs - }, wantRes: []Transaction{ { clientID: &[]byte{0, 1}, @@ -792,6 +790,12 @@ func TestHandleNewFolder(t *testing.T) { Config: &Config{ FileRoot: "/Files", }, + FS: func() *MockFileStore { + mfs := &MockFileStore{} + mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil) + mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist) + return mfs + }(), }, }, t: NewTransaction( @@ -799,12 +803,6 @@ func TestHandleNewFolder(t *testing.T) { NewField(fieldFileName, []byte("testFolder")), ), }, - setup: func() { - mfs := &MockFileStore{} - mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil) - mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist) - FS = mfs - }, wantRes: []Transaction{ { clientID: &[]byte{0, 1}, @@ -834,6 +832,12 @@ func TestHandleNewFolder(t *testing.T) { Config: &Config{ FileRoot: "/Files/", }, + FS: func() *MockFileStore { + mfs := &MockFileStore{} + mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil) + mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist) + return mfs + }(), }, }, t: NewTransaction( @@ -844,12 +848,6 @@ func TestHandleNewFolder(t *testing.T) { }), ), }, - setup: func() { - mfs := &MockFileStore{} - mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil) - mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist) - FS = mfs - }, wantRes: []Transaction{}, wantErr: true, }, @@ -870,6 +868,12 @@ func TestHandleNewFolder(t *testing.T) { Config: &Config{ FileRoot: "/Files/", }, + FS: func() *MockFileStore { + mfs := &MockFileStore{} + mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil) + mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist) + return mfs + }(), }, }, t: NewTransaction( @@ -877,12 +881,6 @@ func TestHandleNewFolder(t *testing.T) { NewField(fieldFileName, []byte("../../testFolder")), ), }, - setup: func() { - mfs := &MockFileStore{} - mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil) - mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist) - FS = mfs - }, wantRes: []Transaction{ { clientID: &[]byte{0, 1}, @@ -911,6 +909,12 @@ func TestHandleNewFolder(t *testing.T) { Config: &Config{ FileRoot: "/Files/", }, + FS: func() *MockFileStore { + mfs := &MockFileStore{} + mfs.On("Mkdir", "/Files/foo/testFolder", fs.FileMode(0777)).Return(nil) + mfs.On("Stat", "/Files/foo/testFolder").Return(nil, os.ErrNotExist) + return mfs + }(), }, }, t: NewTransaction( @@ -927,12 +931,6 @@ func TestHandleNewFolder(t *testing.T) { }), ), }, - setup: func() { - mfs := &MockFileStore{} - mfs.On("Mkdir", "/Files/foo/testFolder", fs.FileMode(0777)).Return(nil) - mfs.On("Stat", "/Files/foo/testFolder").Return(nil, os.ErrNotExist) - FS = mfs - }, wantRes: []Transaction{ { clientID: &[]byte{0, 1}, @@ -947,7 +945,6 @@ func TestHandleNewFolder(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - tt.setup() gotRes, err := HandleNewFolder(tt.args.cc, tt.args.t) if (err != nil) != tt.wantErr { @@ -1078,23 +1075,12 @@ func TestHandleMakeAlias(t *testing.T) { } tests := []struct { name string - setup func() args args wantRes []Transaction wantErr bool }{ { name: "with valid input and required permissions", - setup: func() { - mfs := &MockFileStore{} - path, _ := os.Getwd() - mfs.On( - "Symlink", - path+"/test/config/Files/foo/testFile", - path+"/test/config/Files/bar/testFile", - ).Return(nil) - FS = mfs - }, args: args{ cc: &ClientConn{ Account: &Account{ @@ -1113,6 +1099,16 @@ func TestHandleMakeAlias(t *testing.T) { }(), }, Logger: NewTestLogger(), + FS: func() *MockFileStore { + mfs := &MockFileStore{} + path, _ := os.Getwd() + mfs.On( + "Symlink", + path+"/test/config/Files/foo/testFile", + path+"/test/config/Files/bar/testFile", + ).Return(nil) + return mfs + }(), }, }, t: NewTransaction( @@ -1136,16 +1132,6 @@ func TestHandleMakeAlias(t *testing.T) { }, { name: "when symlink returns an error", - setup: func() { - mfs := &MockFileStore{} - path, _ := os.Getwd() - mfs.On( - "Symlink", - path+"/test/config/Files/foo/testFile", - path+"/test/config/Files/bar/testFile", - ).Return(errors.New("ohno")) - FS = mfs - }, args: args{ cc: &ClientConn{ Account: &Account{ @@ -1164,6 +1150,16 @@ func TestHandleMakeAlias(t *testing.T) { }(), }, Logger: NewTestLogger(), + FS: func() *MockFileStore { + mfs := &MockFileStore{} + path, _ := os.Getwd() + mfs.On( + "Symlink", + path+"/test/config/Files/foo/testFile", + path+"/test/config/Files/bar/testFile", + ).Return(errors.New("ohno")) + return mfs + }(), }, }, t: NewTransaction( @@ -1188,8 +1184,7 @@ func TestHandleMakeAlias(t *testing.T) { wantErr: false, }, { - name: "when user does not have required permission", - setup: func() {}, + name: "when user does not have required permission", args: args{ cc: &ClientConn{ Account: &Account{ @@ -1242,8 +1237,6 @@ func TestHandleMakeAlias(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - tt.setup() - gotRes, err := HandleMakeAlias(tt.args.cc, tt.args.t) if (err != nil) != tt.wantErr { t.Errorf("HandleMakeAlias(%v, %v)", tt.args.cc, tt.args.t) @@ -1400,18 +1393,12 @@ func TestHandleDeleteUser(t *testing.T) { } tests := []struct { name string - setup func() args args wantRes []Transaction wantErr assert.ErrorAssertionFunc }{ { name: "when user exists", - setup: func() { - mfs := &MockFileStore{} - mfs.On("Remove", "Users/testuser.yaml").Return(nil) - FS = mfs - }, args: args{ cc: &ClientConn{ Account: &Account{ @@ -1431,6 +1418,11 @@ func TestHandleDeleteUser(t *testing.T) { Access: &[]byte{1}, }, }, + FS: func() *MockFileStore { + mfs := &MockFileStore{} + mfs.On("Remove", "Users/testuser.yaml").Return(nil) + return mfs + }(), }, }, t: NewTransaction( @@ -1451,8 +1443,7 @@ func TestHandleDeleteUser(t *testing.T) { wantErr: assert.NoError, }, { - name: "when user does not have required permission", - setup: func() {}, + name: "when user does not have required permission", args: args{ cc: &ClientConn{ Account: &Account{ @@ -1488,7 +1479,6 @@ func TestHandleDeleteUser(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - tt.setup() gotRes, err := HandleDeleteUser(tt.args.cc, tt.args.t) if !tt.wantErr(t, err, fmt.Sprintf("HandleDeleteUser(%v, %v)", tt.args.cc, tt.args.t)) { return @@ -2136,3 +2126,121 @@ func TestHandleDisconnectUser(t *testing.T) { }) } } + +func TestHandleSendInstantMsg(t *testing.T) { + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr assert.ErrorAssertionFunc + }{ + { + name: "when client 1 sends a message to client 2", + args: args{ + cc: &ClientConn{ + ID: &[]byte{0, 1}, + UserName: []byte("User1"), + Server: &Server{ + Clients: map[uint16]*ClientConn{ + uint16(2): { + AutoReply: []byte(nil), + }, + }, + }, + }, + t: NewTransaction( + tranSendInstantMsg, + &[]byte{0, 1}, + NewField(fieldData, []byte("hai")), + NewField(fieldUserID, []byte{0, 2}), + ), + }, + wantRes: []Transaction{ + *NewTransaction( + tranServerMsg, + &[]byte{0, 2}, + NewField(fieldData, []byte("hai")), + NewField(fieldUserName, []byte("User1")), + NewField(fieldUserID, []byte{0, 1}), + NewField(fieldOptions, []byte{0, 1}), + ), + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0x0, 0x6c}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field(nil), + }, + }, + wantErr: assert.NoError, + }, + { + name: "when client 2 has autoreply enabled", + args: args{ + cc: &ClientConn{ + ID: &[]byte{0, 1}, + UserName: []byte("User1"), + Server: &Server{ + Clients: map[uint16]*ClientConn{ + uint16(2): { + ID: &[]byte{0, 2}, + UserName: []byte("User2"), + AutoReply: []byte("autohai"), + }, + }, + }, + }, + t: NewTransaction( + tranSendInstantMsg, + &[]byte{0, 1}, + NewField(fieldData, []byte("hai")), + NewField(fieldUserID, []byte{0, 2}), + ), + }, + wantRes: []Transaction{ + *NewTransaction( + tranServerMsg, + &[]byte{0, 2}, + NewField(fieldData, []byte("hai")), + NewField(fieldUserName, []byte("User1")), + NewField(fieldUserID, []byte{0, 1}), + NewField(fieldOptions, []byte{0, 1}), + ), + *NewTransaction( + tranServerMsg, + &[]byte{0, 1}, + NewField(fieldData, []byte("autohai")), + NewField(fieldUserName, []byte("User2")), + NewField(fieldUserID, []byte{0, 2}), + NewField(fieldOptions, []byte{0, 1}), + ), + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0x0, 0x6c}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field(nil), + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotRes, err := HandleSendInstantMsg(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleSendInstantMsg(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +}