-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_controller_test.go
130 lines (96 loc) · 4.02 KB
/
auth_controller_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"testing"
"github.com/abjrcode/swervo/internal/eventing"
"github.com/abjrcode/swervo/internal/migrations"
"github.com/abjrcode/swervo/internal/security/vault"
"github.com/abjrcode/swervo/internal/testhelpers"
"github.com/stretchr/testify/require"
)
func initAuthController(t *testing.T) (*AuthController, *testhelpers.MockClock) {
db, err := migrations.NewInMemoryMigratedDatabase(t, "auth-controller-tests.db")
require.NoError(t, err)
mockClock := testhelpers.NewMockClock()
bus := eventing.NewEventbus(db, mockClock)
vault := vault.NewVault(db, bus, mockClock)
controller := NewAuthController(vault)
return controller, mockClock
}
func TestAuthController_IsVaultConfigured(t *testing.T) {
controller, mockTimeProvider := initAuthController(t)
ctx := testhelpers.NewMockAppContext()
isVaultConfigured, err := controller.IsVaultConfigured(ctx)
require.NoError(t, err)
require.False(t, isVaultConfigured)
mockTimeProvider.On("NowUnix").Return(1)
err = controller.ConfigureVault(ctx, Auth_ConfigureVaultCommandInput{Password: "password"})
require.NoError(t, err)
isVaultConfigured, err = controller.IsVaultConfigured(ctx)
require.NoError(t, err)
require.True(t, isVaultConfigured)
}
func TestAuthController_ConfigureVault(t *testing.T) {
controller, mockTimeProvider := initAuthController(t)
ctx := testhelpers.NewMockAppContext()
mockTimeProvider.On("NowUnix").Return(1)
err := controller.ConfigureVault(ctx, Auth_ConfigureVaultCommandInput{Password: "password"})
require.NoError(t, err)
isVaultConfigured, err := controller.IsVaultConfigured(ctx)
require.NoError(t, err)
require.True(t, isVaultConfigured)
}
func TestAuthController_UnlockVault(t *testing.T) {
controller, mockTimeProvider := initAuthController(t)
ctx := testhelpers.NewMockAppContext()
mockTimeProvider.On("NowUnix").Return(1)
err := controller.ConfigureVault(ctx, Auth_ConfigureVaultCommandInput{Password: "password"})
require.NoError(t, err)
mockTimeProvider.On("NowUnix").Return(2)
unlocked, err := controller.UnlockVault(ctx, Auth_UnlockCommandInput{Password: "password"})
require.NoError(t, err)
require.True(t, unlocked)
}
func TestAuthController_UnlockVault_WithWrongPassword(t *testing.T) {
controller, mockTimeProvider := initAuthController(t)
ctx := testhelpers.NewMockAppContext()
mockTimeProvider.On("NowUnix").Return(1)
err := controller.ConfigureVault(ctx, Auth_ConfigureVaultCommandInput{Password: "password"})
require.NoError(t, err)
controller.LockVault(ctx)
mockTimeProvider.On("NowUnix").Return(2)
unlocked, err := controller.UnlockVault(ctx, Auth_UnlockCommandInput{Password: "wrong-password"})
require.NoError(t, err)
require.False(t, unlocked)
}
func TestAuthController_UnlockVault_WithoutConfiguringFirst(t *testing.T) {
controller, _ := initAuthController(t)
ctx := testhelpers.NewMockAppContext()
_, err := controller.UnlockVault(ctx, Auth_UnlockCommandInput{
Password: "password",
})
require.Error(t, err, vault.ErrVaultNotConfigured)
}
func TestAuthController_LockVault_WithoutConfiguringFirst(t *testing.T) {
controller, _ := initAuthController(t)
mockContext := testhelpers.NewMockAppContext()
controller.LockVault(mockContext)
}
func TestAuthController_LockVault_WithoutUnlockingFirst(t *testing.T) {
controller, mockTimeProvider := initAuthController(t)
ctx := testhelpers.NewMockAppContext()
mockTimeProvider.On("NowUnix").Return(1)
err := controller.ConfigureVault(ctx, Auth_ConfigureVaultCommandInput{Password: "password"})
require.NoError(t, err)
controller.LockVault(ctx)
}
func TestAuthController_UnlockVault_WithoutLockingFirst(t *testing.T) {
controller, mockTimeProvider := initAuthController(t)
ctx := testhelpers.NewMockAppContext()
mockTimeProvider.On("NowUnix").Return(1)
err := controller.ConfigureVault(ctx, Auth_ConfigureVaultCommandInput{Password: "password"})
require.NoError(t, err)
mockTimeProvider.On("NowUnix").Return(2)
unlocked, err := controller.UnlockVault(ctx, Auth_UnlockCommandInput{Password: "password"})
require.NoError(t, err)
require.True(t, unlocked)
}