Skip to content

Commit

Permalink
testcase: ftp validation - added
Browse files Browse the repository at this point in the history
  • Loading branch information
darsan-in committed Nov 29, 2024
1 parent 2265de2 commit 6c01bf1
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ jobs:
run: yarn

- name: Run tests
run: yarn test
run: yarn ci-test
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
"build": "cls && eslint && rimraf dist && tsc -p tsconfig.json && ncp ./bin/userconfig-template.js ./dist/bin/userconfig-template.js",
"watch": "tsc --watch",
"clean": "cls && rimraf dist",
"deploy": "yarn test && yarn build && yarn publish --access public && git push"
"deploy": "yarn test && yarn build && yarn publish --access public && git push",
"start-mock-server": "npx ts-node test/mock-api/app.ts",
"ci-test": "start-server-and-test start-mock-server http://localhost:8080 test"
},
"keywords": [
"seo automation",
Expand Down Expand Up @@ -89,8 +91,9 @@
"jest": "29.7.0",
"ncp": "^2.0.0",
"rimraf": "6.0.1",
"start-server-and-test": "^2.0.8",
"ts-node": "10.9.2",
"typescript": "5.7.2",
"xsd-schema-validator": "^0.10.0"
}
}
}
8 changes: 8 additions & 0 deletions test/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { chdir } from "node:process";
import { Hawk } from "../lib/core";
import ftpValidator from "./utils/ftp-validator";
import strategyValidator from "./utils/strategy-validator";
import validateRoutes from "./utils/validate-routes";
import validateSitemap from "./utils/validate-sitemap";
Expand All @@ -9,6 +10,9 @@ const testSampleRootPath = "./test/test-sample";

beforeAll(() => {
process.env.isdev = "true"; //for strategyValidator

//delete indexnow key
hawkInstance.utils.lastStateWriter({ secretKey: "" }); // for ftpValidator
});

beforeEach(() => {
Expand All @@ -31,3 +35,7 @@ test("Routes validation", () => {
test("Strategy validation", async () => {
expect(await strategyValidator(hawkInstance)).toBe(true);
});

test("FTP validation", async () => {
expect(await ftpValidator(hawkInstance)).toBe(true);
});
3 changes: 3 additions & 0 deletions test/mock-api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,7 @@ router.post("/indexnow", function (req: any, res: any, _next: () => void) {
}
});

router.get("/", function (_req: any, res: any, _next: () => void) {
res.status(200).send("Hello World");
});
export default router;
42 changes: 42 additions & 0 deletions test/utils/ftp-validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Client as FTP } from "basic-ftp";
import { relative } from "path";
import { cwd } from "process";
import { type Hawk } from "../../lib/core";

export default function ftpValidator(
hawkInstance: Hawk,
): Promise<boolean> {
return new Promise(async (resolve, reject) => {
//check if secretfile and sitemap available
const secretfile = relative(
cwd(),
`${hawkInstance.utils.lastStateReader("secretKey")}.txt`,
);
const sitemap = relative(
cwd(),
hawkInstance.configurations.sitemapPath,
);

try {
const ftp: FTP = new FTP();

const {
ftpCredential: { username, password, hostname },
} = hawkInstance.configurations;

await ftp.access({
user: username,
password: password,
host: hostname,
});

const secretfileMeta = (await ftp.list(secretfile))[0];
const sitemapMeta = (await ftp.list(sitemap))[0];

ftp.close();
resolve(secretfileMeta ? !!sitemapMeta : false);
} catch (err) {
reject(err);
}
});
}
2 changes: 1 addition & 1 deletion test/utils/validate-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function validateRoutes(hawkInstance: Hawk): boolean {

//change mod time of routes
const simulatedLastSubmisssionTime = Date.now();
const newRouteModTime = new Date(simulatedLastSubmisssionTime + 10); //simulate as 10ms latest
const newRouteModTime = new Date(simulatedLastSubmisssionTime + 600000); //simulate as 10ms latest

//pick some random number of routes and update
const simulated_updatedRoutes = availableRoutes
Expand Down
Loading

0 comments on commit 6c01bf1

Please sign in to comment.