Skip to content

Commit 6c01bf1

Browse files
committed
testcase: ftp validation - added
1 parent 2265de2 commit 6c01bf1

File tree

7 files changed

+276
-7
lines changed

7 files changed

+276
-7
lines changed

.github/workflows/test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ jobs:
3333
run: yarn
3434

3535
- name: Run tests
36-
run: yarn test
36+
run: yarn ci-test

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
"build": "cls && eslint && rimraf dist && tsc -p tsconfig.json && ncp ./bin/userconfig-template.js ./dist/bin/userconfig-template.js",
4141
"watch": "tsc --watch",
4242
"clean": "cls && rimraf dist",
43-
"deploy": "yarn test && yarn build && yarn publish --access public && git push"
43+
"deploy": "yarn test && yarn build && yarn publish --access public && git push",
44+
"start-mock-server": "npx ts-node test/mock-api/app.ts",
45+
"ci-test": "start-server-and-test start-mock-server http://localhost:8080 test"
4446
},
4547
"keywords": [
4648
"seo automation",
@@ -89,8 +91,9 @@
8991
"jest": "29.7.0",
9092
"ncp": "^2.0.0",
9193
"rimraf": "6.0.1",
94+
"start-server-and-test": "^2.0.8",
9295
"ts-node": "10.9.2",
9396
"typescript": "5.7.2",
9497
"xsd-schema-validator": "^0.10.0"
9598
}
96-
}
99+
}

test/main.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { chdir } from "node:process";
22
import { Hawk } from "../lib/core";
3+
import ftpValidator from "./utils/ftp-validator";
34
import strategyValidator from "./utils/strategy-validator";
45
import validateRoutes from "./utils/validate-routes";
56
import validateSitemap from "./utils/validate-sitemap";
@@ -9,6 +10,9 @@ const testSampleRootPath = "./test/test-sample";
910

1011
beforeAll(() => {
1112
process.env.isdev = "true"; //for strategyValidator
13+
14+
//delete indexnow key
15+
hawkInstance.utils.lastStateWriter({ secretKey: "" }); // for ftpValidator
1216
});
1317

1418
beforeEach(() => {
@@ -31,3 +35,7 @@ test("Routes validation", () => {
3135
test("Strategy validation", async () => {
3236
expect(await strategyValidator(hawkInstance)).toBe(true);
3337
});
38+
39+
test("FTP validation", async () => {
40+
expect(await ftpValidator(hawkInstance)).toBe(true);
41+
});

test/mock-api/routes.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,7 @@ router.post("/indexnow", function (req: any, res: any, _next: () => void) {
130130
}
131131
});
132132

133+
router.get("/", function (_req: any, res: any, _next: () => void) {
134+
res.status(200).send("Hello World");
135+
});
133136
export default router;

test/utils/ftp-validator.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Client as FTP } from "basic-ftp";
2+
import { relative } from "path";
3+
import { cwd } from "process";
4+
import { type Hawk } from "../../lib/core";
5+
6+
export default function ftpValidator(
7+
hawkInstance: Hawk,
8+
): Promise<boolean> {
9+
return new Promise(async (resolve, reject) => {
10+
//check if secretfile and sitemap available
11+
const secretfile = relative(
12+
cwd(),
13+
`${hawkInstance.utils.lastStateReader("secretKey")}.txt`,
14+
);
15+
const sitemap = relative(
16+
cwd(),
17+
hawkInstance.configurations.sitemapPath,
18+
);
19+
20+
try {
21+
const ftp: FTP = new FTP();
22+
23+
const {
24+
ftpCredential: { username, password, hostname },
25+
} = hawkInstance.configurations;
26+
27+
await ftp.access({
28+
user: username,
29+
password: password,
30+
host: hostname,
31+
});
32+
33+
const secretfileMeta = (await ftp.list(secretfile))[0];
34+
const sitemapMeta = (await ftp.list(sitemap))[0];
35+
36+
ftp.close();
37+
resolve(secretfileMeta ? !!sitemapMeta : false);
38+
} catch (err) {
39+
reject(err);
40+
}
41+
});
42+
}

test/utils/validate-routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function validateRoutes(hawkInstance: Hawk): boolean {
1111

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

1616
//pick some random number of routes and update
1717
const simulated_updatedRoutes = availableRoutes

0 commit comments

Comments
 (0)