-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #418 from inaturalist/378-password-reset-endpoint
Add v2 endpoint for user password reset #378
- Loading branch information
Showing
7 changed files
with
140 additions
and
6 deletions.
There are no files selected for viewing
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
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,37 @@ | ||
const UsersController = require( "../../../../lib/controllers/v2/users_controller" ); | ||
|
||
module.exports = sendWrapper => { | ||
async function POST( req, res ) { | ||
await UsersController.resetPassword( req ); | ||
sendWrapper( req, res.status( 204 ) ); | ||
} | ||
|
||
POST.apiDoc = { | ||
tags: ["Users"], | ||
summary: "Reset a user password", | ||
security: [{ | ||
appJwtRequired: [] | ||
}], | ||
requestBody: { | ||
content: { | ||
"application/json": { | ||
schema: { | ||
$ref: "#/components/schemas/UsersResetPassword" | ||
} | ||
} | ||
} | ||
}, | ||
responses: { | ||
204: { | ||
description: "No response body; success implies reset request was received" | ||
}, | ||
default: { | ||
$ref: "#/components/responses/Error" | ||
} | ||
} | ||
}; | ||
|
||
return { | ||
POST | ||
}; | ||
}; |
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,7 @@ | ||
const Joi = require( "joi" ); | ||
|
||
module.exports = Joi.object( ).keys( { | ||
user: Joi.object( ).keys( { | ||
email: Joi.string( ).email( ).required( ) | ||
} ) | ||
} ); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -319,4 +319,48 @@ describe( "Users", ( ) => { | |
.expect( 200, done ); | ||
} ); | ||
} ); | ||
|
||
describe( "reset_password", ( ) => { | ||
const currentUser = fixtures.elasticsearch.users.user[0]; | ||
const userToken = jwt.sign( { user_id: currentUser.id }, | ||
config.jwtSecret || "secret", | ||
{ algorithm: "HS512" } ); | ||
const applicationToken = jwt.sign( | ||
{ application: "whatever" }, | ||
config.jwtApplicationSecret || "application_secret", | ||
{ algorithm: "HS512" } | ||
); | ||
|
||
it( "should 401 without auth", function ( done ) { | ||
request( this.app ) | ||
.post( "/v2/users/reset_password" ) | ||
.expect( 401, done ); | ||
} ); | ||
|
||
it( "should 401 with a user token", function ( done ) { | ||
request( this.app ) | ||
.post( "/v2/users/reset_password" ) | ||
.set( "Authorization", userToken ) | ||
.expect( 401, done ); | ||
} ); | ||
|
||
it( "should hit the Rails equivalent and return 200", function ( done ) { | ||
const nockScope = nock( "http://localhost:3000" ) | ||
.post( "/users/password" ) | ||
.reply( 200 ); | ||
request( this.app ) | ||
.post( "/v2/users/reset_password" ) | ||
.set( "Authorization", applicationToken ) | ||
.send( { | ||
user: { | ||
email: "[email protected]" | ||
} | ||
} ) | ||
.expect( ( ) => { | ||
// Raise an exception if the nocked endpoint doesn't get called | ||
nockScope.done( ); | ||
} ) | ||
.expect( 204, done ); | ||
} ); | ||
} ); | ||
} ); |