forked from mattn/lastpass-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
vault_test.go
166 lines (136 loc) · 5 KB
/
vault_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package lastpass
import (
"github.com/stretchr/testify/assert"
"os"
"testing"
"gopkg.in/jarcoal/httpmock.v1"
)
const (
GoLpEmail = "GO_LP_EMAIL"
GoLpPass = "GO_LP_PASS"
)
// DONT USE YOUR REAL LASTPASS ACCOUNT
// THESE TEST WILL WIPE YOUR DATA
//
// to include personal passwords,
// set env vars GO_LP_EMAIL & GO_LP_PASS
//
// DONT USE YOUR REAL LASTPASS ACCOUNT
// THESE TEST WILL WIPE YOUR DATA
var config = struct {
email string
password string
}{
email: os.Getenv(GoLpEmail),
password: os.Getenv(GoLpPass),
}
func TestInvalidEmail(t *testing.T) {
lp, err := New("[email protected]", "fakepassword")
assert.Nil(t, lp)
assert.EqualError(t, err, ErrInvalidEmail.Error())
}
func TestInvalidPassword(t *testing.T) {
lp, err := New(config.email, "fakepassword")
assert.Nil(t, lp)
assert.EqualError(t, err, ErrInvalidPassword.Error())
}
func TestCRUD(t *testing.T) {
needsLogin(t)
accs := map[string]*Account{
"site1": {Name: "site1", Username: "[email protected]", Password: "site1", Url: "site1.com"},
"site2": {Name: "site2", Username: "[email protected]", Password: "site2", Url: "site2.com"},
"site3": {Name: "site3", Username: "[email protected]", Password: "site3", Url: "site2.com"},
}
lp, err := New(config.email, config.password)
assert.NoError(t, err)
assert.NotNil(t, lp)
// start fresh
mustDeleteAccounts(lp)
for _, a := range accs {
newa, err := lp.CreateAccount(a)
assert.NoError(t, err)
assert.NotNil(t, newa)
}
actuals, err := lp.GetAccounts()
assert.NoError(t, err)
assert.Equal(t, len(accs), len(actuals))
for _, act := range actuals {
acc, exists := accs[act.Name]
assert.True(t, exists)
assert.Equal(t, acc.Username, act.Username)
assert.Nil(t, lp.DeleteAccount(act))
}
actuals, err = lp.GetAccounts()
assert.NoError(t, err)
assert.Empty(t, actuals)
}
func TestChangePassword(t *testing.T) {
needsLogin(t)
lp, err:=New(config.email, config.password)
assert.NoError(t, err)
assert.NotNil(t, lp)
acc := &Account{Name: "testchange", Username: "[email protected]", Password: "site1", Url: "site1.com"}
acc, err = lp.CreateAccount(acc)
assert.NotEqual(t, "0", acc.Id)
assert.NoError(t, err)
acc.Password = "newpass"
_, err = lp.UpdateAccount(acc)
assert.NoError(t, err)
a, err :=lp.GetAccount(acc.Id)
assert.NoError(t, err)
assert.Equal(t, "newpass", a.Password)
assert.NoError(t, lp.DeleteAccountById(a.Id))
}
func TestIncorrectGoogleAuthCode(t *testing.T) {
if os.Getenv("MOCK_LP") != "" {
t.Logf("running %s in mock mode", t.Name())
httpmock.Activate()
defer httpmock.DeactivateAndReset()
data := `<response><error message="Google Authenticator authentication failed!" cause="googleauthfailed" allowmultifactortrust="true" tempuid="160828192" trustexpired="0" trustlabel="" hidedisable="false" /></response>`
httpmock.RegisterResponder("POST", buildLastPassURL(iterationsPage).String(), httpmock.NewStringResponder(200, "5461"))
httpmock.RegisterResponder("POST", buildLastPassURL(loginPage).String(), httpmock.NewStringResponder(200, data))
}
lp, err := New("[email protected]", "qwerty123", WithMultiFactor("-3"))
assert.Nil(t, lp)
assert.EqualError(t, err, ErrInvalidGoogleAuthCode.Error())
}
func TestNoGoogleAuthCodeGiven(t *testing.T) {
if os.Getenv("MOCK_LP") != "" {
t.Logf("running %s in mock mode", t.Name())
httpmock.Activate()
defer httpmock.DeactivateAndReset()
data := `<response><error message="Google Authenticator authentication required! Upgrade your browser extension so you can enter it." cause="googleauthrequired" allowmultifactortrust="true" tempuid="160828192" trustexpired="0" trustlabel="" hidedisable="false" /></response>`
httpmock.RegisterResponder("POST", buildLastPassURL(iterationsPage).String(), httpmock.NewStringResponder(200, "5461"))
httpmock.RegisterResponder("POST", buildLastPassURL(loginPage).String(), httpmock.NewStringResponder(200, data))
}
lp, err := New("[email protected]", "qwerty123")
assert.Nil(t, lp)
assert.EqualError(t, err, ErrInvalidGoogleAuthCode.Error())
}
func TestInvalidYubiKey(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
t.Logf("running %s in mock mode", t.Name())
data := `<response><error message="blah blah" cause="yubikeyrestricted" allowmultifactortrust="true" tempuid="160828192" trustexpired="0" trustlabel="" hidedisable="false" /></response>`
httpmock.RegisterResponder("POST", buildLastPassURL(iterationsPage).String(), httpmock.NewStringResponder(200, "5461"))
httpmock.RegisterResponder("POST", buildLastPassURL(loginPage).String(), httpmock.NewStringResponder(200, data))
lp, err := New("[email protected]", "qwerty123")
assert.Nil(t, lp)
assert.EqualError(t, err, ErrInvalidYubiKey.Error())
}
func mustDeleteAccounts(lp *Vault) {
accs, err := lp.GetAccounts()
if err != nil {
panic(err)
}
for _, act := range accs {
if err = lp.DeleteAccount(act); err != nil {
panic(err)
}
}
}
func needsLogin(t *testing.T) {
if config.email == "" || config.password == "" {
t.Skipf("skipping test %s. Login requirement not met", t.Name())
}
}