Submodule 4
Submodule 4 of Backend Development Mini-Quest
Final Project: API and Postman Validation
This final submodule requires you to demonstrate your skills by building a functional RESTful API and validating its operation using a Postman Collection.
Repository template:
https://github.com/Open-Coding-Society/flask.git
1. Capstone Implementation Steps
Follow these steps to build the Blog Platform API, focusing on the simplified requirements.
Step 1: Project Setup & Database Design
- Clone the Template: Clone the repository template and set up your local development environment
- Database Schema: Define the database model for your blog posts.
- Create the
PostModel: Define fields liketitle,content, and, crucially, the foreign key relationship to theUsermodel (author_id).
- Create the
- Run Migrations: Apply your model changes to the database to create the necessary Users and Posts tables.
Step 2: Authentication (Security Foundation)
- Enable Register & Login: Utilize the template’s authentication logic for Registration (
POST /api/auth/register) and Login (POST /api/auth/login).- Security Check: Confirm that passwords are hashed before storage.
- Verify Protected Routes: Test that the built-in authentication dependency (middleware) is protecting routes.
- Test: An unauthenticated request to a protected endpoint must return a
401 Unauthorized.
- Test: An unauthenticated request to a protected endpoint must return a
Step 3: Implement Posts CRUD Endpoints
| Feature | Description | Example Endpoints |
|---|---|---|
| Read (Public) | Retrieve all posts and a single post by ID. | GET /api/posts, GET /api/posts/:id |
| Create (Protected) | Create a new post. Must require authentication. | POST /api/posts |
| Update/Delete (Authorized) | Modify or remove a post. Must require authentication and authorization. | PUT /api/posts/:id, DELETE /api/posts/:id |
Authorization Logic:
For PUT and DELETE requests, add logic to check that the ID of the currently authenticated user matches the author_id of the post. If they do not match, return a 403 Forbidden.
Step 4: Error Handling and Validation
- Input Validation: Implement checks for required fields (
title,content) and return a400 Bad Requestif validation fails. - Resource Not Found: Ensure requests for non-existent IDs return a
404 Not Foundstatus code.
2. Postman Validation Steps
Create a complete Postman Collection that serves as the official test suite for your API.
Step 1: Set Up Postman Environment
- Create an Environment: Create a new Postman Environment (e.g.,
Capstone Blog API). - Set Variables: Add two variables:
baseURL: Your local server address (e.g.,http://localhost:8000).authToken: Initial value should be empty.
Step 2: Build the Authentication Workflow
- Login & Capture Token: Create the request for
POST /api/auth/login. -
Add Test Script: In the Tests tab of the Login request, add a script to save the returned token:
// Verify success status pm.test("Status code is 200", function () { pm.response.to.have.status(200); }); // Save the token to the environment var jsonData = pm.response.json(); // Use the correct key based on your template (e.g., 'access_token' or 'token') pm.environment.set("authToken", jsonData.access_token);
Step 3: Validate CRUD with Full Test Sequence
- Set Bearer Token: For the
POST,PUT, andDELETErequests, set the Authorization type to Bearer Token and use the value ``. - Full Test Sequence: Execute the collection in a logical order to prove the full workflow:
- Register a new user.
- Login the user (token is saved).
- Create Post (using the token).
- Get All Posts (verify new post exists).
- Update Post (using the token).
- Delete Post (using the token).
Postman Simulator
Take a look at what your Postman could potentially look like with various endpoints.