|  | 
|  | 1 | +package locker | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"github.com/bivas/rivi/bot/mock" | 
|  | 5 | +	"github.com/stretchr/testify/assert" | 
|  | 6 | +	"testing" | 
|  | 7 | +) | 
|  | 8 | + | 
|  | 9 | +type mockLockableEventData struct { | 
|  | 10 | +	mock.MockEventData | 
|  | 11 | +	locked                   bool | 
|  | 12 | +	lockCalled, unlockCalled bool | 
|  | 13 | +} | 
|  | 14 | + | 
|  | 15 | +func (m *mockLockableEventData) Lock() { | 
|  | 16 | +	m.locked = true | 
|  | 17 | +	m.lockCalled = true | 
|  | 18 | +} | 
|  | 19 | + | 
|  | 20 | +func (m *mockLockableEventData) Unlock() { | 
|  | 21 | +	m.locked = false | 
|  | 22 | +	m.unlockCalled = true | 
|  | 23 | +} | 
|  | 24 | + | 
|  | 25 | +func (m *mockLockableEventData) LockState() bool { | 
|  | 26 | +	return m.locked | 
|  | 27 | +} | 
|  | 28 | + | 
|  | 29 | +func TestNotLockable(t *testing.T) { | 
|  | 30 | +	action := action{rule: &rule{}} | 
|  | 31 | +	meta := &mock.MockEventData{Labels: []string{}} | 
|  | 32 | +	config := &mock.MockConfiguration{} | 
|  | 33 | +	action.Apply(config, meta) | 
|  | 34 | +	assert.NotNil(t, action.err, "can't merge") | 
|  | 35 | +} | 
|  | 36 | + | 
|  | 37 | +func TestLock(t *testing.T) { | 
|  | 38 | +	action := action{rule: &rule{State: "lock"}} | 
|  | 39 | +	meta := &mockLockableEventData{MockEventData: mock.MockEventData{}} | 
|  | 40 | +	config := &mock.MockConfiguration{} | 
|  | 41 | +	action.Apply(config, meta) | 
|  | 42 | +	assert.Nil(t, action.err, "shouldn't error") | 
|  | 43 | +	assert.True(t, meta.locked, "should be locked") | 
|  | 44 | +	assert.True(t, meta.lockCalled, "should be locked") | 
|  | 45 | +} | 
|  | 46 | + | 
|  | 47 | +func TestLockWhenLocked(t *testing.T) { | 
|  | 48 | +	action := action{rule: &rule{State: "lock"}} | 
|  | 49 | +	meta := &mockLockableEventData{MockEventData: mock.MockEventData{}, locked: true} | 
|  | 50 | +	config := &mock.MockConfiguration{} | 
|  | 51 | +	action.Apply(config, meta) | 
|  | 52 | +	assert.Nil(t, action.err, "shouldn't error") | 
|  | 53 | +	assert.True(t, meta.locked, "should be locked") | 
|  | 54 | +	assert.False(t, meta.lockCalled, "no need to relock") | 
|  | 55 | +} | 
|  | 56 | + | 
|  | 57 | +func TestUnlockWhenLocked(t *testing.T) { | 
|  | 58 | +	action := action{rule: &rule{State: "unlock"}} | 
|  | 59 | +	meta := &mockLockableEventData{MockEventData: mock.MockEventData{}, locked: true} | 
|  | 60 | +	config := &mock.MockConfiguration{} | 
|  | 61 | +	action.Apply(config, meta) | 
|  | 62 | +	assert.Nil(t, action.err, "shouldn't error") | 
|  | 63 | +	assert.False(t, meta.locked, "should be unlocked") | 
|  | 64 | +	assert.True(t, meta.unlockCalled, "should be unlocked") | 
|  | 65 | +} | 
|  | 66 | + | 
|  | 67 | +func TestUnlockWhenUnlocked(t *testing.T) { | 
|  | 68 | +	action := action{rule: &rule{State: "unlock"}} | 
|  | 69 | +	meta := &mockLockableEventData{MockEventData: mock.MockEventData{}} | 
|  | 70 | +	config := &mock.MockConfiguration{} | 
|  | 71 | +	action.Apply(config, meta) | 
|  | 72 | +	assert.Nil(t, action.err, "shouldn't error") | 
|  | 73 | +	assert.False(t, meta.locked, "should be unlocked") | 
|  | 74 | +	assert.False(t, meta.unlockCalled, "no need to re-unlock") | 
|  | 75 | +} | 
|  | 76 | + | 
|  | 77 | +func TestStateChangeFromUnlocked(t *testing.T) { | 
|  | 78 | +	action := action{rule: &rule{State: "change"}} | 
|  | 79 | +	meta := &mockLockableEventData{MockEventData: mock.MockEventData{}} | 
|  | 80 | +	config := &mock.MockConfiguration{} | 
|  | 81 | +	action.Apply(config, meta) | 
|  | 82 | +	assert.Nil(t, action.err, "shouldn't error") | 
|  | 83 | +	assert.True(t, meta.locked, "should be locked") | 
|  | 84 | +	assert.True(t, meta.lockCalled, "lock") | 
|  | 85 | +} | 
|  | 86 | + | 
|  | 87 | +func TestStateChangeFromLocked(t *testing.T) { | 
|  | 88 | +	action := action{rule: &rule{State: "change"}} | 
|  | 89 | +	meta := &mockLockableEventData{MockEventData: mock.MockEventData{}, locked: true} | 
|  | 90 | +	config := &mock.MockConfiguration{} | 
|  | 91 | +	action.Apply(config, meta) | 
|  | 92 | +	assert.Nil(t, action.err, "shouldn't error") | 
|  | 93 | +	assert.False(t, meta.locked, "should be unlocked") | 
|  | 94 | +	assert.True(t, meta.unlockCalled, "unlock") | 
|  | 95 | +} | 
0 commit comments