-
Notifications
You must be signed in to change notification settings - Fork 84
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 #413 from masterchief-Dave/feature/auth-forgot-res…
…et-password feat: test api route
- Loading branch information
Showing
12 changed files
with
238 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -393,6 +393,79 @@ const changePassword = async ( | |
|
||
const googleSignIn = async () => {}; | ||
|
||
/** | ||
* @swagger | ||
* /api/v1/auth/magic-link: | ||
* post: | ||
* tags: | ||
* - Auth | ||
* summary: Passwordless sign-in with email | ||
* description: API endpoint to initiate passwordless sign-in by sending email to the registered user | ||
* requestBody: | ||
* required: true | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* email: | ||
* type: string | ||
* format: email | ||
* example: [email protected] | ||
* responses: | ||
* 200: | ||
* description: Sign-in token sent to email | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* status_code: | ||
* type: integer | ||
* example: 200 | ||
* message: | ||
* type: string | ||
* example: Sign-in token sent to email | ||
* 400: | ||
* description: Bad request | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* status_code: | ||
* type: integer | ||
* example: 400 | ||
* message: | ||
* type: string | ||
* example: Invalid request body | ||
* 404: | ||
* description: User not found | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* status_code: | ||
* type: integer | ||
* example: 404 | ||
* message: | ||
* type: string | ||
* example: User not found | ||
* 500: | ||
* description: Internal server error | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* status_code: | ||
* type: integer | ||
* example: 500 | ||
* message: | ||
* type: string | ||
* example: Internal server error | ||
*/ | ||
const createMagicToken = async ( | ||
req: Request, | ||
res: Response, | ||
|
@@ -419,6 +492,80 @@ const createMagicToken = async ( | |
} | ||
}; | ||
|
||
/** | ||
* @swagger | ||
* /api/v1/auth/magic-link: | ||
* get: | ||
* tags: | ||
* - Auth | ||
* summary: Authenticate user with magic link | ||
* description: Validates the magic link token and authenticates the user | ||
* parameters: | ||
* - in: query | ||
* name: token | ||
* required: true | ||
* schema: | ||
* type: string | ||
* description: Magic link token | ||
* - in: query | ||
* name: redirect | ||
* schema: | ||
* type: boolean | ||
* description: Whether to redirect after authentication (true/false) | ||
* responses: | ||
* 200: | ||
* description: User authenticated successfully | ||
* headers: | ||
* Authorization: | ||
* schema: | ||
* type: string | ||
* description: Bearer token for authentication | ||
* Set-Cookie: | ||
* schema: | ||
* type: string | ||
* description: Contains the hng_token | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* status: | ||
* type: string | ||
* example: ok | ||
* data: | ||
* type: object | ||
* properties: | ||
* id: | ||
* type: string | ||
* example: user123 | ||
* email: | ||
* type: string | ||
* example: [email protected] | ||
* name: | ||
* type: string | ||
* example: John Doe | ||
* access_token: | ||
* type: string | ||
* example: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9... | ||
* 302: | ||
* description: Redirect to home page (when redirect=true) | ||
* 400: | ||
* description: Bad request | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* status: | ||
* type: string | ||
* example: error | ||
* message: | ||
* type: string | ||
* example: Invalid Request | ||
* 500: | ||
* description: Internal server error | ||
* security: [] | ||
*/ | ||
const authenticateUserMagicLink = async ( | ||
req: Request, | ||
res: Response, | ||
|
@@ -461,7 +608,7 @@ const authenticateUserMagicLink = async ( | |
return res.redirect("/"); | ||
} else { | ||
return res.status(200).json({ | ||
status: "ok", | ||
status_code: 200, | ||
data: responseData, | ||
access_token, | ||
}); | ||
|
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,28 +1,35 @@ | ||
import { Request, Response } from "express"; | ||
import { exec } from "child_process"; | ||
import { Request, Response } from "express"; | ||
|
||
export const runTestController = async (req: Request, res: Response) => { | ||
exec("python3 ./tests/run_test.py", (error, stdout, stderr) => { | ||
if (stderr) { | ||
console.error(`stderr: ${stderr}`); | ||
res.status(500).json({ | ||
status_code: 500, | ||
message: "Script error", | ||
error: stderr, | ||
}); | ||
} else if (error) { | ||
res.status(500).json({ | ||
status_code: 500, | ||
message: "Script error", | ||
error: error, | ||
}); | ||
} else { | ||
console.log(`stdout: ${stdout}`); | ||
res.status(200).json({ | ||
status_code: 200, | ||
message: "Script executed successfully", | ||
data: stdout, | ||
}); | ||
} | ||
}); | ||
exec( | ||
"python3 src/controllers/tests/tests/run_tests.py", | ||
(error, stdout, stderr) => { | ||
if (stderr) { | ||
res.status(500).json({ | ||
status_code: 500, | ||
message: "Script error", | ||
error: stderr, | ||
}); | ||
} else if (error) { | ||
res.status(500).json({ | ||
status_code: 500, | ||
message: "Script error", | ||
error: error, | ||
}); | ||
} else { | ||
const formattedOutput = stdout | ||
.split("\n") | ||
.map((line) => line.trim()) | ||
.filter((line) => line.length > 0) | ||
.join("\n"); | ||
|
||
res.status(200).json({ | ||
status_code: 200, | ||
message: "Script executed successfully", | ||
data: formattedOutput, | ||
}); | ||
} | ||
}, | ||
); | ||
}; |
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,3 +11,4 @@ python-dateutil==2.9.0.post0 | |
requests==2.32.3 | ||
six==1.16.0 | ||
urllib3==2.2.2 | ||
pprintpp==0.4.0 |
Binary file not shown.
Binary file added
BIN
+21 KB
src/controllers/tests/tests/__pycache__/test_auth.cpython-310-pytest-8.3.2.pyc
Binary file not shown.
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
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,8 +1,8 @@ | ||
import { Router, Response, Request } from "express"; | ||
import { Router } from "express"; | ||
import { runTestController } from "../controllers"; | ||
|
||
const runTestRouter = Router(); | ||
|
||
runTestRouter.get("/run-test", runTestController); | ||
runTestRouter.get("/", runTestController); | ||
|
||
export { runTestRouter }; |
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,44 @@ | ||
import { object, string, TypeOf } from "zod"; | ||
import { emailSchema } from "../utils/request-body-validator"; | ||
|
||
/** | ||
* @openapi | ||
* components: | ||
* schemas: | ||
* CreateMagicLink: | ||
* type: object | ||
* required: | ||
* properties: | ||
* email: | ||
* type: string | ||
* format: email | ||
* example: [email protected] | ||
* ValidateMagicLink: | ||
* type: object | ||
* required: | ||
* - token | ||
* properties: | ||
* token: | ||
* type: string | ||
* example: "exampleToken123" | ||
*/ | ||
|
||
const createMagicLinkPayload = { | ||
body: object({ | ||
email: emailSchema, | ||
}), | ||
}; | ||
|
||
const magicLinkQuery = object({ | ||
token: string().min(1, "Token is required"), | ||
}); | ||
|
||
export const validateMagicLinkSchema = object({ | ||
query: magicLinkQuery, | ||
}); | ||
|
||
export const createMagicLinkSchema = object({ ...createMagicLinkPayload }); | ||
|
||
export type validateMagicLinkInput = TypeOf<typeof validateMagicLinkSchema>; | ||
export type CreateMagicLinkInput = TypeOf<typeof createMagicLinkSchema>; |
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