def test_add_new_text(self): args = [ { 'action': 'action', 'effect': 'effect', 'setup': 'setup', 'breakdown': 'breakdown', }, { 'action': 'action 1', 'effect': 'effect 1', 'setup': 'setup 1', 'breakdown': 'breakdown 1', }, { 'action': 'action 1', 'effect': 'effect 1', 'setup': 'setup 1', 'breakdown': 'break into several steps', }, { 'action': 'take some action', 'effect': 'expected result', 'setup': 'setup environemnt', 'breakdown': 'break into several steps', 'action_checksum': checksum('take some action'), 'effect_checksum': checksum('expected result') }, { 'action': 'action 1', 'effect': 'effect 1', 'setup': 'setup 1', 'breakdown': 'breakdown 1', 'action_checksum': checksum('action 1'), 'effect_checksum': checksum('effect 1'), 'setup_checksum': checksum('setup 1'), 'breakdown_checksum': checksum('breakdown 1'), }, ] for expected_text_version, kwargs in enumerate(args, 1): self.case.add_text(**kwargs) new_text = self.case.text.order_by('pk').last() self.assertEqual(expected_text_version, new_text.case_text_version) self.assertEqual(kwargs['action'], new_text.action) self.assertEqual(kwargs['effect'], new_text.effect) self.assertEqual(kwargs['setup'], new_text.setup) self.assertEqual(kwargs['breakdown'], new_text.breakdown) self.assertEqual(checksum(kwargs['action']), new_text.action_checksum) self.assertEqual(checksum(kwargs['effect']), new_text.effect_checksum) self.assertEqual(checksum(kwargs['setup']), new_text.setup_checksum) self.assertEqual(checksum(kwargs['breakdown']), new_text.breakdown_checksum)
def setUpTestData(cls): super().setUpTestData() cls.upload_dir = tempfile.mkdtemp() cls.text_file_content = 'hello Nitrate' with open(os.path.join(cls.upload_dir, 'a-1.txt'), 'w', encoding='utf-8') as f: f.write(cls.text_file_content) cls.binary_file_content = b'\x00\x01\x11\x10' with open(os.path.join(cls.upload_dir, 'b.bin'), 'wb') as f: f.write(cls.binary_file_content) cls.logo_png_content = b'\x00\x01\x10' with open(os.path.join(cls.upload_dir, 'logo.png'), 'wb') as f: f.write(cls.logo_png_content) cls.text_file = TestAttachment.objects.create( submitter_id=cls.tester.id, description='description', file_name='a.txt', stored_name='a-1.txt', create_date=datetime.now(), mime_type='text/plain', checksum=checksum(cls.text_file_content)) cls.binary_file = TestAttachment.objects.create( submitter_id=cls.tester.id, description='binary file', file_name='b.txt', stored_name='b.bin', create_date=datetime.now(), mime_type='application/x-binary', checksum=checksum(cls.binary_file_content)) cls.logo_png = TestAttachment.objects.create( submitter_id=cls.tester.id, description='binary file', file_name='logo.png', # stored_name is not set, use file_name to find out attachment instead. stored_name=None, create_date=datetime.now(), mime_type='image/png', checksum=checksum(cls.logo_png_content)) cls.file_deleted = TestAttachment.objects.create( submitter_id=cls.tester.id, description='case plan', file_name='case-plan.txt', stored_name=None, create_date=datetime.now(), mime_type='text/plain', checksum='1234567')
def test_do_not_add_if_all_checksum_are_same(self): action_checksum = checksum('action') effect_checksum = checksum('effect') setup_checksum = checksum('setup') breakdown_checksum = checksum('breakdown') args = [ { 'action': 'action', 'effect': 'effect', 'setup': 'setup', 'breakdown': 'breakdown', }, { 'action': 'action', 'effect': 'effect', 'setup': 'setup', 'breakdown': 'breakdown', 'action_checksum': action_checksum, 'setup_checksum': setup_checksum, }, { 'action': 'action', 'effect': 'effect', 'setup': 'setup', 'breakdown': 'breakdown', 'action_checksum': action_checksum, 'effect_checksum': effect_checksum, 'setup_checksum': setup_checksum, 'breakdown_checksum': breakdown_checksum, }, ] text = self.case.add_text(**args[0]) for kwargs in args: new_text = self.case.add_text(**kwargs) self.assertEqual(text.case_text_version, new_text.case_text_version) self.assertEqual(text.action, new_text.action) self.assertEqual(text.effect, new_text.effect) self.assertEqual(text.setup, new_text.setup) self.assertEqual(text.breakdown, new_text.breakdown) self.assertEqual(action_checksum, new_text.action_checksum) self.assertEqual(effect_checksum, new_text.effect_checksum) self.assertEqual(setup_checksum, new_text.setup_checksum) self.assertEqual(breakdown_checksum, new_text.breakdown_checksum)
def add_text(self, action, effect, setup, breakdown, author=None, create_date=None, case_text_version=1, action_checksum=None, effect_checksum=None, setup_checksum=None, breakdown_checksum=None): if not author: author = self.author new_action_checksum = checksum(action) new_effect_checksum = checksum(effect) new_setup_checksum = checksum(setup) new_breakdown_checksum = checksum(breakdown) old_action, old_effect, old_setup, old_breakdown = self.text_checksum() if (old_action != new_action_checksum or old_effect != new_effect_checksum or old_setup != new_setup_checksum or old_breakdown != new_breakdown_checksum): case_text_version = self.latest_text_version() + 1 latest_text = TestCaseText.objects.create( case=self, case_text_version=case_text_version, create_date=create_date or datetime.now(), author=author, action=action, effect=effect, setup=setup, breakdown=breakdown, action_checksum=action_checksum or new_action_checksum, effect_checksum=effect_checksum or new_effect_checksum, setup_checksum=setup_checksum or new_setup_checksum, breakdown_checksum=breakdown_checksum or new_breakdown_checksum) else: latest_text = self.latest_text() return latest_text
def calculate_uploaded_files_checksum(apps, schema_editor): TestAttachment = apps.get_model('management', 'TestAttachment') for attachment in TestAttachment.objects.all(): stored_filename = attachment_stored_filename(attachment.stored_name) if not os.path.exists(stored_filename): logger.info('Server side stored file %s does not exist.', stored_filename) continue with open(stored_filename, 'rb') as f: attachment.checksum = checksum(f.read()) attachment.save(update_fields=['checksum'])
def setUpTestData(cls): super().setUpTestData() cls.action = '<p>First step:</p>' cls.effect = '''<ul> <li>effect 1</li> <li>effect 2</li> </ul>''' cls.setup = '<p><a href="/setup_guide">setup</a></p>' cls.breakdown = '<span>breakdown</span>' cls.text_author = User.objects.create_user(username='******', email='*****@*****.**') TestCaseText.objects.create(case=cls.case, case_text_version=1, author=cls.text_author, action=cls.action, effect=cls.effect, setup=cls.setup, breakdown=cls.breakdown, action_checksum=checksum(cls.action), effect_checksum=checksum(cls.effect), setup_checksum=checksum(cls.setup), breakdown_checksum=checksum(cls.breakdown))
def add_text(self, author, plan_text, create_date=datetime.now(), plan_text_version=None, text_checksum=None): if not plan_text_version: latest_text = self.latest_text() if latest_text: plan_text_version = latest_text.plan_text_version + 1 else: plan_text_version = 1 if not text_checksum: old_checksum = self.text_checksum() new_checksum = checksum(plan_text) if old_checksum == new_checksum: return self.latest_text() return self.text.create(plan_text_version=plan_text_version, author=author, create_date=create_date, plan_text=plan_text, checksum=text_checksum or checksum(plan_text))
def edit(request, plan_id, template_name='plan/edit.html'): """Edit test plan view""" # Define the default sub module SUB_MODULE_NAME = 'plans' try: tp = TestPlan.objects.select_related().get(plan_id=plan_id) except ObjectDoesNotExist: raise Http404 # If the form is submitted if request.method == "POST": form = EditPlanForm(request.POST, request.FILES) if request.POST.get('product'): form.populate(product_id=request.POST['product']) else: form.populate() # FIXME: Error handle if form.is_valid(): if form.cleaned_data.get('upload_plan_text'): # Set the summary form field to the uploaded text form.data['text'] = form.cleaned_data['text'] # Generate the form context_data = { 'module': MODULE_NAME, 'sub_module': SUB_MODULE_NAME, 'form': form, 'test_plan': tp, } return render(request, template_name, context=context_data) if request.user.has_perm('testplans.change_testplan'): tp.name = form.cleaned_data['name'] tp.parent = form.cleaned_data['parent'] tp.product = form.cleaned_data['product'] tp.product_version = form.cleaned_data['product_version'] tp.type = form.cleaned_data['type'] tp.is_active = form.cleaned_data['is_active'] tp.extra_link = form.cleaned_data['extra_link'] tp.owner = form.cleaned_data['owner'] # IMPORTANT! tp.current_user is an instance attribute, # added so that in post_save, current logged-in user info # can be accessed. # Instance attribute is usually not a desirable solution. tp.current_user = request.user tp.save() if request.user.has_perm('testplans.add_testplantext'): new_text = request.POST.get('text') text_checksum = checksum(new_text) if not tp.text_exist() or text_checksum != tp.text_checksum(): tp.add_text(author=request.user, plan_text=request.POST.get('text'), text_checksum=text_checksum) if request.user.has_perm('management.change_tcmsenvplanmap'): tp.clear_env_groups() if request.POST.get('env_group'): env_groups = TCMSEnvGroup.objects.filter( id__in=request.POST.getlist('env_group')) for env_group in env_groups: tp.add_env_group(env_group=env_group) # Update plan email settings update_plan_email_settings(tp, form) return HttpResponseRedirect( reverse('plan-get', args=[plan_id, slugify(tp.name)])) else: # Generate a blank form # Temporary use one environment group in this case if tp.env_group.all(): for env_group in tp.env_group.all(): env_group_id = env_group.id break else: env_group_id = None form = EditPlanForm( initial={ 'name': tp.name, 'product': tp.product_id, 'product_version': tp.product_version_id, 'type': tp.type_id, 'text': tp.latest_text() and tp.latest_text().plan_text or '', 'parent': tp.parent_id, 'env_group': env_group_id, 'is_active': tp.is_active, 'extra_link': tp.extra_link, 'owner': tp.owner, 'auto_to_plan_owner': tp.email_settings.auto_to_plan_owner, 'auto_to_plan_author': tp.email_settings.auto_to_plan_author, 'auto_to_case_owner': tp.email_settings.auto_to_case_owner, 'auto_to_case_default_tester': tp.email_settings.auto_to_case_default_tester, 'notify_on_plan_update': tp.email_settings.notify_on_plan_update, 'notify_on_case_update': tp.email_settings.notify_on_case_update, 'notify_on_plan_delete': tp.email_settings.notify_on_plan_delete, }) form.populate(product_id=tp.product_id) context_data = { 'module': MODULE_NAME, 'sub_module': SUB_MODULE_NAME, 'test_plan': tp, 'form': form, } return render(request, template_name, context=context_data)