def make_client(user=None, first_name='client', activated=True, make_card=True): if not user: user = PhoneAuthUser.objects.create( phone=str(random.randint(1, 2000000))) if activated: client = Client.objects.create( user=user, first_name=first_name, status=ClientStatus.VERIFIED, avatar=utils.make_in_memory_image('supername'), gender=Gender.MALE, date_of_birth=timezone.now()) Address.objects.create(location=Location.objects.create(lat=10, lon=10), city='kazan', street_name='latstr', building='4', floor=2, apt_number=79, entrance=6, has_intercom=True, client=client, is_default=True) if make_card: PaymentCard.objects.create(client=client, cryptogram='BLABL', client_name_on_card='JOHN', card_number='190') else: client = Client.objects.create(user=user, status=ClientStatus.DUMMY) return client
def make_master(name, lon=15, lat=10, user=None, activated=True, about='awesome master!', email='', make_portfolio=True, make_balance=False): def _rand(): random.seed() return random.randint(100000, 200000) if not user: user = PhoneAuthUser.objects.create(phone=str(_rand())) if activated: if not email: email = str(_rand()) + '*****@*****.**' master = Master.objects.create( user=user, first_name=name, about=about, email=email, avatar=utils.make_in_memory_image('supername'), status=MasterStatus.VERIFIED, gender=Gender.FEMALE, date_of_birth=timezone.now(), location=Location.objects.create(lat=lat, lon=lon)) if make_portfolio: PortfolioImage.objects.create( image=utils.make_in_memory_image('heyho'), description=str(_rand()) + 'description', status=PortfolioImageStatus.ON_MODERATION, master=master) if make_balance: Balance.objects.create(master=master, future=_rand(), on_hold=_rand(), transferred=_rand()) else: Balance.objects.create(master=master) else: master = Master.objects.create(user=user, status=MasterStatus.DUMMY) return master
def test_upload_multiple(self): self.assertEqual(len(self.master_object.portfolio.all()), 0) resp = self.client.post(reverse(AddPortfolioItemsView.view_name, args=[self.master_object.id]), data={'images': [ utils.make_in_memory_image( 'avatar'), utils.make_in_memory_image( 'avatar2')] }, format='multipart') self.assertEqual(resp.status_code, status.HTTP_201_CREATED) self.assertEqual(len(resp.data), 2) updated_master = Master.objects.get(pk=self.master_object.id) self.assertIsNotNone(updated_master.portfolio) self.assertEqual(len(updated_master.portfolio.all()), 2) self.assertEqual(updated_master.portfolio.first().status, PortfolioImageStatus.ON_MODERATION)
def setUp(self): self.category = ServiceCategory( name='category', image=utils.make_in_memory_image('testfile')) self.category.save() self.service = Service.objects.create(category=self.category, name='s1', description='desc', cost=100, min_duration=100, max_duration=120)
def test_upload(self): self.client_object.avatar.delete() self.assertEqual(self.client_object.avatar, None) resp = self.client.patch(reverse(ClientAvatarUpdateView.view_name, args=[self.client_object.id]), data={'image': utils.make_in_memory_image( 'avatar')}) self.assertEqual(resp.status_code, status.HTTP_201_CREATED) updated_client = Client.objects.get(pk=self.client_object.id) self.assertNotEqual(updated_client.avatar, None)
def setUp(self): self.category = ServiceCategory.objects.create( name='category', image=utils.make_in_memory_image('img')) self.service = Service.objects.create(category=self.category, name='Service обычный', description='d', cost=10, min_duration=30, max_duration=60)
def test_upload_not_client(self): master_user = make_master('123', 22) token, _ = Token.objects.get_or_create(master=master_user) self.client.credentials(HTTP_AUTHORIZATION=f'Token {token.key}') resp = self.client.patch(reverse(ClientAvatarUpdateView.view_name, args=[self.client_object.id]), data={'image': utils.make_in_memory_image( 'avatar')}) self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)
def make_display_item(*categories, name=None, special=None): if len(categories) == 1: di = DisplayItem.objects.create() di.categories.add(categories[0]) else: di = DisplayItem.objects.create( name=name, image=utils.make_in_memory_image('supername'), special=special and {'type': 'composite'}) for category in categories: di.categories.add(category) return di
def test_upload_not_master(self): client = make_client() token, _ = Token.objects.get_or_create(client=client) self.client.credentials(HTTP_AUTHORIZATION=f'Token {token.key}') resp = self.client.post(reverse(AddPortfolioItemsView.view_name, args=[self.master_object.id]), data={'images': [utils.make_in_memory_image( 'avatar')]}, format='multipart') self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)
def test_delete_ok(self): portfolio = PortfolioImage.objects.create( image=utils.make_in_memory_image('kek'), master=self.master_object, description='blabla') resp = self.client.delete(reverse(DeletePortfolioItemView.view_name, args=[self.master_object.id, portfolio.id])) self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT) self.master_object = Master.objects.get(pk=self.master_object.id) self.assertEqual(len(self.master_object.portfolio.all()), 0) self.assertEqual(PortfolioImage.objects.count(), 0)
def test_display_items(self): di = DisplayItem.objects.create( name='DI', image=utils.make_in_memory_image('avatar'), special={'type': 'supertype'}) di.categories.add(self.category) serializer = DisplayItemSerializer(di) data = serializer.data self.assertEqual(data['name'], 'DI') self.assertEqual(data['special'], {'type': 'supertype'}) self.assertIn('image', data) cats = data['categories'] self.assertEqual(len(cats), 1) self.assertEqual(cats[0]['name'], self.category.name) self.assertIn('services', cats[0])
def test_set_description(self): self.assertEqual(len(self.master_object.portfolio.all()), 0) image = PortfolioImage.objects.create(master=self.master_object, image=utils.make_in_memory_image( 'kek')) resp = self.client.patch( reverse(AddPortfolioItemDescriptionView.view_name, args=[self.master_object.id]), data=[{'image_id': image.id, 'description': 'superdescription'}], format='json') self.assertEqual(resp.status_code, status.HTTP_200_OK) image = PortfolioImage.objects.get(pk=image.id) self.assertEqual(image.description, 'superdescription')
def make_category(category_name): category = ServiceCategory.objects.create( name=category_name, image=utils.make_in_memory_image('img')) simple = Service.objects.create(category=category, name=category_name + ' обычный', description='d', cost=random.randint(10, 90), min_duration=30, max_duration=60) complex = Service.objects.create(category=category, name=category_name + ' топовый', description='d', cost=random.randint(10, 90), min_duration=60, max_duration=90) return category
def test_create_master(self): resp = self.client.post(reverse(MasterListCreateView.view_name), data={ 'first_name': 'SUPER_MASTER', 'gender': Gender.MALE, 'date_of_birth': utils.get_date(-100), 'email': '*****@*****.**', 'services': ','.join( [str(service.id) for service in Service.objects.all()]), 'avatar': utils.make_in_memory_image('azz') }, format='multipart') self.assertEqual(resp.status_code, status.HTTP_201_CREATED) new_master = Master.objects.get(first_name='SUPER_MASTER') self.assertEqual(new_master.status, MasterStatus.ON_REVIEW) self.assertEqual(new_master.gender, Gender.MALE) self.assertEqual(new_master.email, '*****@*****.**') self.assertIsNotNone(new_master.balance) self.assertEqual(len(new_master.services.all()), len(Service.objects.all()))
def test_upload_wrong_master(self): resp = self.client.post(reverse(AddPortfolioItemsView.view_name, args=[100]), data={'image': [utils.make_in_memory_image( 'avatar')]}, format='multipart') self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)
def test_upload_wrong_client(self): resp = self.client.patch(reverse(ClientAvatarUpdateView.view_name, args=[100]), data={'image': utils.make_in_memory_image( 'avatar')}) self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)