From b35acb7d13248b8dbfbc0635a2e308994eba77ec Mon Sep 17 00:00:00 2001 From: Volodymyr Zotov Date: Tue, 8 Jul 2025 16:57:59 -0500 Subject: [PATCH] Add test case for item with file --- .../onepassworditem_controller_test.go | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/internal/controller/onepassworditem_controller_test.go b/internal/controller/onepassworditem_controller_test.go index 58cf9f2..701de59 100644 --- a/internal/controller/onepassworditem_controller_test.go +++ b/internal/controller/onepassworditem_controller_test.go @@ -2,6 +2,7 @@ package controller import ( "context" + "fmt" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -303,6 +304,54 @@ var _ = Describe("OnePasswordItem controller", func() { }, timeout, interval).Should(BeTrue()) Expect(secret.Type).Should(Equal(v1.SecretType(customType))) }) + + It("Should handle 1Password Item with a file and populate secret correctly", func() { + ctx := context.Background() + spec := onepasswordv1.OnePasswordItemSpec{ + ItemPath: item1.Path, + } + + key := types.NamespacedName{ + Name: "item-with-file", + Namespace: namespace, + } + + toCreate := &onepasswordv1.OnePasswordItem{ + ObjectMeta: metav1.ObjectMeta{ + Name: key.Name, + Namespace: key.Namespace, + }, + Spec: spec, + } + + fileContent := []byte("dummy-cert-content") + item := item1.ToModel() + item.Files = []model.File{ + { + ID: "file-id-123", + Name: "server.crt", + ContentPath: fmt.Sprintf("/v1/vaults/%s/items/%s/files/file-id-123/content", item.VaultID, item.ID), + }, + } + item.Files[0].SetContent(fileContent) + + mockGetItemByIDFunc.Return(item, nil) + mockGetItemByIDFunc.On("GetFileContent", item.VaultID, item.ID, "file-id-123").Return(fileContent, nil) + + By("Creating a new OnePasswordItem with file successfully") + Expect(k8sClient.Create(ctx, toCreate)).Should(Succeed()) + + createdSecret := &v1.Secret{} + Eventually(func() bool { + err := k8sClient.Get(ctx, key, createdSecret) + if err != nil { + return false + } + return true + }, timeout, interval).Should(BeTrue()) + + Expect(createdSecret.Data).Should(HaveKeyWithValue("server.crt", fileContent)) + }) }) Context("Unhappy path", func() {