+
+func TestHandleGetUser(t *testing.T) {
+ type args struct {
+ cc *ClientConn
+ t *Transaction
+ }
+ tests := []struct {
+ name string
+ args args
+ wantRes []Transaction
+ wantErr assert.ErrorAssertionFunc
+ }{
+ {
+ name: "when account is valid",
+ args: args{
+ cc: &ClientConn{
+ Account: &Account{
+ Access: func() *[]byte {
+ var bits accessBitmap
+ bits.Set(accessOpenUser)
+ access := bits[:]
+ return &access
+ }(),
+ },
+ Server: &Server{
+ Accounts: map[string]*Account{
+ "guest": {
+ Login: "guest",
+ Name: "Guest",
+ Password: "password",
+ Access: &[]byte{1},
+ },
+ },
+ },
+ },
+ t: NewTransaction(
+ tranGetUser, &[]byte{0, 1},
+ NewField(fieldUserLogin, []byte("guest")),
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ Flags: 0x00,
+ IsReply: 0x01,
+ Type: []byte{0x01, 0x60},
+ ID: []byte{0x9a, 0xcb, 0x04, 0x42},
+ ErrorCode: []byte{0, 0, 0, 0},
+ Fields: []Field{
+ NewField(fieldUserName, []byte("Guest")),
+ NewField(fieldUserLogin, negateString([]byte("guest"))),
+ NewField(fieldUserPassword, []byte("password")),
+ NewField(fieldUserAccess, []byte{1}),
+ },
+ },
+ },
+ wantErr: assert.NoError,
+ },
+ {
+ name: "when user does not have required permission",
+ args: args{
+ cc: &ClientConn{
+ Account: &Account{
+ Access: func() *[]byte {
+ var bits accessBitmap
+ access := bits[:]
+ return &access
+ }(),
+ },
+ Server: &Server{
+ Accounts: map[string]*Account{},
+ },
+ },
+ t: NewTransaction(
+ tranGetUser, &[]byte{0, 1},
+ NewField(fieldUserLogin, []byte("nonExistentUser")),
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ Flags: 0x00,
+ IsReply: 0x01,
+ Type: []byte{0, 0x00},
+ ID: []byte{0x9a, 0xcb, 0x04, 0x42},
+ ErrorCode: []byte{0, 0, 0, 1},
+ Fields: []Field{
+ NewField(fieldError, []byte("You are not allowed to view accounts.")),
+ },
+ },
+ },
+ wantErr: assert.NoError,
+ },
+ {
+ name: "when account does not exist",
+ args: args{
+ cc: &ClientConn{
+ Account: &Account{
+ Access: func() *[]byte {
+ var bits accessBitmap
+ bits.Set(accessOpenUser)
+ access := bits[:]
+ return &access
+ }(),
+ },
+ Server: &Server{
+ Accounts: map[string]*Account{},
+ },
+ },
+ t: NewTransaction(
+ tranGetUser, &[]byte{0, 1},
+ NewField(fieldUserLogin, []byte("nonExistentUser")),
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ Flags: 0x00,
+ IsReply: 0x01,
+ Type: []byte{0, 0x00},
+ ID: []byte{0x9a, 0xcb, 0x04, 0x42},
+ ErrorCode: []byte{0, 0, 0, 1},
+ Fields: []Field{
+ NewField(fieldError, []byte("Account does not exist.")),
+ },
+ },
+ },
+ wantErr: assert.NoError,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ gotRes, err := HandleGetUser(tt.args.cc, tt.args.t)
+ if !tt.wantErr(t, err, fmt.Sprintf("HandleGetUser(%v, %v)", tt.args.cc, tt.args.t)) {
+ return
+ }
+
+ tranAssertEqual(t, tt.wantRes, gotRes)
+ })
+ }
+}
+
+func TestHandleDeleteUser(t *testing.T) {
+ type args struct {
+ cc *ClientConn
+ t *Transaction
+ }
+ 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{
+ Access: func() *[]byte {
+ var bits accessBitmap
+ bits.Set(accessDeleteUser)
+ access := bits[:]
+ return &access
+ }(),
+ },
+ Server: &Server{
+ Accounts: map[string]*Account{
+ "testuser": {
+ Login: "testuser",
+ Name: "Testy McTest",
+ Password: "password",
+ Access: &[]byte{1},
+ },
+ },
+ },
+ },
+ t: NewTransaction(
+ tranDeleteUser, &[]byte{0, 1},
+ NewField(fieldUserLogin, negateString([]byte("testuser"))),
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ Flags: 0x00,
+ IsReply: 0x01,
+ Type: []byte{0x1, 0x5f},
+ ID: []byte{0x9a, 0xcb, 0x04, 0x42},
+ ErrorCode: []byte{0, 0, 0, 0},
+ Fields: []Field(nil),
+ },
+ },
+ wantErr: assert.NoError,
+ },
+ {
+ name: "when user does not have required permission",
+ setup: func() {},
+ args: args{
+ cc: &ClientConn{
+ Account: &Account{
+ Access: func() *[]byte {
+ var bits accessBitmap
+ access := bits[:]
+ return &access
+ }(),
+ },
+ Server: &Server{
+ Accounts: map[string]*Account{},
+ },
+ },
+ t: NewTransaction(
+ tranDeleteUser, &[]byte{0, 1},
+ NewField(fieldUserLogin, negateString([]byte("testuser"))),
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ Flags: 0x00,
+ IsReply: 0x01,
+ Type: []byte{0, 0x00},
+ ID: []byte{0x9a, 0xcb, 0x04, 0x42},
+ ErrorCode: []byte{0, 0, 0, 1},
+ Fields: []Field{
+ NewField(fieldError, []byte("You are not allowed to delete accounts.")),
+ },
+ },
+ },
+ wantErr: assert.NoError,
+ },
+ }
+ 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
+ }
+
+ tranAssertEqual(t, tt.wantRes, gotRes)
+ })
+ }
+}