def test_exist_name_business(self): ''' Test business updating with existing busiiness name under one user ''' self.add_business() # Data to be saved to test same name businesses under one person new_business_data = { 'user_id': self.sample_user['id'], 'name': 'TRM', 'description': 'Enjoy Coffee and Pizzas', 'category': 'Mall', 'country': 'Kenya', 'city': 'Nakuru' } business = Business( user_id=self.sample_user['id'], name='TRM', description=self.business_data['description'], category=self.business_data['category'], country=self.business_data['country'], city=self.business_data['city'], ) db.session.add(business) db.session.commit() response = self.app.put(self.url_prefix + 'businesses/' + self.business_data['hashid'], 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 a business with same name', response.data)
def test_same_business_registration(self): ''' Test business registration with the same name under same user ''' data = self.business_data data['user_id'] = self.sample_user['id'] Business().save( self.business_data) # Save business assigned to sample user response = self.app.post(self.url_prefix + 'businesses', data=json.dumps(self.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 business', response.data)
def add_business(self): ''' Add sample business in database ''' business = Business( user_id=self.sample_user['id'], name=self.business_data['name'], description=self.business_data['description'], category=self.business_data['category'], country=self.business_data['country'], city=self.business_data['city'], ) db.session.add(business) db.session.commit() self.business_data['hashid'] = business.hashid() self.business_data['id'] = business.id
def setUp(self): ''' Set up test data ''' self.main = create_app('testing') self.app = self.main.test_client() self.app_context = self.main.app_context() self.app_context.push() with self.app_context: db.init_app(self.main) db.create_all() self.sample_user = { 'username': '******', 'email': '*****@*****.**', 'password': '******', 'confirm_password': '******' } self.exist_user = { 'username': '******', 'email': '*****@*****.**', 'password': '******', 'confirm_password': '******' } self.unconfirmed_user = { 'username': '******', 'email': '*****@*****.**', 'password': '******', 'confirm_password': '******', 'activation_token': 'AvauDT0T7wo_O6vnb5XJxKzuPteTIpJVv_0HRokS' } self.business_data = { 'name': 'Inzora rooftop coffee', 'description': 'We have best coffee for you,', 'category': 'Coffee-shop', 'country': 'Kenya', 'city': 'Nairobi' } # Business sample data self.rev_business_data = { 'name': 'KFC', 'description': 'Finger lickin\' good', 'category': 'Food', 'country': 'Kenya', 'city': 'Nairobi' } with self.main.test_request_context(): # Orphan id: User id that will be used to create an orphan token orphan_user = User(username="******", email="*****@*****.**", password=self.sample_user['password']) user = User(username=self.sample_user['username'], email=self.sample_user['email'], password=generate_password_hash( self.sample_user['password']), activation_token=None) unconfirmed_account = User( username=self.unconfirmed_user['username'], email=self.unconfirmed_user['email'], password=generate_password_hash( self.unconfirmed_user['password']), activation_token=self.unconfirmed_user['activation_token']) db.session.add(user) db.session.add(orphan_user) db.session.add(unconfirmed_account) db.session.commit() self.sample_user['id'] = user.id self.orphan_id = orphan_user.id self.unconfirmed_user_id = unconfirmed_account.id db.session.remove() token = Token(user_id=self.sample_user['id'], access_token=get_token(self.sample_user['id'])) orphan_token = Token(user_id=self.orphan_id, access_token=get_token(self.orphan_id)) unconfirmed_user_token = Token(user_id=self.unconfirmed_user_id, access_token=get_token( self.unconfirmed_user_id)) expired_token = Token(user_id=self.sample_user['id'], access_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 = Token(user_id=self.sample_user['id'], access_token=get_token( self.sample_user['id'], 3600, 'other_signature')) business = Business( user_id=self.sample_user['id'], name=self.rev_business_data['name'], description=self.rev_business_data['description'], category=self.rev_business_data['category'], country=self.rev_business_data['country'], city=self.rev_business_data['city'], ) db.session.add(token) db.session.add(orphan_token) db.session.add(expired_token) db.session.add(unconfirmed_user_token) db.session.add(other_signature_token) db.session.add(business) db.session.commit() self.test_token = token.access_token self.expired_test_token = expired_token.access_token self.other_signature_token = other_signature_token.access_token self.orphan_token = orphan_token.access_token self.unconfirmed_user_token = unconfirmed_user_token.access_token
from faker import Faker from api.models.business import Business from api import create_app from api import db app = create_app('development') fake = Faker() with app.app_context(): businesses = [] for _ in range(100): business = Business( user_id=1, name=fake.company(), description=fake.text(max_nb_chars=300, ext_word_list=None), category=fake.job(), country=fake.country(), city=fake.city(), ) db.session.add(business) db.session.commit() businesses.append(business)