From 3e3a3c7147310980874307a96db500af677a4377 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Mon, 4 Jul 2016 15:03:04 +0200 Subject: Test wizard --- ishtar_common/tests.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) (limited to 'ishtar_common/tests.py') diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index 82ab009e0..63bf73eb7 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -54,6 +54,75 @@ class OOOGenerationTest(TestCase): """ +def create_superuser(): + username = 'username4277' + password = 'dcbqj756456!@%' + user = User.objects.create_superuser(username, "nomail@nomail.com", + password) + return username, password, user + + +def create_user(): + username = 'username678' + password = 'dcbqj756456!@%' + user = User.objects.create_user(username, email="nomail2@nomail.com") + user.set_password(password) + user.save() + return username, password, user + + +class WizardTest(object): + url = None + wizard_name = '' + steps = None + condition_dict = None + form_datas = [] + + def setUp(self): + self.username, self.password, self.user = create_superuser() + + def pre_wizard(self): + self.client.login(**{'username': self.username, + 'password': self.password}) + + def post_wizard(self): + pass + + def test_wizard(self): + url = reverse(self.url_name) + self.pre_wizard() + for form_data, ignored in self.form_datas: + for idx, step in enumerate(self.steps): + current_step, current_form = step + if current_step in ignored: + continue + data = { + '{}{}-current_step'.format(self.url_name, + self.wizard_name): + [current_step], + } + if current_step in form_data: + d = form_data[current_step] + for k in d: + data['{}-{}'.format(current_step, k)] = d[k] + + next_idx, next_form = idx + 1, None + while len(self.steps) > next_idx: + if self.steps[idx + 1][0] not in ignored: + next_form = self.steps[idx + 1][0] + break + next_idx = next_idx + 1 + + if next_form: + response = self.client.post(url, data) + self.assertRedirects( + response, + '/{}/{}'.format(self.url_name, next_form)) + else: + response = self.client.post(url, data, follow=True) + self.post_wizard() + + class MergeTest(TestCase): def setUp(self): self.user, created = User.objects.get_or_create(username='username') -- cgit v1.2.3 From d3577c944b73dd80a30c74f0da33d65f33c72cd9 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Sat, 16 Jul 2016 14:56:15 +0200 Subject: Test administrativ act creation - improve test wizard --- archaeological_operations/tests.py | 42 ++++++++++++++++++++++++++++++++++++ archaeological_operations/views.py | 14 +++++++----- archaeological_operations/wizards.py | 2 +- ishtar_common/tests.py | 21 ++++++++++++++++-- 4 files changed, 71 insertions(+), 8 deletions(-) (limited to 'ishtar_common/tests.py') diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 29a7fdb00..5b7f71e1b 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -648,3 +648,45 @@ class OperationWizardCreationTest(WizardTest, OperationInitTest, TestCase): def post_wizard(self): self.assertEqual(models.Operation.objects.count(), self.operation_number + 1) + + +class OperationAdminActWizardCreationTest(WizardTest, OperationInitTest, + TestCase): + fixtures = [settings.ROOT_PATH + + '../fixtures/initial_data-auth-fr.json', + settings.ROOT_PATH + + '../ishtar_common/fixtures/initial_data-fr.json', + settings.ROOT_PATH + + '../archaeological_files/fixtures/initial_data.json', + settings.ROOT_PATH + + '../archaeological_operations/fixtures/initial_data-fr.json'] + url_name = 'operation_administrativeactop' + wizard_name = 'operation_administrative_act_wizard' + steps = views.administrativeactop_steps + form_datas = [[ + # data + { + 'selec-operation_administrativeactop': { + }, + 'administrativeact-operation_administrativeactop': { + 'signature_date': str(datetime.date.today()) + } + }, + # ignored + [] + ]] + + def pre_wizard(self): + ope = self.get_default_operation() + self.operation_number = models.Operation.objects.count() + data = self.form_datas[0][0] + data['selec-operation_administrativeactop']['pk'] = ope.pk + act = models.ActType.objects.filter(intented_to='O').all()[0].pk + + data['administrativeact-operation_administrativeactop'][ + 'act_type'] = act + super(OperationAdminActWizardCreationTest, self).pre_wizard() + + def post_wizard(self): + self.assertEqual(models.Operation.objects.count(), + self.operation_number + 1) diff --git a/archaeological_operations/views.py b/archaeological_operations/views.py index 90f71abfe..d9baa4b7a 100644 --- a/archaeological_operations/views.py +++ b/archaeological_operations/views.py @@ -384,12 +384,16 @@ operation_administrativeactop_search_wizard = SearchWizard.as_view([ label=_(u"Administrative act search"), url_name='operation_administrativeactop_search',) +administrativeactop_steps = [ + ('selec-operation_administrativeactop', OperationFormSelection), + ('administrativeact-operation_administrativeactop', + AdministrativeActOpeForm), + ('final-operation_administrativeactop', FinalForm)] + + operation_administrativeactop_wizard = \ - OperationAdministrativeActWizard.as_view([ - ('selec-operation_administrativeactop', OperationFormSelection), - ('administrativeact-operation_administrativeactop', - AdministrativeActOpeForm), - ('final-operation_administrativeactop', FinalForm)], + OperationAdministrativeActWizard.as_view( + administrativeactop_steps, label=_(u"Operation: new administrative act"), url_name='operation_administrativeactop',) diff --git a/archaeological_operations/wizards.py b/archaeological_operations/wizards.py index c51b88d0b..0cc336cc8 100644 --- a/archaeological_operations/wizards.py +++ b/archaeological_operations/wizards.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (C) 2012-2015 Étienne Loks +# Copyright (C) 2012-2016 Étienne Loks # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index 63bf73eb7..1b8601dfb 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -17,10 +17,13 @@ # See the file COPYING for details. +from BeautifulSoup import BeautifulSoup as Soup + from django.conf import settings from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.core.cache import cache +from django.core.exceptions import ValidationError from django.core.urlresolvers import reverse from django.template.defaultfilters import slugify from django.test import TestCase @@ -72,7 +75,7 @@ def create_user(): class WizardTest(object): - url = None + url_name = None wizard_name = '' steps = None condition_dict = None @@ -114,7 +117,21 @@ class WizardTest(object): next_idx = next_idx + 1 if next_form: - response = self.client.post(url, data) + try: + response = self.client.post(url, data) + except ValidationError as e: + raise ValidationError(u"Errors: {} on {}.".format( + u" - ".join(e.messages), current_step)) + if "errorlist" in response.content: + soup = Soup(response.content) + errorlist = soup.findAll( + "ul", {"class": "errorlist"}) + errors = [] + for li in errorlist: + lbl = li.findParent().findParent().findChild().text + errors.append(u"{} - {}".format(lbl, li.text)) + raise ValidationError(u"Errors: {} on {}.".format( + u" ".join(errors), current_step)) self.assertRedirects( response, '/{}/{}'.format(self.url_name, next_form)) -- cgit v1.2.3