Skip to content

Commit

Permalink
Improved tests coverage for messeger
Browse files Browse the repository at this point in the history
  • Loading branch information
oneils committed Aug 11, 2023
1 parent ea64f3f commit 61274dd
Showing 1 changed file with 278 additions and 50 deletions.
328 changes: 278 additions & 50 deletions backend/app/messager/messeger_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package messager

import (
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -40,53 +41,280 @@ func TestMessageProc_MakeMessage(t *testing.T) {
assert.Equal(t, 1, len(c.EncryptCalls()))
}

// func TestMessageProc_LoadMessage(t *testing.T) {
// s := &engine.MockEngine{}
// c := &MockCrypter{}
// m := New(s, c, Params{MaxPinAttempts: 2, MaxDuration: time.Minute})
//
// pinHash, err := m.makeHash("56789")
// require.NoError(t, err)
//
// s.On("Load", mock.AnythingOfType("string")).Return(&engine.Message{
// Data: []byte("data"),
// PinHash: pinHash,
// Key: "somekey",
// Exp: time.Now().Add(time.Minute),
// }, nil)
// s.On("Remove", "somekey").Return(nil)
// c.On("Decrypt", mock.Anything).Return([]byte("decrypted blah"), nil)
//
// r, err := m.LoadMessage("somekey", "56789")
// t.Logf("%+v", r)
// require.NoError(t, err)
// assert.Equal(t, "decrypted blah", string(r.Data))
// assert.Equal(t, 0, r.Errors)
// assert.Contains(t, r.PinHash, "$2a$")
//
// s.AssertExpectations(t)
// c.AssertExpectations(t)
// }
//
// func TestMessageProc_BadPin(t *testing.T) {
// s := &engine.MockEngine{}
// c := &MockCrypter{}
// m := New(s, c, Params{MaxPinAttempts: 2, MaxDuration: time.Minute})
//
// s.On("Load", mock.AnythingOfType("string")).Return(&engine.Message{
// Data: []byte("data"),
// PinHash: "bad bad",
// Key: "somekey",
// Exp: time.Now().Add(time.Minute),
// }, nil)
// s.On("IncErr", "somekey").Return(1, nil).Times(1)
// s.On("IncErr", "somekey").Return(2, nil).Times(1)
// s.On("Remove", "somekey").Return(nil).Times(1)
//
// _, err := m.LoadMessage("somekey", "56789")
// require.EqualError(t, err, "wrong pin attempt")
// _, err = m.LoadMessage("somekey", "56789")
// require.EqualError(t, err, "wrong pin")
//
// s.AssertExpectations(t)
// }
func TestMessageProc_MakeMessage_Errors(t *testing.T) {
s := &EngineMock{}
c := &CrypterMock{}

type args struct {
duration time.Duration
pin string
}

tests := []struct {
name string
args args
wantErr error
}{
{
name: "bad pin error",
wantErr: ErrBadPin,
args: args{
duration: time.Second * 30,
pin: "",
},
},
{
name: "bad duration",
wantErr: ErrDuration,
args: args{
duration: time.Minute * 30,
pin: "1234",
},
},
{
name: "internal error when pin too long",
wantErr: ErrInternal,
args: args{
duration: time.Second * 30,
pin: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed gravida varius nisi, id cursus justo. Nulla facilities. Sed auctor, ex eget bibendum aliquet, nisi turpis",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := New(s, c, Params{MaxPinAttempts: 2, MaxDuration: time.Minute})
r, err := m.MakeMessage(tt.args.duration, "message", tt.args.pin)
t.Logf("%+v", r)
require.EqualError(t, err, tt.wantErr.Error())

assert.Equal(t, 0, len(s.SaveCalls()))
assert.Equal(t, 0, len(c.EncryptCalls()))
})
}
}

func TestMessageProc_MakeMessage_CrypterError(t *testing.T) {
s := &EngineMock{}
c := &CrypterMock{
EncryptFunc: func(req Request) ([]byte, error) {
return nil, fmt.Errorf("crypter error")
},
}

m := New(s, c, Params{MaxPinAttempts: 2, MaxDuration: time.Minute})
r, err := m.MakeMessage(time.Second*30, "message", "56789")
t.Logf("%+v", r)
require.EqualError(t, err, "crypto error")

assert.Equal(t, 0, len(s.SaveCalls()))
assert.Equal(t, 1, len(c.EncryptCalls()))
}

func TestMessageProc_LoadMessage_Err(t *testing.T) {
s := &EngineMock{
LoadFunc: func(key string) (*store.Message, error) {
return nil, fmt.Errorf("load error")
},
}
c := &CrypterMock{}

m := New(s, c, Params{MaxPinAttempts: 2, MaxDuration: time.Minute})
r, err := m.LoadMessage("somekey", "56789")
t.Logf("%+v", r)

require.EqualError(t, err, "load error")

assert.Equal(t, 1, len(s.LoadCalls()))
assert.Equal(t, 0, len(s.RemoveCalls()))
assert.Equal(t, 0, len(s.IncErrCalls()))
assert.Equal(t, 0, len(c.DecryptCalls()))
}

func TestMessageProc_LoadMessage_ExpiredErr(t *testing.T) {
s := &EngineMock{
LoadFunc: func(key string) (*store.Message, error) {
return &store.Message{
Data: []byte("data"),
PinHash: "some-hash",
Exp: time.Now().Add(-1 * time.Minute),
}, nil
},
RemoveFunc: func(key string) error {
return nil
},
}

c := &CrypterMock{}

m := New(s, c, Params{MaxPinAttempts: 2, MaxDuration: time.Minute})
r, err := m.LoadMessage("somekey", "56789")
t.Logf("%+v", r)

require.EqualError(t, err, "message expired")

assert.Equal(t, 1, len(s.LoadCalls()))
assert.Equal(t, 1, len(s.RemoveCalls()))
assert.Equal(t, 0, len(s.IncErrCalls()))
assert.Equal(t, 0, len(c.DecryptCalls()))
}

func TestMessageProc_LoadMessage_BadPin(t *testing.T) {
s := &EngineMock{
LoadFunc: func(key string) (*store.Message, error) {
return &store.Message{
Data: []byte("data"),
PinHash: "some-hash",
Exp: time.Now().Add(1 * time.Minute),
}, nil
},

IncErrFunc: func(key string) (int, error) {
return 0, fmt.Errorf("inc error")
},
}

c := &CrypterMock{}

m := New(s, c, Params{MaxPinAttempts: 2, MaxDuration: time.Minute})
r, err := m.LoadMessage("somekey", "56789")
t.Logf("%+v", r)

require.EqualError(t, err, "wrong pin")

assert.Equal(t, 1, len(s.LoadCalls()))
assert.Equal(t, 0, len(s.RemoveCalls()))
assert.Equal(t, 1, len(s.IncErrCalls()))
assert.Equal(t, 0, len(c.DecryptCalls()))
}

func TestMessageProc_LoadMessage_BadPin_MaxAttempts(t *testing.T) {
s := &EngineMock{
LoadFunc: func(key string) (*store.Message, error) {
return &store.Message{
Data: []byte("data"),
PinHash: "some-hash",
Exp: time.Now().Add(1 * time.Minute),
}, nil
},
RemoveFunc: func(key string) error {
return nil
},

IncErrFunc: func(key string) (int, error) {
return 2, nil
},
}

c := &CrypterMock{}

m := New(s, c, Params{MaxPinAttempts: 2, MaxDuration: time.Minute})
r, err := m.LoadMessage("somekey", "56789")
t.Logf("%+v", r)

require.EqualError(t, err, "wrong pin")

assert.Equal(t, 1, len(s.LoadCalls()))
assert.Equal(t, 1, len(s.RemoveCalls()))
assert.Equal(t, 1, len(s.IncErrCalls()))
assert.Equal(t, 0, len(c.DecryptCalls()))
}

func TestMessageProc_LoadMessage_BadPinAttempt(t *testing.T) {
msg := &store.Message{
Data: []byte("data"),
PinHash: "some-hash",
Exp: time.Now().Add(1 * time.Minute),
}

s := &EngineMock{
LoadFunc: func(key string) (*store.Message, error) {
return msg, nil
},
IncErrFunc: func(key string) (int, error) {
return 1, nil
},
}

c := &CrypterMock{}

m := New(s, c, Params{MaxPinAttempts: 2, MaxDuration: time.Minute})
r, err := m.LoadMessage("somekey", "56789")
t.Logf("%+v", r)

require.EqualError(t, err, "wrong pin attempt")

assert.Equal(t, r, msg)

assert.Equal(t, 1, len(s.LoadCalls()))
assert.Equal(t, 0, len(s.RemoveCalls()))
assert.Equal(t, 1, len(s.IncErrCalls()))
assert.Equal(t, 0, len(c.DecryptCalls()))
}

func TestMessageProc_LoadMessage_DecryptError(t *testing.T) {
s := &EngineMock{
LoadFunc: func(key string) (*store.Message, error) {
return &store.Message{
Data: []byte("data"),
PinHash: "$2a$10$2d9OIFG2.zuVIiZznlpy/uJoTl4quQPbDSFnHbi0LuYDILuxHYkDu",
Exp: time.Now().Add(1 * time.Minute),
}, nil
},
RemoveFunc: func(key string) error {
return nil
},
}

c := &CrypterMock{
DecryptFunc: func(req Request) ([]byte, error) {
return nil, fmt.Errorf("decrypt error")
},
}

m := New(s, c, Params{MaxPinAttempts: 2, MaxDuration: time.Minute})
r, err := m.LoadMessage("somekey", "123456")
t.Logf("%+v", r)

require.EqualError(t, err, "wrong pin")

assert.Equal(t, 1, len(s.LoadCalls()))
assert.Equal(t, 1, len(s.RemoveCalls()))
assert.Equal(t, 0, len(s.IncErrCalls()))
assert.Equal(t, 1, len(c.DecryptCalls()))
}

func TestMessageProc_LoadMessage(t *testing.T) {
s := &EngineMock{
LoadFunc: func(key string) (*store.Message, error) {
return &store.Message{
Data: []byte("data"),
PinHash: "$2a$10$2d9OIFG2.zuVIiZznlpy/uJoTl4quQPbDSFnHbi0LuYDILuxHYkDu",
Exp: time.Now().Add(1 * time.Minute),
}, nil
},
RemoveFunc: func(key string) error {
return nil
},
}

c := &CrypterMock{
DecryptFunc: func(req Request) ([]byte, error) {
return []byte("decrypted blah"), nil
},
}

m := New(s, c, Params{MaxPinAttempts: 2, MaxDuration: time.Minute})
r, err := m.LoadMessage("somekey", "123456")
t.Logf("%+v", r)

require.NoError(t, err)
assert.Equal(t, "decrypted blah", string(r.Data))
assert.Equal(t, 0, r.Errors)
assert.Contains(t, r.PinHash, "$2a$")

assert.Equal(t, 1, len(s.LoadCalls()))
assert.Equal(t, 1, len(s.RemoveCalls()))
assert.Equal(t, 0, len(s.IncErrCalls()))
assert.Equal(t, 1, len(c.DecryptCalls()))
}

0 comments on commit 61274dd

Please sign in to comment.