-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
144 lines (119 loc) · 4.18 KB
/
integration_test.go
File metadata and controls
144 lines (119 loc) · 4.18 KB
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
package passwordmanager_test
import (
"os"
"path/filepath"
"testing"
"github.com/simp-lee/passwordmanager/internal/crypto"
"github.com/simp-lee/passwordmanager/internal/generator"
"github.com/simp-lee/passwordmanager/internal/model"
"github.com/simp-lee/passwordmanager/internal/storage"
)
// Integration Test: Test the basic workflow
func TestBasicFlow(t *testing.T) {
// Create a temporary directory
tmpDir, err := os.MkdirTemp("", "passwordmanager-integration-*")
if err != nil {
t.Fatalf("Failed to create temporary directory: %v", err)
}
defer os.RemoveAll(tmpDir) // Clean up the temporary directory afterwards
// 1. Initialize storage
store, err := storage.New(tmpDir)
if err != nil {
t.Fatalf("Failed to initialize storage: %v", err)
}
// 2. Create vault
masterPassword := "test-master-password"
if err := store.CreateVault(masterPassword); err != nil {
t.Fatalf("Failed to create vault: %v", err)
}
// 3. Unlock vault
if err := store.UnlockVault(masterPassword); err != nil {
t.Fatalf("Failed to unlock vault: %v", err)
}
// 4. Generate password
passwordOpts := generator.DefaultOptions()
password, err := generator.GeneratePassword(passwordOpts)
if err != nil {
t.Fatalf("Failed to generate password: %v", err)
}
// 5. Encrypt password
encryptedPassword, err := crypto.Encrypt([]byte(password), store.GetEncryptionKey())
if err != nil {
t.Fatalf("Failed to encrypt password: %v", err)
}
// 6. Add account
account := &model.Account{
ID: "test-account-id",
Platform: "TestPlatform",
Username: "test-username",
Email: "test@example.com",
EncryptedPassword: encryptedPassword,
URL: "https://test.example.com",
Notes: "Test account notes",
}
if err := store.AddAccount(account); err != nil {
t.Fatalf("Failed to add account: %v", err)
}
// 7. Get and verify account
retrievedAccount, err := store.GetAccountByID(account.ID)
if err != nil {
t.Fatalf("Failed to get account: %v", err)
}
if retrievedAccount.Platform != account.Platform {
t.Errorf("Expected platform %s, got %s", account.Platform, retrievedAccount.Platform)
}
// 8. Decrypt password
decryptedPasswordBytes, err := crypto.Decrypt(retrievedAccount.EncryptedPassword, store.GetEncryptionKey())
if err != nil {
t.Fatalf("Failed to decrypt password: %v", err)
}
if string(decryptedPasswordBytes) != password {
t.Errorf("Decrypted password does not match original password")
}
// 9. Change master password
newMasterPassword := "new-master-password"
if err := store.ChangeMasterPassword(newMasterPassword); err != nil {
t.Fatalf("Failed to change master password: %v", err)
}
// 10. Unlock with new password
if err := store.UnlockVault(newMasterPassword); err != nil {
t.Fatalf("Failed to unlock with new password: %v", err)
}
// 11. Verify data is still accessible
accounts, err := store.GetAccounts()
if err != nil {
t.Fatalf("Failed to get accounts after password change: %v", err)
}
if len(accounts) != 1 {
t.Errorf("Expected 1 account after password change, got %d", len(accounts))
}
// 12. Export and Import Test
exportPath := filepath.Join(tmpDir, "exported-vault")
if err := store.ExportVault(exportPath); err != nil {
t.Fatalf("Failed to export vault: %v", err)
}
// Create a new storage instance in a different directory
newStore, err := storage.New(filepath.Join(tmpDir, "new-data"))
if err != nil {
t.Fatalf("Failed to create new storage instance: %v", err)
}
// Import the exported vault
if err := newStore.ImportVault(exportPath); err != nil {
t.Fatalf("Failed to import vault: %v", err)
}
// Unlock the imported vault with the new password
if err := newStore.UnlockVault(newMasterPassword); err != nil {
t.Fatalf("Failed to unlock imported vault: %v", err)
}
// Verify imported data
importedAccounts, err := newStore.GetAccounts()
if err != nil {
t.Fatalf("Failed to get accounts from imported vault: %v", err)
}
if len(importedAccounts) != 1 {
t.Errorf("Expected 1 account after import, got %d", len(importedAccounts))
}
if importedAccounts[0].ID != account.ID {
t.Errorf("Imported account ID mismatch: expected %s, got %s", account.ID, importedAccounts[0].ID)
}
}