diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 21b7877c218d97a305c47fb5e24232a7aaa26561..10035b349c0e684e5f7d8f2de248b00cb6701b9f 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,30 +1,16 @@ image: python:3.9 -# variables: -# MYSQL_DATABASE: $MYSQL_DATABASE -# MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD -# MYSQL_USER: $MYSQL_USER -# MYSQL_PASSWORD: $MYSQL_PASSWORD -# MYSQL_DATABASE: $MYSQL_DATABASE +services: + - name: mysql:latest + command: ["mysqld", "--authentication-policy=mysql_native_password"] + alias: mysql -# services: -# - name: mysql:latest -# command: ["mysqld", "--authentication-policy=mysql_native_password"] -# alias: mysql - -# services: -# - name: mysql:latest -# command: ["mysqld", "--authentication-policy=mysql_native_password"] -# alias: mysql - -# variables: -# MYSQL_DATABASE: $MYSQL_DATABASE -# MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD - -# Host: mysql -# User: $MYSQL_USER -# Password: $MYSQL_PASSWORD -# Database: $MYSQL_DATABASE +variables: + MYSQL_DATABASE: $MYSQL_DATABASE + MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD + MYSQL_USER: $MYSQL_USER + MYSQL_PASSWORD: $MYSQL_PASSWORD + MYSQL_DATABASE: $MYSQL_DATABASE stages: @@ -51,6 +37,16 @@ cache: #### #### ####################################################################################################################################### +install-virtualenv: + stage: install + script: + - python3 -m venv venv/ + artifacts: + paths: + - venv/ + expire_in: 30 mins + + install-npm-packages: image: node:14.6.0 stage: install @@ -72,11 +68,12 @@ lint-back: stage: test allow_failure: true before_script: - - python3 -m venv venv/ - source venv/bin/activate - script: - pip install pycodestyle + script: - pycodestyle --config=./backend/setup.cnf ./backend + dependencies: + - install-virtualenv lint-front: @@ -90,24 +87,25 @@ lint-front: - install-npm-packages -# test: -# stage: test -# variables: -# MYSQL_DATABASE: $MYSQL_DATABASE -# MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD -# MYSQL_DATABASE: $MYSQL_DATABASE -# MYSQL_USER: $MYSQL_USER -# MYSQL_PASSWORD: $MYSQL_PASSWORD -# DB_HOST: mysql -# DB_PORT: 3306 -# WEB_ROOT: http://localhost:3000 -# before_script: -# - source ./venv/bin/activate -# script: -# - cd ./backend -# - docker build -t app ./backend -# - docker run --detach -p 8000:80 app --env-file ./backend/.env -# - curl "http://localhost:8000/api/health" +test: + stage: test + variables: + # MYSQL_DATABASE: $MYSQL_DATABASE + # MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD + # MYSQL_DATABASE: $MYSQL_DATABASE + # MYSQL_USER: $MYSQL_USER + # MYSQL_PASSWORD: $MYSQL_PASSWORD + DB_HOST: mysql + DB_PORT: 3306 + WEB_ROOT: http://localhost:3000 + before_script: + - source ./venv/bin/activate + - pip install pytest + script: + - cd ./backend + - pytest + dependencies: + - install-virtualenv ####################################################################################################################################### #### #### @@ -162,16 +160,6 @@ lint-front: - scp -r build/ eatfast@"$DOMAIN":/var/www/eatfast-website/frontend ; fi -# deploy-back-staging: -# extends: .deploy -# rules: -# - if: $CI_COMMIT_BRANCH == $STAGING_BRANCH -# when: always -# variables: -# DOMAIN: nofist.test.viarezo.fr -# PRIVATE_KEY: "$SSH_PRIVATE_KEY_STAGING" - - deploy-back-prod: extends: .deploy rules: diff --git a/backend/test_main.py b/backend/test_main.py new file mode 100644 index 0000000000000000000000000000000000000000..b36336b66676a1d676018a5591f961b8fa5e8468 --- /dev/null +++ b/backend/test_main.py @@ -0,0 +1,50 @@ +from fastapi.testclient import TestClient +from datetime import datetime, timedelta +import pytz + +from main import app + +client = TestClient(app) + +test = { + "restaurant": { + "place": "restaurant test", + "day": datetime.now().weekday(), + "open_time": datetime.now(tz=pytz.timezone("Europe/Paris")).strftime("%H:%M:%S"), + "close_time": (datetime.now(tz=pytz.timezone("Europe/Paris")) + timedelta(hours=1)).strftime("%H:%M:%S") + }, + "news": { + "title": "news test", + "content": "content test", + "end_date": (datetime.now(tz=pytz.timezone("Europe/Paris")) + timedelta(hours=1)).strftime("%Y-%m-%dT%H:%M:%S"), + "place": "restaurant test" + }, +} + + +def test_post_opening_hours(): + response = client.post( + "/api/opening_hours", + headers={"accept": "application/json", "Content-Type": "application/json"}, + json=test["restaurant"], + ) + assert response.status_code == 200 + assert response.json()["place"] == test["restaurant"]["place"] + + +def test_get_restaurants(): + response = client.get("/api/restaurants") + assert response.status_code == 200 + assert len(response.json()) == 1 + assert response.json()[0]["name"] == test["restaurant"]["place"] + assert response.json()[0]["status"] + + +def test_post_news_not_admin(): + response = client.post( + "/api/news", + headers={"Content-Type": "application/json", "Cookie": "connect_id=wrong_cookie"}, + json=test["news"], + ) + assert response.status_code != 200 + assert response.json() == {"detail": "Invalid cookie"}