Skip to content

Commit

Permalink
Disabled restore on trial. Closes fgaudin#221.
Browse files Browse the repository at this point in the history
  • Loading branch information
fgaudin committed May 6, 2011
1 parent c6162c0 commit 596c51e
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 97 deletions.
4 changes: 2 additions & 2 deletions backup/fixtures/backup_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@
"model": "autoentrepreneur.subscription",
"fields": {
"expiration_date": "2011-06-06",
"state": 3,
"state": 2,
"error_message": null,
"transaction_id": "TRIAL-201147-4"
}
Expand All @@ -486,7 +486,7 @@
"model": "autoentrepreneur.subscription",
"fields": {
"expiration_date": "2011-06-06",
"state": 3,
"state": 2,
"error_message": null,
"transaction_id": "TRIAL-201147-5"
}
Expand Down
52 changes: 37 additions & 15 deletions backup/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from accounts.models import Invoice, INVOICE_STATE_PAID, PAYMENT_TYPE_CHECK, \
PAYMENT_TYPE_BANK_CARD
from contact.models import Contact, CONTACT_TYPE_COMPANY, Address
from autoentrepreneur.models import Subscription
from autoentrepreneur.models import Subscription, SUBSCRIPTION_STATE_TRIAL
from django.test.testcases import TransactionTestCase, TestCase
from core.models import OwnedObject
from django.core.exceptions import SuspiciousOperation
Expand Down Expand Up @@ -42,7 +42,7 @@ def tearDown(self):
if os.path.exists('%s_backup' % (settings.FILE_UPLOAD_DIR[:-1])):
shutil.move('%s_backup' % (settings.FILE_UPLOAD_DIR[:-1]), settings.FILE_UPLOAD_DIR[:-1])

def test_backup(self):
def testBackup(self):
response = self.client.get(reverse('backup'))
self.assertEquals(response.status_code, 200)

Expand All @@ -58,7 +58,7 @@ def test_backup(self):
self.user1.get_profile().uuid,
self.user1.backuprequest.creation_datetime.strftime('%Y%m%d%H%M'))))

def test_restore_add_missing(self):
def testRestoreAddMissing(self):
response = self.client.post(reverse('backup'),
{'backup_or_restore': 'backup'})
call_command('backup_user_data')
Expand Down Expand Up @@ -121,7 +121,7 @@ def test_restore_add_missing(self):

self.assertEquals(Contact.objects.filter(pk=c.id).count(), 1)

def test_restore_add_and_update(self):
def testRestoreAddAndUpdate(self):
response = self.client.post(reverse('backup'),
{'backup_or_restore': 'backup'})
call_command('backup_user_data')
Expand Down Expand Up @@ -184,7 +184,7 @@ def test_restore_add_and_update(self):

self.assertEquals(Contact.objects.filter(pk=c.id).count(), 1)

def test_restore_delete_all_and_restore(self):
def testRestoreDeleteAllAndRestore(self):
response = self.client.post(reverse('backup'),
{'backup_or_restore': 'backup'})
call_command('backup_user_data')
Expand Down Expand Up @@ -247,7 +247,7 @@ def test_restore_delete_all_and_restore(self):

self.assertEquals(Contact.objects.filter(pk=c.id).count(), 0)

def test_cannot_add_user(self):
def testCannotAddUser(self):
backup_file = '%s/backup/fixtures/backup_injected_user.tar.gz' % (settings.BASE_PATH)
restore_file = '%s%s/restore/backup_injected_user.tar.gz' % (settings.FILE_UPLOAD_DIR,
self.user1.get_profile().uuid)
Expand All @@ -271,7 +271,7 @@ def test_cannot_add_user(self):

self.assertEquals(User.objects.count(), 2)

def test_cannot_update_subscription(self):
def testCannotUpdateSubscription(self):
backup_file = '%s/backup/fixtures/backup_injected_subscription.tar.gz' % (settings.BASE_PATH)
restore_file = '%s%s/restore/backup_injected_subscription.tar.gz' % (settings.FILE_UPLOAD_DIR,
self.user1.get_profile().uuid)
Expand All @@ -294,9 +294,9 @@ def test_cannot_update_subscription(self):
self.assertEquals(OwnedObject.objects.filter(owner=self.user2).count(), 12)

