Skip to content

Commit

Permalink
Merge pull request #268 from fga-eps-mds/dev
Browse files Browse the repository at this point in the history
Updating master
  • Loading branch information
lucasca73 authored Nov 3, 2018
2 parents d2009ff + f2f4cc8 commit d337645
Show file tree
Hide file tree
Showing 46 changed files with 1,093 additions and 149 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vscode
.env
.DS_Store
47 changes: 18 additions & 29 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,6 @@ stages:
- publish
- deploy

unit tests:
image: python:3
before_script:
- pip install -r api/api_gateway/requirements/dev.txt
stage: test
script:
- cd api && sh run-tests.sh

publish codecov:
image: python:3
before_script:
- pip install -r api/api_gateway/requirements/dev.txt
only:
- master
stage: publish
dependencies:
- unit tests
script:
- cd api && sh run-tests.sh
- cd api_gateway && codecov -t ${CODECOV_TOKEN}

# github pages
publish pages:
image: docker:stable
Expand Down Expand Up @@ -63,7 +42,7 @@ publish image to staging:
- cd api && sh run-publish-docker.sh staging

deploy to production:
image: python:3
image: python:3.5.6-slim-stretch
only:
- tags
stage: deploy
Expand All @@ -72,12 +51,22 @@ deploy to production:
- sh install-kubectl.sh
- sh run-deploy.sh production

deploy to staging:
image: python:3
only:
- dev
# deploy to staging:
# image: python:3
# only:
# - dev
# stage: deploy
# script:
# - cd api
# - sh install-kubectl.sh
# - sh run-deploy.sh staging

deploy to heroku:
before_script:
- gem install dpl
stage: deploy
script:
- cd api
- sh install-kubectl.sh
- sh run-deploy.sh staging
- sh heroku-helper.sh
- cd api/api_gateway && dpl --provider=heroku --app=integra-vendas-api-gateway --api-key=$HEROKU_DEV_KEY
only:
- dev
32 changes: 28 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# .travis.yml
sudo: required

language: python
python:
- "3.4"
Expand All @@ -12,10 +14,32 @@ virtualenv:
system_site_packages: true

stages:
- test
- Staging Tests
- Production Tests

services:
- docker

env:
- DOCKER_COMPOSE_VERSION=1.22.0

before_install:
- sudo rm /usr/local/bin/docker-compose
- curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
- chmod +x docker-compose
- sudo mv docker-compose /usr/local/bin

jobs:
include:
- stage: "Tests"
name: "Unit Tests"
script: cd api && sh run-tests.sh
- stage: "Integration Tests"
name: "Unit Integrated Tests"
script:
- make integration-tests file=dc-integration-test.staging.yml
- cd api/api_gateway && codecov -t ${CODECOV_TOKEN}

- stage: "Integration Tests with Production images"
name: "Publishing test coverage"
if: branch = master
script:
- make integration-tests file=dc-integration-test.production.yml
- cd api/api_gateway && codecov -t ${CODECOV_TOKEN}
33 changes: 32 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
default:
cd api && make

run:
cd api && make run
cd api && make run

integration-tests:
docker-compose -f ${file} build
docker-compose -f ${file} up -d
echo "Running Integration Tests"
docker exec api-gateway bash -c "ls && bash ./check-services.sh && sh run-tests.sh"
docker-compose -f ${file} down
docker-compose -f ${file} rm -f -s

staging-integration-tests:
sh remove-all-containers.sh || true
make integration-tests file=dc-integration-test.staging.yml


production-integration-tests:
sh remove-all-containers.sh || true
make integration-tests file=dc-integration-test.production.yml

backend:
sh remove-all-containers.sh || true
docker-compose -f dc-backend-production.yml build
docker-compose -f dc-backend-production.yml up

build-staging:
docker-compose -f dc-integration-test.staging.yml build

build-production:
docker-compose -f dc-integration-test.production.yml build
14 changes: 8 additions & 6 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
FROM python:3
FROM python:3.5.6-slim-stretch
ENV PYTHONUNBUFFERED 1

RUN mkdir /code
WORKDIR /code

ADD ./api_gateway /code/
ADD . /code
WORKDIR /code/api_gateway
ADD VERSION /code

RUN pip install --upgrade pip
RUN pip install -r requirements/dev.txt
RUN pip install -r requirements/dev.txt

