{"id":21364908,"url":"https://github.com/enzorooschqueiroz/flask-user-crud-api","last_synced_at":"2026-04-19T06:37:59.565Z","repository":{"id":263201381,"uuid":"863223464","full_name":"enzorooschqueiroz/Flask-User-CRUD-API","owner":"enzorooschqueiroz","description":"The main goal is to provide a solid foundation for learning technologies like Docker, unit testing, continuous integration (CI/CD), and automated deployment.","archived":false,"fork":false,"pushed_at":"2024-11-20T03:58:14.000Z","size":48,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-13T21:35:19.701Z","etag":null,"topics":["actions","ci-cd","devops","docker","flask-api","unit-testing"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/enzorooschqueiroz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-25T23:47:03.000Z","updated_at":"2024-11-16T22:37:44.000Z","dependencies_parsed_at":"2025-03-16T07:36:49.725Z","dependency_job_id":null,"html_url":"https://github.com/enzorooschqueiroz/Flask-User-CRUD-API","commit_stats":null,"previous_names":["enzorooschqueiroz/flask-user-crud-api"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/enzorooschqueiroz/Flask-User-CRUD-API","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enzorooschqueiroz%2FFlask-User-CRUD-API","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enzorooschqueiroz%2FFlask-User-CRUD-API/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enzorooschqueiroz%2FFlask-User-CRUD-API/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enzorooschqueiroz%2FFlask-User-CRUD-API/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/enzorooschqueiroz","download_url":"https://codeload.github.com/enzorooschqueiroz/Flask-User-CRUD-API/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enzorooschqueiroz%2FFlask-User-CRUD-API/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279017037,"owners_count":26085951,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-13T02:00:06.723Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["actions","ci-cd","devops","docker","flask-api","unit-testing"],"created_at":"2024-11-22T07:08:11.903Z","updated_at":"2025-10-13T21:35:20.694Z","avatar_url":"https://github.com/enzorooschqueiroz.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flask User CRUD API\n\nThis is a basic project for a RESTful API built with Flask to manage users, focusing on learning Docker, unit tests, CI/CD, and deployment.\n\n## Architecture\n\nThe application follows the CRUD (Create, Read, Update, Delete) structure to manage users. The API uses MongoDB via `mongoengine` as the database.\n\n### Implemented Features\n\n1. **GET /users** - Returns the list of all registered users.\n2. **GET /user/\u003ccpf\u003e** - Returns the details of a specific user by CPF.\n3. **POST /user** - Creates a new user with the provided information (CPF, email, first name, last name, and birth date). User creation validates the CPF using a verification algorithm.\n\n### Implemented Validations\n\n- **CPF**: The API validates the format and authenticity of the provided CPF.\n  - The CPF must follow the format `xxx.xxx.xxx-xx`.\n  - The verification follows the standard CPF check digits.\n- **Email**: Required field.\n- **First Name and Last Name**: Required fields.\n- **Birth Date**: Required field.\n\n### Example Usage\n\n#### User Creation (POST)\n\n```json\nPOST /user\n{\n  \"cpf\": \"123.456.789-09\",\n  \"email\": \"example@domain.com\",\n  \"first_name\": \"FirstName\",\n  \"last_name\": \"LastName\",\n  \"birth_date\": \"1990-01-01\"\n}\n```\n\n#### Retrieve All Users (GET)\n\n```\nGET /users\n```\n\n#### Retrieve User by CPF (GET)\n\n```\nGET /user/123.456.789-09\n```\n\n### Delete User by CPF (DELETE)\n\n```\nDELETE /user/123.456.789-09\n```\n\n###  Update User by CPF (PUT)\n\n```json\nPUT /user/123.456.789-09\n{\n  \"cpf\": \"123.456.789-09\",\n  \"email\": \"updated@domain.com\",\n  \"first_name\": \"UpdatedFirstName\",\n  \"last_name\": \"UpdatedLastName\",\n  \"birth_date\": \"1990-01-01\"\n}\n```\n---\n\n## Data Models\n\nThe API uses MongoDB to persist user data, and the data model is defined using `mongoengine`.\n\n### User Model (UserModel)\n\nBelow are the fields for the `UserModel`, representing a user in the application:\n\n- **cpf**: `StringField` (Required, Unique) — User's CPF, must be unique and valid.\n- **email**: `EmailField` (Required) — User's email address.\n- **first_name**: `StringField` (Required, Max: 50 characters) — User's first name.\n- **last_name**: `StringField` (Required, Max: 50 characters) — User's last name.\n- **birth_date**: `DateTimeField` (Required) — User's birth date.\n\n---\n\nExample of a user JSON object:\n\n```json\n{\n  \"cpf\": \"123.456.789-09\",\n  \"email\": \"example@domain.com\",\n  \"first_name\": \"FirstName\",\n  \"last_name\": \"LastName\",\n  \"birth_date\": \"1990-01-01T00:00:00\"\n}\n```\n\n---\n\n## Unit Tests\n\nThe project includes unit tests to verify the API's behavior and ensure that all CRUD operations work correctly. The tests are implemented using `pytest`.\n\n### Test Configuration\n\nThe tests use a test client provided by Flask, allowing routes to be tested without running the actual server.\n\n#### Fixtures\n\n- **client**: Creates a test client with the application's configuration.\n- **valid_user**: Provides a dictionary of valid user data for user creation tests.\n- **invalid_user**: Provides a dictionary of invalid user data to test error scenarios.\n\n### Implemented Tests\n\n1. **Test Retrieve All Users** (`test_get_users`)\n   - **Method**: `GET /users`\n   - **Verifies**: The response returns status 200 (OK).\n\n2. **Test User Creation** (`test_post_user`)\n   - **Method**: `POST /user`\n   - **Verifies**:\n     - A valid user is successfully created (status 200).\n     - The creation of an invalid user returns status 400 and an error message.\n\n3. **Test Retrieve User by CPF** (`test_get_user`)\n   - **Method**: `GET /user/\u003ccpf\u003e`\n   - **Verifies**:\n     - A valid user is returned with correct information.\n     - An attempt to retrieve an invalid user returns status 404 and the message \"User not found.\"\n\n### How to Run the Tests\n\n1. **Install the dependencies**:\n   - Ensure that all project dependencies, including `pytest`, are installed.\n\n2. **Run the tests**:\n   - To run the tests, execute the following command in the terminal:\n     ```bash\n     pytest\n     ```\n\n---\n\n## Docker and Docker Compose\n\nThis project is containerized using Docker, and the environment is configured using `docker-compose` to facilitate development and deployment.\n\n- **MongoDB Service**:\n  - Uses the `mongo:5.0.8` image.\n  - Defines environment variables for MongoDB username and password.\n  - Automatically restarts if the service fails.\n  \n- **API Service**:\n  - Builds the Flask API using the `Dockerfile`.\n  - Maps container port `5000` to host port `5000`.\n  - Defines environment variables to connect to MongoDB.\n  - Depends on the MongoDB service, ensuring that the database is started before the API.\n  - Mounts the `./application` directory as a volume in the container so that local code changes reflect inside the container.\n\n### How to Run the Application with Docker\n\n1. **Build the Image and Start the Containers**:\n   - In the root directory of the project, run the following command to build the image and run the containers:\n     ```bash\n     docker-compose up --build\n     ```\n\n2. **Access the Application**:\n   - The API will be available at `http://localhost:5000`.\n\n3. **Stop the Containers**:\n   - To stop and remove the containers, run:\n     ```bash\n     docker-compose down\n     ```\n\n---\n\n## CI/CD with GitHub Actions\n\nThis project uses GitHub Actions to automate the process of testing and deploying the application whenever there is a push to the `main` branch.\n\n### Workflow File\n\nThe workflow file `test.yml` defines two main jobs: `test` and `deploy`.\n\n### Explanation of the Jobs\n\n- **Test Job (`test`)**:\n  - This job runs whenever a push is made to the `main` branch.\n  - Uses Python 3.9 to run unit tests.\n  - After installing dependencies via `pip`, the `make test` command is executed to run the unit tests.\n\n- **Deploy Job (`deploy`)**:\n  - The deploy is only executed if the tests pass (`needs: test`).\n  - Uses Node.js version 16 to set up the environment.\n  - Installs project dependencies with `yarn`.\n  - Installs the Railway CLI for deployment.\n  - Deploys the application to the `Railway` service using the command `railway up`, with the token and database URL passed as environment variables via `secrets`.\n\n### How to Configure CI/CD\n\n1. **Create Secrets in GitHub**:\n   - To configure automatic deployment in GitHub, you need to add the `RAILWAY_TOKEN` and `DATABASE_URL` secrets to the repository:\n     - In the GitHub repository, go to *Settings* \u003e *Secrets* \u003e *Actions* and add the secrets.\n\n2. **Manual Execution**:\n   - Besides automatic deployment on pushes to the `main` branch, you can also manually start the workflow via the GitHub Actions interface by clicking \"Run workflow\" under *Actions*.\n\n3. **Automatic Deployment**:\n   - Whenever you push to the `main` branch, the tests will automatically run, and if they pass, the deployment will be performed on Railway.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenzorooschqueiroz%2Fflask-user-crud-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fenzorooschqueiroz%2Fflask-user-crud-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenzorooschqueiroz%2Fflask-user-crud-api/lists"}