Skip to content

Commit

Permalink
Merge f50b8f5 into 7c90eb8
Browse files Browse the repository at this point in the history
  • Loading branch information
mulondo committed Dec 18, 2018
2 parents 7c90eb8 + f50b8f5 commit ad64795
Show file tree
Hide file tree
Showing 14 changed files with 278 additions and 22 deletions.
1 change: 1 addition & 0 deletions authors/apps/articles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from authors.apps.profiles.models import UserProfile
from authors.apps.core.models import TimestampedModel
from django.contrib.postgres.fields import ArrayField
from ..profiles.models import UserProfile


class Article(TimestampedModel):
Expand Down
12 changes: 9 additions & 3 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
from rest_framework.response import Response
from rest_framework.exceptions import NotFound
from rest_framework.generics import (
RetrieveUpdateDestroyAPIView, ListCreateAPIView
)
RetrieveUpdateDestroyAPIView,RetrieveUpdateAPIView,
ListCreateAPIView)
from .models import Article
from authors.apps.profiles.models import UserProfile
from rest_framework.generics import (
RetrieveUpdateDestroyAPIView,RetrieveUpdateAPIView,
ListCreateAPIView)
from rest_framework.views import APIView
from .models import Article, Rating

from .renderers import ArticleJSONRenderer
from .serializers import ArticleSerializer, RatingSerializer
from .pagination import PageNumbering
Expand Down Expand Up @@ -70,7 +76,7 @@ def update(self, request, slug):
def destroy(self, request, slug):
try:
serializer_instance = self.queryset.get(slug=slug)
except Article.DoesNotExist:
except Article.DoesNotExist:
raise NotFound('An article with this slug does not exist.')

return Response(
Expand Down
2 changes: 2 additions & 0 deletions authors/apps/authentication/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,6 @@ def update(self, instance, validated_data):

class PasswordSerializer(serializers.Serializer):
new_password = serializers.CharField(max_length=255, required=True)



82 changes: 82 additions & 0 deletions authors/apps/authentication/tests/base_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from django.urls import path, reverse
from rest_framework import status
from rest_framework.test import APITestCase, APIRequestFactory, APIClient
from authors.apps.authentication.views import RegistrationAPIView, AccountVerified
from authors.apps.authentication.models import User
from authors.apps.authentication.backends import JWTAuthentication


class BaseTest(APITestCase):

def setUp(self):
self.slug = 0
self.user_data = {
"user": {
"username": "minime",
"email": "alexkayabula@gmail.com",
"password": "W123456/78"}}
self.url = reverse("registration")
self.client = APIClient()
self.client.post(self.url, self.user_data, format='json')
self.request = APIRequestFactory().post(
reverse("registration")
)

self.new_article = {
"article": {
"title": "How to fight dragons 8",
"description": "Ever wonder jckvlahvhow?",
"body": "You have kenglto believe"
}
}

self.update_article = {
"article": {
"title": "How to fight dragons 8",
"description": "Ever wonder jckvlahvhow?",
"body": "You have kenglto believe"
}
}

self.article_without_title = {
"article": {
"description": "Ever wonder jckvlahvhow?",
"body": "You have kenglto believe"
}
}

user = User.objects.get()
request = self.request
token, uid = RegistrationAPIView.generate_token(user, request)
response = self.account_verification(token, uid)
self.assertEqual(response.status_code, status.HTTP_200_OK)
user = User.objects.get()
self.assertTrue(user.is_verified)

response = self.client.post(
'/api/users/login/',
self.user_data,
format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.login_token = response.data['token']
self.client.credentials(
HTTP_AUTHORIZATION='Bearer ' +
self.login_token)

def account_verification(self, token, uid):
request = APIRequestFactory().get(
reverse("verify_account", kwargs={"token": token, "uid": uid})
)
verify = AccountVerified.as_view()
response = verify(request, token=token, uid=uid)
return response

def get_slug(self, response=[]):
self.client.post(
'/api/articles/',
data=self.new_article,
format='json')
response = self.client.get('/api/articles/', format='json')
for i in response.data:
self.slug = i['slug']
return self.slug
30 changes: 30 additions & 0 deletions authors/apps/authentication/tests/test_reset_password.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from .base_test import BaseTest
from rest_framework import status


class TestRestPassWord(BaseTest):

def test_reset_password(self):
usr = {
"user": {
"username": "minime",
"email": "alexkayabula@gmail.com",
"password": "W123456/78"}}
self.client.post('/api/users/password_reset/',data = usr,format = 'json')
reset_data = {
"user": {
"username": "minime",
"email": "alexkayabula@gmail.com",
"password": "Whbdc^734"}}
response = self.client.post('/api/users/password_reset/',data = reset_data ,format = 'json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

def test_reset_password_with_missing_email(self):
usr = {
"user": {
"username": "minime",
"email": "",
"password": "W123456/78"}}
response = self.client.post('/api/users/password_reset/',data = usr,format = 'json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

4 changes: 2 additions & 2 deletions authors/apps/authentication/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import status
from rest_framework.generics import RetrieveUpdateAPIView
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.permissions import (AllowAny,IsAuthenticated,IsAuthenticatedOrReadOnly)
from rest_framework.response import Response
from rest_framework.views import APIView
from .validations import validate_registration
Expand All @@ -16,12 +16,12 @@
from django.contrib.sites.shortcuts import get_current_site
from rest_framework.generics import GenericAPIView
from rest_framework.reverse import reverse

from .renderers import UserJSONRenderer
from .serializers import (
LoginSerializer, RegistrationSerializer, UserSerializer, PasswordSerializer
)
from .models import User
from authors.apps.articles.models import Article
from django.http import JsonResponse


Expand Down
10 changes: 5 additions & 5 deletions authors/apps/profiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
from django.dispatch import receiver
from django.db.models.signals import post_save
from authors.apps.authentication.models import User
from django.contrib.postgres.fields import ArrayField


class UserProfile(models.Model):
photo = models.URLField(blank=True)
user = models.OneToOneField(User,on_delete= models.CASCADE)
bio = models.TextField(blank=True)
fun_fact = models.TextField(blank=True)

time_when_updated = models.DateTimeField(auto_now=True)

def __str__(self):
return self.user.username

favorite_article = ArrayField(models.CharField(max_length=100), default=list)

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
user_profile=UserProfile(user=instance)
return user_profile.save()

6 changes: 6 additions & 0 deletions authors/apps/profiles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ class UpdateProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = ['photo','bio','fun_fact']

class FavoriteArticleSerializer(serializers.Serializer):

class Meta:
model = UserProfile
fields = ['favorite_article']
Empty file.
74 changes: 74 additions & 0 deletions authors/apps/profiles/tests/base_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from django.urls import path, reverse
from rest_framework import status
from rest_framework.test import APITestCase, APIRequestFactory, APIClient
from authors.apps.authentication.views import RegistrationAPIView, AccountVerified
from authors.apps.authentication.models import User
from authors.apps.authentication.backends import JWTAuthentication


class BaseTest(APITestCase):

def setUp(self):
self.slug = 0
self.user_data = {
"user": {
"username": "minime",
"email": "alexkayabula@gmail.com",
"password": "W123456/78"}}
self.url = reverse("registration")
self.client = APIClient()
self.client.post(self.url, self.user_data, format='json')
self.request = APIRequestFactory().post(
reverse("registration")
)

self.new_article = {
"article": {
"title": "How to fight dragons 8",
"description": "Ever wonder jckvlahvhow?",
"body": "You have kenglto believe"
}
}

self.article_without_title = {
"article": {
"description": "Ever wonder jckvlahvhow?",
"body": "You have kenglto believe"
}
}

user = User.objects.get()
request = self.request
token, uid = RegistrationAPIView.generate_token(user, request)
response = self.account_verification(token, uid)
self.assertEqual(response.status_code, status.HTTP_200_OK)
user = User.objects.get()
self.assertTrue(user.is_verified)

response = self.client.post(
'/api/users/login/',
self.user_data,
format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.login_token = response.data['token']
self.client.credentials(
HTTP_AUTHORIZATION='Bearer ' +
self.login_token)

def account_verification(self, token, uid):
request = APIRequestFactory().get(
reverse("verify_account", kwargs={"token": token, "uid": uid})
)
verify = AccountVerified.as_view()
response = verify(request, token=token, uid=uid)
return response

def get_slug(self, response=[]):
self.client.post(
'/api/articles/',
data=self.new_article,
format='json')
response = self.client.get('/api/articles/', format='json')
for i in response.data['results']:
self.slug = i['slug']
return self.slug
28 changes: 28 additions & 0 deletions authors/apps/profiles/tests/test_favorite_article.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from .base_test import BaseTest
from rest_framework import status

class TestfavoriteArticle(BaseTest):

def test_favorite_article(self):
article = self.client.post('/api/articles/', data = self.new_article,format='json')
slug = self.get_slug( article)
response = self.client.put('/api/users/article/favorite/{}'.format(slug))
self.assertEqual(response.status_code,status.HTTP_200_OK)

def test_favorite_article_which_doesnot_exist(self):
response = self.client.put('/api/users/article/favorite/best-43hbc',format='json')
self.assertEqual(response.status_code,status.HTTP_404_NOT_FOUND)

def test_unfavorite_article(self):
article = self.client.post('/api/articles/', data = self.new_article,format='json')
slug = self.get_slug( article)
self.client.put('/api/users/article/favorite/{}'.format(slug))
response = self.client.put('/api/users/article/favorite/{}'.format(slug))
self.assertEqual(response.status_code,status.HTTP_200_OK)

def test_unfavorite_article_which_doesnot_exist(self):
article = self.client.post('/api/articles/', data = self.new_article,format='json')
slug = self.get_slug( article)
self.client.put('/api/users/article/favorite/{}'.format(slug))
response = self.client.put('/api/users/article/favorite/man-1628t492')
self.assertEqual(response.status_code,status.HTTP_404_NOT_FOUND)
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from rest_framework import status
from django.urls import path, reverse
from rest_framework.test import APITestCase, APIRequestFactory, APIClient
from ..authentication.views import RegistrationAPIView, AccountVerified
from ..authentication.models import User
from ..authentication.backends import JWTAuthentication
from authors.apps.authentication.views import RegistrationAPIView, AccountVerified
from authors.apps.authentication.models import User
from authors.apps.authentication.backends import JWTAuthentication


class TestUserProfile(APITestCase):
Expand Down Expand Up @@ -55,4 +55,4 @@ def test_update_profile(self):
}

response = self.client.put('/api/users/profiles/', update_data, format ='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.status_code, status.HTTP_200_OK)
5 changes: 3 additions & 2 deletions authors/apps/profiles/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.urls import path

from .views import UserProfiles,Updateprofile
from .views import UserProfiles,Updateprofile,FavoriteArticle

urlpatterns = [
path('users/profiles', UserProfiles.as_view()),
path('users/profiles/', Updateprofile.as_view(),name="profile")
path('users/profiles/', Updateprofile.as_view(),name="profile"),
path('users/article/favorite/<slug>',FavoriteArticle.as_view())

]
Loading

0 comments on commit ad64795

Please sign in to comment.