-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from andela/ft-share-articles-across-diff-chan…
…nels-#161967028 #161967028 Share articles to other social sites
- Loading branch information
Showing
12 changed files
with
217 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import json | ||
|
||
from django.urls import reverse | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
from django.contrib.auth import get_user_model | ||
|
||
from authors.apps.articles.models import Articles | ||
from authors.apps.authentication.models import User | ||
|
||
|
||
class TestShareEndpoint(APITestCase): | ||
|
||
def setUp(self): | ||
|
||
self.token = self.get_user_token() | ||
|
||
self.data = { | ||
"slug": "posting_test", | ||
"title": "Posting Test", | ||
"description": "this is a posting test", | ||
"body": "The test was successful", | ||
"tagList": "live again", | ||
"author": "TestAuthor" | ||
} | ||
self.all_setup() | ||
|
||
def test_get_facebook_link(self): | ||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) | ||
url = reverse('articleSpecific', kwargs={'slug': 'life-love-death'}) | ||
self.client.post(url, self.data, format='json') | ||
link_url = reverse( | ||
'share_article', | ||
kwargs={ | ||
'slug': 'life-love-death', | ||
'provider': 'facebook'}) | ||
response = self.client.get(link_url, format='json') | ||
self.assertEqual(response.data['provider'], 'facebook') | ||
|
||
def test_get_twitter_link(self): | ||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) | ||
url = reverse('articleSpecific', kwargs={'slug': 'life-love-death'}) | ||
self.client.post(url, self.data, format='json') | ||
link_url = reverse( | ||
'share_article', | ||
kwargs={ | ||
'slug': 'life-love-death', | ||
'provider': 'twitter'}) | ||
response = self.client.get(link_url, format='json') | ||
self.assertEqual(response.data['provider'], 'twitter') | ||
|
||
def test_get_email_link(self): | ||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) | ||
url = reverse('articleSpecific', kwargs={'slug': 'life-love-death'}) | ||
self.client.post(url, self.data, format='json') | ||
link_url = reverse( | ||
'share_article', | ||
kwargs={ | ||
'slug': 'life-love-death', | ||
'provider': 'email'}) | ||
response = self.client.get(link_url, format='json') | ||
self.assertEqual(response.data['provider'], 'email') | ||
|
||
def test_using_unexisting_article_slug(self): | ||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) | ||
url = reverse('articleSpecific', kwargs={'slug': 'life-love-death'}) | ||
self.client.post(url, self.data, format='json') | ||
link_url = reverse( | ||
'share_article', | ||
kwargs={ | ||
'slug': 'life-love', | ||
'provider': 'email'}) | ||
response = self.client.get(link_url, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
self.assertEqual(response.data['message'], 'Article does not exist') | ||
|
||
def test_using_invalid_provider(self): | ||
|
||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) | ||
url = reverse('articleSpecific', kwargs={'slug': 'life-love-death'}) | ||
self.client.post(url, self.data, format='json') | ||
link_url = reverse( | ||
'share_article', | ||
kwargs={ | ||
'slug': 'life-love-death', | ||
'provider': 'reddit'}) | ||
response = self.client.get(link_url, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertEqual(response.data['error'], 'provider link is invalid') | ||
|
||
def all_setup(self): | ||
|
||
self.slug = "life-love-death" | ||
self.title = "Life Love and Death" | ||
self.description = "What is life?" | ||
self.body = "This is the real life body." | ||
self.tagList = "life,love,death" | ||
self.favorited = True | ||
self.favoritesCount = 4 | ||
self.author = 'TestAuthor' | ||
|
||
self.article = Articles( | ||
slug=self.slug, | ||
title=self.title, | ||
description=self.description, | ||
body=self.body, | ||
tagList=self.tagList, | ||
favorited=self.favorited, | ||
favoritesCount=self.favoritesCount, | ||
author=User.objects.get(username=self.author)) | ||
|
||
self.article.save() | ||
|
||
def get_user_token(self): | ||
user = { | ||
"user": { | ||
"username": "TestAuthor", | ||
"email": "test_user@email.com", | ||
"password": "test123user#Password" | ||
} | ||
} | ||
|
||
response = self.client.post( | ||
reverse('register'), data=user, format='json') | ||
user = get_user_model() | ||
user = user.objects.get(username="TestAuthor") | ||
user.is_active = True | ||
user.save() | ||
response.render() | ||
data = response.content | ||
token = json.loads(data.decode('utf-8'))['user']['token'] | ||
return token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.urls import path | ||
from .views import ShareArticleView | ||
|
||
|
||
urlpatterns = [ | ||
path('articles/<str:slug>/share/<str:provider>', | ||
ShareArticleView.as_view(), | ||
name='share_article'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from django.urls import reverse | ||
from django_social_share.templatetags import social_share | ||
|
||
from rest_framework import status | ||
from rest_framework.permissions import IsAuthenticated | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
|
||
from authors.apps.articles.models import Articles | ||
from authors.apps.authentication.models import User | ||
|
||
|
||
class ShareArticleView(APIView): | ||
permission_classes = (IsAuthenticated,) | ||
|
||
def get(self, request, *args, **kwargs): | ||
|
||
provider = kwargs['provider'] | ||
example_providers = ['facebook', 'email', 'twitter'] | ||
|
||
if provider not in example_providers: | ||
return Response( | ||
{ | ||
'error': 'provider link is invalid', | ||
}, status.HTTP_400_BAD_REQUEST) | ||
|
||
try: | ||
article = Articles.objects.get(slug=kwargs['slug']) | ||
|
||
except Exception as error: | ||
print('Received error is : {}'.format(error)) | ||
return Response({"message": "Article does not exist"}, | ||
status.HTTP_404_NOT_FOUND) | ||
|
||
article_link = request.build_absolute_uri( | ||
reverse('articleSpecific', kwargs={'slug': article.slug}) | ||
) | ||
|
||
context = {'request': request} | ||
text = "Daily Digest from Authors Haven" | ||
user = User.objects.get(id=article.author_id).username | ||
subject = "Read: {} by {} from Authors Haven".format( | ||
article.title, | ||
user) | ||
if provider == 'email': | ||
link = social_share.send_email_url( | ||
context, | ||
subject, | ||
text, | ||
article_link)['mailto_url'] | ||
return Response( | ||
{'link': link, 'provider': provider}, | ||
status=status.HTTP_200_OK) | ||
return Response( | ||
{ | ||
'link': self.set_link( | ||
context, | ||
provider, | ||
article_link, args), | ||
'provider': provider}, status.HTTP_200_OK) | ||
|
||
def set_link(self, context, provider, article_link, *args): | ||
providers = { | ||
"twitter": [social_share.post_to_twitter_url, "tweet_url"], | ||
"facebook": [social_share.post_to_facebook_url, "facebook_url"] | ||
} | ||
provider_link = providers.get(provider, providers['facebook']) | ||
return provider_link[0](context, article_link)[provider_link[1]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters