Skip to content

Commit

Permalink
add endpoint for checking subdomain availability
Browse files Browse the repository at this point in the history
  • Loading branch information
groovecoder committed Aug 23, 2021
1 parent 2cd345b commit fd4e305
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
10 changes: 8 additions & 2 deletions emails/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class Profile(models.Model):
def __str__(self):
return '%s Profile' % self.user

@staticmethod
def subdomain_available(subdomain):
bad_word = has_bad_words(subdomain)
taken = len(Profile.objects.filter(subdomain=subdomain)) > 0
return not bad_word and not taken

@property
def num_active_address(self):
return RelayAddress.objects.filter(user=self.user).count()
Expand Down Expand Up @@ -137,8 +143,8 @@ def add_subdomain(self, subdomain):
raise CannotMakeSubdomainException(NOT_PREMIUM_USER_ERR_MSG.format('set a subdomain'))
if self.subdomain is not None:
raise CannotMakeSubdomainException('You cannot change your subdomain.')
subdomain_exists = Profile.objects.filter(subdomain=subdomain)
if not subdomain or has_bad_words(subdomain) or subdomain_exists:
subdomain_available = Profile.subdomain_available(subdomain)
if not subdomain or not subdomain_available:
raise CannotMakeSubdomainException(TRY_DIFFERENT_VALUE_ERR_MSG.format('Subdomain'))
self.subdomain = subdomain
self.save()
Expand Down
18 changes: 18 additions & 0 deletions emails/tests/models_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,24 @@ def test_add_subdomain_to_unlimited_profile_with_badword_subdomain_raises_except
return
self.fail("Should have raised CannotMakeSubdomainException")

def test_subdomain_available_bad_word_returns_False(self):
assert Profile.subdomain_available('angry') == False

def test_subdomain_available_taken_returns_False(self):
premium_user = baker.make(User)
random_sub = random.choice(
settings.SUBSCRIPTIONS_WITH_UNLIMITED.split(',')
)
baker.make(
SocialAccount,
user=premium_user,
provider='fxa',
extra_data={'subscriptions': [random_sub]}
)
premium_profile = Profile.objects.get(user=premium_user)
premium_profile.add_subdomain('thisisfine')
assert Profile.subdomain_available('thisisfine') == False

def test_display_name_exists(self):
display_name = 'Display Name'
social_account = baker.make(
Expand Down
17 changes: 10 additions & 7 deletions privaterelay/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
from django.apps import apps
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.db import connections
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect
Expand All @@ -32,10 +29,10 @@
from emails.models import (
CannotMakeSubdomainException,
DomainAddress,
has_bad_words,
RelayAddress
Profile,
RelayAddress,
NOT_PREMIUM_USER_ERR_MSG
)
from emails.utils import get_post_data_from_request

FXA_PROFILE_CHANGE_EVENT = (
'https://schemas.accounts.firefox.com/event/profile-change'
Expand Down Expand Up @@ -104,11 +101,17 @@ def _get_fxa(request):
return request.user.socialaccount_set.filter(provider='fxa').first()


@require_http_methods(["POST"])
@require_http_methods(['POST', 'GET'])
def profile_subdomain(request):
if (not request.user or request.user.is_anonymous):
return redirect(reverse('fxa_login'))
profile = request.user.profile_set.first()
if not profile.has_unlimited:
raise CannotMakeSubdomainException(NOT_PREMIUM_USER_ERR_MSG.format('check a subdomain'))
if request.method == 'GET':
subdomain = request.GET.get('subdomain', None)
available = Profile.subdomain_available(subdomain)
return JsonResponse({'available':available})
try:
profile.add_subdomain(request.POST.get('subdomain', None))
except CannotMakeSubdomainException as e:
Expand Down

0 comments on commit fd4e305

Please sign in to comment.