self.assertEquals(Subscription.objects.filter(owner=self.user1).count(), 1)
self.assertEquals(Subscription.objects.filter(owner=self.user1)[0].state, 3)
self.assertEquals(Subscription.objects.filter(owner=self.user1)[0].state, 2)

def test_cannot_reference_not_owned_object(self):
def testCannotReferenceNotOwnedObject(self):
backup_file = '%s/backup/fixtures/backup_with_disallowed_reference.tar.gz' % (settings.BASE_PATH)
restore_file = '%s%s/restore/backup_with_disallowed_reference.tar.gz' % (settings.FILE_UPLOAD_DIR,
self.user1.get_profile().uuid)
Expand Down Expand Up @@ -326,7 +326,7 @@ def test_cannot_reference_not_owned_object(self):
self.assertEquals(len(mail.outbox), 1)
self.assertEquals(mail.outbox[0].subject, ugettext("%sRestore failed") % (settings.EMAIL_SUBJECT_PREFIX))

def test_uuid_from_other_create_new_object(self):
def testUuidFromOtherCreateNewObject(self):
backup_file = '%s/backup/fixtures/backup_from_other_user.tar.gz' % (settings.BASE_PATH)
restore_file = '%s%s/restore/backup_from_other_user.tar.gz' % (settings.FILE_UPLOAD_DIR,
self.user1.get_profile().uuid)
Expand All @@ -353,7 +353,7 @@ def test_uuid_from_other_create_new_object(self):
self.assertNotEquals(Project.objects.get(owner=self.user1).customer.uuid, 'fa73c0f1-1abc-4b60-a6a6-42b7dea8e989')
self.assertEquals(Project.objects.get(owner=self.user1).customer.owner, self.user1)

def test_tar_with_unneeded_files(self):
def testTarWithUnneededFiles(self):
"""
with files that won't be restored
"""
Expand Down Expand Up @@ -381,7 +381,7 @@ def test_tar_with_unneeded_files(self):
self.assertEquals(set(os.listdir('%s%s' % (settings.FILE_UPLOAD_DIR, self.user1.get_profile().uuid))),
set([u'proposal', u'restore', u'contract', u'logo']))

def test_tar_trying_to_add_files_in_parent_directories(self):
def testTarTryingToAddFilesInParentDirectories(self):
backup_file = '%s/backup/fixtures/backup_dest_dir_modified.tar.gz' % (settings.BASE_PATH)
restore_file = '%s%s/restore/backup_dest_dir_modified.tar.gz' % (settings.FILE_UPLOAD_DIR,
self.user1.get_profile().uuid)
Expand All @@ -406,7 +406,7 @@ def test_tar_trying_to_add_files_in_parent_directories(self):
self.assertFalse(os.path.exists('%s/injected_file.txt' % (self.user1.get_profile().uuid)))
self.assertFalse(os.path.exists('%s/../injected_file.txt' % (self.user1.get_profile().uuid)))

def test_dump_referencing_other_user_files(self):
def testDumpReferencingOtherUserFiles(self):
backup_file = '%s/backup/fixtures/backup_referencing_other_files.tar.gz' % (settings.BASE_PATH)
restore_file = '%s%s/restore/backup_referencing_other_files.tar.gz' % (settings.FILE_UPLOAD_DIR,
self.user1.get_profile().uuid)
Expand All @@ -430,7 +430,7 @@ def test_dump_referencing_other_user_files(self):
self.assertEquals(Proposal.objects.get(uuid='2e6bed41-43e5-41b1-8c7f-60353e7727a2').contract_file.name,
'%s/proposal/contract.pdf' % (self.user1.get_profile().uuid))

def test_username_injection(self):
def testUsernameInjection(self):
"""
injected username can't pass through upload dir restriction
"""
Expand All @@ -451,7 +451,7 @@ def test_username_injection(self):
'action': RESTORE_ACTION_DELETE_ALL_AND_RESTORE,
'backup_file': open(backup_file, 'rb')})

