Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # from django.core.serializers import json
- import json
- from django.test import TestCase
- from api import messages
- from api.models import *
- # Create your tests here.
- def response_to_json(response):
- return json.loads(response.content)
- class DummyRoles(TestCase):
- # creating DummyRoles , So User can be created using user_role_id
- def setUp(self):
- self.user_role_1 = Role.objects.create(role_name="Admin")
- self.user_role_2 = Role.objects.create(role_name="Restaurant_Admin")
- self.user_role_3 = Role.objects.create(role_name="Manager")
- self.user_role_4 = Role.objects.create(role_name="Customer")
- class DummyUsers(DummyRoles):
- def setUp(self):
- super(DummyUsers, self).setUp()
- self.user_1 = User.objects.create_user(email="admin@localhost", password="admin", user_role=self.user_role_1,
- contact_no=8460373610)
- self.user_2 = User.objects.create_user(email='restaurantAdmin@localhost', password='admin', user_role=self.
- user_role_2,
- contact_no=8888888888)
- self.user_3 = User.objects.create_user(email="Manager@localhost", password="admin", user_role=self.user_role_3,
- contact_no=8986373610)
- self.user_4 = User.objects.create_user(email="customer3@localhost", password="admin", user_role=self.user_role_4,
- contact_no=8986373610)
- self.token_1 = Token.objects.get(user=self.user_1).key
- self.token_2 = Token.objects.get(user=self.user_2).key
- self.token_3 = Token.objects.get(user=self.user_3).key
- self.token_4 = Token.objects.get(user=self.user_4).key
- self.header_1 = {"HTTP_AUTHORIZATION": 'Token {}'.format(self.token_1)}
- self.header_2 = {"HTTP_AUTHORIZATION": 'Token {}'.format(self.token_2)}
- self.header_3 = {"HTTP_AUTHORIZATION": 'Token {}'.format(self.token_3)}
- self.header_4 = {"HTTP_AUTHORIZATION": 'Token {}'.format(self.token_4)}
- class UserRegistrationTest(DummyRoles):
- def setUp(self):
- super(UserRegistrationTest, self).setUp()
- def test_user_creation_with_email(self):
- data = {
- "email": "hello@localhost",
- "password": "a123",
- "user_role": self.user_role_2.id,
- "contact_no": "0123456789"
- }
- response = self.client.post('/api/users/', data=data)
- self.assertEqual(response.status_code, 201)
- self.assertEqual(User.objects.all().count(), 1)
- def test_user_registration_with_email_fail(self):
- data = {
- "email": "invalidemail.com",
- "password": "admin",
- "user_role": self.user_role_2.id,
- "contact_no": "0123456789"
- }
- response = self.client.post('/api/users/', data=data)
- self.assertEqual(response.status_code, 400)
- self.assertIn(messages.INVALID_EMAIL_ADDRESS['message'], response.content)
- def test_duplicate_email_not_allowed(self):
- data = {
- "email": "hello@localhost",
- "password": "a123",
- "user_role": self.user_role_2.id,
- "contact_no": "0123456789"
- }
- self.client.post('/api/users/', data=data)
- response = self.client.post('/api/users/', data=data)
- self.assertEqual(response.status_code, 400)
- self.assertIn(messages.USER_ALREADY_EXISTS['message'], response.content)
- def test_without_email_not_allowed(self):
- data = {
- "password": "admin",
- "user_role": self.user_role_2.id,
- "contact_no": "0123456789"
- }
- self.client.post('/api/users', data=data)
- response = self.client.post('/api/users/', data=data)
- self.assertEqual(response.status_code, 400)
- self.assertIn(messages.REQUIRED_EMAIL['message'], response.content)
- def test_without_password_not_allowed(self):
- data = {
- "email": "new@new.com",
- "user_role": self.user_role_1.id,
- "contact_no": "0123456789"
- }
- self.client.post('/api/users', data=data)
- response = self.client.post('/api/users/', data=data)
- self.assertEqual(response.status_code, 400)
- def test_without_role_not_allowed(self):
- data = {
- "email": "new@new.com",
- "password": "123",
- "contact_no": "0123456789"
- }
- self.client.post('/api/users', data=data)
- response = self.client.post('/api/users/', data=data)
- self.assertEqual(response.status_code, 400)
- def test_without_contact_no_not_allowed(self):
- data = {
- "email": "new@new.com",
- "password": "123",
- "user_role": self.user_role_1.id,
- }
- self.client.post('/api/users', data=data)
- response = self.client.post('/api/users/', data=data)
- self.assertEqual(response.status_code, 400)
- class UserDetailTest(DummyUsers):
- def setUp(self):
- super(UserDetailTest, self).setUp()
- def test_get_successful_user_detail(self):
- response = self.client.get('/api/users/%d/' % self.user_1.id,
- {}, **self.header_1)
- json_response = response_to_json(response)
- self.assertEqual(json_response['email'], self.user_1.email)
- def test_get_failing_user_detail(self):
- response = self.client.get('/api/users/%d/' % self.user_1.id,
- {}, **self.header_2)
- self.assertIn(messages.TOKEN_UNAUTHORIZED["message"], response.content)
- def test_put_successful_user_detail(self):
- data_dict = {"first_name": "snow", "email": self.user_1.email, "user_role":self.user_1.user_role_id}
- data = json.dumps(data_dict)
- response = self.client.put('/api/users/%d/' % self.user_1.id,
- data=data,
- content_type='application/json',
- **self.header_1)
- self.assertEqual(response.status_code, 200)
- json_response = response_to_json(response)
- self.assertEqual(data_dict['first_name'], json_response['first_name'])
- def test_put_fail_user_detail(self):
- response = self.client.get('/api/users/%d/' % self.user_1.id, {},
- **self.header_1)
- data_dict = {"first_name": "snow", "email": self.user_1.email}
- data = json.dumps(data_dict)
- response = self.client.put('/api/users/%d/' % self.user_1.id,
- data=data,
- content_type='application/json',
- **self.header_2)
- self.assertEqual(response.status_code, 400)
- self.assertIn(messages.TOKEN_UNAUTHORIZED["message"], response.content)
- class UserLoginTest(DummyUsers):
- def test_user_login_with_email_returns_token(self):
- data = {
- "email": "admin@localhost",
- "password": "admin"
- }
- response = self.client.post('/api/users/login', data=data)
- # json_response = response_to_json(response)
- self.assertEqual(response.status_code, 200)
- # self.assertNotEqual(json_response['token'], None)
- def test_user_login_with_email_fails(self):
- data = {
- "email": "notExist@localhost",
- "password": "admin"
- }
- response = self.client.post('/api/users/login', data=data)
- self.assertIn(messages.USER_WITH_EMAIL_DOES_NOT_EXISTS["message"], response.content)
- self.assertEqual(response.status_code, 404)
- def test_user_login_with_password_fails(self):
- data = {"email": "admin@localhost", "password": "wrong_password"}
- response = self.client.post('/api/users/login', data=data)
- self.assertIn(messages.INVALID_EMAIL_OR_PASSWORD["message"], response.content)
- self.assertEqual(response.status_code, 401)
- def test_user_login_without_password(self):
- data = {"email": "user1@localhost"}
- response = self.client.post('/api/users/login', data=data)
- self.assertIn(messages.REQUIRED_EMAIL_AND_PASSWORD["message"], response.content)
- self.assertEqual(response.status_code, 400)
- class ChangePasswordTest(DummyUsers):
- def setUp(self):
- super(ChangePasswordTest, self).setUp()
- def test_change_password(self):
- data_dict = {"current_password": "admin",
- "new_password": "admin_2"}
- data = json.dumps(data_dict)
- response = self.client.put('/api/users/%d/change_password/' % self.user_1.id,
- data=data,
- content_type='application/json',
- **self.header_1)
- self.assertEqual(200, response.status_code)
- self.assertIn("Password changed successfully", response.content)
- def test_with_incorrect_current_password(self):
- data_dict = {"current_password": "admin_2",
- "new_password": "admin"}
- data = json.dumps(data_dict)
- response = self.client.put('/api/users/%d/change_password/' % self.user_1.id,
- data=data,
- content_type='application/json',
- **self.header_1)
- self.assertEqual(401, response.status_code)
- self.assertIn("Current password is incorrect.", response.content)
- def test_both_password_same(self):
- data_dict = {"current_password": "admin",
- "new_password": "admin"}
- data = json.dumps(data_dict)
- response = self.client.put('/api/users/%d/change_password/' % self.user_1.id,
- data=data,
- content_type='application/json',
- **self.header_1)
- self.assertEqual(400, response.status_code)
- self.assertIn("New password cannot be same as current password",
- response.content)
- class PasswordResetTest(TestCase):
- def setUp(self):
- self.user_role_1 = Role.objects.create(role_name="Admin")
- self.user_1 = User.objects.create_user(email="user1@localhost", password="admin", user_role=self.user_role_1,
- contact_no=545641852)
- self.email_2 = "does_not_exist@localhost"
- def test_successful_password_reset(self):
- response = self.client.post(
- '/api/password_reset/',
- data={"email": self.user_1.email}
- )
- self.assertIn("Password Reset Link sent.", response.content)
- def test_fail_password_reset(self):
- response = self.client.post(
- '/api/password_reset/',
- data={"email": self.email_2}
- )
- self.assertIn("User with specified email does not exist.",
- response.content)
- self.assertEqual(response.status_code, 404)
- class DummyRestaurants(DummyUsers):
- def setUp(self):
- super(DummyRestaurants, self).setUp()
- self.restaurant_1 = Restaurant.objects.create(
- name="Havmore",
- address="wefve,gjcgf,,,gfyuey",
- longitude=12.9,
- latitude=20,
- )
- self.restaurant_2 = Restaurant.objects.create(
- name="Honest",
- address="wefve",
- longitude=12.9,
- latitude=20)
- self.restaurant_3 = Restaurant.objects.create(
- name="pizza-hut",
- address="wefve",
- longitude=12.9,
- latitude=20)
- class RestaurantsRegistrationTest(DummyRestaurants):
- def setUp(self):
- super(RestaurantsRegistrationTest, self).setUp()
- def test_successful_restaurant_create(self):
- data_dict = {
- "name": "havmor",
- "area":"vghvjv",
- "address": "wefve",
- "longitude": 12990,
- "latitude": 288,
- "restaurant_contact_no":56789354,
- "email": "dummyuser89@yop.com",
- "password": "123",
- "contact_no": "0123456787",
- "created": "2016-01-28 17:45:43",
- "first_name":"fgfnb",
- "last_name":"fgeh",
- "is_deleted":"True",
- "modified":"null",
- # "user_role": self.user_role_2.id
- }
- data = json.dumps(data_dict)
- response = self.client.post('/api/restaurants', data=data, content_type='application/json')
- self.assertEqual(response.status_code, 201)
- """
- def test_failed_restaurant_create(self):
- data_dict = {
- "restaurant_name": "abcsfhdfvh",
- "restaurant_area": "prahladnagar",
- "address": "wefve",
- "owner_name": "abc",
- "longitude": 12.9,
- "latitude": 20
- }
- data = json.dumps(data_dict)
- response = self.client.post('/api/restaurants', data=data, content_type='application/json', **self.header_2)
- self.assertIn(messages.TOKEN_UNAUTHORIZED["message"], response.content)
- self.assertEquals(response.status_code, 403)
- def test_failed_restaurant_without_restaurant_name_not_allowed(self):
- data_dict = {
- "restaurant_area": "prahladnagar",
- "address": "wefve",
- "owner_name": "abc",
- "longitude": 12.9,
- "latitude": 20
- }
- data = json.dumps(data_dict)
- response = self.client.post('/api/restaurants', data=data, content_type='application/json', **self.header_2)
- # self.assertIn(messages.TOKEN_UNAUTHORIZED["message"], response.content)
- self.assertEquals(response.status_code, 403)
- def test_failed_restaurant_without_restaurant_area_not_allowed(self):
- data_dict = {
- "restaurant_name": "abcsfcdhdfvh",
- "address": "wefve",
- "owner_name": "abc",
- "longitude": 12.9,
- "latitude": 20
- }
- data = json.dumps(data_dict)
- response = self.client.post('/api/restaurants', data=data, content_type='application/json', **self.header_1)
- self.assertEquals(response.status_code, 400)
- def test_failed_restaurant_without_owner_name_not_allowed(self):
- data_dict = {
- "restaurant_name": "abcsfcdhdfvh",
- "restaurant_area": "prahladnagar",
- "address": "wefve",
- "longitude": 12.9,
- "latitude": 20
- }
- data = json.dumps(data_dict)
- response = self.client.post('/api/restaurants', data=data, content_type='application/json', **self.header_1)
- self.assertEquals(response.status_code, 400)
- def test_failed_restaurant_without_status_not_allowed(self):
- data_dict = {
- "restaurant_name": "abcsfcdhdfvh",
- "restaurant_area": "prahladnagar",
- "address": "wefve",
- "owner_name": "abc",
- "longitude": 12.9,
- "latitude": 20
- }
- data = json.dumps(data_dict)
- response = self.client.post('/api/restaurants', data=data, content_type='application/json', **self.header_1)
- self.assertEquals(response.status_code, 400)
- """
- """
- class RestaurantsDetailTest(DummyRestaurants, DummyUsers):
- def setUp(self):
- super(RestaurantsDetailTest, self).setUp()
- def test_get_successful_restaurant_detail(self):
- response = self.client.get('/api/restaurants/%d/' % self.restaurant_1.id)
- self.assertEquals(response.status_code, 200)
- def test_put_successful_restaurant_detail(self):
- data_dict = {
- "status": "pending",
- "restaurant_name": "abcsfhdfvh",
- "restaurant_area": "prahladnagar",
- "address": "wefvrgcvcvghvgvgftft6587678.,,,,fgvfgde",
- "owner_name": "abc",
- "longitude": 12.9,
- "latitude": 20
- }
- data = json.dumps(data_dict)
- response = self.client.put('/api/restaurants/update/%d/' % self.restaurant_1.id,
- data=data,
- content_type='application/json',
- **self.header_1)
- self.assertEquals(response.status_code, 200)
- class ManagerRegistration(DummyRestaurants):
- def setUp(self):
- super(ManagerRegistration, self).setUp()
- def test_successful_manager_create(self):
- data_dict = {
- "email": "manager11@create.com",
- "password": "admin",
- "contact_no": "0123456789",
- "created": "2016-01-28 17:45:43",
- "first_name": "fgf",
- "last_name": "fgeh"
- }
- data = json.dumps(data_dict)
- response = self.client.post('api/restaurants/%d/manager' % self.restaurant_1.id,
- data=data,
- content_type='application/json',
- **self.header_1)
- self.assertEquals(response.status_code, 201)
- class QueueRegistrationTest(DummyRestaurants):
- def setUp(self):
- super(QueueRegistrationTest, self).setUp()
- def test_successful_Queue_create(self):
- data_dict = {
- "no_of_seated": 3,
- "restaurant": self.restaurant_1.id,
- "user": self.user_1.id,
- "queue_type": "dinner"
- }
- data = json.dumps(data_dict)
- response = self.client.post('/api/restaurants/%d/queue' % self.restaurant_1.id, data=data,
- content_type='application/json', **self.header_1)
- self.assertEqual(response.status_code, 201)
- def test_failed_Queue_create(self):
- data_dict = {
- "no_of_seated": 3,
- "restaurant": self.restaurant_1.id,
- "user": self.user_1.id
- }
- data = json.dumps(data_dict)
- response = self.client.post('/api/restaurants/%d/queue' % self.restaurant_1.id, data=data,
- content_type='application/json', **self.header_3)
- self.assertEqual(response.status_code, 403)
- class DummyQueue(DummyRestaurants):
- def setUp(self):
- super(DummyQueue, self).setUp()
- self.queue_1 = Queue.objects.create(no_of_seated=3, restaurant=self.restaurant_1, user=self.user_1,
- queue_type='dinner')
- self.queue_2 = Queue.objects.create(no_of_seated=3, restaurant=self.restaurant_2, user=self.user_2,
- queue_type='lunch')
- class QueueDetailTest(DummyQueue):
- def setUp(self):
- super(QueueDetailTest, self).setUp()
- def test_get_successful_restaurant_detail(self):
- response = self.client.get('/api/restaurants/%d/queue/%d/' % (self.restaurant_1.id, self.queue_1.id), {},
- **self.header_1)
- self.assertEquals(response.status_code, 200)
- def test_put_successful_restaurant_detail(self):
- data_dict = {
- "no_of_seated": 3,
- "restaurant": self.restaurant_1.id,
- "user": self.user_1.id,
- "queue_type": "dinner"
- }
- data = json.dumps(data_dict)
- response = self.client.put('/api/restaurants/%d/queue/%d/' % (self.restaurant_1.id, self.queue_1.id),
- data=data,
- content_type='application/json',
- **self.header_1)
- self.assertEquals(response.status_code, 200)
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement