-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Ensure that user profile is created for a user at point of registration - Ensure users of the application can view user profiles - Ensure that only the owner of the profile can edit it [Finishes #164069222]
- Loading branch information
wasibani roy
committed
Mar 11, 2019
1 parent
8bbf8f9
commit 9455809
Showing
24 changed files
with
549 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
web: gunicorn authors.wsgi | ||
release: python manage.py migrate --noinput |
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
45 changes: 45 additions & 0 deletions
45
authors/apps/authentication/tests/test_registration_validation.py
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,45 @@ | ||
import json | ||
import os | ||
|
||
from django.test import TestCase | ||
from django.urls import reverse | ||
|
||
from .test_base import BaseTest | ||
from .test_data import (valid_user, empty_username, | ||
invalid_user_email, short_password, missing_username_key, | ||
invalid_username, invalid_password, empty_email, empty_password, | ||
expired_token, invalid_token, | ||
missing_username_key, responses, same_email, | ||
same_username) | ||
|
||
class RegistrationValidationTest(BaseTest): | ||
|
||
def test_invalid_short_password(self): | ||
|
||
expected_response = responses['password_is_too_short'] | ||
resp = self.client.post( | ||
self.registration_url, short_password, format='json') | ||
self.assertDictEqual(resp.data, expected_response) | ||
|
||
def test_signup_existing_email(self): | ||
|
||
expected_response = responses['email_already_exists'] | ||
self.client.post( | ||
self.registration_url, valid_user, format='json') | ||
resp = self.client.post( | ||
self.registration_url, same_email, format='json') | ||
self.assertDictEqual(resp.data, expected_response) | ||
|
||
def test_signup_existing_username(self): | ||
expected_response = responses['username_already_exists'] | ||
self.client.post( | ||
self.registration_url, valid_user, format='json') | ||
resp = self.client.post( | ||
self.registration_url, same_username, format='json') | ||
self.assertDictEqual(resp.data, expected_response) | ||
|
||
def test_signup_invalid_email(self): | ||
expected_response = responses['invalid_email'] | ||
resp = self.client.post( | ||
self.registration_url, invalid_user_email, format='json') | ||
self.assertDictEqual(resp.data, expected_response) |
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,56 @@ | ||
import json | ||
import os | ||
|
||
from django.test import TestCase | ||
from django.urls import reverse | ||
|
||
from ..models import User | ||
|
||
|
||
class TestUserModel(TestCase): | ||
def setUp(self): | ||
self.username = 'sample12' | ||
self.email = 'sample@gmail.com' | ||
self.password = 'password' | ||
|
||
def test_user_creation(self): | ||
response = User.objects.create_user(username=self.username, | ||
email=self.email, | ||
password=self.password) | ||
self.assertEqual(response.get_full_name, 'sample12') | ||
|
||
def test_super_user_creation(self): | ||
response = User.objects.create_superuser(username=self.username, | ||
email=self.email, | ||
password=self.password) | ||
self.assertEqual(response.get_full_name, 'sample12') | ||
|
||
def test_super_user_missing_password(self): | ||
with self.assertRaises(TypeError): | ||
response = User.objects.create_superuser(username=self.username, | ||
email=self.email, | ||
password=None) | ||
|
||
def test_missing_email(self): | ||
with self.assertRaises(TypeError): | ||
response = User.objects.create_user(username=self.username, | ||
email=None, | ||
password=self.password) | ||
|
||
def test_missing_username(self): | ||
with self.assertRaises(TypeError): | ||
response = User.objects.create_user(username=None, | ||
email=self.email, | ||
password=self.password) | ||
|
||
def test_return_short_name(self): | ||
self.response = User.objects.create_user(username=self.username, | ||
email=self.email, | ||
password=self.password) | ||
self.assertEqual(self.response.get_short_name(), 'sample12') | ||
|
||
def test_str_return(self): | ||
self.response = User.objects.create_user(username=self.username, | ||
email=self.email, | ||
password=self.password) | ||
self.assertEqual(self.response.__str__(), 'sample@gmail.com') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import re | ||
|
||
from django.shortcuts import get_object_or_404 | ||
|
||
from rest_framework import serializers | ||
|
||
from authors.apps.authentication.models import User | ||
|
||
def validate_email(email): | ||
check_email = User.objects.filter(email=email) | ||
if not re.search(r'^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$', email): | ||
raise serializers.ValidationError("Incorrect email format please try again") | ||
if check_email.exists(): | ||
raise serializers.ValidationError("This email has already been used to create a user") | ||
return email |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
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,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ProfilesConfig(AppConfig): | ||
name = 'profiles' |
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,31 @@ | ||
# Generated by Django 2.1.5 on 2019-03-06 06:58 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Profile', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('display_name', models.CharField(blank=True, max_length=120, null=True)), | ||
('bio', models.TextField(blank=True, null=True)), | ||
('image', models.URLField(null=True)), | ||
('following', models.BooleanField(default=False)), | ||
('created_at', models.DateTimeField(auto_now_add=True)), | ||
('updated_at', models.DateTimeField(auto_now=True)), | ||
('followers', models.ManyToManyField(blank=True, related_name='is_following', to=settings.AUTH_USER_MODEL)), | ||
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
], | ||
), | ||
] |
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,40 @@ | ||
from django.db import models | ||
from django.db.models.signals import post_save | ||
from authors.apps.authentication.models import User | ||
|
||
|
||
class Profile(models.Model): | ||
""" | ||
Creates the profile model that will hold user profiles | ||
""" | ||
|
||
user = models.OneToOneField(User, on_delete=models.CASCADE) | ||
display_name = models.CharField(max_length=120, blank=True, null=True) | ||
followers = models.ManyToManyField( | ||
User, related_name='is_following', blank=True) | ||
bio = models.TextField(blank=True, null=True) | ||
image = models.URLField(null=True) | ||
following = models.BooleanField(default=False) | ||
created_at = models.DateTimeField(auto_now_add=True) | ||
updated_at = models.DateTimeField(auto_now=True) | ||
|
||
def __str__(self): | ||
return f"{self.user.username}" | ||
|
||
@property | ||
def owner(self): | ||
return self.user | ||
|
||
|
||
def user_post_save_receiver(instance, created, *args, **kwargs): | ||
""" | ||
Handle creating the profile when a user finishes | ||
the signup process | ||
""" | ||
if created: | ||
Profile.objects.get_or_create( | ||
user=instance, | ||
) | ||
|
||
|
||
post_save.connect(user_post_save_receiver, sender=User) |
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,11 @@ | ||
from rest_framework.permissions import BasePermission, SAFE_METHODS | ||
|
||
|
||
class IsOwnerOrReadOnly(BasePermission): | ||
message = 'You are not the owner of this profile.' | ||
|
||
def has_object_permission(self, request, view, obj): | ||
|
||
if request.method in SAFE_METHODS: | ||
return True | ||
return obj.user == request.user |
Oops, something went wrong.