-
Notifications
You must be signed in to change notification settings - Fork 6
Assignments week 2 Ahmad Zetouny #10
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
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 @@ | ||
sources/keys.js | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import app from "../sources/app.js"; | ||
import * as keys from "../sources/keys.js"; | ||
import supertest from "supertest"; | ||
import fetch from "node-fetch"; | ||
|
||
const request = supertest(app); | ||
|
||
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. Can you also add a test case for the GET / route? |
||
describe("POST /weather", () => { | ||
it("Checking for request body", (done) => { | ||
request | ||
.post("/weather") | ||
.expect(400, { weatherText: "Request body is missing" }, done); | ||
}); | ||
|
||
it("CityName should exist", (done) => { | ||
request | ||
.post("/weather") | ||
.send({ City: "" }) | ||
.expect(400, { weatherText: "City name is missing" }, done); | ||
}); | ||
|
||
it("CityName should be a string", (done) => { | ||
request | ||
.post("/weather") | ||
.send({ cityName: 5 }) | ||
.expect(400, { weatherText: "City name should be a string" }, done); | ||
}); | ||
|
||
it("Checking if the WeatherAPI is online", async () => { | ||
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. There is no need for this test. This is a test that the developers of the weather API should write! You should be only testing the code you wrote to make sure it works as expected. I would replace the fetch call with a call to your api and then see if a 200 is returned! |
||
const response = await fetch( | ||
`https://api.openweathermap.org/data/2.5/weather?q=Arnhem,nl&appid=${keys.API_KEY}&units=metric` | ||
); | ||
expect(response.status).toBe(200); | ||
}); | ||
|
||
it("Checking if temperature is a number", async () => { | ||
const response = await fetch( | ||
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. Same as above :) Call your endpoint! |
||
`https://api.openweathermap.org/data/2.5/weather?q=Arnhem,nl&appid=${keys.API_KEY}&units=metric` | ||
); | ||
const json = await response.json(); | ||
const temp = json.main.temp; | ||
expect(typeof temp).toBe("number"); | ||
}); | ||
}); |
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,25 @@ | ||
{ | ||
"name": "hackyourtemperature", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "server.js", | ||
"scripts": { | ||
"test": "jest", | ||
"start": "node server.js" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"type": "module", | ||
"dependencies": { | ||
"express": "^5.1.0", | ||
"express-handlebars": "^8.0.2", | ||
"node-fetch": "^3.3.2" | ||
}, | ||
"devDependencies": { | ||
"@babel/preset-env": "^7.27.1", | ||
"babel-jest": "^29.7.0", | ||
"jest": "^29.7.0", | ||
"supertest": "^7.1.0" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import app from "./sources/app.js"; | ||
|
||
const PORT = 3000; | ||
|
||
app.listen(PORT, () => { | ||
console.log(`Example app listening on port ${PORT}`); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import express from "express"; | ||
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. Nice job on separating your app into server and app files, the app.js file should be in the main directory and not in sources to keep consistency! |
||
import fetch from "node-fetch"; | ||
import * as keys from "./keys.js"; | ||
|
||
const app = express(); | ||
|
||
app.use(express.json()); | ||
|
||
app.get("/", (req, res) => { | ||
res.send("hello from backend to frontend!"); | ||
}); | ||
|
||
app.post("/weather", async (req, res) => { | ||
try { | ||
if (!req.body) { | ||
throw { weatherText: "Request body is missing" }; | ||
} | ||
|
||
const { cityName } = req.body; | ||
|
||
if (!cityName) { | ||
throw { weatherText: "City name is missing" }; | ||
} | ||
|
||
if (typeof cityName !== "string") { | ||
throw { weatherText: "City name should be a string" }; | ||
} | ||
|
||
const response = await fetch( | ||
`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&APPID=${keys.API_KEY}&units=metric` | ||
); | ||
|
||
if (!response.ok) { | ||
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. Good work on checking the response code here! A suggestion would be to check if (!temp?.main?.temp) so that you are sure that the temperature is actually returned! |
||
throw { weatherText: "City is not found!" }; | ||
} | ||
|
||
const temp = await response.json(); | ||
|
||
res.status(200).json({ | ||
weatherText: `The temperature in ${cityName} is ${temp.main.temp}℃`, | ||
}); | ||
} catch (error) { | ||
res.status(400).json(error); | ||
} | ||
}); | ||
|
||
export default app; |
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.
Great job on adding your api key in the gitignore!