Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#1282): fix validation for missing references #1296

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ changes.
- Remove wrongly appended `Yourself` filter on DRep Directory [Issue 1028](https://github.com/IntersectMBO/govtool/issues/1028)
- Fix validation of uris in metadata [Issue 1011](https://github.com/IntersectMBO/govtool/issues/1011)
- Fix wrong link to the GA Details once it is in progress [Issue 1252](https://github.com/IntersectMBO/govtool/issues/1252)
- Fix validation of the GAs with missing references [Issue 1282](https://github.com/IntersectMBO/govtool/issues/1282)

### Changed

Expand Down
50 changes: 0 additions & 50 deletions govtool/metadata-validation/src/app.controller.spec.ts

This file was deleted.

164 changes: 164 additions & 0 deletions govtool/metadata-validation/src/app.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { Test, TestingModule } from '@nestjs/testing';
import { HttpService } from '@nestjs/axios';
import { of, throwError } from 'rxjs';
import * as blake from 'blakejs';

import { AppService } from './app.service';
import { ValidateMetadataDTO } from '@dto';
import { MetadataValidationStatus } from '@enums';
import { MetadataStandard } from '@types';
import { canonizeJSON, validateMetadataStandard, parseMetadata } from '@utils';
import { AxiosResponse, AxiosRequestHeaders } from 'axios';

jest.mock('@utils');

describe('AppService', () => {
let service: AppService;
let httpService: HttpService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
AppService,
{
provide: HttpService,
useValue: {
get: jest.fn(),
},
},
],
}).compile();

service = module.get<AppService>(AppService);
httpService = module.get<HttpService>(HttpService);
});

it('should validate metadata correctly', async () => {
const url = 'http://example.com';
const hash = 'correctHash';
const standard = MetadataStandard.CIP108;
const validateMetadataDTO: ValidateMetadataDTO = { hash, url, standard };
const data = {
body: 'testBody',
headers: {},
};
const canonizedMetadata = 'canonizedMetadata';
const parsedMetadata = { parsed: 'metadata' };
const response: AxiosResponse = {
data,
status: 200,
statusText: 'OK',
headers: {},
config: {
headers: {} as AxiosRequestHeaders,
url,
},
};
jest.spyOn(httpService, 'get').mockReturnValueOnce(of(response));
(validateMetadataStandard as jest.Mock).mockResolvedValueOnce(undefined);
(parseMetadata as jest.Mock).mockReturnValueOnce(parsedMetadata);
(canonizeJSON as jest.Mock).mockResolvedValueOnce(canonizedMetadata);
jest.spyOn(blake, 'blake2bHex').mockReturnValueOnce(hash);

const result = await service.validateMetadata(validateMetadataDTO);

expect(result).toEqual({
status: undefined,
valid: true,
metadata: parsedMetadata,
});
expect(validateMetadataStandard).toHaveBeenCalledWith(data, standard);
expect(parseMetadata).toHaveBeenCalledWith(data.body, standard);
expect(canonizeJSON).toHaveBeenCalledWith(data);
});

it('should handle URL_NOT_FOUND error', async () => {
const url = 'http://example.com';
const hash = 'correctHash';
const validateMetadataDTO: ValidateMetadataDTO = { hash, url };

jest
.spyOn(httpService, 'get')
.mockReturnValueOnce(
throwError(() => MetadataValidationStatus.URL_NOT_FOUND),
);

const result = await service.validateMetadata(validateMetadataDTO);

expect(result).toEqual({
status: MetadataValidationStatus.URL_NOT_FOUND,
valid: false,
metadata: undefined,
});
});

it('should handle INVALID_HASH error', async () => {
const url = 'http://example.com';
const hash = 'incorrectHash';
const standard = MetadataStandard.CIP108;
const validateMetadataDTO: ValidateMetadataDTO = { hash, url, standard };
const data = {
body: 'testBody',
};
const canonizedMetadata = 'canonizedMetadata';
const parsedMetadata = { parsed: 'metadata' };

const response: AxiosResponse = {
data,
status: 200,
statusText: 'OK',
headers: {},
config: {
headers: {} as AxiosRequestHeaders,
url,
},
};
jest.spyOn(httpService, 'get').mockReturnValueOnce(of(response));
(validateMetadataStandard as jest.Mock).mockResolvedValueOnce(undefined);
(parseMetadata as jest.Mock).mockReturnValueOnce(parsedMetadata);
(canonizeJSON as jest.Mock).mockResolvedValueOnce(canonizedMetadata);
jest.spyOn(blake, 'blake2bHex').mockReturnValueOnce('differentHash');

const result = await service.validateMetadata(validateMetadataDTO);

expect(result).toEqual({
status: MetadataValidationStatus.INVALID_HASH,
valid: false,
metadata: parsedMetadata,
});
});

it('should handle INVALID_JSONLD error', async () => {
const url = 'http://example.com';
const hash = 'correctHash';
const standard = MetadataStandard.CIP108;
const validateMetadataDTO: ValidateMetadataDTO = { hash, url, standard };
const data = {
body: 'testBody',
};
const parsedMetadata = { parsed: 'metadata' };

const response: AxiosResponse = {
data,
status: 200,
statusText: 'OK',
headers: {},
config: {
headers: {} as AxiosRequestHeaders,
url,
},
};
jest.spyOn(httpService, 'get').mockReturnValueOnce(of(response));
(validateMetadataStandard as jest.Mock).mockResolvedValueOnce(undefined);
(parseMetadata as jest.Mock).mockReturnValueOnce(parsedMetadata);
(canonizeJSON as jest.Mock).mockRejectedValueOnce(new Error());

const result = await service.validateMetadata(validateMetadataDTO);

expect(result).toEqual({
status: MetadataValidationStatus.INVALID_JSONLD,
valid: false,
metadata: parsedMetadata,
});
});
});
Loading