Skip to content

Test task. FastAPI CRUD app with registration / JWT authentication, use of external APIs, Redis caching, pytest test suites and Docker functionality for fast set-up

Notifications You must be signed in to change notification settings

RogerRabbit32/Python_FastAPI---TestTask_SocialNetwork

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastAPI Social Network

Description

FastAPI Social Network is a simple social networking CRUD application built using FastAPI framework. It provides basic functionality to register and authenticate users, who can create, edit, delete, and view posts. Users can also like or dislike posts made by other users, but not their own posts.

Table of Contents

Key Features

Registration/Authorization: Users can sign up and login, using JWT tokens. There are also registration credentials requirements implemented, which disallow duplicate usernames and emails and restrict creation of too simple passwords and logins.

NOTE! As additional features there are also email validation and user data enrichment in place. These features use external APIs (hunter.io and clearbit.com) and require API keys to be set up as project environment variables. Without them, the project will not work as expected.

Post Management: Users can create, edit, delete, and view posts. Posts are stored in the PostgreSQL database, utilizing Docker volumes, ensuring data persistence.

Post Likes/Dislikes: Users can like or dislike posts made by other users, but not their own posts. Likes are stored in a Redis cache for faster access.

Set up project

Prerequisites

Before running the application, make sure you have the following dependencies installed on your system:

Installation

  1. Clone the repository to your local machine and make it your current working directory:
git clone https://github.com/RogerRabbit32/Python_FastAPI---TestTask_SocialNetwork.git
cd SocialNetwork
  1. Inside the directory, create a '.env' file (this particular name is referenced in the docker-compose environment variable), where you have to specify two variables, storing your API keys:

    HUNTER_API_KEY=#your secret key, no spaces around equal sign, no quotes, here

    CLEARBIT_API_KEY=#your secret key, no spaces around equal sign, no quotes, here

    These keys will grant the application access to hunter.io and clearbit.com services. Without them, user registration will not be available and the project will not work properly.

  2. Build the Docker image. Run the command:

docker-compose build
  1. Start the containers. Run the command:
docker-compose up

If the installation is successful, your app should be available at http://localhost:8000

Usage

All project API routes are available for tryouts via Swagger documentation http://localhost:8000/docs

Register user

Create a new user in the database with the specified login and password.

Route: POST /accounts/signup

NOTE! A valid email has to be provided at this stage, since all emails are verified through emailhunter.co If additional data is available on the user through clearbit.com email enrichment, the user's full name, if not provided by the user himself, will be automatically added to registration credentials.

Login user

Create a new JWT access token for the user. (Authentication also available via form data in Swagger docs)

Route: POST /accounts/login

Create Post

Create a new post with the specified title and text.

Route: POST /posts

Get All Posts

Fetch all posts with optional pagination support (limit and offset).

Route: GET /posts

Get User's Own Posts

Fetch all posts created by the currently authenticated user.

Route: GET /posts/user

Get Single Post

Fetch a single post by its ID.

Route: GET /posts/{post_id}

Update User's Own Post

Update a post created by the currently authenticated user.

Route: PUT /posts/{post_id}

Delete a Post

Delete a post created by the currently authenticated user.

Route: DELETE /posts/{post_id}

Like/Dislike a Post

Like or dislike a post made by other users.

Route: POST /posts/{post_id}/like

Unlike/Remove Like from a Post

Remove a previously liked or disliked post.

Route: DELETE /posts/{post_id}/like

Get Post Likes

Fetch all likes (and dislikes) for a specific post.

Route: GET /accounts/{post_id}/likes

Running tests

When the containers are up, you can run the tests for the application by typing the following command in a new terminal:

docker exec <container name or id> pytest

This will execute the pytest autotests suite, stored in /tests directory

Technologies used

  • FastAPI: Python Web framework for building APIs
  • pytest: Python framework for tests management
  • PostgreSQL: Database management system for storing application data
  • Redis: In-memory data store for handling likes/dislikes functionality
  • Docker + Docker Compose: Containerization for easy deployment

About

Test task. FastAPI CRUD app with registration / JWT authentication, use of external APIs, Redis caching, pytest test suites and Docker functionality for fast set-up

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages