Skip to content

Commit

Permalink
int
Browse files Browse the repository at this point in the history
  • Loading branch information
trungkodekloud committed Dec 6, 2023
0 parents commit 872bee7
Show file tree
Hide file tree
Showing 27 changed files with 4,562 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# ---------------------------------------------------------------------------------*
# This will prevent your local modules and debug logs from being copied onto your
# Docker image and possibly overwriting modules installed within your image.
# ---------------------------------------------------------------------------------*
node_modules
npm-debug.log
# ignore .git and .cache folders
.git
.cache
# ignore all markdown files (md) beside all README*.md other than README-secret.md
*.md
!README*.md
README-secret.md
# github related files
.github/
# nodejs releated files
node_modules
solar-system.png
.nyc_output
.talismanrc
coverage
test-results.xml
33 changes: 33 additions & 0 deletions .github/workflows/solar-system.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Solar System Workflow

on:
workflow_dispatch:
push:
branches:
- main
- 'feature/*'

jobs:
unit-testing:
name: Unit Testing
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Setup NodeJS Version
uses: actions/setup-node@v3
with:
node-version: 20

- name: Install Dependencies
run: npm install

- name: Unit Testing
run: npm test

- name: Archive Test Result
uses: actions/upload-artifact@v3
with:
name: Mocha-Test-Result
path: test-results.xml
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
solar-system.png
.nyc_output
.talismanrc
coverage
test-results.xml
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM node:18-alpine3.17

WORKDIR /usr/app

COPY package*.json /usr/app/

RUN npm install

COPY . .

ENV MONGO_URI=uriPlaceholder
ENV MONGO_USERNAME=usernamePlaceholder
ENV MONGO_PASSWORD=passwordPlaceholder

EXPOSE 3000

CMD [ "npm", "start" ]
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Solar System NodeJS Application

A simple HTML+MongoDB+NodeJS project to display Solar System and it's planets.

---
## Requirements

For development, you will only need Node.js and NPM installed in your environement.

### Node
- #### Node installation on Windows

Just go on [official Node.js website](https://nodejs.org/) and download the installer.
Also, be sure to have `git` available in your PATH, `npm` might need it (You can find git [here](https://git-scm.com/)).

- #### Node installation on Ubuntu

You can install nodejs and npm easily with apt install, just run the following commands.

$ sudo apt install nodejs
$ sudo apt install npm

- #### Other Operating Systems
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/).

If the installation was successful, you should be able to run the following command.

$ node --version
v8.11.3

$ npm --version
6.1.0

---
## Install Dependencies from `package.json`
$ npm install

## Run Unit Testing
$ npm test

## Run Code Coverage
$ npm run coverage

## Run Application
$ npm start

## Access Application on Browser
http://localhost:3000/

67 changes: 67 additions & 0 deletions app-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
console.log('We are inside client.js');

/* on page load */
window.onload = function() {
const planet_id = document.getElementById("planetID").value
console.log("onLoad - Request Planet ID - " + planet_id)

fetch("/os", {
method: "GET"
})
.then(function(res) {
if (res.ok) {
return res.json();
}
thrownewError('Request failed');
}).catch(function(error) {
console.log(error);
})
.then(function(data) {
document.getElementById('hostname').innerHTML = `Pod - ${data.os} `
// document.getElementById('environment').innerHTML = ` Env - ${data.env} `
});
};



const btn = document.getElementById('submit');
if (btn) {
btn.addEventListener('click', func);
}

function func() {
const planet_id = document.getElementById("planetID").value
console.log("onClick Submit - Request Planet ID - " + planet_id)

fetch("/planet", {
method: "POST",
body: JSON.stringify({
id: document.getElementById("planetID").value
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(function(res2) {
if (res2.ok) {
return res2.json();
}
thrownewError('Request failed.');
}).catch(function(error) {
alert("Ooops, We have 8 planets.\nSelect a number from 0 - 8")
console.log(error);
})
.then(function(data) {
document.getElementById('planetName').innerHTML = ` ${data.name} `

const element = document.getElementById("planetImage");
const image = ` ${data.image} `
element.style.backgroundImage = "url("+image+")"

const planet_description = ` ${data.description} `
document.getElementById('planetDescription').innerHTML = planet_description.replace(/(.{80})/g, "$1<br>");


});

}
190 changes: 190 additions & 0 deletions app-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
let mongoose = require("mongoose");
let server = require("./app");
let chai = require("chai");
let chaiHttp = require("chai-http");


// Assertion
chai.should();
chai.use(chaiHttp);

describe('Planets API Suite', () => {

describe('Fetching Planet Details', () => {
it('it should fetch a planet named Mercury', (done) => {
let payload = {
id: 1
}
chai.request(server)
.post('/planet')
.send(payload)
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.property('id').eql(1);
res.body.should.have.property('name').eql('Mercury');
done();
});
});

it('it should fetch a planet named Venus', (done) => {
let payload = {
id: 2
}
chai.request(server)
.post('/planet')
.send(payload)
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.property('id').eql(2);
res.body.should.have.property('name').eql('Venus');
done();
});
});

it('it should fetch a planet named Earth', (done) => {
let payload = {
id: 3
}
chai.request(server)
.post('/planet')
.send(payload)
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.property('id').eql(3);
res.body.should.have.property('name').eql('Earth');
done();
});
});
it('it should fetch a planet named Mars', (done) => {
let payload = {
id: 4
}
chai.request(server)
.post('/planet')
.send(payload)
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.property('id').eql(4);
res.body.should.have.property('name').eql('Mars');
done();
});
});

it('it should fetch a planet named Jupiter', (done) => {
let payload = {
id: 5
}
chai.request(server)
.post('/planet')
.send(payload)
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.property('id').eql(5);
res.body.should.have.property('name').eql('Jupiter');
done();
});
});

it('it should fetch a planet named Satrun', (done) => {
let payload = {
id: 6
}
chai.request(server)
.post('/planet')
.send(payload)
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.property('id').eql(6);
res.body.should.have.property('name').eql('Saturn');
done();
});
});

it('it should fetch a planet named Uranus', (done) => {
let payload = {
id: 7
}
chai.request(server)
.post('/planet')
.send(payload)
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.property('id').eql(7);
res.body.should.have.property('name').eql('Uranus');
done();
});
});

it('it should fetch a planet named Neptune', (done) => {
let payload = {
id: 8
}
chai.request(server)
.post('/planet')
.send(payload)
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.property('id').eql(8);
res.body.should.have.property('name').eql('Neptune');
done();
});
});

// it('it should fetch a planet named Pluto', (done) => {
// let payload = {
// id: 9
// }
// chai.request(server)
// .post('/planet')
// .send(payload)
// .end((err, res) => {
// res.should.have.status(200);
// res.body.should.have.property('id').eql(9);
// res.body.should.have.property('name').eql('Sun');
// done();
// });
// });


});
});

//Use below test case to achieve coverage
describe('Testing Other Endpoints', () => {

describe('it should fetch OS Details', () => {
it('it should fetch OS details', (done) => {
chai.request(server)
.get('/os')
.end((err, res) => {
res.should.have.status(200);
done();
});
});
});

describe('it should fetch Live Status', () => {
it('it checks Liveness endpoint', (done) => {
chai.request(server)
.get('/live')
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.property('status').eql('live');
done();
});
});
});

describe('it should fetch Ready Status', () => {
it('it checks Readiness endpoint', (done) => {
chai.request(server)
.get('/ready')
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.property('status').eql('ready');
done();
});
});
});

});
Loading

0 comments on commit 872bee7

Please sign in to comment.