Skip to content

Commit

Permalink
feat(secrets): fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinandan13jan committed Feb 10, 2025
1 parent e528812 commit da46146
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/components/Secrets/SecretsForm/AddSecretForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const AddSecretForm: React.FC = () => {
navigate(-1);
}}
onSubmit={(values, actions) => {
addSecret(values, workspace, namespace)
addSecret(values, namespace)
.then(() => {
navigate(`/workspaces/${workspace}/secrets`);
})
Expand Down
4 changes: 0 additions & 4 deletions src/components/Secrets/__data__/mock-secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@ import {
SecretTypeDropdownLabel,
SourceSecretType,
} from '../../../types';
import { THUMBNAIL_ANNOTATION } from '../../ApplicationThumbnail';

export const mockApplicationRequestData = {
apiVersion: `${ApplicationModel.apiGroup}/${ApplicationModel.apiVersion}`,
kind: ApplicationModel.kind,
metadata: {
name: 'test-application',
namespace: 'test-ns',
annotations: {
[THUMBNAIL_ANNOTATION]: '7',
},
},
spec: {
displayName: 'test-application',
Expand Down
9 changes: 2 additions & 7 deletions src/components/Secrets/utils/service-account-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ import { K8sQueryPatchResource, K8sGetResource } from '../../../k8s';
import { ServiceAccountModel } from '../../../models/service-account';
import { SecretKind, ServiceAccountKind } from '../../../types';

export const linkSecretToServiceAccount = async (
secret: SecretKind,
namespace: string,
workspace: string,
) => {
export const linkSecretToServiceAccount = async (secret: SecretKind, namespace: string) => {
if (!secret || (!namespace && !secret.metadata?.namespace)) {
return;
}
const serviceAccount = await K8sGetResource<ServiceAccountKind>({
model: ServiceAccountModel,
queryOptions: { name: PIPELINE_SERVICE_ACCOUNT, ns: namespace, ws: workspace },
queryOptions: { name: PIPELINE_SERVICE_ACCOUNT, ns: namespace },
});

const existingIPSecrets = serviceAccount?.imagePullSecrets as SecretKind[];
Expand All @@ -31,7 +27,6 @@ export const linkSecretToServiceAccount = async (
queryOptions: {
name: PIPELINE_SERVICE_ACCOUNT,
ns: namespace,
ws: workspace,
},
patches: [
{
Expand Down
54 changes: 26 additions & 28 deletions src/utils/__tests__/create-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
getSecretObject,
addSecret,
} from '../create-utils';
import { createK8sUtilMock, mockWindowFetch } from '../test-utils';
import { mockWindowFetch } from '../test-utils';

jest.mock('../../k8s/k8s-fetch', () => ({
k8sCreateResource: jest.fn(() => Promise.resolve()),
Expand All @@ -38,16 +38,9 @@ jest.mock('../../components/Secrets/utils/service-account-utils', () => {
return { linkSecretToServiceAccount: jest.fn() };
});

const commonFetchMock = createK8sUtilMock('commonFetch');

const createResourceMock = k8sCreateResource as jest.Mock;
const linkSecretToServiceAccountMock = linkSecretToServiceAccount as jest.Mock;

jest.mock('../../components/ApplicationThumbnail', () => {
const actual = jest.requireActual('../../components/ApplicationThumbnail');
return { ...actual, getRandomSvgNumber: () => 7 };
});

describe('Create Utils', () => {
beforeEach(() => {
mockWindowFetch();
Expand Down Expand Up @@ -400,8 +393,8 @@ describe('Create Utils', () => {
});

it('should add correct values for Image pull secret', async () => {
commonFetchMock.mockClear();
commonFetchMock.mockImplementationOnce((props) => Promise.resolve(props));
createResourceMock.mockClear();
createResourceMock.mockImplementationOnce((props) => Promise.resolve(props));

await createSecret(
{
Expand All @@ -413,19 +406,20 @@ describe('Create Utils', () => {
false,
);

expect(commonFetchMock).toHaveBeenCalledTimes(1);
expect(createResourceMock).toHaveBeenCalledTimes(1);

expect(commonFetchMock).toHaveBeenCalledWith(
'/workspaces/test-ws/api/v1/namespaces/test-ns/secrets',
expect(createResourceMock).toHaveBeenCalledWith(
expect.objectContaining({
body: expect.stringContaining('"test":"test-value"'),
model: SecretModel,
queryOptions: { ns: 'test-ns' },
resource: expect.objectContaining({ stringData: { test: 'test-value' } }),
}),
);
});

it('should create a Source secret', async () => {
commonFetchMock.mockClear();
commonFetchMock.mockImplementationOnce((props) => Promise.resolve(props));
createResourceMock.mockClear();
createResourceMock.mockImplementationOnce((props) => Promise.resolve(props));

await createSecret(
{
Expand All @@ -437,19 +431,20 @@ describe('Create Utils', () => {
false,
);

expect(commonFetchMock).toHaveBeenCalledTimes(1);
expect(createResourceMock).toHaveBeenCalledTimes(1);

expect(commonFetchMock).toHaveBeenCalledWith(
'/workspaces/test-ws/api/v1/namespaces/test-ns/secrets',
expect(createResourceMock).toHaveBeenCalledWith(
expect.objectContaining({
body: expect.stringContaining('"type":"kubernetes.io/basic-auth"'),
model: SecretModel,
queryOptions: { ns: 'test-ns' },
resource: expect.objectContaining({ type: 'kubernetes.io/basic-auth' }),
}),
);
});

it('should add correct data for Source secret', async () => {
commonFetchMock.mockClear();
commonFetchMock.mockImplementationOnce((props) => Promise.resolve(props));
createResourceMock.mockClear();
createResourceMock.mockImplementationOnce((props) => Promise.resolve(props));

await createSecret(
{
Expand All @@ -461,12 +456,15 @@ describe('Create Utils', () => {
false,
);

expect(commonFetchMock).toHaveBeenCalledTimes(1);
expect(createResourceMock).toHaveBeenCalledTimes(1);

expect(commonFetchMock).toHaveBeenCalledWith(
'/workspaces/test-ws/api/v1/namespaces/test-ns/secrets',
expect(createResourceMock).toHaveBeenCalledWith(
expect.objectContaining({
body: expect.stringContaining('"username":"dGVzdDE=","password":"cGFzcy10ZXN0"'),
model: SecretModel,
queryOptions: { ns: 'test-ns' },
resource: expect.objectContaining({
stringData: { password: 'cGFzcy10ZXN0', username: 'dGVzdDE=' },
}),
}),
);
});
Expand All @@ -492,7 +490,7 @@ describe('Create Utils', () => {
});
it('should add secret', async () => {
createResourceMock.mockClear();
await addSecret(addSecretFormValues, 'test-ws', 'test-ns');
await addSecret(addSecretFormValues, 'test-ns');
expect(createResourceMock).toHaveBeenCalled();

expect(createResourceMock).toHaveBeenCalledWith(
Expand All @@ -506,7 +504,7 @@ describe('Create Utils', () => {

it('should call linkToServiceAccount For image pull secrets', async () => {
linkSecretToServiceAccountMock.mockClear();
await addSecret(addSecretFormValues, 'test-ws', 'test-ns');
await addSecret(addSecretFormValues, 'test-ns');
expect(linkSecretToServiceAccountMock).toHaveBeenCalled();
expect(linkSecretToServiceAccountMock).toHaveBeenCalledWith(
expect.objectContaining({ metadata: expect.objectContaining({ name: 'test' }) }),
Expand Down
11 changes: 3 additions & 8 deletions src/utils/create-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ export const getSecretObject = (values: SecretFormValues, namespace: string): Se

export const createSecretResource = async (
values: AddSecretFormValues,
workspace: string,
namespace: string,
dryRun: boolean,
) => {
Expand All @@ -374,7 +373,7 @@ export const createSecretResource = async (
};
// if image pull secret, link to service account
if (typeToLabel(secretResource.type) === SecretTypeDisplayLabel.imagePull) {
await linkSecretToServiceAccount(secretResource, namespace, workspace);
await linkSecretToServiceAccount(secretResource, namespace);
}

return await K8sQueryCreateResource({
Expand All @@ -384,12 +383,8 @@ export const createSecretResource = async (
});
};

export const addSecret = async (
values: AddSecretFormValues,
workspace: string,
namespace: string,
) => {
return await createSecretResource(values, workspace, namespace, false);
export const addSecret = async (values: AddSecretFormValues, namespace: string) => {
return await createSecretResource(values, namespace, false);
};

export const createSecret = async (secret: ImportSecret, namespace: string, dryRun: boolean) => {
Expand Down

0 comments on commit da46146

Please sign in to comment.