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_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_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_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 get_tags_example(self, num=1): tags = [] for i in range(num): keyword = FakeKeywordFactory.create() tag = { 'pk': keyword.pk, 'name': keyword.name, } tags.append(tag) return tags
def test_ask_the_ecosystem(self, post_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, ) keyword = FakeKeywordFactory.create() num_files = 3 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_params = { 'team_pk': new_team.id, 'project_pk': sprint.id, } url = reverse('api:forum:questions-team-list', kwargs=url_params) self.client.login(username=self.user.username, password='******') # DO ACTION response = self.client.post(url, data=data) # ASSERTS self.assertTrue(status.is_success(response.status_code)) self.assertIsNotNone(response.data['pk']) post = Post.objects.get(pk=response.data['pk']) self.assertEqual(post.tags.count(), 2) self.assertTrue(post.is_project) self.assertTrue(post_emails_task_mock.called) self.assertEqual(post_emails_task_mock.call_count, 1) self.assertIsNotNone(self.get_mock_kwarg(post_emails_task_mock, 'eta')) self.assertEqual(len(post.uploaded_files), num_files) self.assertEqual(len(response.data.get('uploaded_files')), num_files)
def test_question_update(self): # PREPARE DATA user = self.posts_first_A[0].created_by user_unauth = FakeUserFactory.create(is_superuser=False, is_active=True, password='******') 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, 'pk': self.posts_first_A[0].pk } new_tags = [] for i in range(6): keyword = FakeKeywordFactory.create() new_tags.append({ 'pk': keyword.pk, 'name': keyword.name, }) url = reverse('api:swarms:swarms-project-questions-detail', kwargs=query_params) # DO ACTION self.client.login(username=user.username, password='******') response = self.client.get(url) post = response.data payload_data = { 'title': '{} EDITED'.format(post.get('title')), 'description': post.get('description'), 'tags': new_tags, 'uploaded_files': self.get_files_example(4), } response = self.client.put(url, data=payload_data) self.client.login(username=user_unauth.username, password='******') response_unauth = self.client.put(url, data=payload_data) # ASSERTS self.assertEqual(response_unauth.status_code, status.HTTP_404_NOT_FOUND) data = response.json() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(data.get('title'), payload_data.get('title')) self.assertEqual(data.get('description'), payload_data.get('description')) self.assertEqual(len(data.get('tags')), len(payload_data.get('tags'))) self.assertEqual(len(data.get('uploadedFiles')), len(payload_data.get('uploaded_files')))
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_create_post_in_circle_success(self, post_emails_task_mock): # PREPARE DATA consultant = FakeConsultantFactory.create(user__is_active=True, user__password='******') circle_slug = 'consultants' circle = Circle.objects.get(slug=circle_slug) circle.add_user(consultant.user) url = reverse('api:circles:circles-create', kwargs={'slug': circle_slug}) 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_success(response.status_code)) data = response.json() self.assertIsNotNone(data.get('pk')) post = Post.objects.get(pk=data.get('pk')) self.assertTrue(post.is_circle) self.assertIsNone(post.edited) self.assertTrue(post_emails_task_mock.called) self.assertEqual(post_emails_task_mock.call_count, 1) self.assertIsNotNone(self.get_mock_kwarg(post_emails_task_mock, 'eta')) self.assertEqual(len(post.uploaded_files), num_files) self.assertEqual(len(data.get('uploadedFiles')), num_files)
def test_question_create_fail(self): # PREPARE DATA qa_team = self.qa_session_first.teams.get(team=self.team_A) query_params = { 'project_id': self.qa_session_first.project.pk, 'team_id': self.team_A.pk, 'swarm_id': qa_team.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, }, { 'name': faker.word(), }, ], 'uploaded_files': self.get_files_example(2), } new_post_bad_request = new_post new_post_bad_request.pop('title') # DO ACTION self.client.login(username=self.secondary_user.username, password='******') res_unauth = self.client.post(url, data=new_post) self.client.login(username=self.user.username, password='******') res_bad_req = self.client.post(url, data=new_post_bad_request) # ASSERTS self.assertEqual(res_unauth.status_code, status.HTTP_404_NOT_FOUND) self.assertEqual(res_bad_req.status_code, status.HTTP_400_BAD_REQUEST)
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_update_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}) 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(), }, ], 'uploaded_files': self.get_files_example(), } response = self.client.put(url, data=data) # ASSERTS self.assertTrue(status.is_success(response.status_code)) post.refresh_from_db() self.assertEqual(post.title, data['title']) self.assertEqual(post.description, data['description']) self.assertEqual(post.tags.count(), 2) self.assertEqual(len(response.data.get('uploaded_files')), 1)