Skip to content

Commit

Permalink
Merge 1eca936 into 8e47333
Browse files Browse the repository at this point in the history
  • Loading branch information
JennyRemolina committed Jul 10, 2019
2 parents 8e47333 + 1eca936 commit 037c65c
Show file tree
Hide file tree
Showing 15 changed files with 513 additions and 27 deletions.
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ docs/_build/
# PyBuilder
target/

# Images
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Images
media/
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
language: python

python:
- "3.5.2"
- "3.6"

services:
- redis-server
addons:
postgresql: "9.6"
env:
- DJANGO_VERSION=2.1.7

before_install:
- export DJANGO_DB_NAME=travis_ci_test
- export DJANGO_USERNAME=postgres
- export DJANGO_PASSWORD=''
- export SECRET_KEY=7uz9h6wsq
install:
- pip install -r requirements.txt

Expand All @@ -22,7 +23,7 @@ before_script:

script:
- python manage.py migrate
- coverage run manage.py test
- python manage.py test -v 2

after_success:
- coveralls
- coveralls
19 changes: 19 additions & 0 deletions api/migrations/0004_auto_20190709_1849.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.1.7 on 2019-07-09 18:49

import api.models
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0003_auto_20190607_1921'),
]

operations = [
migrations.AlterField(
model_name='project',
name='project_photo',
field=models.ImageField(blank=True, null=True, upload_to=api.models.project_directory_path),
),
]
14 changes: 14 additions & 0 deletions api/migrations/0005_merge_20190710_2004.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 2.1.7 on 2019-07-10 20:04

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('api', '0004_auto_20190710_0859'),
('api', '0004_auto_20190709_1849'),
]

operations = [
]
2 changes: 1 addition & 1 deletion api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Project(models.Model):
title = models.CharField(max_length=30)
description = models.TextField()
project_photo = models.ImageField(
upload_to=project_directory_path, blank=True)
upload_to=project_directory_path, blank=True, null=True)
creator = models.ForeignKey(
CustomUser, on_delete=models.CASCADE, related_name="project_creator")
created_at = models.DateField(default=timezone.now)
Expand Down
172 changes: 172 additions & 0 deletions api/tests/test_projects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import json

from rest_framework.test import APITestCase

from users.models import CustomUser

from api.models import *
from api.serializers import *


class AuthBaseTestCase(APITestCase):
email = "@testuser.com"
password = "12345Hola"

def setUp(self):
self.user = CustomUser.objects.create_user('user', 'user'+self.email, self.password)
self.jefe = CustomUser.objects.create_user('jefe', 'jefe'+self.email, self.password)
self.emp1 = CustomUser.objects.create_user('emp1', 'emp1'+self.email, self.password)
self.emp2 = CustomUser.objects.create_user('emp2', 'emp2'+self.email, self.password)
self.client.login(username='jefe', password=self.password)



class ProjectTestCase(AuthBaseTestCase):
url = '/api/v1/projects'

def setUp(self):
super().setUp() # Authenticanting
self.data = {
"title": "black mesa",
"description": "particle accelerator",
"creator": self.jefe,
}
self.project = Project.objects.create(**self.data)
self.data['creator'] = self.jefe.id


def test_read_project_get(self):
"""
Test to verify GET project valid (Model and Serializer)
"""
response = self.client.get(self.url)
print(response.status_code, response.content)
self.assertEqual(200, response.status_code)
response_data = json.loads(response.content)['results']
project = Project.objects.get(title="black mesa")
projectSerial = ProjectSerializer(instance=project)
self.assertEqual(projectSerial.data, response_data[0])


def test_read_project_get_by_another_user(self):
"""
Test to verify GET project by a different user
"""
self.client.logout()
self.client.login(username='user', password=self.password)
url = self.url+'/{id}'.format(id=self.project.id)
response = self.client.get(url)
print(response.status_code, response.content)
# the project doesn't belong to the user
self.assertEqual(404, response.status_code) # Not found


def test_create_project_post(self):
"""
Test to verify POST project valid
"""
self.data['title'] = 'black mesa 2'
response = self.client.post(self.url, self.data)
print(response.status_code, response.content)
self.assertEqual(201, response.status_code)

response = self.client.get(self.url)
print(response.status_code, response.content)
self.assertEqual(200, response.status_code)
response_data = json.loads(response.content)
self.assertEqual(2, response_data['count']) # 2 projects


def test_delete_project_delete(self):
"""
Test to verify DELETE project valid
"""
url = self.url+'/{id}'.format(id=self.project.id)
response = self.client.get(url)
print(response.status_code, response.content)
self.assertEqual(200, response.status_code)

response = self.client.delete(url)
print(response.status_code, response.content)
self.assertEqual(204, response.status_code)
self.assertEqual(b'', response.content)


def test_update_project_patch(self):
"""
Test to verify PATCH project valid
"""
data = {'title': 'Black Mesa.'}
url = self.url+'/{id}'.format(id=self.project.id)
response = self.client.get(url)
print(response.status_code, response.content)
self.assertEqual(200, response.status_code)

