forked from b00tc4mp/isdi-bootcamp-202405
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create handlers update email, update product price and create product…
… and tests from api b00tc4mp#101
- Loading branch information
1 parent
46ff525
commit 154e682
Showing
16 changed files
with
107 additions
and
34 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
staff/lucas-orts/project/api/handlers/createProductHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { logic } from 'cor' | ||
|
||
export default (req, res, next) => { | ||
const { userId } = req | ||
|
||
const { name, type, minprice, maxprice, image } = req.body | ||
|
||
try { | ||
logic.createProduct(userId, name, type, minprice, maxprice, image) | ||
.then(() => res.status(201).send()) | ||
.catch(error => next(error)) | ||
} catch (error) { | ||
next(error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,21 @@ | ||
import registerUserHandler from './registerUserHandler.js' | ||
import authenticateUserHandler from './authenticateUserHandler.js' | ||
import getUserNameHandler from './getUserNameHandler.js' | ||
import updatePasswordHandler from './updatePasswordHandler.js' | ||
import updateUserAddressHandler from './updateUserAddressHandler.js' | ||
import updateUserPhoneHandler from './updateUserPhoneHandler.js' | ||
import updateEmailHandler from './updateEmailHandler.js' | ||
import createProductHandler from './createProductHandler.js' | ||
import updateProductPriceHandler from './updateProductPriceHandler.js' | ||
|
||
export { | ||
registerUserHandler, | ||
authenticateUserHandler, | ||
getUserNameHandler | ||
getUserNameHandler, | ||
updatePasswordHandler, | ||
updateUserAddressHandler, | ||
updateUserPhoneHandler, | ||
updateEmailHandler, | ||
createProductHandler, | ||
updateProductPriceHandler | ||
} |
15 changes: 15 additions & 0 deletions
15
staff/lucas-orts/project/api/handlers/updateEmailHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { logic } from 'cor' | ||
|
||
export default (req, res, next) => { | ||
const { userId } = req | ||
|
||
const { email, password } = req.body | ||
|
||
try { | ||
logic.updateEmail(userId, email, password) | ||
.then(() => res.status(204).send()) | ||
.catch(error => next(error)) | ||
} catch (error) { | ||
next(error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
staff/lucas-orts/project/api/handlers/updateProductPriceHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { logic } from 'cor' | ||
|
||
export default (req, res, next) => { | ||
const { userId } = req | ||
|
||
const { productId } = req.params | ||
|
||
const { minprice, maxprice } = req.body | ||
|
||
try { | ||
logic.updateProductPrice(userId, productId, minprice, maxprice) | ||
.then(() => res.status(204).send()) | ||
.catch(error => next(error)) | ||
} catch (error) { | ||
next(error) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
staff/lucas-orts/project/api/handlers/updateUserAddressHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
staff/lucas-orts/project/api/handlers/updateUserPhoneHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
curl -v http://localhost:8080/users/address -X PATCH -d '{"address":"Calle Loturia 5, Marbella"}' -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmM3NGM4YTM2MGNmY2UwN2U3OWI5YTIiLCJpYXQiOjE3MjQzMzczNTN9.XbsybN3JliqNyBnOcK59BNJ88Btz_lbdaWRD8Tk4xTY" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
curl -v http://localhost:8080/users/email -X PATCH -d '{"email":"[email protected]", "password":"123456789"}' -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmM3NGM4YTM2MGNmY2UwN2U3OWI5YTIiLCJpYXQiOjE3MjQzMzczNTN9.XbsybN3JliqNyBnOcK59BNJ88Btz_lbdaWRD8Tk4xTY" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
curl -v http://localhost:8080/users/password -X PATCH -d '{"oldPassword":"123123123", "newPassword":"123456789", "newPasswordRepeat":"123456789"}' -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmM3NGM4YTM2MGNmY2UwN2U3OWI5YTIiLCJpYXQiOjE3MjQzMzczNTN9.XbsybN3JliqNyBnOcK59BNJ88Btz_lbdaWRD8Tk4xTY" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
curl -v http://localhost:8080/users/phone -X PATCH -d '{"phone":"+34 965152331"}' -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmM3NGM4YTM2MGNmY2UwN2U3OWI5YTIiLCJpYXQiOjE3MjQzMzczNTN9.XbsybN3JliqNyBnOcK59BNJ88Btz_lbdaWRD8Tk4xTY" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ import { User } from '../data/models.js' | |
|
||
import errors from '../../com/errors.js' | ||
|
||
const { NotFoundError, ValidationError } = errors | ||
const { NotFoundError, ValidationError, CredentialsError } = errors | ||
|
||
describe('updateEmail', () => { | ||
before(() => mongoose.connect(process.env.MONGODB_URI)) | ||
|
@@ -22,10 +22,10 @@ describe('updateEmail', () => { | |
debugger | ||
return bcrypt.hash('123123123', 8) | ||
.then(hash => User.create({ name: 'Ester', surname: 'Colero', email: '[email protected]', phone: '966234731', address: 'calle Tertulia 3, Cuenca', password: hash })) | ||
.then(user => updateEmail(user.id, 'Peta@zeta.com', '123123123') | ||
.then(() => User.findOne({ email: 'Peta@zeta.com' }).lean() | ||
.then(user => updateEmail(user.id, 'peta@zeta.com', '123123123') | ||
.then(() => User.findOne({ email: 'peta@zeta.com' }).lean() | ||
.then(user => { | ||
expect(user.email).to.equal('Peta@zeta.com') | ||
expect(user.email).to.equal('peta@zeta.com') | ||
}) | ||
.then(match => expect(match).to.be.true) | ||
) | ||
|
@@ -35,31 +35,19 @@ describe('updateEmail', () => { | |
it('fails on non-existing user', () => { | ||
let _error | ||
|
||
return updateEmail(new ObjectId().toString(), 'Peta@zeta.com', '123123123') | ||
return updateEmail(new ObjectId().toString(), 'peta@zeta.com', '123123123') | ||
.catch(error => _error = error) | ||
.finally(() => { | ||
expect(_error).to.be.instanceOf(NotFoundError) | ||
expect(_error.message).to.equal('user not found') | ||
}) | ||
}) | ||
|
||
it('fails on non matching passwords', () => { | ||
let _error | ||
|
||
return User.create({ name: 'Ester', surname: 'Colero', email: '[email protected]', phone: '966234731', address: 'calle Tertulia 3, Cuenca', password: '123123123' }) | ||
.then(user => updateEmail(user.id, '[email protected]', '123123124')) | ||
.catch(error => _error = error) | ||
.finally(() => { | ||
expect(_error).to.be.instanceOf(ValidationError) | ||
expect(_error.message).to.equal('wrong password') | ||
}) | ||
}) | ||
|
||
it('fails on non-string userId', () => { | ||
let error | ||
|
||
try { | ||
updateEmail(123, 'Peta@zeta.com', '123123123') | ||
updateEmail(123, 'peta@zeta.com', '123123123') | ||
} catch (_error) { | ||
error = _error | ||
} finally { | ||
|
@@ -72,7 +60,7 @@ describe('updateEmail', () => { | |
let error | ||
|
||
try { | ||
updateEmail(new ObjectId().toString(), 'Peta@zeta.com', 123123123) | ||
updateEmail(new ObjectId().toString(), 'peta@zeta.com', 123123123) | ||
} catch (_error) { | ||
error = _error | ||
} finally { | ||
|
@@ -85,7 +73,7 @@ describe('updateEmail', () => { | |
let error | ||
|
||
try { | ||
updateEmail(new ObjectId().toString(), 'Peta@zeta.com', '123123') | ||
updateEmail(new ObjectId().toString(), 'peta@zeta.com', '123123') | ||
} catch (_error) { | ||
error = _error | ||
} finally { | ||
|
@@ -98,7 +86,7 @@ describe('updateEmail', () => { | |
let error | ||
|
||
try { | ||
updateEmail(new ObjectId().toString(), 'Peta@zeta.com', '123123 123') | ||
updateEmail(new ObjectId().toString(), 'peta@zeta.com', '123123 123') | ||
} catch (_error) { | ||
error = _error | ||
} finally { | ||
|
@@ -111,11 +99,11 @@ describe('updateEmail', () => { | |
let error | ||
|
||
try { | ||
updateEmail(new ObjectId().toString(), 'Peta@zeta.com', '123123123') | ||
updateEmail(new ObjectId().toString(), 'peta@zeta.com', '123123123') | ||
} catch (_error) { | ||
error = _error | ||
} finally { | ||
expect(error).to.be.instanceOf(Error) | ||
expect(error).to.be.instanceOf(CredentialsError) | ||
expect(error.message).to.equal('passwords do not match') | ||
} | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ import updateEmail from './updateEmail.js' | |
import mongoose from 'mongoose' | ||
|
||
mongoose.connect(process.env.MONGODB_URI) | ||
.then(() => updateEmail('66c44943fedca00b82f34b98', '[email protected]', '123123123')) | ||
.then(() => updateEmail('66c7435ea25f13cef7c37a31', '[email protected]', '123123123')) | ||
.then(() => console.log('Email updated')) | ||
.catch(error => console.error(error)) | ||
.finally(() => mongoose.disconnect()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters