mirror of
https://github.com/1Password/onepassword-operator.git
synced 2025-10-21 23:18:06 +00:00
Update tests to use testify mock
This commit is contained in:
@@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
|
||||
kind: CustomResourceDefinition
|
||||
metadata:
|
||||
annotations:
|
||||
controller-gen.kubebuilder.io/version: v0.13.0
|
||||
controller-gen.kubebuilder.io/version: v0.14.0
|
||||
name: onepassworditems.onepassword.com
|
||||
spec:
|
||||
group: onepassword.com
|
||||
@@ -20,14 +20,19 @@ spec:
|
||||
description: OnePasswordItem is the Schema for the onepassworditems API
|
||||
properties:
|
||||
apiVersion:
|
||||
description: 'APIVersion defines the versioned schema of this representation
|
||||
of an object. Servers should convert recognized schemas to the latest
|
||||
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
|
||||
description: |-
|
||||
APIVersion defines the versioned schema of this representation of an object.
|
||||
Servers should convert recognized schemas to the latest internal value, and
|
||||
may reject unrecognized values.
|
||||
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
|
||||
type: string
|
||||
kind:
|
||||
description: 'Kind is a string value representing the REST resource this
|
||||
object represents. Servers may infer this from the endpoint the client
|
||||
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
|
||||
description: |-
|
||||
Kind is a string value representing the REST resource this object represents.
|
||||
Servers may infer this from the endpoint the client submits requests to.
|
||||
Cannot be updated.
|
||||
In CamelCase.
|
||||
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
|
||||
type: string
|
||||
metadata:
|
||||
type: object
|
||||
|
@@ -2,9 +2,6 @@ package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/1Password/connect-sdk-go/onepassword"
|
||||
"github.com/1Password/onepassword-operator/pkg/mocks"
|
||||
op "github.com/1Password/onepassword-operator/pkg/onepassword"
|
||||
"time"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
@@ -17,6 +14,7 @@ import (
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
|
||||
onepasswordv1 "github.com/1Password/onepassword-operator/api/v1"
|
||||
op "github.com/1Password/onepassword-operator/pkg/onepassword"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -106,17 +104,8 @@ var _ = Describe("Deployment controller", func() {
|
||||
}
|
||||
|
||||
mockGetItemFunc := func() {
|
||||
mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
|
||||
item := onepassword.Item{}
|
||||
item.Fields = []*onepassword.ItemField{}
|
||||
for k, v := range item1.Data {
|
||||
item.Fields = append(item.Fields, &onepassword.ItemField{Label: k, Value: v})
|
||||
}
|
||||
item.Version = item1.Version
|
||||
item.Vault.ID = vaultUUID
|
||||
item.ID = uuid
|
||||
return &item, nil
|
||||
}
|
||||
// mock GetItemByID to return test item 'item1'
|
||||
mockGetItemByIDFunc.Return(item1.ToModel(), nil)
|
||||
}
|
||||
|
||||
BeforeEach(func() {
|
||||
@@ -151,17 +140,10 @@ var _ = Describe("Deployment controller", func() {
|
||||
|
||||
It("Should update existing K8s Secret using deployment", func() {
|
||||
By("Updating secret")
|
||||
mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
|
||||
item := onepassword.Item{}
|
||||
item.Fields = []*onepassword.ItemField{}
|
||||
for k, v := range item2.Data {
|
||||
item.Fields = append(item.Fields, &onepassword.ItemField{Label: k, Value: v})
|
||||
}
|
||||
item.Version = item2.Version
|
||||
item.Vault.ID = vaultUUID
|
||||
item.ID = uuid
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
// mock GetItemByID to return test item 'item2'
|
||||
mockGetItemByIDFunc.Return(item2.ToModel(), nil)
|
||||
|
||||
Eventually(func() error {
|
||||
updatedDeployment := &appsv1.Deployment{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
|
@@ -2,10 +2,6 @@ package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/1Password/connect-sdk-go/onepassword"
|
||||
"github.com/1Password/onepassword-operator/pkg/mocks"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
@@ -16,6 +12,7 @@ import (
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
|
||||
onepasswordv1 "github.com/1Password/onepassword-operator/api/v1"
|
||||
"github.com/1Password/onepassword-operator/pkg/onepassword/model"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -32,17 +29,8 @@ var _ = Describe("OnePasswordItem controller", func() {
|
||||
err = k8sClient.DeleteAllOf(context.Background(), &v1.Secret{}, client.InNamespace(namespace))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
|
||||
item := onepassword.Item{}
|
||||
item.Fields = []*onepassword.ItemField{}
|
||||
for k, v := range item1.Data {
|
||||
item.Fields = append(item.Fields, &onepassword.ItemField{Label: k, Value: v})
|
||||
}
|
||||
item.Version = item1.Version
|
||||
item.Vault.ID = vaultUUID
|
||||
item.ID = uuid
|
||||
return &item, nil
|
||||
}
|
||||
item := item1.ToModel()
|
||||
mockGetItemByIDFunc.Return(item, nil)
|
||||
})
|
||||
|
||||
Context("Happy path", func() {
|
||||
@@ -99,17 +87,13 @@ var _ = Describe("OnePasswordItem controller", func() {
|
||||
"password": []byte("##newPassword##"),
|
||||
"extraField": []byte("dev"),
|
||||
}
|
||||
mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
|
||||
item := onepassword.Item{}
|
||||
item.Fields = []*onepassword.ItemField{}
|
||||
for k, v := range newData {
|
||||
item.Fields = append(item.Fields, &onepassword.ItemField{Label: k, Value: v})
|
||||
}
|
||||
item.Version = item1.Version + 1
|
||||
item.Vault.ID = vaultUUID
|
||||
item.ID = uuid
|
||||
return &item, nil
|
||||
|
||||
item := item2.ToModel()
|
||||
for k, v := range newData {
|
||||
item.Fields = append(item.Fields, model.ItemField{Label: k, Value: v})
|
||||
}
|
||||
mockGetItemByIDFunc.Return(item, nil)
|
||||
|
||||
_, err := onePasswordItemReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: key})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
@@ -178,18 +162,11 @@ var _ = Describe("OnePasswordItem controller", func() {
|
||||
"ice-cream-type": []byte(iceCream),
|
||||
}
|
||||
|
||||
mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
|
||||
item := onepassword.Item{}
|
||||
item.Title = "!my sECReT it3m%"
|
||||
item.Fields = []*onepassword.ItemField{}
|
||||
for k, v := range testData {
|
||||
item.Fields = append(item.Fields, &onepassword.ItemField{Label: k, Value: v})
|
||||
}
|
||||
item.Version = item1.Version + 1
|
||||
item.Vault.ID = vaultUUID
|
||||
item.ID = uuid
|
||||
return &item, nil
|
||||
item := item2.ToModel()
|
||||
for k, v := range testData {
|
||||
item.Fields = append(item.Fields, model.ItemField{Label: k, Value: v})
|
||||
}
|
||||
mockGetItemByIDFunc.Return(item, nil)
|
||||
|
||||
By("Creating a new OnePasswordItem successfully")
|
||||
Expect(k8sClient.Create(ctx, toCreate)).Should(Succeed())
|
||||
|
@@ -26,13 +26,12 @@ package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/1Password/onepassword-operator/pkg/mocks"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
@@ -45,6 +44,8 @@ import (
|
||||
"sigs.k8s.io/controller-runtime/pkg/log/zap"
|
||||
|
||||
onepasswordcomv1 "github.com/1Password/onepassword-operator/api/v1"
|
||||
"github.com/1Password/onepassword-operator/pkg/mocks"
|
||||
"github.com/1Password/onepassword-operator/pkg/onepassword/model"
|
||||
//+kubebuilder:scaffold:imports
|
||||
)
|
||||
|
||||
@@ -78,8 +79,11 @@ var (
|
||||
cancel context.CancelFunc
|
||||
onePasswordItemReconciler *OnePasswordItemReconciler
|
||||
deploymentReconciler *DeploymentReconciler
|
||||
mockGetItemByIDFunc *mock.Call
|
||||
|
||||
item1 = &TestItem{
|
||||
ItemID: "nwrhuano7bcwddcviubpp4mhfq",
|
||||
VaultID: "hfnjvi6aymbsnfc2xeeoheizda",
|
||||
Name: "test-item",
|
||||
Version: 123,
|
||||
Path: "vaults/hfnjvi6aymbsnfc2xeeoheizda/items/nwrhuano7bcwddcviubpp4mhfq",
|
||||
@@ -94,6 +98,8 @@ var (
|
||||
}
|
||||
|
||||
item2 = &TestItem{
|
||||
ItemID: "nwrhuano7bcwddcviubpp4mhf2",
|
||||
VaultID: "hfnjvi6aymbsnfc2xeeoheizd2",
|
||||
Name: "test-item2",
|
||||
Path: "vaults/hfnjvi6aymbsnfc2xeeoheizd2/items/nwrhuano7bcwddcviubpp4mhf2",
|
||||
Version: 456,
|
||||
@@ -109,6 +115,8 @@ var (
|
||||
)
|
||||
|
||||
type TestItem struct {
|
||||
ItemID string
|
||||
VaultID string
|
||||
Name string
|
||||
Version int
|
||||
Path string
|
||||
@@ -116,6 +124,20 @@ type TestItem struct {
|
||||
SecretData map[string][]byte
|
||||
}
|
||||
|
||||
func (ti *TestItem) ToModel() *model.Item {
|
||||
item := &model.Item{}
|
||||
item.Version = ti.Version
|
||||
item.VaultID = ti.VaultID
|
||||
item.ID = ti.ItemID
|
||||
|
||||
item.Fields = []model.ItemField{}
|
||||
for k, v := range ti.Data {
|
||||
item.Fields = append(item.Fields, model.ItemField{Label: k, Value: v})
|
||||
}
|
||||
|
||||
return item
|
||||
}
|
||||
|
||||
func TestAPIs(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
|
||||
@@ -153,12 +175,13 @@ var _ = BeforeSuite(func() {
|
||||
})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
opConnectClient := &mocks.TestClient{}
|
||||
mockOpClient := &mocks.TestClient{}
|
||||
mockGetItemByIDFunc = mockOpClient.On("GetItemByID", mock.Anything, mock.Anything)
|
||||
|
||||
onePasswordItemReconciler = &OnePasswordItemReconciler{
|
||||
Client: k8sManager.GetClient(),
|
||||
Scheme: k8sManager.GetScheme(),
|
||||
OpConnectClient: opConnectClient,
|
||||
Client: k8sManager.GetClient(),
|
||||
Scheme: k8sManager.GetScheme(),
|
||||
OpClient: mockOpClient,
|
||||
}
|
||||
err = (onePasswordItemReconciler).SetupWithManager(k8sManager)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
@@ -167,7 +190,7 @@ var _ = BeforeSuite(func() {
|
||||
deploymentReconciler = &DeploymentReconciler{
|
||||
Client: k8sManager.GetClient(),
|
||||
Scheme: k8sManager.GetScheme(),
|
||||
OpConnectClient: opConnectClient,
|
||||
OpClient: mockOpClient,
|
||||
OpAnnotationRegExp: r,
|
||||
}
|
||||
err = (deploymentReconciler).SetupWithManager(k8sManager)
|
||||
|
@@ -3,11 +3,10 @@ package kubernetessecrets
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/1Password/onepassword-operator/pkg/onepassword/model"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/1Password/connect-sdk-go/onepassword"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
@@ -21,10 +20,10 @@ func TestCreateKubernetesSecretFromOnePasswordItem(t *testing.T) {
|
||||
secretName := "test-secret-name"
|
||||
namespace := "test"
|
||||
|
||||
item := onepassword.Item{}
|
||||
item := model.Item{}
|
||||
item.Fields = generateFields(5)
|
||||
item.Version = 123
|
||||
item.Vault.ID = "hfnjvi6aymbsnfc2xeeoheizda"
|
||||
item.VaultID = "hfnjvi6aymbsnfc2xeeoheizda"
|
||||
item.ID = "h46bb3jddvay7nxopfhvlwg35q"
|
||||
|
||||
kubeClient := fake.NewClientBuilder().Build()
|
||||
@@ -49,10 +48,10 @@ func TestKubernetesSecretFromOnePasswordItemOwnerReferences(t *testing.T) {
|
||||
secretName := "test-secret-name"
|
||||
namespace := "test"
|
||||
|
||||
item := onepassword.Item{}
|
||||
item := model.Item{}
|
||||
item.Fields = generateFields(5)
|
||||
item.Version = 123
|
||||
item.Vault.ID = "hfnjvi6aymbsnfc2xeeoheizda"
|
||||
item.VaultID = "hfnjvi6aymbsnfc2xeeoheizda"
|
||||
item.ID = "h46bb3jddvay7nxopfhvlwg35q"
|
||||
|
||||
kubeClient := fake.NewClientBuilder().Build()
|
||||
@@ -94,10 +93,10 @@ func TestUpdateKubernetesSecretFromOnePasswordItem(t *testing.T) {
|
||||
secretName := "test-secret-update"
|
||||
namespace := "test"
|
||||
|
||||
item := onepassword.Item{}
|
||||
item := model.Item{}
|
||||
item.Fields = generateFields(5)
|
||||
item.Version = 123
|
||||
item.Vault.ID = "hfnjvi6aymbsnfc2xeeoheizda"
|
||||
item.VaultID = "hfnjvi6aymbsnfc2xeeoheizda"
|
||||
item.ID = "h46bb3jddvay7nxopfhvlwg35q"
|
||||
|
||||
kubeClient := fake.NewClientBuilder().Build()
|
||||
@@ -111,10 +110,10 @@ func TestUpdateKubernetesSecretFromOnePasswordItem(t *testing.T) {
|
||||
}
|
||||
|
||||
// Updating kubernetes secret with new item
|
||||
newItem := onepassword.Item{}
|
||||
newItem := model.Item{}
|
||||
newItem.Fields = generateFields(6)
|
||||
newItem.Version = 456
|
||||
newItem.Vault.ID = "hfnjvi6aymbsnfc2xeeoheizda"
|
||||
newItem.VaultID = "hfnjvi6aymbsnfc2xeeoheizda"
|
||||
newItem.ID = "h46bb3jddvay7nxopfhvlwg35q"
|
||||
err = CreateKubernetesSecretFromItem(kubeClient, secretName, namespace, &newItem, restartDeploymentAnnotation, secretLabels, secretType, nil)
|
||||
if err != nil {
|
||||
@@ -147,7 +146,7 @@ func TestBuildKubernetesSecretFromOnePasswordItem(t *testing.T) {
|
||||
annotations := map[string]string{
|
||||
annotationKey: annotationValue,
|
||||
}
|
||||
item := onepassword.Item{}
|
||||
item := model.Item{}
|
||||
item.Fields = generateFields(5)
|
||||
labels := map[string]string{}
|
||||
secretType := ""
|
||||
@@ -173,10 +172,10 @@ func TestBuildKubernetesSecretFixesInvalidLabels(t *testing.T) {
|
||||
"annotationKey": "annotationValue",
|
||||
}
|
||||
labels := map[string]string{}
|
||||
item := onepassword.Item{}
|
||||
item := model.Item{}
|
||||
secretType := ""
|
||||
|
||||
item.Fields = []*onepassword.ItemField{
|
||||
item.Fields = []model.ItemField{
|
||||
{
|
||||
Label: "label w%th invalid ch!rs-",
|
||||
Value: "value1",
|
||||
@@ -209,10 +208,10 @@ func TestCreateKubernetesTLSSecretFromOnePasswordItem(t *testing.T) {
|
||||
secretName := "tls-test-secret-name"
|
||||
namespace := "test"
|
||||
|
||||
item := onepassword.Item{}
|
||||
item := model.Item{}
|
||||
item.Fields = generateFields(5)
|
||||
item.Version = 123
|
||||
item.Vault.ID = "hfnjvi6aymbsnfc2xeeoheizda"
|
||||
item.VaultID = "hfnjvi6aymbsnfc2xeeoheizda"
|
||||
item.ID = "h46bb3jddvay7nxopfhvlwg35q"
|
||||
|
||||
kubeClient := fake.NewClientBuilder().Build()
|
||||
@@ -235,13 +234,13 @@ func TestCreateKubernetesTLSSecretFromOnePasswordItem(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func compareAnnotationsToItem(annotations map[string]string, item onepassword.Item, t *testing.T) {
|
||||
func compareAnnotationsToItem(annotations map[string]string, item model.Item, t *testing.T) {
|
||||
actualVaultId, actualItemId, err := ParseVaultIdAndItemIdFromPath(annotations[ItemPathAnnotation])
|
||||
if err != nil {
|
||||
t.Errorf("Was unable to parse Item Path")
|
||||
}
|
||||
if actualVaultId != item.Vault.ID {
|
||||
t.Errorf("Expected annotation vault id to be %v but was %v", item.Vault.ID, actualVaultId)
|
||||
if actualVaultId != item.VaultID {
|
||||
t.Errorf("Expected annotation vault id to be %v but was %v", item.VaultID, actualVaultId)
|
||||
}
|
||||
if actualItemId != item.ID {
|
||||
t.Errorf("Expected annotation item id to be %v but was %v", item.ID, actualItemId)
|
||||
@@ -255,7 +254,7 @@ func compareAnnotationsToItem(annotations map[string]string, item onepassword.It
|
||||
}
|
||||
}
|
||||
|
||||
func compareFields(actualFields []*onepassword.ItemField, secretData map[string][]byte, t *testing.T) {
|
||||
func compareFields(actualFields []model.ItemField, secretData map[string][]byte, t *testing.T) {
|
||||
for i := 0; i < len(actualFields); i++ {
|
||||
value, found := secretData[actualFields[i].Label]
|
||||
if !found {
|
||||
@@ -267,14 +266,13 @@ func compareFields(actualFields []*onepassword.ItemField, secretData map[string]
|
||||
}
|
||||
}
|
||||
|
||||
func generateFields(numToGenerate int) []*onepassword.ItemField {
|
||||
fields := []*onepassword.ItemField{}
|
||||
func generateFields(numToGenerate int) []model.ItemField {
|
||||
fields := []model.ItemField{}
|
||||
for i := 0; i < numToGenerate; i++ {
|
||||
field := onepassword.ItemField{
|
||||
fields = append(fields, model.ItemField{
|
||||
Label: "key" + fmt.Sprint(i),
|
||||
Value: "value" + fmt.Sprint(i),
|
||||
}
|
||||
fields = append(fields, &field)
|
||||
})
|
||||
}
|
||||
return fields
|
||||
}
|
||||
|
@@ -1,151 +1,37 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"github.com/1Password/connect-sdk-go/onepassword"
|
||||
"github.com/stretchr/testify/mock"
|
||||
|
||||
"github.com/1Password/onepassword-operator/pkg/onepassword/model"
|
||||
)
|
||||
|
||||
type TestClient struct {
|
||||
GetVaultsFunc func() ([]onepassword.Vault, error)
|
||||
GetVaultsByTitleFunc func(title string) ([]onepassword.Vault, error)
|
||||
GetVaultFunc func(uuid string) (*onepassword.Vault, error)
|
||||
GetVaultByUUIDFunc func(uuid string) (*onepassword.Vault, error)
|
||||
GetVaultByTitleFunc func(title string) (*onepassword.Vault, error)
|
||||
GetItemFunc func(itemQuery string, vaultQuery string) (*onepassword.Item, error)
|
||||
GetItemByUUIDFunc func(uuid string, vaultQuery string) (*onepassword.Item, error)
|
||||
GetItemByTitleFunc func(title string, vaultQuery string) (*onepassword.Item, error)
|
||||
GetItemsFunc func(vaultQuery string) ([]onepassword.Item, error)
|
||||
GetItemsByTitleFunc func(title string, vaultQuery string) ([]onepassword.Item, error)
|
||||
CreateItemFunc func(item *onepassword.Item, vaultQuery string) (*onepassword.Item, error)
|
||||
UpdateItemFunc func(item *onepassword.Item, vaultQuery string) (*onepassword.Item, error)
|
||||
DeleteItemFunc func(item *onepassword.Item, vaultQuery string) error
|
||||
DeleteItemByIDFunc func(itemUUID string, vaultQuery string) error
|
||||
DeleteItemByTitleFunc func(title string, vaultQuery string) error
|
||||
GetFilesFunc func(itemQuery string, vaultQuery string) ([]onepassword.File, error)
|
||||
GetFileFunc func(uuid string, itemQuery string, vaultQuery string) (*onepassword.File, error)
|
||||
GetFileContentFunc func(file *onepassword.File) ([]byte, error)
|
||||
DownloadFileFunc func(file *onepassword.File, targetDirectory string, overwrite bool) (string, error)
|
||||
LoadStructFromItemByUUIDFunc func(config interface{}, itemUUID string, vaultQuery string) error
|
||||
LoadStructFromItemByTitleFunc func(config interface{}, itemTitle string, vaultQuery string) error
|
||||
LoadStructFromItemFunc func(config interface{}, itemQuery string, vaultQuery string) error
|
||||
LoadStructFunc func(config interface{}) error
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
var (
|
||||
DoGetVaultsFunc func() ([]onepassword.Vault, error)
|
||||
DoGetVaultsByTitleFunc func(title string) ([]onepassword.Vault, error)
|
||||
DoGetVaultFunc func(uuid string) (*onepassword.Vault, error)
|
||||
DoGetVaultByUUIDFunc func(uuid string) (*onepassword.Vault, error)
|
||||
DoGetVaultByTitleFunc func(title string) (*onepassword.Vault, error)
|
||||
DoGetItemFunc func(itemQuery string, vaultQuery string) (*onepassword.Item, error)
|
||||
DoGetItemByUUIDFunc func(uuid string, vaultQuery string) (*onepassword.Item, error)
|
||||
DoGetItemByTitleFunc func(title string, vaultQuery string) (*onepassword.Item, error)
|
||||
DoGetItemsFunc func(vaultQuery string) ([]onepassword.Item, error)
|
||||
DoGetItemsByTitleFunc func(title string, vaultQuery string) ([]onepassword.Item, error)
|
||||
DoCreateItemFunc func(item *onepassword.Item, vaultQuery string) (*onepassword.Item, error)
|
||||
DoUpdateItemFunc func(item *onepassword.Item, vaultQuery string) (*onepassword.Item, error)
|
||||
DoDeleteItemFunc func(item *onepassword.Item, vaultQuery string) error
|
||||
DoDeleteItemByIDFunc func(itemUUID string, vaultQuery string) error
|
||||
DoDeleteItemByTitleFunc func(title string, vaultQuery string) error
|
||||
DoGetFilesFunc func(itemQuery string, vaultQuery string) ([]onepassword.File, error)
|
||||
DoGetFileFunc func(uuid string, itemQuery string, vaultQuery string) (*onepassword.File, error)
|
||||
DoGetFileContentFunc func(file *onepassword.File) ([]byte, error)
|
||||
DoDownloadFileFunc func(file *onepassword.File, targetDirectory string, overwrite bool) (string, error)
|
||||
DoLoadStructFromItemByUUIDFunc func(config interface{}, itemUUID string, vaultQuery string) error
|
||||
DoLoadStructFromItemByTitleFunc func(config interface{}, itemTitle string, vaultQuery string) error
|
||||
DoLoadStructFromItemFunc func(config interface{}, itemQuery string, vaultQuery string) error
|
||||
DoLoadStructFunc func(config interface{}) error
|
||||
)
|
||||
|
||||
// Do is the mock client's `Do` func
|
||||
|
||||
func (m *TestClient) GetVaults() ([]onepassword.Vault, error) {
|
||||
return DoGetVaultsFunc()
|
||||
func (tc *TestClient) GetItemByID(vaultID, itemID string) (*model.Item, error) {
|
||||
args := tc.Called(vaultID, itemID)
|
||||
if args.Get(0) == nil {
|
||||
return nil, args.Error(1)
|
||||
}
|
||||
return args.Get(0).(*model.Item), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *TestClient) GetVaultsByTitle(title string) ([]onepassword.Vault, error) {
|
||||
return DoGetVaultsByTitleFunc(title)
|
||||
func (tc *TestClient) GetItemsByTitle(vaultID, itemTitle string) ([]model.Item, error) {
|
||||
args := tc.Called(vaultID, itemTitle)
|
||||
return args.Get(0).([]model.Item), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *TestClient) GetVault(vaultQuery string) (*onepassword.Vault, error) {
|
||||
return DoGetVaultFunc(vaultQuery)
|
||||
func (tc *TestClient) GetFileContent(vaultID, itemID, fileID string) ([]byte, error) {
|
||||
args := tc.Called(vaultID, itemID, fileID)
|
||||
if args.Get(0) == nil {
|
||||
return nil, args.Error(1)
|
||||
}
|
||||
return args.Get(0).([]byte), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *TestClient) GetVaultByUUID(uuid string) (*onepassword.Vault, error) {
|
||||
return DoGetVaultByUUIDFunc(uuid)
|
||||
}
|
||||
|
||||
func (m *TestClient) GetVaultByTitle(title string) (*onepassword.Vault, error) {
|
||||
return DoGetVaultByTitleFunc(title)
|
||||
}
|
||||
|
||||
func (m *TestClient) GetItem(itemQuery string, vaultQuery string) (*onepassword.Item, error) {
|
||||
return DoGetItemFunc(itemQuery, vaultQuery)
|
||||
}
|
||||
|
||||
func (m *TestClient) GetItemByUUID(uuid string, vaultQuery string) (*onepassword.Item, error) {
|
||||
return DoGetItemByUUIDFunc(uuid, vaultQuery)
|
||||
}
|
||||
|
||||
func (m *TestClient) GetItemByTitle(title string, vaultQuery string) (*onepassword.Item, error) {
|
||||
return DoGetItemByTitleFunc(title, vaultQuery)
|
||||
}
|
||||
|
||||
func (m *TestClient) GetItems(vaultQuery string) ([]onepassword.Item, error) {
|
||||
return DoGetItemsFunc(vaultQuery)
|
||||
}
|
||||
|
||||
func (m *TestClient) GetItemsByTitle(title string, vaultQuery string) ([]onepassword.Item, error) {
|
||||
return DoGetItemsByTitleFunc(title, vaultQuery)
|
||||
}
|
||||
|
||||
func (m *TestClient) CreateItem(item *onepassword.Item, vaultQuery string) (*onepassword.Item, error) {
|
||||
return DoCreateItemFunc(item, vaultQuery)
|
||||
}
|
||||
|
||||
func (m *TestClient) UpdateItem(item *onepassword.Item, vaultQuery string) (*onepassword.Item, error) {
|
||||
return DoUpdateItemFunc(item, vaultQuery)
|
||||
}
|
||||
|
||||
func (m *TestClient) DeleteItem(item *onepassword.Item, vaultQuery string) error {
|
||||
return DoDeleteItemFunc(item, vaultQuery)
|
||||
}
|
||||
|
||||
func (m *TestClient) DeleteItemByID(itemUUID string, vaultQuery string) error {
|
||||
return DoDeleteItemByIDFunc(itemUUID, vaultQuery)
|
||||
}
|
||||
|
||||
func (m *TestClient) DeleteItemByTitle(title string, vaultQuery string) error {
|
||||
return DoDeleteItemByTitleFunc(title, vaultQuery)
|
||||
}
|
||||
|
||||
func (m *TestClient) GetFiles(itemQuery string, vaultQuery string) ([]onepassword.File, error) {
|
||||
return DoGetFilesFunc(itemQuery, vaultQuery)
|
||||
}
|
||||
|
||||
func (m *TestClient) GetFile(uuid string, itemQuery string, vaultQuery string) (*onepassword.File, error) {
|
||||
return DoGetFileFunc(uuid, itemQuery, vaultQuery)
|
||||
}
|
||||
|
||||
func (m *TestClient) GetFileContent(file *onepassword.File) ([]byte, error) {
|
||||
return DoGetFileContentFunc(file)
|
||||
}
|
||||
|
||||
func (m *TestClient) DownloadFile(file *onepassword.File, targetDirectory string, overwrite bool) (string, error) {
|
||||
return DoDownloadFileFunc(file, targetDirectory, overwrite)
|
||||
}
|
||||
|
||||
func (m *TestClient) LoadStructFromItemByUUID(config interface{}, itemUUID string, vaultQuery string) error {
|
||||
return DoLoadStructFromItemByUUIDFunc(config, itemUUID, vaultQuery)
|
||||
}
|
||||
|
||||
func (m *TestClient) LoadStructFromItemByTitle(config interface{}, itemTitle string, vaultQuery string) error {
|
||||
return DoLoadStructFromItemByTitleFunc(config, itemTitle, vaultQuery)
|
||||
}
|
||||
|
||||
func (m *TestClient) LoadStructFromItem(config interface{}, itemQuery string, vaultQuery string) error {
|
||||
return DoLoadStructFromItemFunc(config, itemQuery, vaultQuery)
|
||||
}
|
||||
|
||||
func (m *TestClient) LoadStruct(config interface{}) error {
|
||||
return DoLoadStructFunc(config)
|
||||
func (tc *TestClient) GetVaultsByTitle(title string) ([]model.Vault, error) {
|
||||
args := tc.Called(title)
|
||||
return args.Get(0).([]model.Vault), args.Error(1)
|
||||
}
|
||||
|
@@ -4,11 +4,14 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
|
||||
"github.com/1Password/onepassword-operator/pkg/mocks"
|
||||
"github.com/1Password/onepassword-operator/pkg/onepassword/model"
|
||||
|
||||
"github.com/1Password/connect-sdk-go/onepassword"
|
||||
"github.com/stretchr/testify/assert"
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
errors2 "k8s.io/apimachinery/pkg/api/errors"
|
||||
@@ -802,19 +805,20 @@ func TestUpdateSecretHandler(t *testing.T) {
|
||||
// Create a fake client to mock API calls.
|
||||
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objs...).Build()
|
||||
|
||||
opConnectClient := &mocks.TestClient{}
|
||||
mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
|
||||
|
||||
item := onepassword.Item{}
|
||||
item.Fields = generateFields(testData.opItem["username"], testData.opItem["password"])
|
||||
item.Version = itemVersion
|
||||
item.Vault.ID = vaultUUID
|
||||
item.ID = uuid
|
||||
return &item, nil
|
||||
}
|
||||
mockOpClient := &mocks.TestClient{}
|
||||
mockOpClient.On("GetItemByID", mock.Anything, mock.Anything).Return(createItem(), nil)
|
||||
//mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
|
||||
//
|
||||
// item := onepassword.Item{}
|
||||
// item.Fields = generateFields(testData.opItem["username"], testData.opItem["password"])
|
||||
// item.Version = itemVersion
|
||||
// item.Vault.ID = vaultUUID
|
||||
// item.ID = uuid
|
||||
// return &item, nil
|
||||
//}
|
||||
h := &SecretUpdateHandler{
|
||||
client: cl,
|
||||
opConnectClient: opConnectClient,
|
||||
opClient: mockOpClient,
|
||||
shouldAutoRestartDeploymentsGlobal: testData.globalAutoRestartEnabled,
|
||||
}
|
||||
|
||||
@@ -879,16 +883,23 @@ func TestIsUpdatedSecret(t *testing.T) {
|
||||
assert.True(t, isUpdatedSecret(secretName, updatedSecrets))
|
||||
}
|
||||
|
||||
func generateFields(username, password string) []*onepassword.ItemField {
|
||||
fields := []*onepassword.ItemField{
|
||||
{
|
||||
Label: "username",
|
||||
Value: username,
|
||||
},
|
||||
{
|
||||
Label: "password",
|
||||
Value: password,
|
||||
func createItem() *model.Item {
|
||||
return &model.Item{
|
||||
ID: itemId,
|
||||
VaultID: vaultId,
|
||||
Version: itemVersion,
|
||||
Tags: []string{"tag1", "tag2"},
|
||||
Fields: []model.ItemField{
|
||||
{
|
||||
Label: "username",
|
||||
Value: username,
|
||||
},
|
||||
{
|
||||
Label: "password",
|
||||
Value: password,
|
||||
},
|
||||
},
|
||||
Files: []model.File{},
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
return fields
|
||||
}
|
||||
|
Reference in New Issue
Block a user