Guest User

Untitled

a guest
Dec 25th, 2017
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. import unittest
  2. import os
  3. import json
  4. import app
  5. from app import create_app, db
  6.  
  7.  
  8. class CategoryTestCase(unittest.TestCase):
  9. """This class represents the Category test case"""
  10.  
  11. def setUp(self):
  12. """setup test variables"""
  13. self.app = create_app(config_name="testing")
  14. self.client = self.app.test_client
  15. self.category_data = {'name' : 'Yummy'}
  16.  
  17. # binds the app with the current context
  18. with self.app.app_context():
  19. #create all tables
  20. db.session.close()
  21. db.drop_all()
  22. db.create_all()
  23.  
  24. def register_user(self, first_name='Tester', last_name='Api', username='apitester', email='tester@api.com', password='abc'):
  25. """This helper method helps register a test user"""
  26. user_data = {
  27. 'first_name' : first_name,
  28. 'last_name' : last_name,
  29. 'username' : username,
  30. 'email' : email,
  31. 'password' : password
  32. }
  33.  
  34. return self.client().post('/api/v1.0/register', data=json.dumps(user_data), content_type='application/json')
  35.  
  36. def login_user(self, email='tester@api.com', password='abc'):
  37. """this helper method helps log in a test user"""
  38. user_data = {
  39. 'email' : email,
  40. 'password' : password
  41. }
  42.  
  43. return self.client().post('/api/v1.0/login', data=json.dumps(user_data), content_type='application/json')
  44.  
  45. def test_category_creation(self):
  46. """Test that the Api can create a category"""
  47. self.register_user()
  48. login_result = self.login_user()
  49.  
  50. token = json.loads(login_result.data)
  51. token = token['access_token']
  52.  
  53. # Create a category by going to that link
  54. response = self.client().post('/api/v1.0/category', headers=dict(Authorization="Bearer " + token), data=json.dumps(self.category_data), content_type='application/json')
  55. self.assertEquals(response.status_code, 201)
Add Comment
Please, Sign In to add comment