Skip to content

Commit

Permalink
(Feature #162482222):
Browse files Browse the repository at this point in the history
- add endpoint to retrieve user articles
- add tests for retrieving user articles
[Feature #162482222]
  • Loading branch information
Njeri Ngigi authored and Njeri Ngigi committed Dec 6, 2018
1 parent 5530ed1 commit 93a2a0b
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 76 deletions.
15 changes: 9 additions & 6 deletions authors/apps/articles/tests/test_articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ def test_get_all_articles(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertGreaterEqual(len(json.loads(response.content)), 0)

def test_get_user_articles(self):
'''test get a user's articles'''
response = self.client.get('/api/profiles/test_user/articles')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertGreaterEqual(len(json.loads(response.content)), 0)

def test_get_single_article(self):
'''test get single article'''
# test successful get
Expand Down Expand Up @@ -225,7 +231,6 @@ def test_time_to_read(self):
serializer = ArticleSerializer()
self.assertEqual(serializer.get_time_to_read(text, images), 2)


def test_if_article_returns_share_links(self):
"""This method tests whether the API returns share links"""
res = self.client.get(self.TESTARTICLE)
Expand All @@ -235,7 +240,6 @@ def test_if_article_returns_share_links(self):
self.assertIn("mail", json.dumps(res.data))
self.assertIn("url", json.dumps(res.data))


def test_get_tags(self):
"""Tests whether we can get all article tags"""
response = self.client.post(
Expand All @@ -253,7 +257,7 @@ def test_get_tags(self):
response = self.get_article_tags()
self.assertEquals(response.status_code, status.HTTP_200_OK)
expected_tag = response.data['tags'][0]
self.assertEqual(expected_tag,'TDD')
self.assertEqual(expected_tag, 'TDD')

def test_article_payload_tag(self):
"""Tests whether article payload has tags upon creating"""
Expand All @@ -271,6 +275,5 @@ def test_article_payload_tag(self):
response = self.create_article(self.test_article_data, token)
self.assertTrue(response.data['tagList'])
self.assertEquals(response.status_code, status.HTTP_201_CREATED)
tag_in_payload =response.data['tagList'][0]
self.assertEqual(tag_in_payload,'TDD')

tag_in_payload = response.data['tagList'][0]
self.assertEqual(tag_in_payload, 'TDD')
135 changes: 86 additions & 49 deletions authors/apps/authentication/tests/test_reset_password.py
Original file line number Diff line number Diff line change
@@ -1,105 +1,142 @@
'''tests/test_reset_password.py'''
from .base import *


class ResetPasswordTestCase(BaseTest):
''' Class to test resetting password'''

def test_forget_password(self):
'''Test successful user forget password'''
response = self.client.post(self.FORGOT_URL, {"email": "njery.ngigi@gmail.com"}, format="json")
response = self.client.post(
self.FORGOT_URL, {"email": "njery.ngigi@gmail.com"}, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(json.loads(response.content)["message"],
"Reset link has been successfully sent to your email. Check your spam folder if you don't find it.")
"Reset link has been successfully sent to your email. Check your spam folder if you don't find it.")

def test_missing_email(self):
'''Test missing email'''
response = self.client.post(self.FORGOT_URL, {"email": ""}, format="json")
response = self.client.post(
self.FORGOT_URL, {"email": ""}, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(json.loads(response.content)["email"][0], "This field may not be blank.")

self.assertEqual(json.loads(response.content)[
"email"][0], "This field may not be blank.")

def test_invalid_email(self):
'''Test invalid email'''
response = self.client.post(self.FORGOT_URL, {"email": "qw"}, format="json")
response = self.client.post(
self.FORGOT_URL, {"email": "qw"}, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_unregistered_user(self):
'''Test unregister user'''
response = self.client.post(self.FORGOT_URL, {"email": "testing214@co.ke"}, format="json")
response = self.client.post(
self.FORGOT_URL, {"email": "testing214@co.ke"}, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(json.loads(response.content)["error"][0], "A user with this email was not found.")
self.assertEqual(json.loads(response.content)[
"error"][0], "A user with this email was not found.")

def test_reset_password(self):
def test_reset_password(self):
'''Test all cases of user reset password'''
response = self.client.post(self.FORGOT_URL, {"email": "njery.ngigi@gmail.com"}, format="json")
response = self.client.post(
self.FORGOT_URL, {"email": "njery.ngigi@gmail.com"}, format="json")
token = json.loads(response.content)["token"]
RESET_URL = '/api/users/reset-password/{}/'.format(token)

# Test very long password
very_long_password = "qwertyuiopasdfghjklzxcvbnmhgfdsaewrfjcjgfxhgvghefnldvb7349ijd848jnjnjvbgfydfui43ufnlefknkdgifefldhfgdkfdngdgfif78rejleh7hdkfjdgjdgjfkdfhriehrnerbrgrriugyoiotgkmlgn842n3kjbjkfgig"
response2 = self.client.post(RESET_URL, {"new_password": very_long_password,
very_long_password = "qwertyuiopasdfghjklzxcvbnmhgfdsaewrfjcjgfxhgvghe\
fnldvb7349ijd848jnjnjvbgfydfui43ufnlefknkdgifefldhfgdkfdngdgfif\
78rejleh7hdkfjdgjdgjfkdfhriehrnerbrgrriugyoiotgkmlgn842n3kj\
bjkfgig"
response2 = self.client.post(RESET_URL, {"new_password": very_long_password,
"confirm_password": very_long_password}, format="json")
self.assertEqual(response2.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(json.loads(response2.content)["new_password"][0], "Password cannot be more than 128 characters")

self.assertEqual(json.loads(response2.content)[
"new_password"][0], "Password cannot be more than 128 characters")

# Test missing fields
response3 = self.client.post(RESET_URL, {}, format="json")
self.assertEqual(response3.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(json.loads(response3.content)["new_password"][0], "This field is required.")
self.assertEqual(json.loads(response3.content)["confirm_password"][0], "This field is required.")

self.assertEqual(json.loads(response3.content)[
"new_password"][0], "This field is required.")
self.assertEqual(json.loads(response3.content)[
"confirm_password"][0], "This field is required.")

# Test missing new_password field
response4 = self.client.post(RESET_URL, {"confirm_password": "secret123"}, format="json")
response4 = self.client.post(
RESET_URL, {"confirm_password": "secret123"}, format="json")
self.assertEqual(response4.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(json.loads(response4.content)["new_password"][0], "This field is required.")

self.assertEqual(json.loads(response4.content)[
"new_password"][0], "This field is required.")

# Test missing confirm_pasword field
response5 = self.client.post(RESET_URL, {"new_password": "secret123"}, format="json")
response5 = self.client.post(
RESET_URL, {"new_password": "secret123"}, format="json")
self.assertEqual(response5.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(json.loads(response5.content)["confirm_password"][0], "This field is required.")

self.assertEqual(json.loads(response5.content)[
"confirm_password"][0], "This field is required.")

# Test blank Fields
response6 = self.client.post(RESET_URL, {"new_password": "", "confirm_password": ""}, format="json")
response6 = self.client.post(
RESET_URL, {"new_password": "", "confirm_password": ""}, format="json")
self.assertEqual(response6.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(json.loads(response6.content)["new_password"][0], "This field may not be blank.")
self.assertEqual(json.loads(response6.content)["confirm_password"][0], "This field may not be blank.")

self.assertEqual(json.loads(response6.content)[
"new_password"][0], "This field may not be blank.")
self.assertEqual(json.loads(response6.content)[
"confirm_password"][0], "This field may not be blank.")

# Test whitespaces
response7 = self.client.post(RESET_URL, {"new_password": " ", "confirm_password": " "}, format="json")
response7 = self.client.post(
RESET_URL, {"new_password": " ", "confirm_password": " "}, format="json")
self.assertEqual(response7.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(json.loads(response7.content)["new_password"][0], "This field may not be blank.")
self.assertEqual(json.loads(response7.content)["confirm_password"][0], "This field may not be blank.")
self.assertEqual(json.loads(response7.content)[
"new_password"][0], "This field may not be blank.")
self.assertEqual(json.loads(response7.content)[
"confirm_password"][0], "This field may not be blank.")

# Test short password
response8 = self.client.post(RESET_URL, {"new_password": "mart12", "confirm_password": "mart12"}, format="json")
response8 = self.client.post(
RESET_URL, {"new_password": "mart12", "confirm_password": "mart12"}, format="json")
self.assertEqual(response8.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(json.loads(response8.content)["new_password"][0], "Password must contain at least 8 characters")

self.assertEqual(json.loads(response8.content)[
"new_password"][0], "Password must contain at least 8 characters")

# Test password with repeating characters
response9 = self.client.post(RESET_URL, {"new_password": "aaaaaa1111", "confirm_password": "aaaaaa1111"}, format="json")
response9 = self.client.post(RESET_URL, {
"new_password": "aaaaaa1111", "confirm_password": "aaaaaa1111"}, format="json")
self.assertEqual(response9.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(json.loads(response9.content)["new_password"][0], "Password must contain a number and a letter and that are not repeating more that two times")

self.assertEqual(json.loads(response9.content)[
"new_password"][0], "Password must contain a number and a letter and that are not repeating more that two times")

# Test non-alphanumeric password'''
response10 = self.client.post(RESET_URL, {"new_password": "qwertyuiop", "confirm_password": "qwertyuiop"}, format="json")
response10 = self.client.post(RESET_URL, {
"new_password": "qwertyuiop", "confirm_password": "qwertyuiop"}, format="json")
self.assertEqual(response10.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(json.loads(response10.content)["new_password"][0], "Password must contain a number and a letter and that are not repeating more that two times")
self.assertEqual(json.loads(response10.content)[
"new_password"][0], "Password must contain a number and a letter and that are not repeating more that two times")

response11 = self.client.post(RESET_URL, {"new_password": "1234567890", "confirm_password": "1234567890"}, format="json")
response11 = self.client.post(RESET_URL, {
"new_password": "1234567890", "confirm_password": "1234567890"}, format="json")
self.assertEqual(response11.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(json.loads(response11.content)["new_password"][0], "Password must contain a number and a letter and that are not repeating more that two times")

self.assertEqual(json.loads(response11.content)[
"new_password"][0], "Password must contain a number and a letter and that are not repeating more that two times")

# Test passwords not matching
response12 = self.client.post(RESET_URL, {"new_password": "secret123", "confirm_password": "mysecret123"}, format="json")
response12 = self.client.post(RESET_URL, {
"new_password": "secret123", "confirm_password": "mysecret123"}, format="json")
self.assertEqual(response12.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(json.loads(response12.content)["error"][0], "Passwords don't match.")
self.assertEqual(json.loads(response12.content)[
"error"][0], "Passwords don't match.")

# Test successful password reset
response13 = self.client.post(RESET_URL, {"new_password": "secret123", "confirm_password": "secret123"}, format="json")
response13 = self.client.post(RESET_URL, {
"new_password": "secret123", "confirm_password": "secret123"}, format="json")
self.assertEqual(response13.status_code, status.HTTP_200_OK)
self.assertEqual(json.loads(response13.content)["message"], "Congratulations! You have successfully changed your password.")

self.assertEqual(json.loads(response13.content)[
"message"], "Congratulations! You have successfully changed your password.")

# Test invalid token
response13 = self.client.post(RESET_URL, {"new_password": "secret123", "confirm_password": "secret123"}, format="json")
response13 = self.client.post(RESET_URL, {
"new_password": "secret123", "confirm_password": "secret123"}, format="json")
self.assertEqual(response13.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(json.loads(response13.content)["error"][0], "You either have an invalid token or the token has expired.")
self.assertEqual(json.loads(response13.content)[
"error"][0], "You either have an invalid token or the token has expired.")
3 changes: 2 additions & 1 deletion authors/apps/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ def post(self, request):

time = datetime.now()
time = datetime.strftime(time, '%d-%B-%Y %H:%M')
# current_site = get_current_site(request)
current_site1 = get_current_site(request)
print(current_site1)
# reset_link = 'http://' + current_site.domain + \
current_site = os.getenv('SITE_DOMAIN')
reset_link = 'http://' + current_site + \
Expand Down
15 changes: 8 additions & 7 deletions authors/apps/profiles/tests/test_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ def test_retrieve_non_existing_profile(self):

def test_update_profile(self):
data = {
"username": "mathias",
"surname": "code",
"last_name": "duty",
"avatar": "https://pbs.twimg.com/profile_images/670856248678596608/2yr7o6QQ_400x400.jpg",
"bio": "codeofdutycodeofdutycodeofduty"
}
response = self.client.put('/api/profiles/mathias', data, format="json")
"username": "mathias",
"surname": "code",
"last_name": "duty",
"avatar": "https://pbs.twimg.com/profile_images/670856248678596608/2yr7o6QQ_400x400.jpg",
"bio": "codeofdutycodeofdutycodeofduty"
}
response = self.client.put(
'/api/profiles/mathias', data, format="json")
self.assertIn("codeofdutycodeofdutycodeofduty", response.data['bio'])
self.assertEqual(response.status_code, status.HTTP_200_OK)
19 changes: 14 additions & 5 deletions authors/apps/profiles/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
from django.urls import path
from .views import ProfileRetrieveUpdateAPIView, ProfileList, ProfileFollowAPIView, FollowersAPIView, FollowingAPIView
from .views import (ProfileRetrieveUpdateAPIView, ProfileList,
ProfileFollowAPIView, FollowersAPIView, FollowingAPIView,
UserArticlesView,
)

app_name = 'profiles'

urlpatterns = [
path('profiles/', ProfileList.as_view(), name='profiles'),
path('profiles/<username>', ProfileRetrieveUpdateAPIView.as_view(), name='profile'),
path('profiles/<username>/follow', ProfileFollowAPIView.as_view(), name='follow'),
path('profiles/<username>/followers', FollowersAPIView.as_view(), name='followers'),
path('profiles/<username>/following', FollowingAPIView.as_view(), name='following'),
path('profiles/<username>',
ProfileRetrieveUpdateAPIView.as_view(), name='profile'),
path('profiles/<username>/follow',
ProfileFollowAPIView.as_view(), name='follow'),
path('profiles/<username>/followers',
FollowersAPIView.as_view(), name='followers'),
path('profiles/<username>/following',
FollowingAPIView.as_view(), name='following'),
path('profiles/<username>/articles',
UserArticlesView.as_view(), name='user_articles'),
]
Loading

0 comments on commit 93a2a0b

Please sign in to comment.