Skip to content

Commit

Permalink
refactor: rename getStorageServiceLink to uploadData
Browse files Browse the repository at this point in the history
Signed-off-by: Nam Hoang <[email protected]>
  • Loading branch information
namhoang1604 committed Oct 15, 2024
1 parent d4fdd6f commit 4e93bfd
Show file tree
Hide file tree
Showing 20 changed files with 84 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { fireEvent, render, screen } from '@testing-library/react';
import { act } from 'react-dom/test-utils';
import { ConformityCredential } from '../components';
import { FetchOptions } from '../types/conformityCredential.types';
import { getJsonDataFromConformityAPI, getStorageServiceLink, getValueByPath } from '@mock-app/services';
import { getJsonDataFromConformityAPI, uploadData, getValueByPath } from '@mock-app/services';
import { checkStoredCredentialsConfig } from '../components/ConformityCredential/utils';

jest.mock('@mock-app/services', () => ({
getStorageServiceLink: jest.fn(),
uploadData: jest.fn(),
generateUUID: jest.fn(),
getJsonDataFromConformityAPI: jest.fn(),
}));
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('ConformityCredential', () => {
(getJsonDataFromConformityAPI as jest.Mock).mockResolvedValue(apiResp);
(getValueByPath as jest.Mock).mockReturnValue(apiResp);
(checkStoredCredentialsConfig as jest.Mock).mockReturnValue({ ok: true, value: '' });
(getStorageServiceLink as jest.Mock).mockResolvedValue('https://storage.example.com/credential');
(uploadData as jest.Mock).mockResolvedValue('https://storage.example.com/credential');

await act(async () => {
render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
validateAndConstructVerifyURL,
generateUUID,
getJsonDataFromConformityAPI,
getStorageServiceLink,
uploadData,
getValueByPath,
} from '@mock-app/services';

Expand Down Expand Up @@ -172,7 +172,7 @@ export const ConformityCredential: React.FC<IConformityCredentialProps> = ({
return;
}

const vcUrl = await getStorageServiceLink(storedCredentialsConfig, extractedCredential, generateUUID());
const vcUrl = await uploadData(storedCredentialsConfig, extractedCredential, generateUUID());
const verifyURL = validateAndConstructVerifyURL(vcUrl);
saveConformityCredentials(credentialRequestConfig.credentialName, verifyURL, credentialRequestConfig.appOnly);

Expand Down
28 changes: 22 additions & 6 deletions packages/services/src/__tests__/aggregationEvent.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vckitService from '../vckit.service';
import { getStorageServiceLink } from '../storage.service';
import { uploadData } from '../storage.service';
import * as linkResolverService from '../linkResolver.service';
import { IAggregationEventContext } from '../types/types';
import { Result } from '../types/validateContext';
Expand All @@ -13,13 +13,14 @@ jest.mock('../vckit.service', () => ({
issueVC: jest.fn(),
}));
jest.mock('../storage.service', () => ({
getStorageServiceLink: jest.fn(),
uploadData: jest.fn(),
}));
jest.mock('../linkResolver.service', () => ({
registerLinkResolver: jest.fn(),
createLinkResolver: jest.fn(),
IdentificationKeyType: jest.fn(),
getLinkResolverIdentifier: jest.fn(),
getLinkResolverIdentifierFromURI: jest.fn(),
LinkType: {
verificationLinkType: 'gs1:verificationService',
certificationLinkType: 'gs1:certificationInfo',
Expand Down Expand Up @@ -55,7 +56,7 @@ describe('processAggregationEvent', () => {

it('should process aggregation event', async () => {
(vckitService.issueVC as jest.Mock).mockImplementationOnce(() => aggregationVCMock);
(getStorageServiceLink as jest.Mock).mockResolvedValueOnce('https://exampleStorage.com/vc.json');
(uploadData as jest.Mock).mockResolvedValueOnce('https://exampleStorage.com/vc.json');

jest
.spyOn(validateContext, 'validateAggregationEventContext')
Expand All @@ -67,7 +68,7 @@ describe('processAggregationEvent', () => {
const aggregationVC = await processAggregationEvent(aggregationEvent, context);

expect(aggregationVC).toBe(aggregationVCMock);
expect(getStorageServiceLink).toHaveBeenCalled();
expect(uploadData).toHaveBeenCalled();
expect(validateContext.validateAggregationEventContext).toHaveBeenCalled();
expect(linkResolverService.registerLinkResolver).toHaveBeenCalled();
});
Expand Down Expand Up @@ -119,6 +120,11 @@ describe('processAggregationEvent', () => {
jest
.spyOn(linkResolverService, 'getLinkResolverIdentifier')
.mockReturnValueOnce({ identifier: '0123456789', qualifierPath: '/10/ABC123' });
jest.spyOn(linkResolverService, 'getLinkResolverIdentifierFromURI').mockReturnValueOnce({
identifier: '0123456789',
qualifierPath: '/10/ABC123',
elementString: '01012345678910ABC123',
});
jest.spyOn(publicAPI, 'post').mockRejectedValueOnce("Can't issue VC");

await processAggregationEvent(aggregationEvent, invalidIssuerContext);
Expand All @@ -142,6 +148,11 @@ describe('processAggregationEvent', () => {
jest
.spyOn(linkResolverService, 'getLinkResolverIdentifier')
.mockReturnValueOnce({ identifier: '0123456789', qualifierPath: '/10/ABC123' });
jest.spyOn(linkResolverService, 'getLinkResolverIdentifierFromURI').mockReturnValueOnce({
identifier: '0123456789',
qualifierPath: '/10/ABC123',
elementString: '01012345678910ABC123',
});
jest.spyOn(publicAPI, 'put').mockRejectedValueOnce('Invalid storage provider');

await processAggregationEvent(aggregationEvent, invalidStorageContext);
Expand All @@ -160,7 +171,7 @@ describe('processAggregationEvent', () => {
dlr: { ...context.dlr, dlrAPIUrl: 'http://invalid-dlr.com' },
};
(vckitService.issueVC as jest.Mock).mockImplementationOnce(() => aggregationVCMock);
(getStorageServiceLink as jest.Mock).mockImplementation(({ url, _data, path }) => {
(uploadData as jest.Mock).mockImplementation(({ url, _data, path }) => {
return `${url}/${path}`;
});
jest
Expand All @@ -169,6 +180,11 @@ describe('processAggregationEvent', () => {
jest
.spyOn(linkResolverService, 'getLinkResolverIdentifier')
.mockReturnValueOnce({ identifier: '0123456789', qualifierPath: '/10/ABC123' });
jest.spyOn(linkResolverService, 'getLinkResolverIdentifierFromURI').mockReturnValueOnce({
identifier: '0123456789',
qualifierPath: '/10/ABC123',
elementString: '01012345678910ABC123',
});
jest.spyOn(linkResolverService, 'createLinkResolver').mockRejectedValueOnce('Invalid DLR API link resolver url');

await processAggregationEvent(aggregationEvent, invalidDLRContext);
Expand All @@ -177,7 +193,7 @@ describe('processAggregationEvent', () => {
expect(error.message).toBe('Invalid DLR API link resolver url');
expect(validateContext.validateAggregationEventContext).toHaveBeenCalled();
expect(vckitService.issueVC).toHaveBeenCalled();
expect(getStorageServiceLink).toHaveBeenCalled();
expect(uploadData).toHaveBeenCalled();
}
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ITraceabilityEvent } from '../../types';
import { processObjectEvent } from '../../epcisEvents/objectEvent';
import * as vckitService from '../../vckit.service';
import { getStorageServiceLink } from '../../storage.service';
import { uploadData } from '../../storage.service';
import * as linkResolverService from '../../linkResolver.service';
import { IAggregationEventContext, IObjectEventContext } from '../../types';
import { Result } from '../../types/validateContext';
Expand All @@ -12,7 +12,7 @@ jest.mock('../../vckit.service', () => ({
issueVC: jest.fn(),
}));
jest.mock('../../storage.service', () => ({
getStorageServiceLink: jest.fn(),
uploadData: jest.fn(),
}));
jest.mock('../../linkResolver.service', () => ({
registerLinkResolver: jest.fn(),
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('processObjectEvent', () => {
(vckitService.issueVC as jest.Mock).mockImplementation(() => ({
credentialSubject: { id: 'https://example.com/123' },
}));
(getStorageServiceLink as jest.Mock).mockResolvedValue('https://exampleStorage.com/vc.json');
(uploadData as jest.Mock).mockResolvedValue('https://exampleStorage.com/vc.json');

jest
.spyOn(validateContext, 'validateObjectEventContext')
Expand Down
2 changes: 2 additions & 0 deletions packages/services/src/__tests__/mocks/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ export const contextDPP = {
dlr: {
dlrAPIUrl: 'http://dlr.example.com',
dlrAPIKey: '1234',
namespace: 'gs1',
linkRegisterPath: '/api/resolver',
},
storage: {
url: 'https://storage.example.com',
Expand Down
22 changes: 14 additions & 8 deletions packages/services/src/__tests__/processDPP.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { processDPP } from '../processDPP.service';
import { issueVC, contextDefault } from '../vckit.service';
import { getStorageServiceLink } from '../storage.service';
import { uploadData } from '../storage.service';
import { registerLinkResolver, IdentificationKeyType, LinkType } from '../linkResolver.service';
import { contextDPP, dataDPP } from './mocks/constants';

Expand All @@ -10,7 +10,7 @@ jest.mock('../vckit.service', () => ({
}));

jest.mock('../storage.service', () => ({
getStorageServiceLink: jest.fn(),
uploadData: jest.fn(),
}));

jest.mock('../linkResolver.service', () => ({
Expand All @@ -24,10 +24,9 @@ jest.mock('../linkResolver.service', () => ({
verificationLinkType: 'gs1:verificationService',
certificationLinkType: 'gs1:certificationInfo',
epcisLinkType: 'gs1:epcis',
}
},
}));


describe('processDPP', () => {
describe('successful case', () => {
afterEach(() => {
Expand Down Expand Up @@ -56,7 +55,7 @@ describe('processDPP', () => {
});

it('should call process DPP', async () => {
(getStorageServiceLink as jest.Mock).mockImplementation(({ url, _data, path }) => {
(uploadData as jest.Mock).mockImplementation(({ url, _data, path }) => {
return `${url}/${dataDPP.data.herd.identifier}`;
});

Expand All @@ -75,7 +74,7 @@ describe('processDPP', () => {
linkTitle,
verificationPage,
dlrAPIKey,
identificationKey
identificationKey,
});
return `${dlrAPIUrl}/${identificationKeyType}/${identificationKey}?linkType=all`;
},
Expand All @@ -84,9 +83,15 @@ describe('processDPP', () => {
const vc = await processDPP(dataDPP, contextDPP);
expect(vc).toEqual({
vc: expectVCResult,
linkResolver: contextDPP.dpp.dlrVerificationPage + '/' + contextDPP.dpp.dlrIdentificationKeyType + '/' + dataDPP.data.herd.identifier + '?linkType=all',
linkResolver:
contextDPP.dpp.dlrVerificationPage +
'/' +
contextDPP.dpp.dlrIdentificationKeyType +
'/' +
dataDPP.data.herd.identifier +
'?linkType=all',
});
expect(getStorageServiceLink).toHaveBeenCalled();
expect(uploadData).toHaveBeenCalled();
expect(registerLinkResolver).toHaveBeenCalled();

const dppContext = contextDPP.dpp;
Expand All @@ -100,6 +105,7 @@ describe('processDPP', () => {
dppContext.dlrVerificationPage,
dlrContext.dlrAPIUrl,
dlrContext.dlrAPIKey,
dlrContext.namespace,
dataDPP.qualifierPath,
LinkType.certificationLinkType,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vckitService from '../vckit.service';
import { getStorageServiceLink } from '../storage.service';
import { uploadData } from '../storage.service';
import * as linkResolverService from '../linkResolver.service';
import { Result } from '../types/validateContext';
import * as validateContext from '../validateContext';
Expand All @@ -11,7 +11,7 @@ jest.mock('../vckit.service', () => ({
issueVC: jest.fn(),
}));
jest.mock('../storage.service', () => ({
getStorageServiceLink: jest.fn(),
uploadData: jest.fn(),
}));
jest.mock('../linkResolver.service', () => ({
registerLinkResolver: jest.fn(),
Expand Down Expand Up @@ -40,7 +40,7 @@ describe('processDigitalFacilityRecord', () => {
(vckitService.issueVC as jest.Mock).mockImplementation(() => ({
credentialSubject: { id: 'https://example.com/123' },
}));
(getStorageServiceLink as jest.Mock).mockResolvedValue('https://exampleStorage.com/vc.json');
(uploadData as jest.Mock).mockResolvedValue('https://exampleStorage.com/vc.json');

jest
.spyOn(validateContext, 'validateDigitalFacilityRecordContext')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vckitService from '../vckit.service';
import { getStorageServiceLink } from '../storage.service';
import { uploadData } from '../storage.service';
import * as linkResolverService from '../linkResolver.service';
import { Result } from '../types/validateContext';
import * as validateContext from '../validateContext';
Expand All @@ -11,7 +11,7 @@ jest.mock('../vckit.service', () => ({
issueVC: jest.fn(),
}));
jest.mock('../storage.service', () => ({
getStorageServiceLink: jest.fn(),
uploadData: jest.fn(),
}));
jest.mock('../linkResolver.service', () => ({
registerLinkResolver: jest.fn(),
Expand Down Expand Up @@ -40,7 +40,7 @@ describe('processDigitalIdentityAnchor', () => {
(vckitService.issueVC as jest.Mock).mockImplementation(() => ({
credentialSubject: { id: 'https://example.com/123' },
}));
(getStorageServiceLink as jest.Mock).mockResolvedValue('https://exampleStorage.com/vc.json');
(uploadData as jest.Mock).mockResolvedValue('https://exampleStorage.com/vc.json');

jest
.spyOn(validateContext, 'validateDigitalIdentityAnchorContext')
Expand Down
4 changes: 1 addition & 3 deletions packages/services/src/__tests__/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ jest.mock('../utils/httpService', () => ({
},
}));


describe('storage service', () => {
afterEach(() => {
jest.resetAllMocks();
Expand Down Expand Up @@ -42,7 +41,6 @@ describe('storage service', () => {
});
expect(url).toEqual({
url: `https://dev-verifiable-credentials.com/${path}`,

});
});

Expand All @@ -64,7 +62,7 @@ describe('storage service', () => {
headers: [],
},
});
expect(url).toEqual(`https://dev-verifiable-credentials.com/${path}`);
expect(url).toEqual({ url: `https://dev-verifiable-credentials.com/${path}` });
});

// error cases
Expand Down
12 changes: 6 additions & 6 deletions packages/services/src/__tests__/transactionEvent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as linkResolverService from '../linkResolver.service';
import * as helpers from '../epcisEvents/helpers';
import * as validateContext from '../validateContext';
import { processTransactionEvent } from '../epcisEvents/transactionEvent';
import { getStorageServiceLink } from '../storage.service';
import { uploadData } from '../storage.service';
import { Result } from '../types/validateContext';
import { ITransactionEventContext } from '../types/types';
import { publicAPI } from '../utils/httpService';
Expand All @@ -13,7 +13,7 @@ jest.mock('../vckit.service', () => ({
issueVC: jest.fn(),
}));
jest.mock('../storage.service', () => ({
getStorageServiceLink: jest.fn(),
uploadData: jest.fn(),
}));
jest.mock('../linkResolver.service', () => ({
registerLinkResolver: jest.fn(),
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('processTransactionEvent', () => {

it('should process transaction event', async () => {
(vckitService.issueVC as jest.Mock).mockImplementationOnce(() => transactionVCMock);
(getStorageServiceLink as jest.Mock).mockImplementation(({ url, _data, path }) => {
(uploadData as jest.Mock).mockImplementation(({ url, _data, path }) => {
return `${url}/${path}`;
});
jest
Expand All @@ -82,7 +82,7 @@ describe('processTransactionEvent', () => {
vc: transactionVCMock,
linkResolver: transactionEventDLRMock,
});
expect(getStorageServiceLink).toHaveBeenCalled();
expect(uploadData).toHaveBeenCalled();
expect(validateContext.validateTransactionEventContext).toHaveBeenCalled();
expect(linkResolverService.registerLinkResolver).toHaveBeenCalled();
});
Expand Down Expand Up @@ -169,7 +169,7 @@ describe('processTransactionEvent', () => {
dlr: { ...context.dlr, dlrAPIUrl: 'http://invalid-dlr.com' },
};
(vckitService.issueVC as jest.Mock).mockImplementationOnce(() => transactionVCMock);
(getStorageServiceLink as jest.Mock).mockResolvedValueOnce('https://storage.com/vc.json');
(uploadData as jest.Mock).mockResolvedValueOnce('https://storage.com/vc.json');
jest
.spyOn(validateContext, 'validateTransactionEventContext')
.mockReturnValueOnce({ ok: true, value: context } as Result<ITransactionEventContext>);
Expand All @@ -181,7 +181,7 @@ describe('processTransactionEvent', () => {
expect(error.message).toBe('Invalid DLR API link resolver url');
expect(validateContext.validateTransactionEventContext).toHaveBeenCalled();
expect(vckitService.issueVC).toHaveBeenCalled();
expect(getStorageServiceLink).toHaveBeenCalled();
expect(uploadData).toHaveBeenCalled();
}
});
});
Loading

0 comments on commit 4e93bfd

Please sign in to comment.