def test_updating_business_with_same_name(self): ''' Test business updating with existing busiiness name under one user ''' # New business details to test updating new_business_data = { 'name': 'TRM', 'description': 'Enjoy Coffee and Pizzas', 'country': 'Kenya', 'city': 'Nakuru' } # Data to be saved to test same name businesses under one person additional_business_data = { 'id': uuid.uuid4().hex, 'user_id': self.sample_user['id'], 'name': 'TRM', 'description': 'Enjoy Coffee and Pizzas', 'country': 'Kenya', 'city': 'Nakuru' } # Add user(owner) to the business data dict self.business_data['user_id'] = self.sample_user['id'] # Save business in the storage list for testing Business.save(self.business_data) Business.save(additional_business_data) response = self.app.put(self.url_prefix + 'businesses/' + self.business_data['id'], data=json.dumps(new_business_data), headers={'Authorization': self.test_token}, content_type='application/json') self.assertEqual(response.status_code, 400) self.assertIn( b'You have already registered this other business with same name', response.data)
def register_business(): """ Register business """ sent_data = request.get_json(force=True) valid = validate(sent_data, REGISTER_BUSINESS_RULES) if valid != True: response = jsonify( status='error', message="Please provide required info", errors=valid) response.status_code = 400 return response user_id = token_id(request.headers.get('Authorization')) data = { 'id': uuid.uuid4().hex, 'user_id': user_id, 'name': sent_data['name'], 'description': sent_data['description'], 'country': sent_data['country'], 'city': sent_data['city'], } if Business.has_same_business(user_id, sent_data['name']): response = jsonify( status='error', message="You have already registered this business") response.status_code = 400 return response Business.save(data) response = jsonify({ 'status': 'ok', 'message': "Your business has been successfully registered" }) response.status_code = 201 return response
def test_no_priv_business_deletion(self): ''' Test removing business without privileges to it ''' # Add user(owner) to the business data dict self.business_data['user_id'] = self.sample_user['id'] # Save business in the storage list for testing Business.save(self.business_data) response = self.app.delete( self.url_prefix + 'businesses/' + self.business_data['id'], data={}, headers={'Authorization': self.orphan_test_token}) self.assertEqual(response.status_code, 400)
def test_business_deletion(self): ''' Test removing business ''' # Add user(owner) to the business data dict self.business_data['user_id'] = self.sample_user['id'] # Save business in the storage list for testing Business.save(self.business_data) response = self.app.delete(self.url_prefix + 'businesses/' + self.business_data['id'], data={}, headers={'Authorization': self.test_token}) self.assertEqual(response.status_code, 202) self.assertIn(b'Your business has been successfully deleted', response.data)
def test_businesses(self): ''' Test retrieving logged in user business ''' # New business details to test updating dummy_business_data = { 'id': uuid.uuid4().hex, 'user_id': self.sample_user['id'], 'name': 'KFC', 'description': 'Finger lickin\' good', 'country': 'Kenya', 'city': 'Nairobi' } # Add user(owner) to the business data dict self.business_data['user_id'] = self.sample_user['id'] # Save businesses to test Business.save(self.business_data) Business.save(dummy_business_data) response = self.app.get(self.url_prefix + 'businesses', headers={'Authorization': self.test_token}) self.assertEqual(response.status_code, 200)
def test_business_update_with_invalid_data(self): ''' Test business updating with invalid data ''' # New business details to test updating new_business_data = { 'name': 'Inzora Nakuru', 'country': 'Kenya', 'city': 'Nakuru' } # Add user(owner) to the business data dict self.business_data['user_id'] = self.sample_user['id'] # Save business in the storage list for testing Business.save(self.business_data) response = self.app.put(self.url_prefix + 'businesses/' + self.business_data['id'], data=json.dumps(new_business_data), headers={'Authorization': self.test_token}, content_type='application/json') self.assertEqual(response.status_code, 400) self.assertIn(b'Please provide required info', response.data)
def test_business_update(self): ''' Test business updating ''' # New business details to test updating new_business_data = { 'name': 'Inzora Nakuru', 'description': 'Enjoy Coffee and Pizzas', 'country': 'Kenya', 'city': 'Nakuru' } # Add user(owner) to the business data dict self.business_data['user_id'] = self.sample_user['id'] # Save business in the storage list for testing Business.save(self.business_data) response = self.app.put(self.url_prefix + 'businesses/' + self.business_data['id'], data=json.dumps(new_business_data), headers={'Authorization': self.test_token}) self.assertEqual(response.status_code, 202) self.assertIn(b'Your business has been successfully updated', response.data)
def register_business(): ''' Register business ''' sent_data = request.get_json(force=True) valid = validate(sent_data, REGISTER_BUSINESS_RULES) if valid is not True: response = jsonify(status='error', message="Please provide required info", errors=valid) response.status_code = 400 return response user_id = token_id(request.headers.get('Authorization')) if Business.query.order_by(desc(Business.created_at)).filter( Business.user_id == user_id, func.lower(Business.name) == func.lower( sent_data['name'])).first() is not None: response = jsonify(status='error', message=("You have already " "registered business with the same name")) response.status_code = 400 return response data = { 'user_id': user_id, 'name': sent_data['name'], 'description': sent_data['description'], 'category': sent_data['category'], 'country': sent_data['country'], 'city': sent_data['city'] } Business.save(data) response = jsonify({ 'status': 'ok', 'message': "Your business has been successfully registered" }) response.status_code = 201 return response
def setUp(self): """ Set up test data """ self.app = APP.test_client() self.app.testing = True self.sample_user = { 'id': uuid.uuid4().hex, 'username': '******', 'email': '*****@*****.**', 'password': '******', 'confirm_password': '******' } self.exist_user = { 'id': uuid.uuid4().hex, 'username': '******', 'email': '*****@*****.**', 'password': '******', 'confirm_password': '******' } self.business_data = { 'id': uuid.uuid4().hex, 'name': 'Inzora rooftop coffee', 'description': 'We have best coffee for you,', 'country': 'Kenya', 'city': 'Nairobi' } save_user = User() save_user.save({ 'id': self.sample_user['id'], 'username': self.sample_user['username'], 'email': self.sample_user['email'], 'password': self.sample_user['password'], 'confirm_password': self.sample_user['confirm_password'] }) # Save business for reviews testing self.rev_business_data = { 'id': uuid.uuid4().hex, 'name': 'KFC', 'description': 'Finger lickin\' good', 'country': 'Kenya', 'city': 'Nairobi' } # Add user(owner) to the business data dict self.rev_business_data['user_id'] = self.sample_user['id'] # Save business in the storage list for testing Business.save(self.rev_business_data) with APP.test_request_context(): # Orphan id: User id that will be used to create an orphan token orphan_id = uuid.uuid4().hex save_user.save({ 'id': orphan_id, 'username': "******", 'email': "*****@*****.**", 'password': self.exist_user['password'], 'confirm_password': self.exist_user['confirm_password'] }) # Issue a token the the test user (sample_user) # Store test token in auth storage auth_token list token = get_token(self.sample_user['id']) # Orphan token: User token that do not have any registered business orphan_token = get_token(orphan_id) expired_token = get_token(self.sample_user['id'], -3600) # Create bad signature token # Bad signature: #nt secret key from the one used in our API used # to hash tokens other_signature_token = get_token(self.sample_user['id'], 3600, 'other_signature') User().add_token(token) User().add_token(expired_token) User().add_token(orphan_token) User().add_token(other_signature_token) self.test_token = token self.expired_test_token = expired_token self.other_signature_token = other_signature_token self.orphan_test_token = orphan_token