def create_cohorts(self): self.cohort_lvl_2 = CertificationCohort.objects.create( date=string_to_datetime(faker.date()), certification=ExOCertification.objects.get( level=settings.EXO_CERTIFICATION_CERTIFICATION_CH_LEVEL_2), seats=25, price=1500, status=settings.EXO_CERTIFICATION_COHORT_STATUS_CH_OPEN, invoice_concept=' '.join(faker.words()), ) self.cohort_lvl_2_ft = CertificationCohort.objects.create( date=string_to_datetime(faker.date()), certification=ExOCertification.objects.get( level=settings.EXO_CERTIFICATION_CERTIFICATION_CH_LEVEL_2A), seats=25, price=1500, status=settings.EXO_CERTIFICATION_COHORT_STATUS_CH_OPEN, invoice_concept=' '.join(faker.words()), ) self.cohort_lvl_3 = CertificationCohort.objects.create( date=string_to_datetime(faker.date()), certification=ExOCertification.objects.get( level=settings.EXO_CERTIFICATION_CERTIFICATION_CH_LEVEL_3), seats=25, price=2500, first_price_tier=1250, status=settings.EXO_CERTIFICATION_COHORT_STATUS_CH_OPEN, invoice_concept=' '.join(faker.words()), )
def test_circle_feed(self): # PREPARE DATA team = FakeTeamFactory.create() participant = FakeUserFactory.create(password='******', is_active=True) self.add_user_to_team(participant, team) user = self.consultant.user user.is_staff = True user.save() questions = [] for i in range(2): questions.append( Post.objects.create_project_team_post(title=' '.join( faker.words()), user_from=participant, team=team)) announcements = [] for i in range(3): announcements.append( Post.objects.create_announcement_post( user_from=self.super_user, title=' '.join(faker.words()), description=faker.text())) posts = [] for i in range(4): posts.append( Post.objects.create_circle_post( circle=Circle.objects.get(slug='ecosystem'), user_from=self.super_user, title=' '.join(faker.words()), description=faker.text())) posts.append( Post.objects.create_circle_post( circle=Circle.objects.get(slug='trainers'), user_from=self.consultant.user, title=' '.join(faker.words()), description=faker.text())) posts[0].reply(user_from=self.consultant.user, comment=faker.text()) announcements[0].reply(user_from=self.consultant.user, comment=faker.text()) questions[0].reply(user_from=self.consultant.user, comment=faker.text()) url = reverse('api:forum:feed') # DO ACTION self.client.login(username=self.consultant.user.username, password='******') response = self.client.get(url) # ASSERTS self.assertTrue(status.is_success(response.status_code)) data = response.json() self.assertEqual(data.get('count'), 10)
def test_get_posts_from_team(self): # PREPARE DATA team_1 = FakeTeamFactory.create() team_2 = FakeTeamFactory.create() user_team_1 = self.user user_team_2 = FakeUserFactory.create(password='******', is_active=True) self.add_user_to_team(user_team_1, team_1) self.add_user_to_team(user_team_2, team_2) search = 'tosearch' post_A_team_1 = Post.objects.create_project_team_post( title='{} {}'.format(' '.join(faker.words()), search), user_from=user_team_1, team=team_1) Post.objects.create_project_team_post(title=' '.join(faker.words()), user_from=user_team_1, team=team_1) post_team_2 = Post.objects.create_project_team_post( title=' '.join(faker.words()), user_from=user_team_2, team=team_2) url_team_1 = reverse('api:forum:questions-team-list', kwargs={ 'project_pk': team_1.project.pk, 'team_pk': team_1.pk }) url_team_2 = reverse('api:forum:questions-team-list', kwargs={ 'project_pk': team_2.project.pk, 'team_pk': team_2.pk }) # DO ACTION self.client.login(username=user_team_1.username, password='******') response = self.client.get(url_team_1) response_search = self.client.get(url_team_1, data={'search': search}) # ASSERTS self.assertTrue(status.is_success(response.status_code)) self.assertEqual(response.data.get('count'), 2) self.assertTrue(status.is_success(response_search.status_code)) self.assertEqual(response_search.data.get('count'), 1) self.assertEqual(post_A_team_1.pk, response_search.data.get('results')[0].get('pk')) self.assertEqual(post_A_team_1.title, response_search.data.get('results')[0].get('title')) self.assertEqual(post_A_team_1.description, response.data.get('results')[0].get('description')) # DO ACTION self.client.login(username=user_team_2.username, password='******') response = self.client.get(url_team_2) # ASSERTS self.assertTrue(status.is_success(response.status_code)) self.assertEqual(response.data.get('count'), 1) self.assertEqual(post_team_2.pk, response.data.get('results')[0].get('pk'))
def test_create_post_in_announcements_fail(self): # PREPARE DATA circle_slug = 'announcements' url = reverse('api:circles:circles-create', kwargs={'slug': circle_slug}) consultant = FakeConsultantFactory.create(user__is_active=True, user__password='******') keyword = FakeKeywordFactory.create() num_files = 2 payload = { 'title': ' '.join(faker.words()), 'description': faker.text(), 'tags': [ { 'pk': keyword.pk, 'name': keyword.name, }, { 'name': faker.word() + faker.numerify(), }, ], 'uploaded_files': self.get_files_example(num_files), } # DO ACTION self.client.login(username=consultant.user.username, password='******') response = self.client.post(url, data=payload) # ASSERTS self.assertTrue(status.is_client_error(response.status_code))
def test_post_list_from_circle_removed(self): # PREPARE DATA consultant = FakeConsultantFactory.create(user__is_active=True, user__password='******') circle = Circle.objects.get(name=settings.CIRCLES_ECOSYSTEM_NAME) circle.add_user(consultant.user) self.client.login(username=consultant.user.username, password='******') for i in range(5): Post.objects.create_circle_post(user_from=self.super_user, circle=circle, title=' '.join(faker.words()), description=faker.text()) url = reverse('api:forum:circle-post-list', kwargs={'circle_slug': circle.slug}) # DO ACTION response = self.client.get(url) # ASSERTS self.assertTrue(status.is_success(response.status_code)) # DO ACTION circle.mark_as_removed() response = self.client.get(url) # ASSERTS self.assertTrue(status.is_client_error(response.status_code)) self.assertEqual(response.status_code, status.HTTP_410_GONE)
def handle(self, *args, **options): self.stdout.write('\n Creating Questions/Answers for testing: \n\n') consultants = Consultant.objects.all() exo_consulting = ExOActivity.objects.get(code=settings.EXO_ACTIVITY_CH_ACTIVITY_CONSULTING) for project in Project.objects.filter(teams__isnull=False): users = User.objects.filter(teams__project=project) if not users: continue self.stdout.write('\n Creating questions for %s' % str(project)) for i in range(random.randint(2, 10)): user = random.choices(users, k=1)[0] try: post = Post.objects.create_project_team_post( user_from=user, team=user.teams.filter(project=project).first(), title=' '.join(faker.words()), description=faker.text(), tags=random.choices( Keyword.objects.all(), k=random.randint(2, 6)), ) except Exception: continue for consultant in set(random.choices(consultants, k=random.randint(2, 10))): exo_activity, _ = consultant.exo_profile.exo_activities.get_or_create( exo_activity=exo_consulting) exo_activity.enable() post.see(consultant.user) for consultant in set(random.choices(consultants, k=random.randint(2, 10))): exo_activity, _ = consultant.exo_profile.exo_activities.get_or_create( exo_activity=exo_consulting) exo_activity.enable() post.see(consultant.user) post.reply(consultant.user, faker.text()) self.stdout.write('\n Finish!! \n\n')
def test_create_post_with_one_mention(self): # PREPARE DATA self.circle = Circle.objects.first() self.user.hubs.create(hub=self.circle.hub) user_mentioned = FakeUserFactory.create(is_active=True) description = '<a {}="{}" {}="{}">{}</a>'.format( DEFAULT_TYPE_PATTERN, user_mentioned.__class__.__name__, DEFAULT_UUID_PATTERN, user_mentioned.pk, user_mentioned.short_name, ) post = Post.objects.create_circle_post(user_from=self.user, circle=self.circle, title=' '.join(faker.words()), description=description) post_id = post.pk # DO ACTIONS self.post = Post.objects.get(pk=post_id) # ASSERTS self.assertTrue(self.user.actor_actions.all().filter( verb=settings.MENTION_VERB).exists()) self.assertEqual( self.user.actor_actions.all().filter( verb=settings.MENTION_VERB).count(), 1)
def test_create_answer_with_mentions(self): # PREPARE DATA self.circle = Circle.objects.first() self.user.hubs.create(hub=self.circle.hub) post = Post.objects.create_circle_post(user_from=self.user, circle=self.circle, title=' '.join(faker.words()), description=faker.text()) post_id = post.pk self.post = Post.objects.get(pk=post_id) user = FakeUserFactory.create(is_active=True, password='******') self.circle.add_user(user) user_mentioned = FakeUserFactory.create(is_active=True) reply_comment = 'Hi \ <a {}="{}" {}="{}">{}</a> \ '.format( DEFAULT_TYPE_PATTERN, user_mentioned.__class__.__name__, DEFAULT_UUID_PATTERN, user_mentioned.pk, user_mentioned.short_name, ) # DO ACTIONS self.answer = self.post.reply(user, reply_comment) # ASSERTS self.assertTrue(user.actor_actions.all().filter( verb=settings.MENTION_VERB).exists())
def handle(self, *args, **options): self.stdout.write('\n Creating Post/Answers for testing: \n\n') consultants = Consultant.objects.all() for circle in Circle.objects.all(): users = followers(circle) if len(users) < 5: for consultant in set( random.choices(consultants, k=random.randint(2, 10))): circle.add_user(consultant.user) users = followers(circle) for i in range(random.randint(2, 10)): user = random.choices(users, k=1)[0] post = Post.objects.create_circle_post( user_from=user, circle=circle, title=' '.join(faker.words()), description=faker.text(), tags=random.choices(Keyword.objects.all(), k=random.randint(2, 6)), ) for consultant in set( random.choices(consultants, k=random.randint(2, 10))): post.see(consultant.user) for consultant in set( random.choices(consultants, k=random.randint(2, 10))): post.see(consultant.user) post.reply(consultant.user, faker.text()) self.stdout.write('\n Finish!! \n\n')
def certificate_consultant(self, email, _type, user_from): certificate = create_certificates.Command() course_name = faker.word() consultant_role_group = certificate.create_consultant_role_group( user_from=user_from, name=course_name, description=' '.join(faker.words()), issued_on=datetime.now(), _type=_type, ) certificate.create_consultant_roles( emails=[email], role_code=certificate.get_role_code(_type), consultant_role_group=consultant_role_group, ) data = { 'name': consultant_role_group.name, 'description': consultant_role_group.description, 'content_object': consultant_role_group, '_type': consultant_role_group._type, 'created_by': user_from, 'instructor_name': user_from.get_full_name(), } data['issued_on'] = consultant_role_group.issued_on data['course_name'] = course_name CertificationGroup.objects.create_group_and_credentials( user_from=user_from, related_objects_list=consultant_role_group.consultant_roles.all(), **data)
def test_question_create_success(self): # PREPARE DATA query_params = { 'project_id': self.qa_session_first.project.pk, 'team_id': self.team_A.pk, 'swarm_id': self.qa_session_first.teams.get(team=self.team_A).pk, } url = reverse('api:swarms:swarms-project-questions-list', kwargs=query_params) keyword = FakeKeywordFactory.create() new_post = { 'title': ' '.join(faker.words()), 'description': faker.text(), 'tags': [{ 'pk': keyword.pk, 'name': keyword.name, }], 'uploaded_files': self.get_files_example(2), } # DO ACTION self.client.login(username=self.user.username, password='******') response = self.client.post(url, data=new_post) # ASSERTS self.assertEqual(response.status_code, status.HTTP_201_CREATED) data = response.json() self.assertIsNotNone(data.get('pk', None)) self.assertEqual(data.get('title'), new_post.get('title')) self.assertEqual(data.get('description'), new_post.get('description')) self.assertEqual(len(data.get('tags', [])), len(new_post.get('tags', []))) self.assertEqual(len(data.get('uploadedFiles', [])), len(new_post.get('uploaded_files', [])))
def test_update_answer_team_project(self, answer_emails_task_mock): # PREPARE DATA sprint = FakeSprintAutomatedFactory.create( status=settings.PROJECT_CH_PROJECT_STATUS_WAITING) new_team = FakeTeamFactory.create(project=sprint.project_ptr) new_team.add_member( user_from=self.super_user, email=self.user.email, name=self.user.short_name, ) post = Post.objects.create_project_team_post( user_from=self.super_user, team=new_team, title=' '.join(faker.words()), description=faker.text(), ) answer = post.reply(self.user, faker.text()) url = reverse('api:forum:answer-detail', kwargs={'pk': answer.pk}) self.client.login(username=self.user.username, password='******') # DO ACTION data = {'comment': faker.text()} response = self.client.put(url, data=data) # ASSERTS self.assertTrue(status.is_success(response.status_code)) answer.refresh_from_db() self.assertEqual(answer.comment, data['comment']) self.assertTrue(answer_emails_task_mock.called) self.assertEqual(answer_emails_task_mock.call_count, 1) self.assertIsNotNone( self.get_mock_kwarg(answer_emails_task_mock, 'eta'))
def test_update_post_several_times_updates_edited_property(self, mock): # PREPARE DATA post = Post.objects.create_announcement_post( user_from=self.user, title=' '.join(faker.words()), description=faker.text(), ) post.action_update(self.user) edit_action = post.action_object_actions.filter( verb=settings.FORUM_ACTION_EDIT_POST).first() edit_action.timestamp = decrease_date( seconds=settings.FORUM_NEW_POST_DELAY * 3) edit_action.save() # DO ACTION post.action_update(self.super_user) datetime_updated = decrease_date( seconds=settings.FORUM_NEW_POST_DELAY * 2) last_edit_action = post.action_object_actions.filter( verb=settings.FORUM_ACTION_EDIT_POST).first() last_edit_action.timestamp = datetime_updated last_edit_action.save() # ASSERTS self.assertEqual(last_edit_action.timestamp.time(), post.edited.timestamp.time())
def test_retrieve_project_post_success(self): # PREPARE DATA post = Post.objects.create_project_team_post( title=''.join(faker.words()), user_from=self.super_user, team=FakeTeamFactory.create()) consultant = FakeConsultantFactory.create() self.certificate_consultant( email=consultant.user.email, _type='consultantrole-foundations', user_from=self.super_user, ) user = consultant.user user.set_password('123456') user.save() url = reverse('api:forum:post-slug-detail', kwargs={'slug': post.slug}) # DO ACTION self.client.login(username=user.username, password='******') response = self.client.get(url) # ASSERTS self.assertTrue(status.is_success(response.status_code))
def create_application(self, consultant=None, level=None): certification_level = level or settings.EXO_CERTIFICATION_CERTIFICATION_CH_LEVEL_2 cert_request = CertificationRequest.objects.create( user=consultant.user, requester_email=consultant.user.email, requester_name=consultant.user.full_name, certification=ExOCertification.objects.get( level=certification_level), ) if certification_level == settings.EXO_CERTIFICATION_CERTIFICATION_CH_LEVEL_2: cohort = self.cohort_lvl_2.pk elif certification_level == settings.EXO_CERTIFICATION_CERTIFICATION_CH_LEVEL_3: cohort = self.cohort_lvl_3.pk payload = { 'country': 'NO', 'recaptcha': faker.word(), 'billingName': faker.name(), 'taxId': faker.word(), 'billingAddress': faker.address(), 'companyName': faker.name(), 'cohort': cohort, 'application_details': { 'reasons': ' '.join(faker.words()), 'whereMetUs': faker.word(), } } self.client.put( reverse('api:exo-certification:applications-detail', kwargs={'pk': cert_request.pk}), data=payload, )
def test_create_circle_post(self, post_emails_task_mock): # PREPARE DATA consultant = FakeConsultantFactory.create(user__is_active=True, user__password='******') circle = Circle.objects.first() circle.add_user(consultant.user) keyword = FakeKeywordFactory.create() num_files = 4 data = { 'title': ' '.join(faker.words()), 'description': faker.text(), 'tags': [ { 'pk': keyword.pk, 'name': keyword.name, }, { 'name': faker.word() + faker.numerify(), }, ], 'uploaded_files': self.get_files_example(num_files), } url = reverse('api:circles:circles-create', kwargs={'slug': circle.slug}) # DO ACTION self.client.login(username=consultant.user.username, password='******') response = self.client.post(url, data) # ASSERTS self.assertTrue(status.is_success(response.status_code)) self.assertEqual(len(response.data.get('uploaded_files')), num_files)
def test_get_post_answer_seen(self): # PREPARE DATA self.circle = Circle.objects.first() self.user.hubs.create(hub=self.circle.hub) post = Post.objects.create_circle_post(circle=self.circle, user_from=self.user, title=' '.join(faker.words()), description=faker.text()) user_1 = FakeUserFactory.create() user_1.hubs.create(hub=self.circle.hub) user_2 = FakeUserFactory.create() user_2.hubs.create(hub=self.circle.hub) post.reply(user_1, faker.text()) post.reply(user_2, faker.text()) post.reply(user_1, faker.text()) url = reverse('api:forum:post-answers', kwargs={'pk': post.pk}) # DO ACTION self.client.login(username=self.user.username, password='******') unseen_response = self.client.get(url) seen_response = self.client.get(url) # ASSERTS self.assertTrue(status.is_success(unseen_response.status_code)) data = unseen_response.json() for item in data.get('results', None): self.assertFalse(item.get('seen')) self.assertTrue(status.is_success(seen_response.status_code)) data = seen_response.json() for item in data.get('results', None): self.assertTrue(item.get('seen'))
def test_announcement_post_list(self): # PREPARE DATA consultant = FakeConsultantFactory.create(user__is_active=True, user__password='******') for i in range(5): Post.objects.create_announcement_post(user_from=self.super_user, title=' '.join( faker.words()), description=faker.text()) url = reverse('api:forum:circle-post-list', kwargs={'circle_slug': 'announcements'}) all_announcements = Post.objects.filter_by__type( settings.FORUM_CH_ANNOUNCEMENT) search = all_announcements.first().title search_announcements = all_announcements.filter_by_search(search) # DO ACTIONS self.client.login(username=consultant.user.username, password='******') response = self.client.get(url) response_search = self.client.get(url, data={'search': search}) self.client.login(username=self.super_user.username, password='******') response_su = self.client.get(url) # ASSERTS self.assertTrue(status.is_success(response.status_code)) self.assertTrue(status.is_success(response_search.status_code)) self.assertTrue(status.is_success(response_su.status_code)) data = response.json() data_search = response_search.json() self.assertEqual(data.get('count'), all_announcements.count()) self.assertEqual(data_search.get('count'), search_announcements.count())
def test_retrieve_post_answers(self): # PREPARE DATA self.circle = Circle.objects.first() self.user.hubs.create(hub=self.circle.hub) post = Post.objects.create_circle_post(circle=self.circle, user_from=self.user, title=' '.join(faker.words()), description=faker.text()) user_1 = FakeUserFactory.create() user_1.hubs.create(hub=self.circle.hub) user_2 = FakeUserFactory.create() user_2.hubs.create(hub=self.circle.hub) post.reply(user_1, faker.text()) post.reply(user_2, faker.text()) post.reply(self.user, faker.text()) post.reply(user_1, faker.text()) url = reverse('api:forum:post-answers', kwargs={'pk': post.pk}) # DO ACTION self.client.login(username=self.user.username, password='******') response = self.client.get(url) # ASSERTS self.assertTrue(status.is_success(response.status_code)) data = response.json() self.assertEqual(data.get('count'), 4)
def test_delete_post(self): # PREPARE DATA post = Post.objects.create_announcement_post( user_from=self.user, title=' '.join(faker.words()), description=faker.text(), ) url = reverse('api:forum:post-detail', kwargs={'pk': post.pk}) url_slug = reverse('api:forum:post-slug-detail', kwargs={'slug': post.slug}) self.client.login(username=self.user.username, password='******') keyword = FakeKeywordFactory.create() # DO ACTION data = { 'title': ' '.join(faker.words()), 'description': faker.text(), 'tags': [ { 'pk': keyword.pk, 'name': keyword.name, }, { 'name': faker.word(), }, ] } response = self.client.delete(url, data=data) # ASSERTS self.assertTrue(status.is_success(response.status_code)) with self.assertRaises(Post.DoesNotExist): post.refresh_from_db() self.assertEqual(Post.all_objects.filter(pk=post.pk).count(), 1) self.assertTrue(Post.all_objects.filter_by_status_removed().exists()) # DO ACTION response_pk = self.client.get(url) response_slug = self.client.get(url_slug) # ASSERTS self.assertTrue(response_pk.status_code, status.HTTP_410_GONE) self.assertTrue(response_slug.status_code, status.HTTP_410_GONE)
def test_update_post_files(self, post_emails_task_mock): # PREPARE DATA keyword = FakeKeywordFactory.create() num_files = 1 files = self.get_files_example(num_files) consultant = FakeConsultantFactory.create(user__is_active=True, user__password='******') circle_slug = 'consultants' circle = Circle.objects.get(slug=circle_slug) circle.add_user(consultant.user) data = { 'title': ' '.join(faker.words()), 'description': faker.text(), 'tags': [ { 'pk': keyword.pk, 'name': keyword.name, }, { 'name': faker.word() + faker.numerify(), }, ], 'uploaded_files': files, } url = reverse('api:circles:circles-create', kwargs={'slug': circle_slug}) self.client.login(username=consultant.user.username, password='******') # DO ACTION response = self.client.post(url, data=data) # ASSERTS self.assertTrue(status.is_success(response.status_code)) self.assertEqual(len(response.data.get('uploaded_files')), num_files) # PREPARE DATA num_files_new = 2 new_files = self.get_files_example(num_files_new) data_put = { 'title': data['title'], 'description': data['description'], 'tags': data['tags'], 'uploaded_files': files + new_files, } # DO ACTION url = reverse('api:forum:post-detail', kwargs={'pk': response.data.get('pk')}) response = self.client.put(url, data=data_put) # ASSERTS self.assertTrue(status.is_success(response.status_code)) self.assertEqual(len(response.data.get('uploaded_files')), num_files + num_files_new)
def test_get_questions_from_participants(self): # PREPARE DATA consultant = FakeConsultantFactory( user__is_active=True, user__password='******', activities=[settings.EXO_ACTIVITY_CH_ACTIVITY_CONSULTING]) team_a = FakeTeamFactory.create() team_b = FakeTeamFactory.create() participant_a = FakeUserFactory.create(password='******', is_active=True) participant_b = FakeUserFactory.create(password='******', is_active=True) self.add_user_to_team(participant_a, team_a) self.add_user_to_team(participant_b, team_b) for i in range(7): post = Post.objects.create_project_team_post( user_from=participant_a, team=team_a, title=' '.join(faker.words()), description=faker.text()) for i in range(12): Post.objects.create_project_team_post(user_from=participant_b, team=team_b, title=' '.join( faker.words()), description=faker.text()) url = reverse('api:forum:questions-participants-list') search = 'tosearch' post.title = '{} {}'.format(post.title, search) post.save() # DO ACTION self.client.login(username=consultant.user.username, password='******') response = self.client.get(url) response_search = self.client.get(url, data={'search': search}) # ASSERTS self.assertTrue(status.is_success(response.status_code)) data = response.json() self.assertEqual(data.get('count'), 19) self.assertTrue(status.is_success(response_search.status_code)) data = response_search.json() self.assertEqual(data.get('count'), 1)
def setUp(self): self.circle = Circle.objects.first() self.user = FakeUserFactory.create(is_active=True) self.user.hubs.create(hub=self.circle.hub) post = Post.objects.create_circle_post( user_from=self.user, circle=self.circle, title=' '.join(faker.words()), description=faker.text()) post_id = post.pk self.post = Post.objects.get(pk=post_id)
def create_cohort(self, level=settings.EXO_CERTIFICATION_CERTIFICATION_CH_LEVEL_2 ): certification = ExOCertification.objects.get(level=level) return CertificationCohort.objects.create( date=string_to_datetime(faker.date()), certification=certification, seats=25, price=2500, status=settings.EXO_CERTIFICATION_COHORT_STATUS_CH_OPEN, invoice_concept=' '.join(faker.words()), )
def setUp(self): super().setUp() self.create_user() self.create_superuser() self.circle = Circle.objects.first() self.user.hubs.create(hub=self.circle.hub) post = Post.objects.create_circle_post(user_from=self.user, circle=self.circle, title=' '.join(faker.words()), description=faker.text()) post_id = post.pk self.post = Post.objects.get(pk=post_id)
def test_update_question_by_user(self): # PREPARE DATA sprint = FakeSprintAutomatedFactory.create( status=settings.PROJECT_CH_PROJECT_STATUS_WAITING) new_team = FakeTeamFactory.create(project=sprint.project_ptr) new_team.add_member( user_from=self.super_user, email=self.user.email, name=self.user.short_name, ) post = Post.objects.create_project_team_post( user_from=self.super_user, team=new_team, title=' '.join(faker.words()), description=faker.text(), ) keyword = FakeKeywordFactory.create() # DO ACTION self.client.login(username=self.user.username, password='******') url = reverse('api:forum:post-detail', kwargs={'pk': post.pk}) data = { 'title': ' '.join(faker.words()), 'description': faker.text(), 'tags': [ { 'pk': keyword.pk, 'name': keyword.name, }, { 'name': faker.word(), }, ] } response = self.client.put(url, data=data) # ASSERTS self.assertTrue(status.is_client_error(response.status_code))
def test_update_post_when_have_replies(self): # PREPARE DATA self.circle = Circle.objects.first() self.user.hubs.create(hub=self.circle.hub) post = Post.objects.create_circle_post(user_from=self.user, circle=self.circle, title=' '.join(faker.words()), description=faker.text()) user = FakeUserFactory.create() user.hubs.create(hub=self.circle.hub) post.reply(user, faker.text()) url = reverse('api:forum:post-detail', kwargs={'pk': post.pk}) keyword = FakeKeywordFactory.create() # DO ACTION data = { 'title': ' '.join(faker.words()), 'description': faker.text(), 'tags': [ { 'pk': keyword.pk, 'name': keyword.name, }, { 'name': faker.word(), }, ] } self.client.login(username=self.user.username, password='******') response = self.client.put(url, data=data) # ASSERTS self.assertTrue(status.is_success(response.status_code)) post.refresh_from_db() self.assertTrue( post.can_update_or_remove(self.user, raise_exceptions=False)) self.assertEqual(post.title, data['title'])
def test_circle_list_success(self): # PREPARE DATA url = reverse('api:circles:circles-list') for circle in Circle.objects.all(): Post.objects.create_circle_post(user_from=self.super_user, circle=circle, title=' '.join(faker.words()), description=faker.text()) Post.objects.create_announcement_post(user_from=self.super_user, title=' '.join(faker.words()), description=faker.text()) Post.objects.create_project_team_post(title=''.join(faker.words()), user_from=self.super_user, team=FakeTeamFactory.create()) open_circle = Circle.objects.create( name=faker.word(), description=faker.text(), created_by=self.consultant_1.user, ) open_circle.add_user(self.consultant_1.user) consultant_circles = following(self.consultant_2.user, Circle) user_circles = following(self.consultant_1.user, Circle) # DO ACTION self.client.login(username=self.consultant_1.user.username, password='******') response_1 = self.client.get(url) self.client.login(username=self.consultant_2.user.username, password='******') response_2 = self.client.get(url) # ASSERTS self.assertTrue(status.is_success(response_1.status_code)) data = response_1.json() self.assertEqual(data.get('count'), len(user_circles) + 2) self.assertTrue(status.is_success(response_2.status_code)) data = response_2.json() self.assertEqual(data.get('count'), len(consultant_circles) + 1)
def test_circle_post_list(self): # PREPARE DATA consultant = FakeConsultantFactory.create(user__is_active=True, user__password='******') self.circle = Circle.objects.first() second_circle = Circle.objects.last() consultant.user.hubs.create(hub=self.circle.hub) consultant.user.hubs.create(hub=second_circle.hub) url_params = {'circle_slug': self.circle.slug} url = reverse('api:forum:circle-post-list', kwargs=url_params) for i in range(6): Post.objects.create_circle_post(circle=self.circle, user_from=consultant.user, title=' '.join(faker.words()), description=faker.text()) for i in range(10): Post.objects.create_circle_post(circle=second_circle, user_from=consultant.user, title=' '.join(faker.words()), description=faker.text()) all_posts = Post.objects.filter_by_circle(self.circle) search = all_posts.first().title search_posts = all_posts.filter_by_search(search) # DO ACTION self.client.login(username=consultant.user.username, password='******') response = self.client.get(url) response_search = self.client.get(url, data={'search': search}) # ASSERTS self.assertTrue(status.is_success(response.status_code)) self.assertTrue(status.is_success(response_search.status_code)) data = response.json() data_search = response_search.json() self.assertEqual(data.get('count'), all_posts.count()) self.assertEqual(data_search.get('count'), search_posts.count())
def test_parse_urls_with_html_tags(self): # PREPARE DATA faked_text = faker.text() faked_url = faker.url() faked_url_text = ' '.join(faker.words()) text_to_parse = '<p>{}</p><a href="{}" rel="nofollow">{}</a>'.format( faked_text, faked_url, faked_url_text) # DO ACTION parsed_text = parse_urls(text_to_parse) # ASSERTIONS self.assertEqual(text_to_parse, parsed_text)