-
Notifications
You must be signed in to change notification settings - Fork 1
/
entry_certificate_test.go
188 lines (150 loc) · 4.94 KB
/
entry_certificate_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package dvls
import (
"io"
"os"
"reflect"
"testing"
"time"
)
var (
testCertificateFilePath string
testCertificateEntryId string
testNewCertificateEntryFile EntryCertificate
testNewCertificateEntryURL EntryCertificate
testCertificateEntry EntryCertificate = EntryCertificate{
VaultId: testVaultId,
Name: "TestK8sCertificate",
Password: testEntryPassword,
Tags: []string{"test", "k8s"},
CertificateIdentifier: "test",
}
)
func Test_EntryCertificate(t *testing.T) {
testCertificateFilePath = os.Getenv("TEST_CERTIFICATE_FILE_PATH")
testCertificateEntryId = os.Getenv("TEST_CERTIFICATE_ENTRY_ID")
testCertificateEntry.ID = testCertificateEntryId
testCertificateEntry.VaultId = testVaultId
location, err := time.LoadLocation("America/Montreal")
if err != nil {
t.Fatal(err)
}
expiration := time.Date(2099, 1, 1, 0, 0, 0, 0, location)
testCertificateEntry.Expiration = expiration
t.Run("GetEntry", test_GetCertificateEntry)
t.Run("NewCertificateFile", test_NewCertificateEntryFile)
t.Run("NewCertificateURL", test_NewCertificateEntryURL)
t.Run("UpdateEntry", test_UpdateCertificateEntry)
t.Run("DeleteEntry", test_DeleteCertificateEntry)
}
func test_GetCertificateEntry(t *testing.T) {
testGetEntry := testCertificateEntry
entry, err := testClient.Entries.Certificate.Get(testGetEntry.ID)
if err != nil {
t.Fatal(err)
}
entry, err = testClient.Entries.Certificate.GetPassword(entry)
if err != nil {
t.Fatal(err)
}
entry.data = testGetEntry.data
if !entry.Expiration.Equal(testGetEntry.Expiration) {
t.Fatalf("fetched entry expiration did not match test entry. Expected %v, got %v", testGetEntry.Expiration, entry.Expiration)
}
entry.Expiration = testGetEntry.Expiration
if !reflect.DeepEqual(entry, testGetEntry) {
t.Fatalf("fetched entry did not match test entry. Expected %#v, got %#v", testGetEntry, entry)
}
}
func test_NewCertificateEntryFile(t *testing.T) {
entry := testCertificateEntry
entry.ID = ""
file, err := os.Open(testCertificateFilePath)
if err != nil {
t.Fatal(err)
}
fileBytes, err := io.ReadAll(file)
if err != nil {
t.Fatal("failed read file. error: %w", err)
}
stat, err := file.Stat()
if err != nil {
t.Fatal("failed read file. error: %w", err)
}
entry.CertificateIdentifier = stat.Name()
entry.UseDefaultCredentials = true
newEntry, err := testClient.Entries.Certificate.NewFile(entry, fileBytes)
if err != nil {
t.Fatal(err)
}
returnedFileBytes, err := testClient.Entries.Certificate.GetFileContent(newEntry.ID)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(fileBytes, returnedFileBytes) {
t.Fatalf("fetched file content did not match test file content. Expected %#v, got %#v", fileBytes, returnedFileBytes)
}
entry.ID = newEntry.ID
entry.data = newEntry.data
newEntry, err = testClient.Entries.Certificate.GetPassword(newEntry)
if err != nil {
t.Fatal(err)
}
testNewCertificateEntryFile = newEntry
if !entry.Expiration.Equal(newEntry.Expiration) {
t.Fatalf("fetched entry expiration did not match test entry. Expected %v, got %v", entry.Expiration, newEntry.Expiration)
}
entry.Expiration = newEntry.Expiration
if !reflect.DeepEqual(entry, newEntry) {
t.Fatalf("fetched entry did not match test entry. Expected %#v, got %#v", entry, newEntry)
}
}
func test_NewCertificateEntryURL(t *testing.T) {
entry := testCertificateEntry
entry.ID = ""
entry.CertificateIdentifier = "https://devolutions.net/"
newEntry, err := testClient.Entries.Certificate.NewURL(entry)
if err != nil {
t.Fatal(err)
}
entry.ID = newEntry.ID
entry.data = newEntry.data
newEntry, err = testClient.Entries.Certificate.GetPassword(newEntry)
if err != nil {
t.Fatal(err)
}
testNewCertificateEntryURL = newEntry
if !entry.Expiration.Equal(newEntry.Expiration) {
t.Fatalf("fetched entry expiration did not match test entry. Expected %v, got %v", entry.Expiration, newEntry.Expiration)
}
entry.Expiration = newEntry.Expiration
if !reflect.DeepEqual(entry, newEntry) {
t.Fatalf("fetched entry did not match test entry. Expected %#v, got %#v", entry, newEntry)
}
}
func test_UpdateCertificateEntry(t *testing.T) {
testUpdatedEntry := testNewCertificateEntryURL
testUpdatedEntry.Name = "TestK8sUpdatedEntry"
entry, err := testClient.Entries.Certificate.Update(testUpdatedEntry)
if err != nil {
t.Fatal(err)
}
entry, err = testClient.Entries.Certificate.GetPassword(entry)
if err != nil {
t.Fatal(err)
}
entry.data = testUpdatedEntry.data
if !reflect.DeepEqual(entry, testUpdatedEntry) {
t.Fatalf("fetched entry did not match test entry. Expected %#v, got %#v", testUpdatedEntry, entry)
}
testNewCertificateEntryURL = entry
}
func test_DeleteCertificateEntry(t *testing.T) {
err := testClient.Entries.Certificate.Delete(testNewCertificateEntryURL.ID)
if err != nil {
t.Fatal(err)
}
err = testClient.Entries.Certificate.Delete(testNewCertificateEntryFile.ID)
if err != nil {
t.Fatal(err)
}
}