-
Notifications
You must be signed in to change notification settings - Fork 6
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
base: main
Are you sure you want to change the base?
Changes from all commits
b83e53f
585d1cd
8753915
70d5dfd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
}); |
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" }); | ||
} | ||
}); | ||
|
||
export default app; |
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", | ||
}, | ||
}, | ||
], | ||
], | ||
}; |
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: [], | ||
}; |
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" | ||
} | ||
} |
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}`); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const keys = {API_KEY: "c34f56e7a569f2c2cf3e24aceac5ddc8"}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: |
There was a problem hiding this comment.
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