RUN apt-get update
RUN apt-get install curl libcurl3 libcurl3-dev -y
19 changes: 14 additions & 5 deletions api/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
default:
docker build . -t docker-django:api-gateway
docker run --rm -p 5000:5000 -v `pwd`:"/app" -w "/app" --name api-gateway -it docker-django:api-gateway bash
docker network create api-backend || true
docker-compose up

run:
docker build . -t docker-django:api-gateway
docker run --rm -p 5000:5000 -v `pwd`:"/app" -w "/app" --net=backend --name api-gateway -d docker-django:api-gateway bash -c "sh run-django.sh"
echo NEED_UPDATE

build:
docker-compose build

enter:
docker exec -ti api-gateway bash
docker-compose exec web bash

test:
docker-compose exec web bash -c "cd api_gateway && python manage.py test"

production:
docker-compose -f docker-compose-production.yml build
docker-compose -f docker-compose-production.yml up
2 changes: 1 addition & 1 deletion api/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1
1.0
2 changes: 2 additions & 0 deletions api/api_gateway/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
release: python manage.py makemigrations && python manage.py migrate --noinput
web: gunicorn api_gateway.wsgi --log-file -
25 changes: 25 additions & 0 deletions api/api_gateway/api/login_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.conf import settings
from rest_framework.status import (
HTTP_403_FORBIDDEN,
HTTP_200_OK,
HTTP_400_BAD_REQUEST,
HTTP_500_INTERNAL_SERVER_ERROR
)
from rest_framework.response import Response
import requests
import json

def verify_token(data_request):
if not 'token' in data_request:
return Response({'error': 'Token vazio'},
status=HTTP_400_BAD_REQUEST)
try:
token = data_request['token']
response = requests.post(settings.LOGIN + '/api/token-verify/', data={'token':token})
if not 'token' in response.json():
return Response({'error': 'Falha na autenticação'},
status=HTTP_403_FORBIDDEN) #Erro de token incorreto
except:
return Response({'error': 'Nao foi possivel se comunicar com o servidor para autenticação'},
status=HTTP_500_INTERNAL_SERVER_ERROR) #Erro de servidor
return Response({''}, HTTP_200_OK)
5 changes: 5 additions & 0 deletions api/api_gateway/api/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .test_user import *
from .test_login import *
from .test_order import *
from .test_product import *
from .test_version import *
30 changes: 30 additions & 0 deletions api/api_gateway/api/tests/login_test_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from django.conf import settings
import requests

default_password = "test1234"

def registrate_new_user(email):

data = {
"username": email,
"email": email,
"password1": default_password,
"password2": default_password
}

response = requests.post(settings.LOGIN + '/api/registration/', data=data)

return response.json()

def login_user(email):

data = {
"username": email,
"password": default_password
}

response = requests.post(settings.LOGIN + '/api/login/', data=data)

return response.json()


16 changes: 16 additions & 0 deletions api/api_gateway/api/tests/order_test_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.conf import settings
import requests

def create_order(fk_product = None, fk_buyer = None, buyer_message = None, quantity = None, total_price = None, product_name = None, token = None):

data = {
'fk_product': fk_product,
'fk_buyer': fk_buyer,
'buyer_message': buyer_message,
'quantity': quantity,
'total_price': total_price,
'product_name': product_name,
'token': token
}

return data
15 changes: 15 additions & 0 deletions api/api_gateway/api/tests/product_test_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.conf import settings
import requests

def create_product(name = None, fk_vendor = None, price = None, photo = None, description = None, token = None):

data = {
'fk_vendor': fk_vendor,
'name': name,
'price': price,
'photo': photo,
'description': description,
'token': token
}

return data
30 changes: 30 additions & 0 deletions api/api_gateway/api/tests/test_login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from django.test import TestCase
from .login_test_helper import registrate_new_user, login_user


# Create your tests here.
class LoginTest(TestCase):

def test_user_registration(self):

email = '[email protected]'

responseJson = registrate_new_user(email)

token = responseJson['token']
user = responseJson['user']

self.assertEqual(user["email"], email)

def test_user_login(self):

email = '[email protected]'

responseJson = registrate_new_user(email)
loginResponseJson = login_user(email)

login_token = loginResponseJson["token"]
user = loginResponseJson["user"]

self.assertEqual(user["email"], email)

Loading

0 comments on commit d337645

Please sign in to comment.