Skip to content

Commit

Permalink
HAN 109: Tests unitarios (#23)
Browse files Browse the repository at this point in the history
added tests
  • Loading branch information
feijooso authored May 25, 2024
1 parent c862324 commit aebf439
Show file tree
Hide file tree
Showing 11 changed files with 391 additions and 8 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Tests

on:
push:
branches:
- main
- develop
pull_request:
branches:
- '*'

jobs:
test:
name: Tests microservice
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.11

- name: Install Python dependencies
run: pip install -r requirements.txt

- name: Run tests & Coverage file
run: |
python -m venv venv
source venv/bin/activate
pytest
pytest --cache-clear --cov=app/service app/tests/ --cov-report=xml > pytest-coverage.txt
env:
DATABASE_URL: postgresql://pepe
PYTHONPATH: ./app

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.xml
directory: ./coverage/reports/
flags: unittests
env_vars: OS,PYTHON
name: codecov-umbrella
fail_ci_if_error: true
path_to_write_report: ./coverage/codecov_report.txt
verbose: true
17 changes: 17 additions & 0 deletions Dockerfile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM python:3.11-slim-buster

WORKDIR /users

RUN apt-get update \
&& apt-get -y install libpq-dev gcc \
&& pip install psycopg2

COPY requirements.txt ./

RUN pip install -r requirements.txt

EXPOSE ${PORT}

COPY . .

CMD ["sh", "-c","DATABASE_URL=postgresql://user:1234@sql:5432/users", "PYTHONPATH=./app", "pytest"]
2 changes: 1 addition & 1 deletion app/controller/Users.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from fastapi import status
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from service.Social import SocialService
from external.Social import SocialService
from service.Users import UsersService


Expand Down
File renamed without changes.
Empty file added app/external/__init__.py
Empty file.
Empty file added app/models/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion app/repository/Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from sqlalchemy.orm import Session
from os import environ
from typing import Optional
from models.database import Base
from app.models.database import Base
from models.users import User
from models.alarms import Alarm
from datetime import date, datetime
Expand Down
10 changes: 5 additions & 5 deletions app/service/Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def delete_notification(self, user_id: int, notification_id: int):
self.user_repository.rollback()
raise e

def _generate_nickname(self, name):
def _generate_nickname(self, name): # pragma: no cover

name_without_spaces = name.replace(" ", "")
uuid_max = 8
Expand Down Expand Up @@ -131,7 +131,7 @@ def login(self, auth_code: str):
algorithm="HS256")
return user, jwt_token

def _get_access_token(self, authorization_code):
def _get_access_token(self, authorization_code): # pragma: no cover
token_url = "https://oauth2.googleapis.com/token"
payload = {
"client_id": os.environ["GOOGLE_CLIENT_ID"],
Expand All @@ -146,7 +146,7 @@ def _get_access_token(self, authorization_code):
else:
return None

def _get_user_info(self, access_token):
def _get_user_info(self, access_token): # pragma: no cover
user_info_url = "https://www.googleapis.com/oauth2/v2/userinfo"
headers = {"Authorization": f"Bearer {access_token}"}
params = {"fields": "id,email,name,picture,gender"}
Expand All @@ -171,14 +171,14 @@ def _validate_location(self, location):
return True
return False

def retrieve_user_id(self, request):
def retrieve_user_id(self, request): # pragma: no cover
token = self.__get_token(request.headers)
payload = jwt.decode(token,
os.environ["JWT_SECRET"],
algorithms=["HS256"])
return int(payload.get("user_id"))

def __get_token(self, headers: dict):
def __get_token(self, headers: dict): # pragma: no cover
keyName = None
for key in headers.keys():
if key.lower() == TOKEN_FIELD_NAME:
Expand Down
Empty file added app/tests/__init__.py
Empty file.
Loading

0 comments on commit aebf439

Please sign in to comment.