Skip to content

Commit 872bee7

Browse files
int
0 parents  commit 872bee7

27 files changed

+4562
-0
lines changed

.dockerignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ---------------------------------------------------------------------------------*
2+
# This will prevent your local modules and debug logs from being copied onto your
3+
# Docker image and possibly overwriting modules installed within your image.
4+
# ---------------------------------------------------------------------------------*
5+
node_modules
6+
npm-debug.log
7+
# ignore .git and .cache folders
8+
.git
9+
.cache
10+
# ignore all markdown files (md) beside all README*.md other than README-secret.md
11+
*.md
12+
!README*.md
13+
README-secret.md
14+
# github related files
15+
.github/
16+
# nodejs releated files
17+
node_modules
18+
solar-system.png
19+
.nyc_output
20+
.talismanrc
21+
coverage
22+
test-results.xml

.github/workflows/solar-system.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Solar System Workflow
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- main
8+
- 'feature/*'
9+
10+
jobs:
11+
unit-testing:
12+
name: Unit Testing
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout Repository
16+
uses: actions/checkout@v4
17+
18+
- name: Setup NodeJS Version
19+
uses: actions/setup-node@v3
20+
with:
21+
node-version: 20
22+
23+
- name: Install Dependencies
24+
run: npm install
25+
26+
- name: Unit Testing
27+
run: npm test
28+
29+
- name: Archive Test Result
30+
uses: actions/upload-artifact@v3
31+
with:
32+
name: Mocha-Test-Result
33+
path: test-results.xml

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
solar-system.png
3+
.nyc_output
4+
.talismanrc
5+
coverage
6+
test-results.xml

Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM node:18-alpine3.17
2+
3+
WORKDIR /usr/app
4+
5+
COPY package*.json /usr/app/
6+
7+
RUN npm install
8+
9+
COPY . .
10+
11+
ENV MONGO_URI=uriPlaceholder
12+
ENV MONGO_USERNAME=usernamePlaceholder
13+
ENV MONGO_PASSWORD=passwordPlaceholder
14+
15+
EXPOSE 3000
16+
17+
CMD [ "npm", "start" ]

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Solar System NodeJS Application
2+
3+
A simple HTML+MongoDB+NodeJS project to display Solar System and it's planets.
4+
5+
---
6+
## Requirements
7+
8+
For development, you will only need Node.js and NPM installed in your environement.
9+
10+
### Node
11+
- #### Node installation on Windows
12+
13+
Just go on [official Node.js website](https://nodejs.org/) and download the installer.
14+
Also, be sure to have `git` available in your PATH, `npm` might need it (You can find git [here](https://git-scm.com/)).
15+
16+
- #### Node installation on Ubuntu
17+
18+
You can install nodejs and npm easily with apt install, just run the following commands.
19+
20+
$ sudo apt install nodejs
21+
$ sudo apt install npm
22+
23+
- #### Other Operating Systems
24+
You can find more information about the installation on the [official Node.js website](https://nodejs.org/) and the [official NPM website](https://npmjs.org/).
25+
26+
If the installation was successful, you should be able to run the following command.
27+
28+
$ node --version
29+
v8.11.3
30+
31+
$ npm --version
32+
6.1.0
33+
34+
---
35+
## Install Dependencies from `package.json`
36+
$ npm install
37+
38+
## Run Unit Testing
39+
$ npm test
40+
41+
## Run Code Coverage
42+
$ npm run coverage
43+
44+
## Run Application
45+
$ npm start
46+
47+
## Access Application on Browser
48+
http://localhost:3000/
49+

app-controller.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
console.log('We are inside client.js');
2+
3+
/* on page load */
4+
window.onload = function() {
5+
const planet_id = document.getElementById("planetID").value
6+
console.log("onLoad - Request Planet ID - " + planet_id)
7+
8+
fetch("/os", {
9+
method: "GET"
10+
})
11+
.then(function(res) {
12+
if (res.ok) {
13+
return res.json();
14+
}
15+
thrownewError('Request failed');
16+
}).catch(function(error) {
17+
console.log(error);
18+
})
19+
.then(function(data) {
20+
document.getElementById('hostname').innerHTML = `Pod - ${data.os} `
21+
// document.getElementById('environment').innerHTML = ` Env - ${data.env} `
22+
});
23+
};
24+
25+
26+
27+
const btn = document.getElementById('submit');
28+
if (btn) {
29+
btn.addEventListener('click', func);
30+
}
31+
32+
function func() {
33+
const planet_id = document.getElementById("planetID").value
34+
console.log("onClick Submit - Request Planet ID - " + planet_id)
35+
36+
fetch("/planet", {
37+
method: "POST",
38+
body: JSON.stringify({
39+
id: document.getElementById("planetID").value
40+
}),
41+
headers: {
42+
"Content-type": "application/json; charset=UTF-8"
43+
}
44+
})
45+
.then(function(res2) {
46+
if (res2.ok) {
47+
return res2.json();
48+
}
49+
thrownewError('Request failed.');
50+
}).catch(function(error) {
51+
alert("Ooops, We have 8 planets.\nSelect a number from 0 - 8")
52+
console.log(error);
53+
})
54+
.then(function(data) {
55+
document.getElementById('planetName').innerHTML = ` ${data.name} `
56+
57+
const element = document.getElementById("planetImage");
58+
const image = ` ${data.image} `
59+
element.style.backgroundImage = "url("+image+")"
60+
61+
const planet_description = ` ${data.description} `
62+
document.getElementById('planetDescription').innerHTML = planet_description.replace(/(.{80})/g, "$1<br>");
63+
64+
65+
});
66+
67+
}

app-test.js

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
let mongoose = require("mongoose");
2+
let server = require("./app");
3+
let chai = require("chai");
4+
let chaiHttp = require("chai-http");
5+
6+
7+
// Assertion
8+
chai.should();
9+
chai.use(chaiHttp);
10+
11+
describe('Planets API Suite', () => {
12+
13+
describe('Fetching Planet Details', () => {
14+
it('it should fetch a planet named Mercury', (done) => {
15+
let payload = {
16+
id: 1
17+
}
18+
chai.request(server)
19+
.post('/planet')
20+
.send(payload)
21+
.end((err, res) => {
22+
res.should.have.status(200);
23+
res.body.should.have.property('id').eql(1);
24+
res.body.should.have.property('name').eql('Mercury');
25+
done();
26+
});
27+
});
28+
29+
it('it should fetch a planet named Venus', (done) => {
30+
let payload = {
31+
id: 2
32+
}
33+
chai.request(server)
34+
.post('/planet')
35+
.send(payload)
36+
.end((err, res) => {
37+
res.should.have.status(200);
38+
res.body.should.have.property('id').eql(2);
39+
res.body.should.have.property('name').eql('Venus');
40+
done();
41+
});
42+
});
43+
44+
it('it should fetch a planet named Earth', (done) => {
45+
let payload = {
46+
id: 3
47+
}
48+
chai.request(server)
49+
.post('/planet')
50+
.send(payload)
51+
.end((err, res) => {
52+
res.should.have.status(200);
53+
res.body.should.have.property('id').eql(3);
54+
res.body.should.have.property('name').eql('Earth');
55+
done();
56+
});
57+
});
58+
it('it should fetch a planet named Mars', (done) => {
59+
let payload = {
60+
id: 4
61+
}
62+
chai.request(server)
63+
.post('/planet')
64+
.send(payload)
65+
.end((err, res) => {
66+
res.should.have.status(200);
67+
res.body.should.have.property('id').eql(4);
68+
res.body.should.have.property('name').eql('Mars');
69+
done();
70+
});
71+
});
72+
73+
it('it should fetch a planet named Jupiter', (done) => {
74+
let payload = {
75+
id: 5
76+
}
77+
chai.request(server)
78+
.post('/planet')
79+
.send(payload)
80+
.end((err, res) => {
81+
res.should.have.status(200);
82+
res.body.should.have.property('id').eql(5);
83+
res.body.should.have.property('name').eql('Jupiter');
84+
done();
85+
});
86+
});
87+
88+
it('it should fetch a planet named Satrun', (done) => {
89+
let payload = {
90+
id: 6
91+
}
92+
chai.request(server)
93+
.post('/planet')
94+
.send(payload)
95+
.end((err, res) => {
96+
res.should.have.status(200);
97+
res.body.should.have.property('id').eql(6);
98+
res.body.should.have.property('name').eql('Saturn');
99+
done();
100+
});
101+
});
102+
103+
it('it should fetch a planet named Uranus', (done) => {
104+
let payload = {
105+
id: 7
106+
}
107+
chai.request(server)
108+
.post('/planet')
109+
.send(payload)
110+
.end((err, res) => {
111+
res.should.have.status(200);
112+
res.body.should.have.property('id').eql(7);
113+
res.body.should.have.property('name').eql('Uranus');
114+
done();
115+
});
116+
});
117+
118+
it('it should fetch a planet named Neptune', (done) => {
119+
let payload = {
120+
id: 8
121+
}
122+
chai.request(server)
123+
.post('/planet')
124+
.send(payload)
125+
.end((err, res) => {
126+
res.should.have.status(200);
127+
res.body.should.have.property('id').eql(8);
128+
res.body.should.have.property('name').eql('Neptune');
129+
done();
130+
});
131+
});
132+
133+
// it('it should fetch a planet named Pluto', (done) => {
134+
// let payload = {
135+
// id: 9
136+
// }
137+
// chai.request(server)
138+
// .post('/planet')
139+
// .send(payload)
140+
// .end((err, res) => {
141+
// res.should.have.status(200);
142+
// res.body.should.have.property('id').eql(9);
143+
// res.body.should.have.property('name').eql('Sun');
144+
// done();
145+
// });
146+
// });
147+
148+
149+
});
150+
});
151+
152+
//Use below test case to achieve coverage
153+
describe('Testing Other Endpoints', () => {
154+
155+
describe('it should fetch OS Details', () => {
156+
it('it should fetch OS details', (done) => {
157+
chai.request(server)
158+
.get('/os')
159+
.end((err, res) => {
160+
res.should.have.status(200);
161+
done();
162+
});
163+
});
164+
});
165+
166+
describe('it should fetch Live Status', () => {
167+
it('it checks Liveness endpoint', (done) => {
168+
chai.request(server)
169+
.get('/live')
170+
.end((err, res) => {
171+
res.should.have.status(200);
172+
res.body.should.have.property('status').eql('live');
173+
done();
174+
});
175+
});
176+
});
177+
178+
describe('it should fetch Ready Status', () => {
179+
it('it checks Readiness endpoint', (done) => {
180+
chai.request(server)
181+
.get('/ready')
182+
.end((err, res) => {
183+
res.should.have.status(200);
184+
res.body.should.have.property('status').eql('ready');
185+
done();
186+
});
187+
});
188+
});
189+
190+
});

0 commit comments

Comments
 (0)