for #79: more DeletedAddress testing

This commit is contained in:
groovecoder 2020-04-05 13:38:27 -05:00
Родитель 95d1956e29
Коммит 1be1754251
5 изменённых файлов: 81 добавлений и 9 удалений

Просмотреть файл

@ -1,9 +1,9 @@
from django.contrib import admin
from .models import RelayAddress, Profile
from .models import DeletedAddress, Profile, RelayAddress
class RelayAddressAdmin(admin.ModelAdmin):
class DeletedAddressAdmin(admin.ModelAdmin):
pass
@ -11,5 +11,10 @@ class ProfileAdmin(admin.ModelAdmin):
pass
admin.site.register(RelayAddress, RelayAddressAdmin)
class RelayAddressAdmin(admin.ModelAdmin):
pass
admin.site.register(DeletedAddress, DeletedAddressAdmin)
admin.site.register(Profile, ProfileAdmin)
admin.site.register(RelayAddress, RelayAddressAdmin)

Просмотреть файл

@ -0,0 +1,18 @@
# Generated by Django 2.2.10 on 2020-04-05 17:39
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('emails', '0008_deletedaddress'),
]
operations = [
migrations.AlterField(
model_name='deletedaddress',
name='address_hash',
field=models.CharField(db_index=True, max_length=64),
),
]

Просмотреть файл

@ -19,6 +19,10 @@ def address_default():
return ''.join(random.choices(string.ascii_lowercase + string.digits, k=9))
class CannotMakeAddressException(Exception):
pass
class RelayAddress(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
address = models.CharField(
@ -35,7 +39,16 @@ class RelayAddress(models.Model):
def __str__(self):
return self.address
def make_relay_address(user):
def delete(self, *args, **kwargs):
deleted_address = DeletedAddress.objects.create(
address_hash=sha256(self.address.encode('utf-8')).hexdigest()
)
deleted_address.save()
return super(RelayAddress, self).delete(*args, **kwargs)
def make_relay_address(user, num_tries=0):
if num_tries >= 5:
raise CannotMakeAddressException
relay_address = RelayAddress.objects.create(user=user)
address_hash = sha256(relay_address.address.encode('utf-8')).hexdigest()
address_already_deleted = DeletedAddress.objects.filter(
@ -43,9 +56,13 @@ class RelayAddress(models.Model):
).count()
if address_already_deleted > 0:
relay_address.delete()
return RelayAddress.make_relay_address(user)
num_tries += 1
return RelayAddress.make_relay_address(user, num_tries)
return relay_address
class DeletedAddress(models.Model):
address_hash = models.CharField(max_length=64)
address_hash = models.CharField(max_length=64, db_index=True)
def __str__(self):
return self.address_hash

Просмотреть файл

@ -1,15 +1,47 @@
from hashlib import sha256
from unittest.mock import patch
from django.contrib.auth.models import User
from django.test import TestCase
from model_bakery import baker
from ..models import RelayAddress
from ..models import CannotMakeAddressException, DeletedAddress, RelayAddress
class RelayAddressTest(TestCase):
def setUp(self):
self.user = baker.make(User)
def test_make_relay_address(self):
def test_make_relay_address_assigns_to_user(self):
relay_address = RelayAddress.make_relay_address(self.user)
assert relay_address.user == self.user
# TODO: FIXME? this is dumb
def test_make_relay_address_makes_different_addresses(self):
relay_addresses = []
for i in range(1000):
relay_addresses.append(RelayAddress.make_relay_address(self.user))
assert len(relay_addresses) == len(set(relay_addresses))
def test_delete_adds_deleted_address_object(self):
relay_address = baker.make(RelayAddress)
address_hash = sha256(
relay_address.address.encode('utf-8')
).hexdigest()
relay_address.delete()
deleted_count = DeletedAddress.objects.filter(
address_hash=address_hash
).count()
assert deleted_count == 1
# trigger a collision by making address_default always return 'aaaaaaaaa'
@patch.multiple('string', ascii_lowercase='a', digits='')
def test_make_relay_address_doesnt_make_dupe_of_deleted(self):
test_hash = sha256('aaaaaaaaa'.encode('utf-8')).hexdigest()
deleted_address = DeletedAddress.objects.create(address_hash=test_hash)
try:
RelayAddress.make_relay_address(self.user)
except CannotMakeAddressException:
return
self.fail("Should have raise CannotMakeAddressException")

Просмотреть файл

@ -59,7 +59,7 @@ def _index_POST(request):
)
return redirect('profile')
relay_address = RelayAddress.make_relay_address(user)
relay_address = RelayAddress.make_relay_address(user_profile.user)
return_string = '%s@%s' % (
relay_address.address, relay_from_domain(request)['RELAY_DOMAIN']
)