Skip to content

Commit

Permalink
initial tests for building endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
asifpy committed Feb 9, 2018
1 parent 6b4bcd6 commit 15cf312
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 13 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# LANDLORD #

Modern property management tool for landlords, property managers, and renters. It allows landlords to track thier buildings, apartments and tenants information in way much better way.
Modern property management tool for landlords, property managers, and renters. It allows landlords to track thier buildings, apartments and tenants information in much better way.

This is a work-in-progress code branch of landlord.
This is still a work-in-progress code branch of landlord.

**Stack**

* An enterprise class database: [Postgresql](https://www.postgresql.org/)
* Backend: [Django REST Framework](http://www.django-rest-framework.org/)
* Awesome [Angular](https://angular.io/guide/quickstart) on the client side
Expand Down
12 changes: 5 additions & 7 deletions server/api/apartment/views.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
from django.shortcuts import get_object_or_404
from django.core.exceptions import ObjectDoesNotExist

from rest_framework import viewsets
from rest_framework.exceptions import APIException
from rest_framework.response import Response
from rest_framework.decorators import list_route
from rest_framework.permissions import IsAuthenticated

from core.models import Apartment, UserProfile, Tenant
from core.models import Apartment, UserProfile
from api.building.permissions import IsLandlordPermission
from api.tenant.serializers import TenantSerializer
from .serializers import ApartmentSerializer


Expand Down Expand Up @@ -42,12 +38,14 @@ def get_queryset(self):
user = self.request.user
profile = get_object_or_404(UserProfile, user=user)
landlord = profile.landlord
return Apartment.objects.filter(building__owner=landlord)
return Apartment.objects.filter(
building__owner=landlord
).select_related('building')

def retrieve(self, request, pk=None):
"""Return the apartment instace for the given apartment ID."""

# override the detial route to attach related tenants
# override the detail route to attach related tenants
apartment = get_object_or_404(self.get_queryset(), pk=pk)
serializer = ApartmentSerializer(
apartment,
Expand Down
107 changes: 107 additions & 0 deletions server/api/building/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from django.urls import reverse
from django.contrib.auth.models import User

from rest_framework import status
from rest_framework.test import APITestCase
from rest_framework_jwt import utils

from core.models import UserProfile, Building, Landlord
from api.profile.serializers import LandlordSerializer
from api.building.serializers import BuildingSerializer


class BuildingTest(APITestCase):

LIST_ROUTE = 'v1:building-list'
DETAIL_ROUTE = 'v1:building-detail'

def setUp(self):
"""Building setup"""
self.user = User.objects.create_user(
'john',
'john@me.org',
'john'
)
landlord = Landlord.objects.create(
name="John",
email='john@me.org',
mobile='+456452984'
)

UserProfile.objects.create(user=self.user, landlord=landlord)

self.building_data = {
'name': 'Building-1',
'number': '782-31',
'owner': LandlordSerializer(landlord).data
}

payload = utils.jwt_payload_handler(self.user)
self.token = utils.jwt_encode_handler(payload)
self.auth = 'JWT {}'.format(self.token)

def create_building(self):
"""Creates new bulidng instance"""
response = self.client.post(
reverse(self.LIST_ROUTE),
self.building_data,
HTTP_AUTHORIZATION=self.auth
)
return response

def test_create_valid_building(self):
"""Check if building gets created for valid data"""
response = self.create_building()
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Building.objects.count(), 1)
self.assertEqual(Building.objects.get().name, 'Building-1')

def test_create_invalid_building(self):
"""Check if building gets created for invalid data"""

response = self.client.post(
reverse(self.LIST_ROUTE),
{},
HTTP_AUTHORIZATION=self.auth
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_get_buildings(self):
"""Checks buildings for the authenticated user"""

self.create_building()

response = self.client.get(
reverse(self.LIST_ROUTE),
HTTP_AUTHORIZATION=self.auth
)

buildings = Building.objects.all()
serializer = BuildingSerializer(buildings, many=True)
self.assertEqual(response.data, serializer.data)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_building_exists(self):
"""Test building exist for provided id"""
building = self.create_building()

response = self.client.get(
reverse(self.DETAIL_ROUTE, kwargs={'pk': building.data['id']}),
HTTP_AUTHORIZATION=self.auth
)
_building = Building.objects.get(pk=building.data['id'])
serializer = BuildingSerializer(
_building,
context={'enable_apartments': True}
)
self.assertEqual(response.data, serializer.data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(_building.name, building.data['name'])

def test_building_doesnot_exist(self):
"""Test building doesnot exist for the provided id"""
response = self.client.get(
reverse(self.DETAIL_ROUTE, kwargs={'pk': 44}),
HTTP_AUTHORIZATION=self.auth
)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
4 changes: 1 addition & 3 deletions server/api/building/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from django.shortcuts import get_object_or_404

from rest_framework import viewsets
from rest_framework.decorators import detail_route
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated

from core.models import UserProfile, Building, Apartment
from api.apartment.serializers import ApartmentSerializer
from core.models import UserProfile
from .permissions import IsLandlordPermission
from .serializers import BuildingSerializer

Expand Down
3 changes: 2 additions & 1 deletion server/landlord/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
),

'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.NamespaceVersioning'
'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.NamespaceVersioning',
'TEST_REQUEST_DEFAULT_FORMAT': 'json'
}

# Enables django-rest-auth to use JWT tokens instead of regular tokens.
Expand Down

0 comments on commit 15cf312

Please sign in to comment.