def test_filter_apartments(self): realtor = UserFactory.create_realtor(verified=True) client = UserFactory.create_client(verified=True) ApartmentFactory.create_apartment(realtor) ApartmentFactory.create_apartment(realtor) self.client.force_authenticate(user=client) url = '{}?min_size=70'.format(self.url) response = self.client.get(url, format='json') self.assertEqual(len(response.json().get('results')), 2) url = '{}?min_size=90'.format(self.url) response = self.client.get(url, format='json') self.assertEqual(len(response.json().get('results')), 0) url = '{}?max_price=300'.format(self.url) response = self.client.get(url, format='json') self.assertEqual(len(response.json().get('results')), 0) url = '{}?number_of_rooms=4'.format(self.url) response = self.client.get(url, format='json') self.assertEqual(len(response.json().get('results')), 2) url = '{}?number_of_rooms=2'.format(self.url) response = self.client.get(url, format='json') self.assertEqual(len(response.json().get('results')), 0)
def test_client_get_apartments(self): client = UserFactory.create_client(verified=True) owner = UserFactory.create_realtor(verified=True) ApartmentFactory.create_apartment(owner) ApartmentFactory.create_apartment(owner, status='Rented') self.client.force_authenticate(user=client) response = self.client.get(self.url, format='json') self.assertEqual(len(response.json().get('results')), 1)
def test_admin_delete_user(self): admin = UserFactory.create_admin(verified=True) client = UserFactory.create_client() self.client.force_authenticate(user=admin) url = '{}{}/'.format(self.user_url, client.id) self.client.delete(url, format='json') with self.assertRaises(User.DoesNotExist): User.objects.get(pk=client.id)
def test_admin_invite_existing_user(self): admin = UserFactory.create_admin(verified=True) client = UserFactory.create_client() self.client.force_authenticate(user=admin) url = '{}invite/'.format(self.user_url) body = {'email': client.email} response = self.client.post(url, body, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_admin_update_user(self): admin = UserFactory.create_admin(verified=True) client = UserFactory.create_client() self.client.force_authenticate(user=admin) url = '{}{}/'.format(self.user_url, client.id) body = {'last_name': 'User Editted'} response = self.client.patch(url, body, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.json().get('last_name'), 'User Editted')
def test_client_update_delete_apartment(self): client = UserFactory.create_client(verified=True) owner = UserFactory.create_realtor(verified=True) apartment = ApartmentFactory.create_apartment(owner) body = {'name': 'Apartment new name'} url = '{}{}/'.format(self.url, apartment.id) self.client.force_authenticate(user=client) response = self.client.patch(url, body, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) response = self.client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_realtor_update_delete_apartment_of_another(self): realtor = UserFactory.create_realtor(verified=True) realtor1 = UserFactory.create_realtor(email='*****@*****.**', verified=True) apartment = ApartmentFactory.create_apartment(realtor1) self.client.force_authenticate(user=realtor) url = '{}{}/'.format(self.url, apartment.id) body = {'name': 'Apartment New Name'} response = self.client.patch(url, body, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) response = self.client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_realtor_admin_get_apartments(self): owner = UserFactory.create_realtor(verified=True) another_realtor = UserFactory.create_realtor('*****@*****.**', verified=True) admin = UserFactory.create_admin(verified=True) ApartmentFactory.create_apartment(owner) ApartmentFactory.create_apartment(owner, status='Rented') self.client.force_authenticate(user=another_realtor) response = self.client.get(self.url, format='json') self.assertEqual(len(response.json().get('results')), 2) self.client.force_authenticate(user=admin) response = self.client.get(self.url, format='json') self.assertEqual(len(response.json().get('results')), 2)
def test_admin_delete_himself(self): admin = UserFactory.create_admin(verified=True) self.client.force_authenticate(user=admin) url = '{}{}/'.format(self.user_url, admin.id) response = self.client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_admin_get_users(self): admin = UserFactory.create_admin(verified=True) self.client.force_authenticate(user=admin) response = self.client.get(self.user_url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.json().get('results')), 1)
def test_client_get_update_delete_another(self): client1 = UserFactory.create_client(verified=True) client2 = UserFactory.create_client(email='*****@*****.**', verified=True) url = '{}{}/'.format(self.user_url, client2.id) self.client.force_authenticate(user=client1) response = self.client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) body = {'last_name': 'Lastname changed'} response = self.client.patch(url, body, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) response = self.client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_admin_update_delete_apartment(self): realtor = UserFactory.create_realtor(verified=True) admin = UserFactory.create_admin(verified=True) apartment = ApartmentFactory.create_apartment(realtor) self.client.force_authenticate(user=admin) url = '{}{}/'.format(self.url, apartment.id) body = {'name': 'Apartment New Name'} response = self.client.patch(url, body, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) apartment.refresh_from_db() self.assertEqual(apartment.name, body['name']) response = self.client.delete(url, format='json') with self.assertRaises(Apartment.DoesNotExist): Apartment.objects.get(pk=apartment.id)
def test_admin_create_apartment(self): admin = UserFactory.create_admin(verified=True) self.client.force_authenticate(user=admin) response = self.client.post(self.url, self.apartment_data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_client_create_apartment(self): client = UserFactory.create_client(verified=True) self.client.force_authenticate(user=client) response = self.client.post(self.url, self.apartment_data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_admin_invite_new_user_with_invalid_email(self): admin = UserFactory.create_admin(verified=True) self.client.force_authenticate(user=admin) url = '{}invite/'.format(self.user_url) body = {'email': 'new'} response = self.client.post(url, body, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_block_user_with_3_attempts(self): user = UserFactory.create_client(verified=True) credential = {'email': '*****@*****.**', 'password': '******'} self.client.post(self.login_url, credential, format='json') self.client.post(self.login_url, credential, format='json') response = self.client.post(self.login_url, credential, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) user.refresh_from_db() self.assertEqual(user.is_active, False)
def test_client_get_update_delete_themself(self): client = UserFactory.create_client(verified=True) url = '{}{}/'.format(self.user_url, client.id) self.client.force_authenticate(user=client) response = self.client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) body = {'last_name': 'My lastname changed'} response = self.client.patch(url, body, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) response = self.client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_login_user_without_email_verification(self): UserFactory.create_client() credential = {'email': '*****@*****.**', 'password': '******'} response = self.client.post(self.login_url, credential, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_login_user_success(self): UserFactory.create_client(verified=True) credential = {'email': '*****@*****.**', 'password': '******'} response = self.client.post(self.login_url, credential, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK)