def test_API_get_lamp(self): # API headers = { 'Authorization': 'Basic ' + b64encode("{0}:{1}".format(self.API_Username, self.API_Password)) } tester = app.test_client(self) response = tester.get('/ha/api/v1.0/lamps/2', follow_redirects=True, headers=headers, environ_base={'HTTP_USER_AGENT': self.API_UserAgent}) self.assertEqual(response.status_code, 200)
def setUp(self): # API Login data = json.dumps({ 'username': self.API_Username, 'password': hashlib.sha512('admin123').hexdigest() }) tester = app.test_client(self) response = tester.post('/ha/api/v1.0/login', data=data, follow_redirects=True, content_type='application/json', environ_base={'HTTP_USER_AGENT': self.API_UserAgent}) self.assertEqual(response.status_code, 200) self.API_Password = hashlib.sha512("%s%s" % (json.loads(response.data)["login"]["password"], self.API_UserAgent)).hexdigest()
def test_API_power_scene(self): # API headers = { 'Authorization': 'Basic ' + b64encode("{0}:{1}".format(self.API_Username, self.API_Password)) } data = json.dumps({ 'id': "1" }) tester = app.test_client(self) response = tester.post('/ha/api/v1.0/lamps/scene', data=data, follow_redirects=True, headers=headers, content_type='application/json', environ_base={'HTTP_USER_AGENT': self.API_UserAgent}) self.assertEqual(response.status_code, 200)
def testIfClientDontPassPayload(self): with app.test_client() as client: response = client.post('/valueWithTaxes') self.assertEqual(400, response.status_code)
def testIfClientPassParameterWithWrongType(self): payload = {"amount": 10000, "tax": 2} with app.test_client() as client: response = client.post('/valueWithTaxes', json=payload) self.assertEqual(400, response.status_code)
def testIfAPIReturnsLikeProposedInChallenge(self): payload = {"amount": 10000, "tax": 2.2} with app.test_client() as client: response = client.post('/valueWithTaxes', json=payload) self.assertEqual(200, response.status_code) self.assertEqual(10220.00, response.json['valueWithTaxes'])
def setUp(self): """ SetUp method .This method is run before each test.It is where all variables for tests are declared and values set. It is important for setting the state ypour testing for in the application. """ #Declaring testing client self.client = app.test_client() db.init_app(app) db.drop_all() db.create_all() #Declaring User details to use for testing password = "******" self.user = { "username": "******", "email": "*****@*****.**", "password": password } #hashing the users password for better security password_hash = generate_password_hash(password, method='sha256') #Saving the users deatils in the database user = User("david", "*****@*****.**", password=password_hash, user_date_stamp=str(datetime.datetime.now())) db.session.add(user) db.session.commit() #declaring a sample of invalid data self.invalid_data = {} #User details for login self.user_details = {"username": "******", "password": "******"} #Declaring a sample category for testing self.category = { "category_title": "breakfast", "category_description": "First meal of the morning" } #saving a sample category in the database category = Category("1", "breakfast", "First meal of the morning", "*****@*****.**", "2017-12-02 13:39:25.892164") db.session.add(category) db.session.commit() #Declaring a second sample category for testing self.category2 = { "category_title": "Drinks", "category_description": "Taken 30 minutes before and after food." } #saving a second sample category in the database category2 = Category("30", "Drinks", "Taken 30 minutes before and after food.", "*****@*****.**", "2017-12-02 13:39:25.892164") db.session.add(category) db.session.commit() #Declaring a sample recipe for testing self.recipe = { "recipe_title": "rolex", "recipe_description": "1.Obtain eggs" } #saving a second sample recipe in the database recipe = Recipe("1", "rolex", "1.Obtain eggs", "1", "*****@*****.**", "2017-12-02 13:39:25.892164", "False") db.session.add(recipe) db.session.commit() #User details for login and generating a token self.user_logins = {"username": "******", "password": "******"} response = self.client.post( "/login", data=json.dumps(self.user_logins), headers={"Content-Type": "application/json"}) token = json.loads(response.data.decode())["token"] self.headers = {"x-access-token": token}
def setUp(self): app.config['Testing'] = True self.app = app.test_client()