Skip to content

Commit f1bef6a

Browse files
committed
Testing static pages and ping
1 parent 0c37ea8 commit f1bef6a

File tree

7 files changed

+352
-36
lines changed

7 files changed

+352
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Examples:
6666

6767
```xml
6868
<?xml version="1.0"?>
69-
<notifyResult success="true" msg="Thanks for the ping."/>
69+
<result success="true" msg="Thanks for the ping."/>
7070
```
7171

7272
```json

docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ services:
2222

2323
rsscloud-tests:
2424
image: rsscloud
25-
command: dockerize -wait tcp://mongodb:27017 -wait http://rsscloud:5337 -timeout 10s bash -c "node bin/import-data.js && npm test"
25+
command: dockerize -wait tcp://mongodb:27017 -wait http://rsscloud:5337 -timeout 10s bash -c "npm test"
2626
environment:
2727
APP_URL: http://rsscloud:5337
28-
DOMAIN: localhost
29-
PORT: 5337
3028
MONGODB_URI: mongodb://mongodb:27017/rsscloud
29+
MOCK_SERVER_URL: http://rsscloud-tests:8002
30+
MOCK_SERVER_PORT: 8002
3131
expose:
3232
- 8002
3333
depends_on:

package-lock.json

Lines changed: 105 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"import-data": "node ./bin/import-data.js",
1010
"jshint": "echo '=> linting' && jshint ./**/*.js",
1111
"eslint": "echo '=> sniffing' && eslint controllers/ services/",
12-
"test": "mocha --timeout 10000 --bail"
12+
"test": "mocha --timeout 10000 --bail",
13+
"test-api": "docker-compose up --build --abort-on-container-exit"
1314
},
1415
"engines": {
1516
"node": ">=10.6.0"
@@ -42,6 +43,8 @@
4243
"devDependencies": {
4344
"chai": "^4.2.0",
4445
"chai-http": "^4.3.0",
46+
"chai-json": "^1.0.0",
47+
"chai-xml": "^0.3.2",
4548
"eslint": "^6.6.0",
4649
"eslint-config-crockford": "^2.0.0",
4750
"jshint": "^2.10.3",

test/docs.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

test/ping.js

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
const express = require("express");
2+
const bodyParser = require("body-parser");
3+
const cors = require("cors");
4+
const chai = require("chai");
5+
const chaiHttp = require("chai-http");
6+
const chaiXml = require("chai-xml");
7+
const expect = chai.expect;
8+
const config = require("../config");
9+
const mongodb = require('../services/mongodb');
10+
const SERVER_URL = process.env.APP_URL || "http://localhost:5337";
11+
const MOCK_SERVER_PORT = process.env.MOCK_SERVER_PORT || 8002;
12+
const MOCK_SERVER_URL = process.env.MOCK_SERVER_URL || `http://localhost:${MOCK_SERVER_PORT}`;
13+
14+
chai.use(chaiHttp);
15+
chai.use(chaiXml);
16+
17+
const mock = {
18+
app: express(),
19+
server: null,
20+
requests: [],
21+
status: 404,
22+
responseBody: ''
23+
};
24+
25+
const setupMock = (status, body) => {
26+
mock.status = status;
27+
mock.responseBody = body;
28+
};
29+
30+
const initMongo = () => {
31+
return mongodb.connect('rsscloud', config.mongodbUri);
32+
};
33+
34+
const teardownMongo = () => {
35+
return mongodb.close('rsscloud');
36+
};
37+
38+
const initMock = async () => {
39+
mock.app.use(bodyParser.urlencoded({ extended: false }));
40+
mock.app.use(bodyParser.json());
41+
mock.app.get("*", (req, res) => {
42+
mock.requests.push(req);
43+
res.status(mock.status).send(mock.responseBody);
44+
});
45+
mock.app.post("*", (req, res) => {
46+
mock.requests.push(req);
47+
res.status(mock.status).send(mock.responseBody);
48+
});
49+
50+
mock.server = await mock.app.listen(MOCK_SERVER_PORT);
51+
console.log(`Mock server started on port: ${MOCK_SERVER_PORT}`);
52+
};
53+
54+
const teardownMock = () => {
55+
if (mock.server) {
56+
mock.server.close();
57+
delete mock.server;
58+
}
59+
};
60+
61+
describe("REST Ping", () => {
62+
before(async () => {
63+
await initMongo();
64+
await initMock();
65+
});
66+
67+
after(async () => {
68+
await teardownMongo();
69+
teardownMock();
70+
});
71+
72+
beforeEach(async () => {
73+
await mongodb.get('rsscloud').createCollection('events');
74+
await mongodb.get('rsscloud').createCollection('resources');
75+
await mongodb.get('rsscloud').createCollection('subscriptions');
76+
77+
mock.requests = [];
78+
});
79+
80+
afterEach(async () => {
81+
await mongodb.get('rsscloud').collection('events').drop();
82+
await mongodb.get('rsscloud').collection('resources').drop();
83+
await mongodb.get('rsscloud').collection('subscriptions').drop();
84+
});
85+
86+
it('should accept a ping for new resource and return xml', done => {
87+
setupMock(200, '<RSS Feed />');
88+
89+
chai
90+
.request(SERVER_URL)
91+
.post("/ping")
92+
.set('content-type', 'application/x-www-form-urlencoded')
93+
.send({ url: MOCK_SERVER_URL + '/rss.xml' })
94+
.end((err, res) => {
95+
if (err) {
96+
return done(err);
97+
}
98+
99+
expect(res).status(200);
100+
expect(res.text).xml.equal('<result success="true" msg="Thanks for the ping."/>');
101+
done();
102+
});
103+
});
104+
105+
it('should accept a ping for new resource and return json', done => {
106+
setupMock(200, '<RSS Feed />');
107+
108+
chai
109+
.request(SERVER_URL)
110+
.post("/ping")
111+
.set('accept', 'application/json')
112+
.set('content-type', 'application/x-www-form-urlencoded')
113+
.send({ url: MOCK_SERVER_URL + '/rss.xml' })
114+
.end((err, res) => {
115+
if (err) {
116+
return done(err);
117+
}
118+
119+
expect(res).status(200);
120+
expect(res.body).deep.equal({ success: true, msg: 'Thanks for the ping.' });
121+
done();
122+
});
123+
});
124+
125+
it('should reject a ping for bad resource and return xml', done => {
126+
chai
127+
.request(SERVER_URL)
128+
.post("/ping")
129+
.set('content-type', 'application/x-www-form-urlencoded')
130+
.send({ url: 'http://dsajkdljsaldksa/rss.xml' })
131+
.end((err, res) => {
132+
if (err) {
133+
return done(err);
134+
}
135+
136+
expect(res).status(200);
137+
expect(res.text).xml.equal('<result success="false" msg="Error: getaddrinfo ENOTFOUND dsajkdljsaldksa"/>');
138+
done();
139+
});
140+
});
141+
142+
it('should reject a ping for bad resource and return json', done => {
143+
chai
144+
.request(SERVER_URL)
145+
.post("/ping")
146+
.set('accept', 'application/json')
147+
.set('content-type', 'application/x-www-form-urlencoded')
148+
.send({ url: 'http://dsajkdljsaldksa/rss.xml' })
149+
.end((err, res) => {
150+
if (err) {
151+
return done(err);
152+
}
153+
154+
expect(res).status(200);
155+
expect(res.body).deep.equal({ success: false, msg: 'Error: getaddrinfo ENOTFOUND dsajkdljsaldksa' });
156+
done();
157+
});
158+
});
159+
});

0 commit comments

Comments
 (0)