Skip to content

Assignments week 2 Oleksandr Starshynov #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions hackyourtemperature/__tests__/app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { server } from "../server.js";
import app from "../app.js";
import supertest from "supertest";

const request = supertest(app);


describe("POST /", () => {
it("check if city name is valid", async () => {
const response = await request
.post('/weather')
.send({"cityName": "London"});
expect(response.status).toBe(200);
});

it("check if city name is missing", async () => {
const response = await request
.post('/weather')
.send({"cityName": ""});
expect(response.status).toBe(400)
});

it("check if city name is wrong", async () => {
const response = await request
.post('/weather')
.send({"cityName": "Londondondon"});
expect(response.body).toEqual({ error: "API error" });
});

it("check if request body is a string", async () => {
const response = await request
.post("/weather")
.send({"cityName": "London"})
.set("Content-Type", "application/json");
expect(response.status).toBe(200);
});

it("check if the temperature is a number", async () => {
const response = await request
.post('/weather')
.send({"cityName": "London"});
const fetchApi = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=London&appid=c34f56e7a569f2c2cf3e24aceac5ddc8&units=metric`);
const data = await fetchApi.json();
expect(typeof data.main.temp).toBe("number");
});

it("check if the temperature is a valid number", async () => {
const response = await request
.post('/weather')
.send({"cityName": "Ondangwa"});
const fetchApi = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=Ondangwa&appid=c34f56e7a569f2c2cf3e24aceac5ddc8&units=metric`);
const data = await fetchApi.json();
expect(data.main.temp).toBeGreaterThanOrEqual(-90);
expect(data.main.temp).toBeLessThanOrEqual(57);
});

it("check if fetch API is ok", async () => {
const response = await request
.post('/weather')
.send({"cityName": "London"});
const fetchApi = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=London&appid=c34f56e7a569f2c2cf3e24aceac5ddc8&units=metric`);
expect(fetchApi.status).toBe(200)
});
});

afterAll(() => {
server.close();
});
31 changes: 31 additions & 0 deletions hackyourtemperature/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import express from "express";
import { keys } from "./sources/keys.js";
import fetch from "node-fetch";
import { engine } from "express-handlebars";

const app = express();

app.use(express.json())

app.post('/weather', async (req, res) => {
const cityName = req.body.cityName;

if(!cityName) {
return res.status(400).json({weatherText: "City is not found!" })
}

try {
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${keys.API_KEY}&units=metric`);
if (!response.ok) {
return res.status(response.status).json({error: "API error"});
}
const data = await response.json();
return res.status(200).send(`It is ${data.main.temp}°C in ${cityName}`)
}
catch {
console.error("Error fetching data");
res.status(500).json({ error: "Internal server error" });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error is lacking in information the front-end can accurately use, is the error because the API call failed? or is the name of the city not a real city?
You can check the response from the weather API, and if it's 404, return an error with that info, so the front-end can display this to the user and the user will type a different name

}
});

export default app;
13 changes: 13 additions & 0 deletions hackyourtemperature/babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
presets: [
[
// This is a configuration, here we are telling babel what configuration to use
"@babel/preset-env",
{
targets: {
node: "current",
},
},
],
],
};
8 changes: 8 additions & 0 deletions hackyourtemperature/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
// Tells jest that any file that has 2 .'s in it and ends with either js or jsx should be run through the babel-jest transformer
transform: {
"^.+\\.jsx?$": "babel-jest",
},
// By default our `node_modules` folder is ignored by jest, this tells jest to transform those as well
transformIgnorePatterns: [],
};
27 changes: 27 additions & 0 deletions hackyourtemperature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "hackyourtemperature",
"version": "1.0.0",
"main": "server.js",
"type": "module",
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js",
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"express": "^4.21.1",
"express-handlebars": "^8.0.1",
"node-fetch": "^3.3.2"
},
"devDependencies": {
"@babel/preset-env": "^7.26.0",
"babel-jest": "^29.7.0",
"jest": "^29.7.0",
"nodemon": "^3.1.7",
"supertest": "^7.1.0"
}
}
7 changes: 7 additions & 0 deletions hackyourtemperature/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import app from './app.js';

const port = 3000;

export const server = app.listen(port, () => {
console.log(`App listening on port ${port}`);
});
1 change: 1 addition & 0 deletions hackyourtemperature/sources/keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const keys = {API_KEY: "c34f56e7a569f2c2cf3e24aceac5ddc8"};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good practice to not push up API keys to repo's to keep them safe. two ways to do this are:
-> Ignoring this file in the .gitignore.
-> adding a .env file (and ignoring it) - This is better, as the code will still compile, and the .env file is a standard across a lot of software/programs, so loading them into the program at runtime is handleded well.