Coder Social home page Coder Social logo

the_phoenix_project's Introduction

The_Phoenix_Project

A stepping stone codebase to working on "The Unicorn Project". (1, 2)

Build Status Coverage Status Codacy Badge License: MIT

Table of Contents

Requirements

  • Python3.7 or higher
  • SQLite

Python3.7 is the lowest supported version due to heavy usage of Python 3.7's dataclasses feature to improve code readability and self-documentation of data structures. If back porting was a requirement, they could be refactored out to use the NamedTuple class or namedtuple collection objects instead. Use of a 3rd party library such as attrs is also an alternative.

Quickstart Guide

  1. Clone the code.

    git clone https://github.com/DavidWalshe93/The_Phoenix_Project.git
  2. Navigate to the root directory of the project:

    cd <PATH_TO_DIR>/The_Phoenix_Project/
  3. Create a virtual environment

    python3 -m venv venv
  4. Activate the virtual environment

    source ./venv/bin/activate
  5. Install requirements

    pip install -r requirements.txt
  6. Setup ENV Variables

    # Sources Flask application ENV variables.
    source dev_setup.sh
    # Key used for password hashing.
    export SECRET_KEY="abc"
    # Key used for registering as an ADMIN.
    export ADMIN_SECRET_KEY="XYZ"
  7. Run Tests

    pytest
  8. Run Dev Server

    flask run

Brief

  • Create, Update, Delete and List web service endpoints for a User object.
  • User object should contain a name, email address, password and the date of their last login.
  • Provide a login endpoint that validates the email address and password provided by the user matches the one stored in the database

Application

Below are some thoughts behind why the application stack was chosen.

Summary

Component Framework/Library Why
Python -- Most experience with.
API Flask/FlaskRESTful Rapid prototyping with large eco-system of feature plugins.
Database SQLite Good for prototyping and fast dev-test cycle with in-memory functionality, ideal for repeatable unit/functional testing.
ORM SQLAlchemy Easier to work with and faster turn around time than writing raw SQL. Better code readability.
DTO/Schema Marshmallow Easy transfer of database objects/Request bodies into various data-structures including Dicts/Dataclasses/Namespaces.
Login/Role Management FlaskHTTPAuth Supplies a simple role based system out-of-the-box, protects endpoints using the intuitive decorator pattern.
Token Access FlaskHTTPAuth/itsdangerous Allows both Bearer Token and Basic Authentication.
Password Hashing Werkzeug Clear interface for hashing passwords before saving to database and for verifying hashed passwords on login attempts.

Endpoints

Below are the endpoints for the given application.

Endpoint Method Auth Action
/api/v1/register POST ๐Ÿ”ด Registers a new user/admin with the system.
/api/v1/login POST ๐Ÿ”ด Login an existing user.
/api/v1/users GET ๐Ÿ”ต List all user's usernames and last login timestamp.
/api/v1/users/me GET ๐Ÿ”ต Get the current user's information. (email, username, last login)
/api/v1/users/me PUT ๐Ÿ”ต Update the current user's username and/or password.
/api/v1/users/me DELETE ๐Ÿ”ต Close the current user's account.
/api/v1/users/<:id> GET ๐Ÿ”ต Get a single user's id, username and last login from their ID.
/api/v1/users/<:id> GET ๐ŸŸข Get a single user's id, username, email, role and last login, given their ID.
/api/v1/users/<:id> PUT ๐ŸŸข Update a single user's username and/or password based on their ID.
/api/v1/users/<:id> DELETE ๐ŸŸข Delete a single user based on their ID.
/api/v1/users GET ๐ŸŸข Get all user's usernames, emails and last login timestamps.
/api/v1/users DELETE ๐ŸŸข Delete a group of users based on a list of ID's

Access Rights

Role Access Level Description
Anonyomous ๐Ÿ”ด Non-logged in user.
User ๐Ÿ”ต Logged in user with User privileges.
Admin ๐ŸŸข Logged in user with Admin privileges.

Testing

PyTest

Testing was carried out using the PyTest framework with 43 tests created in total.

Pytest was chosen over unittest/nosetest for its expressive syntax and powerful fixture injection features, greatly reducing boilerplate code.

The image below depicts the granular coverage report for the delivered application.

img.png

Postman

Postman was also used to test against a development instance of the application during development. The exported Postman project can be found in the ./postman directory.

To improve repeatability, two helper requests were setup to clear the development application's database and then initialise it with five users before running the rest of the request tests. This allowed requests to start from the same state each test cycle.

The tests were written using Postman's Test API and the Pre/Post request hooks offered by Postman.

img.png

CICD Pipeline

A simple CI/CD flow is created to showcase understanding of core DevOps fundamentals and to keep code honest outside the original development environment.

Components

Component Service Links
SCM GitHub
Build Agent Travis CI Build Status
Coverage Analysis Coveralls Coverage Status
Code Quality Analysis Codacy Codacy Badge

Pipeline Architecture

CICD Architecture

Future Work

Due to timing constraints, some aspects of the project were not fully realised, this section is supplied to highlight where additional time would have been spent.

  • โ— Continue to improve test coverage.

    • Add tox to check against multiple versions of Python.
    • Add additional unittesting to validate rarely activated code paths.
  • โ— Add additional endpoint, allowing admins to create a User.

  • โ— Add Docker support for better test/deployment support.

    • โœ”๏ธSemi-realised by Travis CI, which builds a docker container to run tests on, ensuring no hidden dependencies are on local machine
  • โ— Deploy to PaaS provider such as Digital Ocean or Heroku or a CSP such as AWS or Azure.

Technical References

the_phoenix_project's People

Contributors

davidwalshe93 avatar

Watchers

James Cloos avatar  avatar

Forkers

prateek327

the_phoenix_project's Issues

DB - Create Schema

Create db schema to include username, email, password(hash) and date of last login.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.