response = self.client.patch(url, data)
print(response.status_code, response.content)
self.assertEqual(200, response.status_code)
response_data = json.loads(response.content)
print(response_data)
self.assertEqual(data['title'], response_data['title']) # updated


def test_update_project_put(self):
"""
Test to verify PUT project valid
"""
data = dict(self.data) # copy
data['title'] = 'black mesa 1'
url = self.url+'/{id}'.format(id=self.project.id)
response = self.client.get(url)
print(response.status_code, response.content)
self.assertEqual(200, response.status_code)

response = self.client.put(url, data)
print(response.status_code, response.content)
self.assertEqual(200, response.status_code)
response_data = json.loads(response.content)
print(response_data)
self.assertEqual(data['title'], response_data['title']) # updated


def test_update_project_put_invalid_incomplete(self):
"""
Test to verify PUT project invalid(incomplete)
"""
data = {
"title": "black mesa 1",
"creator": self.jefe.id,
}
url = self.url+'/{id}'.format(id=self.project.id)
response = self.client.get(url)
print(response.status_code, response.content)
self.assertEqual(200, response.status_code)

response = self.client.put(url, data)
print(response.status_code, response.content)
self.assertEqual(400, response.status_code)
response_data = json.loads(response.content)
print(response_data)
self.assertTrue("description" in response_data)


def test_update_project_put_invalid_inexistent(self):
"""
Test to verify PUT project invalid(inexistent)
"""
data = {
"title": "black mesa 9",
"description": "particle accelerator",
"creator": self.jefe.id,
}
url = self.url+'/{id}'.format(id=self.project.id+9)
response = self.client.get(url)
print(response.status_code, response.content)
self.assertEqual(404, response.status_code)

response = self.client.put(url, data)
print(response.status_code, response.content)
self.assertEqual(404, response.status_code)
response_data = json.loads(response.content)
print(response_data)
self.assertTrue("detail" in response_data)
2 changes: 1 addition & 1 deletion projexbackend/config/development.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '10.0.3.2']
ALLOWED_HOSTS = ['127.0.0.1', 'localhost', 'testserver', '10.0.3.2']
35 changes: 23 additions & 12 deletions projexbackend/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,36 @@
# Application definition

INSTALLED_APPS = [
'channels',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'api',
'corsheaders',
'rest_framework',
'rest_framework.authtoken',
'users',
'django.contrib.sites',

'allauth',
'allauth.account',
'channels',
'corsheaders',
'django_countries',
'rest_auth',
'rest_auth.registration',
'django_countries',
'rest_framework',
'rest_framework.authtoken',
'rest_framework_swagger',

'api',
'users',
]

# Use nose to run all tests
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

# Tell nose to measure coverage on the 'api' and 'users' apps
NOSE_ARGS = [
'--with-coverage',
'--cover-package=api,users',
]

ASGI_APPLICATION = 'projexbackend.routing.application'
Expand Down Expand Up @@ -110,11 +121,11 @@
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['DJANGO_DB_NAME'],
'USER': os.environ['DJANGO_USERNAME'],
'PASSWORD': os.environ['DJANGO_PASSWORD'],
'HOST': 'localhost',
'PORT': '',
'HOST': os.getenv('DJANGO_DB_HOST', 'localhost'),
'PORT': os.getenv('DJANGO_DB_PORT',''),#5432
'NAME': os.getenv('DJANGO_DB_NAME'),
'USER': os.getenv('DJANGO_USERNAME'),
'PASSWORD': os.getenv('DJANGO_PASSWORD'),
}
}

Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ constantly==15.1.0
coreapi==2.3.3
coreapi-cli==1.0.9
coreschema==0.0.4
coverage==4.0.3
coverage==4.5.3
cryptography==2.7
daphne==2.3.0
defusedxml==0.6.0
Expand All @@ -32,6 +32,7 @@ django-cors-headers==3.0.2
django-countries==5.3.3
django-filter==2.1.0
django-model-utils==3.1.2
django-nose==1.4.6
django-rest-auth==0.9.5
django-rest-swagger==2.2.0
django-uuidfield==0.5.0
Expand All @@ -58,6 +59,7 @@ mccabe==0.6.1
msgpack==0.5.6
msgpack-python==0.5.6
ndg-httpsclient==0.5.1
nose==1.3.7
oauthlib==3.0.1
openapi-codec==1.3.2
pathlib==1.0.1
Expand Down
19 changes: 19 additions & 0 deletions users/migrations/0002_auto_20190704_1433.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.1.7 on 2019-07-04 14:33

from django.db import migrations, models
import users.models


class Migration(migrations.Migration):

dependencies = [
('users', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='customuser',
name='profile_photo',
field=models.ImageField(blank=True, null=True, upload_to=users.models.user_directory_path),
),
]
14 changes: 14 additions & 0 deletions users/migrations/0003_merge_20190710_2004.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 2.1.7 on 2019-07-10 20:04

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('users', '0002_auto_20190710_0859'),
('users', '0002_auto_20190704_1433'),
]

operations = [
]
Loading

0 comments on commit 037c65c

Please sign in to comment.