-
Notifications
You must be signed in to change notification settings - Fork 86
/
keyring_mock_test.go
118 lines (94 loc) · 2.71 KB
/
keyring_mock_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
package keyring
import (
"errors"
"testing"
)
// TestSet tests setting a user and password in the keyring.
func TestMockSet(t *testing.T) {
mp := mockProvider{}
err := mp.Set(service, user, password)
if err != nil {
t.Errorf("Should not fail, got: %s", err)
}
}
// TestGet tests getting a password from the keyring.
func TestMockGet(t *testing.T) {
mp := mockProvider{}
err := mp.Set(service, user, password)
if err != nil {
t.Errorf("Should not fail, got: %s", err)
}
pw, err := mp.Get(service, user)
if err != nil {
t.Errorf("Should not fail, got: %s", err)
}
if password != pw {
t.Errorf("Expected password %s, got %s", password, pw)
}
}
// TestGetNonExisting tests getting a secret not in the keyring.
func TestMockGetNonExisting(t *testing.T) {
mp := mockProvider{}
_, err := mp.Get(service, user+"fake")
assertError(t, err, ErrNotFound)
}
// TestDelete tests deleting a secret from the keyring.
func TestMockDelete(t *testing.T) {
mp := mockProvider{}
err := mp.Set(service, user, password)
if err != nil {
t.Errorf("Should not fail, got: %s", err)
}
err = mp.Delete(service, user)
if err != nil {
t.Errorf("Should not fail, got: %s", err)
}
}
// TestDeleteNonExisting tests deleting a secret not in the keyring.
func TestMockDeleteNonExisting(t *testing.T) {
mp := mockProvider{}
err := mp.Delete(service, user+"fake")
assertError(t, err, ErrNotFound)
}
func TestMockWithError(t *testing.T) {
mp := mockProvider{mockError: errors.New("mock error")}
err := mp.Set(service, user, password)
assertError(t, err, mp.mockError)
_, err = mp.Get(service, user)
assertError(t, err, mp.mockError)
err = mp.Delete(service, user)
assertError(t, err, mp.mockError)
}
// TestMockDeleteAll tests deleting all secrets for a given service.
func TestMockDeleteAll(t *testing.T) {
mp := mockProvider{}
// Set up multiple secrets for the same service
err := mp.Set(service, user, password)
if err != nil {
t.Errorf("Should not fail, got: %s", err)
}
err = mp.Set(service, user+"2", password+"2")
if err != nil {
t.Errorf("Should not fail, got: %s", err)
}
// Delete all secrets for the service
err = mp.DeleteAll(service)
if err != nil {
t.Errorf("Should not fail, got: %s", err)
}
// Verify that all secrets for the service are deleted
_, err = mp.Get(service, user)
assertError(t, err, ErrNotFound)
_, err = mp.Get(service, user+"2")
assertError(t, err, ErrNotFound)
// Verify that DeleteAll on an empty service doesn't cause an error
err = mp.DeleteAll(service)
if err != nil {
t.Errorf("Should not fail on empty service, got: %s", err)
}
}
func assertError(t *testing.T, err error, expected error) {
if err != expected {
t.Errorf("Expected error %s, got %s", expected, err)
}
}