Skip to content
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

restful-api-ii #5

Open
wants to merge 1 commit 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
14 changes: 7 additions & 7 deletions __tests__/customerRoutes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,38 @@ describe('Customer Resource CRUD Operations', () => {
it('should create a new customer', async () => {
const customerData = { name: 'Jane Doe', email: '[email protected]', isActive: true };
const response = await request(app)
.post('/customers')
.post('/users')
.send(customerData);
expect(response.statusCode).toBe(201);
expect(response.body).toHaveProperty('id');
newCustomerId = response.body.id; // Store the ID for use in subsequent tests
});

it('should list all customers', async () => {
const response = await request(app).get('/customers');
const response = await request(app).get('/users');
expect(response.statusCode).toBe(200);
expect(Array.isArray(response.body)).toBe(true);
});

it('should fetch a single customer by ID', async () => {
const response = await request(app).get(`/customers/${newCustomerId}`);
const response = await request(app).get(`/users/${newCustomerId}`);
expect(response.statusCode).toBe(200);
expect(response.body).toHaveProperty('id', newCustomerId);
});

it('should update a customer', async () => {
const updatedCustomerData = { name: 'John Doe Updated', email: '[email protected]', isActive: false };
const response = await request(app)
.put(`/customers/${newCustomerId}`)
.put(`/users/${newCustomerId}`)
.send(updatedCustomerData);
expect(response.statusCode).toBe(200);
expect(response.body.name).toEqual(updatedCustomerData.name);
expect(response.body.username).toEqual(updatedCustomerData.username);
expect(response.body.isActive).toEqual(updatedCustomerData.isActive);
});

it('should delete a customer', async () => {
const response = await request(app).delete(`/customers/${newCustomerId}`);
const response = await request(app).delete(`/users/${newCustomerId}`);
expect(response.statusCode).toBe(200);
expect(response.text).toContain('Customer successfully deleted');
expect(response.text).toContain('User successfully deleted');
});
});
49 changes: 49 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const express = require('express')
const app = express()

app.use(express.json())

const users = [
{ id: 1, username: "user1", email: "[email protected]", isActive: true },
{ id: 2, username: "user2", email: "[email protected]", isActive: false },
{ id: 3, username: "user3", email: "[email protected]", isActive: true },
{ id: 4, username: "user4", email: "[email protected]", isActive: true }
];

app.get('/users', (req, res) => {
res.json(users)
})

app.post('/users', (req, res) => {
const {username, email, isActive} = req.body
const newUser= {
id: users.length + 1,
username: username,
email: email,
isActive: isActive !== undefined ? isActive : true
}
users.push(newUser)
res.status(201).json(newUser)
})

app.get('/users/:id', (req, res) => {
const user = users.find(u => u.id === parseInt(req.params.id))
res.status(200).send(user)
})

app.put('/users/:id', (req, res) => {
const id = parseInt(req.params.id)
const {username, email, isActive} = req.body
const index = users.findIndex(user => user.id === id)
users[index] = {id, username, email, isActive: isActive!== undefined ? isActive : true}
res.status(200).send(users[index])
})

app.delete('/users/:id', (req, res) => {
const user = users.find(u => u.id === parseInt(req.params.id))
const index = users.indexOf(user)
users.splice(index, 1)
res.send("User successfully deleted")
})

module.exports = app
Loading