def test_post_not_found_in_database(self): try: post = views_helper.update_facebook_post( post_id=1, post_message='test_post_not_found_in_database') self.fail() except Exception as e: self.assertTrue(True) self.assertEqual(str(e), 'Post matching query does not exist.')
def test_missing_post_message(self): try: post = views_helper.update_facebook_post(post_id=1, post_message='') self.fail() except Exception as e: self.assertTrue(True) self.assertEqual(str(e), 'Missing post message')
def test_post_updated_successfully(self): post = views_helper.create_facebook_post( self.user, 'test_post_updated_successfully') try: post = views_helper.update_facebook_post( post_id=post.id, post_message='test_post_updated_successfully update') self.assertEqual(post.message, 'test_post_updated_successfully update') except Exception as e: self.fail(str(e))
def publish_post(request): """Create or update posts to social media""" post_id = request.POST.get('id', None) post_message = request.POST.get('msg', None) response_data = {} if not post_message: response_data['status'] = 'failure' response_data['message'] = 'Please enter a message' return JsonResponse(response_data) if post_id: # Update existing post try: views_helper.update_facebook_post(post_id, post_message) response_data['status'] = 'success' response_data['message'] = 'Successfully updated to Facebook' except Exception as e: response_data['status'] = 'failure' response_data['message'] = 'Failed to update to Facebook: %s' % str(e) finally: return JsonResponse(response_data) else: # Create new post try: user = request.user post = views_helper.create_facebook_post(user, post_message) response_data['status'] = 'success' response_data['post_id'] = post.id response_data['message'] = 'Successfully posted to Facebook' except Exception as e: response_data['status'] = 'failure' response_data['message'] = 'Failed to post to Facebook: %s' % str(e) finally: return JsonResponse(response_data)
def test_post_not_found_on_facebook(self): Post.objects.create(id=1, user=self.user, platform=SocialPlatformName.FACEBOOK, id_on_platform='UNKNOWN', message='test_post_not_found_on_facebook') try: post = views_helper.update_facebook_post( post_id=1, post_message='test_post_not_found_on_facebook update') self.fail() except Exception as e: self.assertTrue(True) self.assertEqual(str(e), 'Failed to update to Facebook')