Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hrax committed Aug 25, 2024
1 parent e9709ec commit 6d882cf
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 105 deletions.
148 changes: 99 additions & 49 deletions spec/linter/Linter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,95 +1,145 @@
/* eslint-disable camelcase */
import fs from "fs";
import { UpdateXMLScan } from "../../src/linter/UpdateXMLScan.js";
import { Linter } from "../../src/linter/Linter.js";
import { Profile } from "../../src/core/ProfileManager.js";
import { Linter, LinterOptions } from "../../src/linter/Linter.js";
import profileManager, { Profile } from "../../src/core/ProfileManager.js";
import { SNUpdateXMLData } from "../../src/core/sn.js";
import { RESTClient } from "../../src/core/RESTClient.js";
import oauthClient from "../../src/core/OAuthClient.js";
import { resetAllWhenMocks, when } from "jest-when";
import { defaultWhenImplementationThrow } from "../helpers.js";
import { ESLint } from "eslint";

describe("LinterSpec", () => {
/* beforeEach(() => {
payload = fs.readFileSync("./spec/payloads/sys_script_include_50ba882f07d610108110f2ae7c1ed00d.xml", {encoding: "utf8"});
data = {
"name": "sys_script_include_50ba882f07d610108110f2ae7c1ed00d",
"sys_id": "50ba882f07d610108110f2ae7c1ed00d",
"action": "INSERT_OR_UPDATE",
"sys_created_by": "admin",
"sys_created_on": "1970-01-01 00:00:01",
"sys_updated_by": "admin",
"sys_updated_on": "1970-01-01 00:00:01",
"type": "Script Include",
"target_name": "sys_script_include_50ba882f07d610108110f2ae7c1ed00d",
"update_set": "1234",
"payload": payload
};
}); */
const payload = fs.readFileSync("./spec/payloads/sys_script_include_50ba882f07d610108110f2ae7c1ed00d.xml", {encoding: "utf8"});
const data: SNUpdateXMLData = {
"name": "sys_script_include_50ba882f07d610108110f2ae7c1ed00d",
"sys_id": "50ba882f07d610108110f2ae7c1ed00d",
"action": "INSERT_OR_UPDATE",
"sys_created_by": "admin",
"sys_created_on": "1970-01-01 00:00:01",
"sys_updated_by": "admin",
"sys_updated_on": "1970-01-01 00:00:01",
"type": "Script Include",
"target_name": "sys_script_include_50ba882f07d610108110f2ae7c1ed00d",
"update_set": "1234",
"payload": payload,
"application": "global",
"payloadHash": -1234,
"sys_mod_count": 1
};

let profile: Profile, client: RESTClient;

beforeEach(() => {
resetAllWhenMocks();
profile = profileManager.fromData({
name: "dev1",
baseUrl: "https://example.com",
auth: {
clientID: "123",
clientSecret: "123",
lastRetrieved: 0,
type: "oauth-token",
token: {
access_token: "123",
refresh_token: "123",
expires_in: 60,
scope: "",
token_type: "Bearer"
}
}
});
client = new RESTClient(oauthClient);
});

it.skip("Fetch and prepare NowUpdateXML records", async() => {
const options = {
it("should fetch and prepare UpdateXML records", async() => {
const options: LinterOptions = {
"title": "Test Report",
"query": "name=Default"
};
const linter = <Linter> {};
// spyOn(linter.instance, "requestUpdateXMLByUpdateSetQuery").and.returnValue({result: [data]});
const linter = new Linter(profile, client, options);
const clientUpdateXMLSpy = jest.spyOn(client, "loadUpdateXMLByUpdateSetQuery");
when(clientUpdateXMLSpy)
.defaultImplementation(defaultWhenImplementationThrow)
.calledWith(profile, options.query).mockResolvedValue([data]);

await linter.fetch();
const changes = linter.changes;

// expect(linter.instance.requestUpdateXMLByUpdateSetQuery).toHaveBeenCalledWith(options.query);
// expect(linter.instance.requestUpdateXMLByUpdateSetQuery).toHaveBeenCalledTimes(1);
// expect(changes.size).toBe(1);
// expect(changes.values().next().value.id).toBe(data.sys_id);
expect(clientUpdateXMLSpy).toHaveBeenCalledTimes(1);
expect(changes.size).toBe(1);
expect((changes.values().next().value as UpdateXMLScan).ID).toBe(data.sys_id);
});

it.skip("Lint NowUpdateXML records", async () => {
it("Lint NowUpdateXML records", async() => {
profile.setTableConfiguration({
tables: {
"sys_script_include": {
name: "sys_script_include",
fields: {
"script": {
name: "script"
}
}
}
}
});
const options = {
"title": "Test Report",
"query": "name=Default"
};
const linter = <Linter> {};
const linter = new Linter(profile, client, options);
const clientUpdateXMLSpy = jest.spyOn(client, "loadUpdateXMLByUpdateSetQuery");
when(clientUpdateXMLSpy)
.defaultImplementation(defaultWhenImplementationThrow)
.calledWith(profile, options.query).mockResolvedValue([data]);

// Not testing NowLoader
// spyOn(linter.instance, "requestUpdateXMLByUpdateSetQuery").and.returnValue({result: [data]});

// Not testing ESLint
// spyOn(linter.eslint, "lintText").and.returnValue([{
// warningCount: 0,
// errorCount: 0
// }]);
const lintTextSpy = jest.spyOn(linter.getESLint(), "lintText").mockResolvedValue([
{
warningCount: 0,
errorCount: 0
} as ESLint.LintResult
]);

await linter.process();

const changes = linter.changes;

// expect(linter.instance.requestUpdateXMLByUpdateSetQuery).toHaveBeenCalledTimes(1);
// expect(linter.eslint.lintText).toHaveBeenCalledTimes(1);
expect(clientUpdateXMLSpy).toHaveBeenCalledTimes(1);
expect(lintTextSpy).toHaveBeenCalledTimes(1);
expect(changes.size).toBe(1);
expect(changes.values().next().value.status).toBe("OK");
console.log("SIZE " + changes.values().next().value.reports.size);
expect(changes.values().next().value.reports.keys().next().value).toBe("script");
expect(changes.values().next().value.getStatus()).toBe("OK");
expect(changes.values().next().value.getReports().keys().next().value).toBe("script");
});

it.skip("Ignore not configured tables", async () => {
it("Ignore not configured tables", async() => {
const options = {
"title": "Test Report",
"query": "name=Default"
};

const linter = <Linter> {};
const linter = new Linter(profile, client, options);

// Not testing NowLoader
// spyOn(linter.instance, "requestUpdateXMLByUpdateSetQuery").and.returnValue({result: [data]});
const clientUpdateXMLSpy = jest.spyOn(client, "loadUpdateXMLByUpdateSetQuery");
when(clientUpdateXMLSpy)
.defaultImplementation(defaultWhenImplementationThrow)
.calledWith(profile, options.query).mockResolvedValue([data]);

// Not testing ESLint
// spyOn(linter.eslint, "lintText").and.callThrough();
const lintTextSpy = jest.spyOn(linter.getESLint(), "lintText");

await linter.process();

const changes = linter.changes;

// expect(linter.instance.requestUpdateXMLByUpdateSetQuery).toHaveBeenCalledTimes(1);
// expect(linter.eslint.lintText).not.toHaveBeenCalled();
expect(clientUpdateXMLSpy).toHaveBeenCalledTimes(1);
expect(lintTextSpy).not.toHaveBeenCalled();

expect(changes.size).toBe(1);
// expect(changes.values().next().value.status).toBe(UpdateXMLScan.STATUS.IGNORED);
expect(changes.values().next().value.hasReports).toBe(false);
expect(changes.values().next().value.getStatus()).toBe("IGNORED");
expect(changes.values().next().value.hasReports()).toBe(false);
});

it.skip("Skip on empty field", async () => {
Expand Down
68 changes: 41 additions & 27 deletions spec/linter/UpdateXMLScan.spec.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,55 @@
/* eslint-disable camelcase */
/* eslint-disable camelcase, no-magic-numbers */
import { ESLint } from "eslint";
import { SNUpdateXMLData } from "../../src/core/sn.js";
import { UpdateXMLScan } from "../../src/linter/UpdateXMLScan.js";

describe("NowUpdateXMLScan", () => {
it("should return correct error/warning count for all reports", () => {
const data: SNUpdateXMLData = {
name: "name",
sys_id: "sysId",
action: "INSERT_OR_UPDATE",
type: "sysId",
target_name: "sysId",
update_set: "sysId",
payload: "sysId",
sys_created_by: "sysId",
sys_created_on: "sysId",
sys_updated_by: "sysId",
sys_updated_on: "sysId",
application: "global",
payloadHash: -1234,
sys_mod_count: 0
};
describe("UpdateXMLScan", () => {
const data: SNUpdateXMLData = {
name: "name",
sys_id: "sysId",
action: "INSERT_OR_UPDATE",
type: "sysId",
target_name: "sysId",
update_set: "sysId",
payload: "sysId",
sys_created_by: "sysId",
sys_created_on: "sysId",
sys_updated_by: "sysId",
sys_updated_on: "sysId",
application: "global",
payloadHash: -1234,
sys_mod_count: 0
};

it("should return correct error count and status for all reports", () => {
const scan = new UpdateXMLScan(data);
scan.reports.set("script", {
scan.setReport("script", {
warningCount: 1,
errorCount: 2
});
scan.reports.set("condition", {
} as ESLint.LintResult);
scan.setReport("condition", {
warningCount: 1,
errorCount: 0
});

} as ESLint.LintResult);

expect(scan.getReportsWarningCount()).toBe(2);
expect(scan.getReportsErrorCount()).toBe(2);
expect(scan.hasReportsWarnings()).toBe(true);
expect(scan.hasReportsErrors()).toBe(true);
expect(scan.getStatus()).toBe("ERROR");
});

it("should return correct warning count and status for all reports", () => {
const scan = new UpdateXMLScan(data);
scan.setReport("script", {
warningCount: 1,
errorCount: 0
} as ESLint.LintResult);
scan.setReport("condition", {
warningCount: 3,
errorCount: 0
} as ESLint.LintResult);

expect(scan.getReportsWarningCount()).toBe(4);
expect(scan.hasReportsWarnings()).toBe(true);
expect(scan.getStatus()).toBe("WARNING");
});
});
6 changes: 5 additions & 1 deletion src/core/ProfileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,14 @@ export class Profile {
return this.tables;
}

setTableConfiguration(config: TableConfig) {
this.tables = config;
}

// TODO: better name, load all other profile files/table setup
async fetchTableConfiguration(): Promise<void> {
if (this.client != null) {
this.tables = await this.client.getTableConfiguration(this);
this.setTableConfiguration(await this.client.getTableConfiguration(this));
}
}
}
Expand Down
Loading

0 comments on commit 6d882cf

Please sign in to comment.