def test_backup_restore_backup_stability(self):
def testBackupRestoreBackupStability(self):
"""
verify that a backup, restore with delete and backup give the
same backup file
Expand Down Expand Up @@ -516,6 +516,28 @@ def test_backup_restore_backup_stability(self):

self.assertEquals(backup_digest, new_backup_digest)

def testCantRestoreIfTrial(self):
sub = Subscription.objects.get(owner__username='test1')
sub.state = SUBSCRIPTION_STATE_TRIAL
sub.save()

response = self.client.post(reverse('backup'),
{'backup_or_restore': 'backup'})
call_command('backup_user_data')

backup_file = '%s%s/backup/backup_%s.tar.gz' % (settings.FILE_UPLOAD_DIR,
self.user1.get_profile().uuid,
self.user1.backuprequest.creation_datetime.strftime('%Y%m%d%H%M'))
restore_file = '%s%s/restore/backup_%s.tar.gz' % (settings.FILE_UPLOAD_DIR,
self.user1.get_profile().uuid,
self.user1.backuprequest.creation_datetime.strftime('%Y%m%d%H%M'))

response = self.client.post(reverse('backup'),
{'backup_or_restore': 'restore',
'action': RESTORE_ACTION_DELETE_ALL_AND_RESTORE,
'backup_file': open(backup_file, 'rb')})
self.assertContains(response, 'You have to subscribe to restore your backups', status_code=200)

class CsvExportTest(TestCase):
fixtures = ['test_users', 'test_contacts', 'test_projects']

Expand Down
53 changes: 30 additions & 23 deletions backup/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
from django.conf import settings
import unicodecsv
from accounts.models import Invoice
from autoentrepreneur.models import SUBSCRIPTION_STATE_TRIAL

@settings_required
@commit_on_success
def backup(request):
subscription = request.user.get_profile().get_last_subscription()

backup_request = None
restore_request = None
old_file = None
Expand Down Expand Up @@ -60,28 +63,31 @@ def backup(request):
messages.info(request, _("Your backup has been scheduled successfully. There are %i other backups before yours.") % (position - 1))
return redirect(reverse('backup'))
elif request.POST.get('backup_or_restore') == 'restore':
restore_form = RestoreForm(request.POST, request.FILES, instance=restore_request)
if restore_form.is_valid():
if request.FILES:
try:
if old_file:
if os.path.exists(old_file.path):
os.remove(old_file.path)
except:
pass

if action_pending:
messages.error(request, _("A backup or restore is already scheduled"))
else:
restore_request = restore_form.save(commit=False)
restore_request.user = request.user
restore_request.state = BACKUP_RESTORE_STATE_PENDING
restore_request.creation_datetime = datetime.datetime.now()
restore_request.last_state_datetime = restore_request.creation_datetime
restore_request.save()
position = RestoreRequest.objects.filter(state__lte=BACKUP_RESTORE_STATE_IN_PROGRESS).count()
messages.info(request, _("Your restore has been scheduled successfully. There are %i other restores before yours.") % (position - 1))
return redirect(reverse('backup'))
if subscription.state == SUBSCRIPTION_STATE_TRIAL:
messages.error(request, _("You have to subscribe to restore your backups"))
else:
restore_form = RestoreForm(request.POST, request.FILES, instance=restore_request)
if restore_form.is_valid():
if request.FILES:
try:
if old_file:
if os.path.exists(old_file.path):
os.remove(old_file.path)
except:
pass

if action_pending:
messages.error(request, _("A backup or restore is already scheduled"))
else:
restore_request = restore_form.save(commit=False)
restore_request.user = request.user
restore_request.state = BACKUP_RESTORE_STATE_PENDING
restore_request.creation_datetime = datetime.datetime.now()
restore_request.last_state_datetime = restore_request.creation_datetime
restore_request.save()
position = RestoreRequest.objects.filter(state__lte=BACKUP_RESTORE_STATE_IN_PROGRESS).count()
messages.info(request, _("Your restore has been scheduled successfully. There are %i other restores before yours.") % (position - 1))
return redirect(reverse('backup'))
else:
messages.error(request, _("Form data have been tempered"))

Expand All @@ -92,7 +98,8 @@ def backup(request):
'backup_form': backup_form,
'restore_form': restore_form,
'csv_form': csv_form,
'action_pending': action_pending
'action_pending': action_pending,
'cant_restore': subscription.state == SUBSCRIPTION_STATE_TRIAL
}
return render_to_response('backup/index.html',
context,
Expand Down
Binary file modified locale/en/LC_MESSAGES/django.mo
Binary file not shown.
Loading

0 comments on commit 596c51e

Please sign in to comment.