76 lines
2.4 KiB
Go
76 lines
2.4 KiB
Go
package entries
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestNewBitwardenExport(t *testing.T) {
|
|
export := NewBitwardenExport([]Secret{{
|
|
ID: "c381ca2b-0f53-4b05-b209-f2eb11f05e19",
|
|
Description: "Example",
|
|
URL: "https://example.com/login",
|
|
Notes: "A note",
|
|
Username: "person@example.com",
|
|
Password: "correct horse battery staple",
|
|
}})
|
|
|
|
if export.Encrypted {
|
|
t.Fatal("plaintext Bitwarden export must set encrypted to false")
|
|
}
|
|
if export.Folders == nil || len(export.Folders) != 0 {
|
|
t.Fatalf("expected a non-nil empty folders array, got %#v", export.Folders)
|
|
}
|
|
if len(export.Items) != 1 {
|
|
t.Fatalf("expected one item, got %d", len(export.Items))
|
|
}
|
|
|
|
item := export.Items[0]
|
|
if item.Type != bitwardenLoginItemType {
|
|
t.Fatalf("expected login item type %d, got %d", bitwardenLoginItemType, item.Type)
|
|
}
|
|
if item.Name != "Example" || item.Notes != "A note" {
|
|
t.Fatalf("item metadata was not mapped: %#v", item)
|
|
}
|
|
if item.Login.Username != "person@example.com" || item.Login.Password != "correct horse battery staple" {
|
|
t.Fatalf("login credentials were not mapped: %#v", item.Login)
|
|
}
|
|
if len(item.Login.URIs) != 1 || item.Login.URIs[0].URI != "https://example.com/login" {
|
|
t.Fatalf("login URI was not mapped: %#v", item.Login.URIs)
|
|
}
|
|
encoded, err := json.Marshal(export)
|
|
if err != nil {
|
|
t.Fatalf("marshal export: %v", err)
|
|
}
|
|
|
|
var document map[string]any
|
|
if err := json.Unmarshal(encoded, &document); err != nil {
|
|
t.Fatalf("unmarshal export: %v", err)
|
|
}
|
|
if _, ok := document["encrypted"]; !ok {
|
|
t.Fatal("export is missing encrypted field")
|
|
}
|
|
if _, ok := document["folders"]; !ok {
|
|
t.Fatal("export is missing folders field")
|
|
}
|
|
if _, ok := document["items"]; !ok {
|
|
t.Fatal("export is missing items field")
|
|
}
|
|
|
|
encodedItem := document["items"].([]any)[0].(map[string]any)
|
|
for _, field := range []string{"id", "organizationId", "folderId", "revisionDate", "creationDate", "deletedDate", "passwordHistory", "collectionIds"} {
|
|
if _, ok := encodedItem[field]; ok {
|
|
t.Fatalf("import-only export must not include server-owned field %q", field)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNewBitwardenExportWithoutURL(t *testing.T) {
|
|
export := NewBitwardenExport([]Secret{{Description: "No URL"}})
|
|
item := export.Items[0]
|
|
|
|
if item.Login.URIs == nil || len(item.Login.URIs) != 0 {
|
|
t.Fatalf("expected an empty URI array, got %#v", item.Login.URIs)
|
|
}
|
|
}
|