Advertisement
Guest User

Untitled

a guest
Mar 1st, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.21 KB | None | 0 0
  1. # from django.core.serializers import json
  2. import json
  3.  
  4. from django.test import TestCase
  5.  
  6. from api import messages
  7. from api.models import *
  8.  
  9.  
  10. # Create your tests here.
  11.  
  12.  
  13. def response_to_json(response):
  14. return json.loads(response.content)
  15.  
  16.  
  17. class DummyRoles(TestCase):
  18. # creating DummyRoles , So User can be created using user_role_id
  19. def setUp(self):
  20. self.user_role_1 = Role.objects.create(role_name="Admin")
  21. self.user_role_2 = Role.objects.create(role_name="Restaurant_Admin")
  22. self.user_role_3 = Role.objects.create(role_name="Manager")
  23. self.user_role_4 = Role.objects.create(role_name="Customer")
  24.  
  25.  
  26. class DummyUsers(DummyRoles):
  27. def setUp(self):
  28. super(DummyUsers, self).setUp()
  29.  
  30. self.user_1 = User.objects.create_user(email="admin@localhost", password="admin", user_role=self.user_role_1,
  31. contact_no=8460373610)
  32. self.user_2 = User.objects.create_user(email='restaurantAdmin@localhost', password='admin', user_role=self.
  33. user_role_2,
  34. contact_no=8888888888)
  35. self.user_3 = User.objects.create_user(email="Manager@localhost", password="admin", user_role=self.user_role_3,
  36. contact_no=8986373610)
  37. self.user_4 = User.objects.create_user(email="customer3@localhost", password="admin", user_role=self.user_role_4,
  38. contact_no=8986373610)
  39.  
  40. self.token_1 = Token.objects.get(user=self.user_1).key
  41. self.token_2 = Token.objects.get(user=self.user_2).key
  42. self.token_3 = Token.objects.get(user=self.user_3).key
  43. self.token_4 = Token.objects.get(user=self.user_4).key
  44.  
  45. self.header_1 = {"HTTP_AUTHORIZATION": 'Token {}'.format(self.token_1)}
  46. self.header_2 = {"HTTP_AUTHORIZATION": 'Token {}'.format(self.token_2)}
  47. self.header_3 = {"HTTP_AUTHORIZATION": 'Token {}'.format(self.token_3)}
  48. self.header_4 = {"HTTP_AUTHORIZATION": 'Token {}'.format(self.token_4)}
  49.  
  50.  
  51. class UserRegistrationTest(DummyRoles):
  52. def setUp(self):
  53. super(UserRegistrationTest, self).setUp()
  54.  
  55. def test_user_creation_with_email(self):
  56. data = {
  57. "email": "hello@localhost",
  58. "password": "a123",
  59. "user_role": self.user_role_2.id,
  60. "contact_no": "0123456789"
  61. }
  62.  
  63. response = self.client.post('/api/users/', data=data)
  64. self.assertEqual(response.status_code, 201)
  65. self.assertEqual(User.objects.all().count(), 1)
  66.  
  67. def test_user_registration_with_email_fail(self):
  68. data = {
  69. "email": "invalidemail.com",
  70. "password": "admin",
  71. "user_role": self.user_role_2.id,
  72. "contact_no": "0123456789"
  73. }
  74. response = self.client.post('/api/users/', data=data)
  75. self.assertEqual(response.status_code, 400)
  76. self.assertIn(messages.INVALID_EMAIL_ADDRESS['message'], response.content)
  77.  
  78. def test_duplicate_email_not_allowed(self):
  79. data = {
  80. "email": "hello@localhost",
  81. "password": "a123",
  82. "user_role": self.user_role_2.id,
  83. "contact_no": "0123456789"
  84. }
  85. self.client.post('/api/users/', data=data)
  86. response = self.client.post('/api/users/', data=data)
  87. self.assertEqual(response.status_code, 400)
  88. self.assertIn(messages.USER_ALREADY_EXISTS['message'], response.content)
  89.  
  90. def test_without_email_not_allowed(self):
  91. data = {
  92. "password": "admin",
  93. "user_role": self.user_role_2.id,
  94. "contact_no": "0123456789"
  95. }
  96. self.client.post('/api/users', data=data)
  97. response = self.client.post('/api/users/', data=data)
  98. self.assertEqual(response.status_code, 400)
  99. self.assertIn(messages.REQUIRED_EMAIL['message'], response.content)
  100.  
  101. def test_without_password_not_allowed(self):
  102. data = {
  103. "email": "new@new.com",
  104. "user_role": self.user_role_1.id,
  105. "contact_no": "0123456789"
  106. }
  107. self.client.post('/api/users', data=data)
  108. response = self.client.post('/api/users/', data=data)
  109. self.assertEqual(response.status_code, 400)
  110.  
  111. def test_without_role_not_allowed(self):
  112. data = {
  113. "email": "new@new.com",
  114. "password": "123",
  115. "contact_no": "0123456789"
  116. }
  117. self.client.post('/api/users', data=data)
  118. response = self.client.post('/api/users/', data=data)
  119. self.assertEqual(response.status_code, 400)
  120.  
  121. def test_without_contact_no_not_allowed(self):
  122. data = {
  123. "email": "new@new.com",
  124. "password": "123",
  125. "user_role": self.user_role_1.id,
  126. }
  127. self.client.post('/api/users', data=data)
  128. response = self.client.post('/api/users/', data=data)
  129. self.assertEqual(response.status_code, 400)
  130.  
  131.  
  132. class UserDetailTest(DummyUsers):
  133. def setUp(self):
  134. super(UserDetailTest, self).setUp()
  135.  
  136. def test_get_successful_user_detail(self):
  137. response = self.client.get('/api/users/%d/' % self.user_1.id,
  138. {}, **self.header_1)
  139. json_response = response_to_json(response)
  140.  
  141. self.assertEqual(json_response['email'], self.user_1.email)
  142.  
  143. def test_get_failing_user_detail(self):
  144. response = self.client.get('/api/users/%d/' % self.user_1.id,
  145. {}, **self.header_2)
  146.  
  147. self.assertIn(messages.TOKEN_UNAUTHORIZED["message"], response.content)
  148.  
  149. def test_put_successful_user_detail(self):
  150. data_dict = {"first_name": "snow", "email": self.user_1.email, "user_role":self.user_1.user_role_id}
  151. data = json.dumps(data_dict)
  152. response = self.client.put('/api/users/%d/' % self.user_1.id,
  153. data=data,
  154. content_type='application/json',
  155. **self.header_1)
  156. self.assertEqual(response.status_code, 200)
  157. json_response = response_to_json(response)
  158.  
  159. self.assertEqual(data_dict['first_name'], json_response['first_name'])
  160.  
  161. def test_put_fail_user_detail(self):
  162. response = self.client.get('/api/users/%d/' % self.user_1.id, {},
  163. **self.header_1)
  164. data_dict = {"first_name": "snow", "email": self.user_1.email}
  165. data = json.dumps(data_dict)
  166.  
  167. response = self.client.put('/api/users/%d/' % self.user_1.id,
  168. data=data,
  169. content_type='application/json',
  170. **self.header_2)
  171.  
  172. self.assertEqual(response.status_code, 400)
  173. self.assertIn(messages.TOKEN_UNAUTHORIZED["message"], response.content)
  174.  
  175.  
  176. class UserLoginTest(DummyUsers):
  177. def test_user_login_with_email_returns_token(self):
  178. data = {
  179. "email": "admin@localhost",
  180. "password": "admin"
  181. }
  182.  
  183. response = self.client.post('/api/users/login', data=data)
  184. # json_response = response_to_json(response)
  185. self.assertEqual(response.status_code, 200)
  186. # self.assertNotEqual(json_response['token'], None)
  187.  
  188. def test_user_login_with_email_fails(self):
  189. data = {
  190. "email": "notExist@localhost",
  191. "password": "admin"
  192. }
  193.  
  194. response = self.client.post('/api/users/login', data=data)
  195. self.assertIn(messages.USER_WITH_EMAIL_DOES_NOT_EXISTS["message"], response.content)
  196. self.assertEqual(response.status_code, 404)
  197.  
  198. def test_user_login_with_password_fails(self):
  199. data = {"email": "admin@localhost", "password": "wrong_password"}
  200. response = self.client.post('/api/users/login', data=data)
  201. self.assertIn(messages.INVALID_EMAIL_OR_PASSWORD["message"], response.content)
  202. self.assertEqual(response.status_code, 401)
  203.  
  204. def test_user_login_without_password(self):
  205. data = {"email": "user1@localhost"}
  206. response = self.client.post('/api/users/login', data=data)
  207. self.assertIn(messages.REQUIRED_EMAIL_AND_PASSWORD["message"], response.content)
  208. self.assertEqual(response.status_code, 400)
  209.  
  210.  
  211. class ChangePasswordTest(DummyUsers):
  212. def setUp(self):
  213. super(ChangePasswordTest, self).setUp()
  214.  
  215. def test_change_password(self):
  216. data_dict = {"current_password": "admin",
  217. "new_password": "admin_2"}
  218. data = json.dumps(data_dict)
  219.  
  220. response = self.client.put('/api/users/%d/change_password/' % self.user_1.id,
  221. data=data,
  222. content_type='application/json',
  223. **self.header_1)
  224.  
  225. self.assertEqual(200, response.status_code)
  226. self.assertIn("Password changed successfully", response.content)
  227.  
  228. def test_with_incorrect_current_password(self):
  229. data_dict = {"current_password": "admin_2",
  230. "new_password": "admin"}
  231. data = json.dumps(data_dict)
  232.  
  233. response = self.client.put('/api/users/%d/change_password/' % self.user_1.id,
  234. data=data,
  235. content_type='application/json',
  236. **self.header_1)
  237. self.assertEqual(401, response.status_code)
  238. self.assertIn("Current password is incorrect.", response.content)
  239.  
  240. def test_both_password_same(self):
  241. data_dict = {"current_password": "admin",
  242. "new_password": "admin"}
  243. data = json.dumps(data_dict)
  244.  
  245. response = self.client.put('/api/users/%d/change_password/' % self.user_1.id,
  246. data=data,
  247. content_type='application/json',
  248. **self.header_1)
  249. self.assertEqual(400, response.status_code)
  250. self.assertIn("New password cannot be same as current password",
  251. response.content)
  252.  
  253.  
  254. class PasswordResetTest(TestCase):
  255. def setUp(self):
  256. self.user_role_1 = Role.objects.create(role_name="Admin")
  257. self.user_1 = User.objects.create_user(email="user1@localhost", password="admin", user_role=self.user_role_1,
  258. contact_no=545641852)
  259. self.email_2 = "does_not_exist@localhost"
  260.  
  261. def test_successful_password_reset(self):
  262. response = self.client.post(
  263. '/api/password_reset/',
  264. data={"email": self.user_1.email}
  265. )
  266.  
  267. self.assertIn("Password Reset Link sent.", response.content)
  268.  
  269. def test_fail_password_reset(self):
  270. response = self.client.post(
  271. '/api/password_reset/',
  272. data={"email": self.email_2}
  273. )
  274.  
  275. self.assertIn("User with specified email does not exist.",
  276. response.content)
  277. self.assertEqual(response.status_code, 404)
  278.  
  279.  
  280. class DummyRestaurants(DummyUsers):
  281. def setUp(self):
  282. super(DummyRestaurants, self).setUp()
  283. self.restaurant_1 = Restaurant.objects.create(
  284. name="Havmore",
  285. address="wefve,gjcgf,,,gfyuey",
  286. longitude=12.9,
  287. latitude=20,
  288. )
  289. self.restaurant_2 = Restaurant.objects.create(
  290. name="Honest",
  291. address="wefve",
  292. longitude=12.9,
  293. latitude=20)
  294. self.restaurant_3 = Restaurant.objects.create(
  295.  
  296. name="pizza-hut",
  297. address="wefve",
  298. longitude=12.9,
  299. latitude=20)
  300.  
  301.  
  302. class RestaurantsRegistrationTest(DummyRestaurants):
  303. def setUp(self):
  304. super(RestaurantsRegistrationTest, self).setUp()
  305.  
  306. def test_successful_restaurant_create(self):
  307. data_dict = {
  308. "name": "havmor",
  309. "area":"vghvjv",
  310. "address": "wefve",
  311. "longitude": 12990,
  312. "latitude": 288,
  313. "restaurant_contact_no":56789354,
  314. "email": "dummyuser89@yop.com",
  315. "password": "123",
  316. "contact_no": "0123456787",
  317. "created": "2016-01-28 17:45:43",
  318. "first_name":"fgfnb",
  319. "last_name":"fgeh",
  320. "is_deleted":"True",
  321. "modified":"null",
  322. # "user_role": self.user_role_2.id
  323. }
  324. data = json.dumps(data_dict)
  325. response = self.client.post('/api/restaurants', data=data, content_type='application/json')
  326. self.assertEqual(response.status_code, 201)
  327.  
  328.  
  329.  
  330. """
  331. def test_failed_restaurant_create(self):
  332. data_dict = {
  333. "restaurant_name": "abcsfhdfvh",
  334. "restaurant_area": "prahladnagar",
  335. "address": "wefve",
  336. "owner_name": "abc",
  337. "longitude": 12.9,
  338. "latitude": 20
  339. }
  340. data = json.dumps(data_dict)
  341. response = self.client.post('/api/restaurants', data=data, content_type='application/json', **self.header_2)
  342. self.assertIn(messages.TOKEN_UNAUTHORIZED["message"], response.content)
  343. self.assertEquals(response.status_code, 403)
  344.  
  345. def test_failed_restaurant_without_restaurant_name_not_allowed(self):
  346. data_dict = {
  347. "restaurant_area": "prahladnagar",
  348. "address": "wefve",
  349. "owner_name": "abc",
  350. "longitude": 12.9,
  351. "latitude": 20
  352. }
  353. data = json.dumps(data_dict)
  354. response = self.client.post('/api/restaurants', data=data, content_type='application/json', **self.header_2)
  355. # self.assertIn(messages.TOKEN_UNAUTHORIZED["message"], response.content)
  356. self.assertEquals(response.status_code, 403)
  357.  
  358. def test_failed_restaurant_without_restaurant_area_not_allowed(self):
  359. data_dict = {
  360. "restaurant_name": "abcsfcdhdfvh",
  361. "address": "wefve",
  362. "owner_name": "abc",
  363. "longitude": 12.9,
  364. "latitude": 20
  365. }
  366. data = json.dumps(data_dict)
  367. response = self.client.post('/api/restaurants', data=data, content_type='application/json', **self.header_1)
  368. self.assertEquals(response.status_code, 400)
  369.  
  370. def test_failed_restaurant_without_owner_name_not_allowed(self):
  371. data_dict = {
  372. "restaurant_name": "abcsfcdhdfvh",
  373. "restaurant_area": "prahladnagar",
  374. "address": "wefve",
  375. "longitude": 12.9,
  376. "latitude": 20
  377. }
  378. data = json.dumps(data_dict)
  379. response = self.client.post('/api/restaurants', data=data, content_type='application/json', **self.header_1)
  380. self.assertEquals(response.status_code, 400)
  381.  
  382. def test_failed_restaurant_without_status_not_allowed(self):
  383. data_dict = {
  384. "restaurant_name": "abcsfcdhdfvh",
  385. "restaurant_area": "prahladnagar",
  386. "address": "wefve",
  387. "owner_name": "abc",
  388. "longitude": 12.9,
  389. "latitude": 20
  390. }
  391. data = json.dumps(data_dict)
  392. response = self.client.post('/api/restaurants', data=data, content_type='application/json', **self.header_1)
  393. self.assertEquals(response.status_code, 400)
  394. """
  395. """
  396. class RestaurantsDetailTest(DummyRestaurants, DummyUsers):
  397. def setUp(self):
  398. super(RestaurantsDetailTest, self).setUp()
  399.  
  400. def test_get_successful_restaurant_detail(self):
  401. response = self.client.get('/api/restaurants/%d/' % self.restaurant_1.id)
  402. self.assertEquals(response.status_code, 200)
  403.  
  404. def test_put_successful_restaurant_detail(self):
  405. data_dict = {
  406. "status": "pending",
  407. "restaurant_name": "abcsfhdfvh",
  408. "restaurant_area": "prahladnagar",
  409. "address": "wefvrgcvcvghvgvgftft6587678.,,,,fgvfgde",
  410. "owner_name": "abc",
  411. "longitude": 12.9,
  412. "latitude": 20
  413. }
  414. data = json.dumps(data_dict)
  415. response = self.client.put('/api/restaurants/update/%d/' % self.restaurant_1.id,
  416. data=data,
  417. content_type='application/json',
  418. **self.header_1)
  419. self.assertEquals(response.status_code, 200)
  420.  
  421.  
  422. class ManagerRegistration(DummyRestaurants):
  423. def setUp(self):
  424. super(ManagerRegistration, self).setUp()
  425.  
  426. def test_successful_manager_create(self):
  427. data_dict = {
  428. "email": "manager11@create.com",
  429. "password": "admin",
  430. "contact_no": "0123456789",
  431. "created": "2016-01-28 17:45:43",
  432. "first_name": "fgf",
  433. "last_name": "fgeh"
  434. }
  435. data = json.dumps(data_dict)
  436. response = self.client.post('api/restaurants/%d/manager' % self.restaurant_1.id,
  437. data=data,
  438. content_type='application/json',
  439. **self.header_1)
  440. self.assertEquals(response.status_code, 201)
  441.  
  442.  
  443. class QueueRegistrationTest(DummyRestaurants):
  444. def setUp(self):
  445. super(QueueRegistrationTest, self).setUp()
  446.  
  447. def test_successful_Queue_create(self):
  448. data_dict = {
  449. "no_of_seated": 3,
  450. "restaurant": self.restaurant_1.id,
  451. "user": self.user_1.id,
  452. "queue_type": "dinner"
  453. }
  454. data = json.dumps(data_dict)
  455. response = self.client.post('/api/restaurants/%d/queue' % self.restaurant_1.id, data=data,
  456. content_type='application/json', **self.header_1)
  457. self.assertEqual(response.status_code, 201)
  458.  
  459. def test_failed_Queue_create(self):
  460. data_dict = {
  461. "no_of_seated": 3,
  462. "restaurant": self.restaurant_1.id,
  463. "user": self.user_1.id
  464. }
  465. data = json.dumps(data_dict)
  466. response = self.client.post('/api/restaurants/%d/queue' % self.restaurant_1.id, data=data,
  467. content_type='application/json', **self.header_3)
  468. self.assertEqual(response.status_code, 403)
  469.  
  470.  
  471. class DummyQueue(DummyRestaurants):
  472. def setUp(self):
  473. super(DummyQueue, self).setUp()
  474. self.queue_1 = Queue.objects.create(no_of_seated=3, restaurant=self.restaurant_1, user=self.user_1,
  475. queue_type='dinner')
  476. self.queue_2 = Queue.objects.create(no_of_seated=3, restaurant=self.restaurant_2, user=self.user_2,
  477. queue_type='lunch')
  478.  
  479.  
  480. class QueueDetailTest(DummyQueue):
  481. def setUp(self):
  482. super(QueueDetailTest, self).setUp()
  483.  
  484. def test_get_successful_restaurant_detail(self):
  485. response = self.client.get('/api/restaurants/%d/queue/%d/' % (self.restaurant_1.id, self.queue_1.id), {},
  486. **self.header_1)
  487. self.assertEquals(response.status_code, 200)
  488.  
  489. def test_put_successful_restaurant_detail(self):
  490. data_dict = {
  491. "no_of_seated": 3,
  492. "restaurant": self.restaurant_1.id,
  493. "user": self.user_1.id,
  494. "queue_type": "dinner"
  495. }
  496. data = json.dumps(data_dict)
  497. response = self.client.put('/api/restaurants/%d/queue/%d/' % (self.restaurant_1.id, self.queue_1.id),
  498. data=data,
  499. content_type='application/json',
  500. **self.header_1)
  501. self.assertEquals(response.status_code, 200)
  502. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement