Esempio n. 1
0
    def test_fingerprint(self):
        test_object = models.CryptographyObject()
        self.assertEqual(len(test_object.fingerprint), 92)

        test_object = models.CryptographyObject(
            cipher=models.Cipher.objects.filter(is_asymmetric=False).first())
        self.assertEqual(test_object.fingerprint, False)
Esempio n. 2
0
    def test_validate(self):
        test_object = models.CryptographyObject()
        test_form = forms.CryptographyObjectForm(instance=test_object)
        self.assertIs(test_form.is_valid(), False)

        test_form = forms.CryptographyObjectForm(data={'name': 'test'},
                                                 instance=test_object)
        self.assertIs(test_form.is_valid(), True)

        test_object.private_key = '123'
        test_form = forms.CryptographyObjectForm(data={'name': 'test'},
                                                 instance=test_object)
        self.assertIs(test_form.is_valid(), False)
        self.assertEqual(len(test_form.errors), 1)

        test_object = models.CryptographyObject()
        test_object.public_key = None
        test_form = forms.CryptographyObjectForm(data={'name': 'test'},
                                                 instance=test_object)
        self.assertIs(test_form.is_valid(), False)
        self.assertEqual(len(test_form.errors), 1)

        test_object = models.CryptographyObject(
            cipher=models.Cipher.objects.filter(is_asymmetric=False).first())
        test_object.public_key = None
        test_form = forms.CryptographyObjectForm(data={'name': 'test'},
                                                 instance=test_object)
        self.assertIs(test_form.is_valid(), True)

        test_object.private_key = None
        test_form = forms.CryptographyObjectForm(data={'name': 'test'},
                                                 instance=test_object)
        self.assertIs(test_form.is_valid(), False)
        self.assertEqual(len(test_form.errors), 1)
Esempio n. 3
0
 def test_export_to_file(self):
     test_object = models.CryptographyObject()
     test_object.name = 'test'
     response = test_object.export_to_file()
     self.assertEqual(response.status_code, 200)
     response = test_object.export_to_file(public_key=True)
     self.assertEqual(response.status_code, 200)
Esempio n. 4
0
    def test_cryptography_object_page(self):
        sample_object = models.CryptographyObject(name='test')
        sample_object.save()
        response = self.client.get(
            'http://127.0.0.1:8000' +
            reverse('main:cryptography_object',
                    kwargs={'object_id': sample_object.id}))
        self.assertEqual(response.status_code, 200)

        response = self.client.get(
            'http://127.0.0.1:8000' +
            reverse('main:cryptography_object', kwargs={'object_id': 99}))
        self.assertEqual(response.status_code, 404)
Esempio n. 5
0
    def test_export_key(self):
        sample_object = models.CryptographyObject(name='test')
        sample_object.save()
        response = self.client.get(
            'http://127.0.0.1:8000' +
            reverse('main:export_key', kwargs={'object_id': sample_object.id}))
        self.assertIsInstance(response, HttpResponse)
        self.assertEqual(response.status_code, 200)

        response = self.client.get('http://127.0.0.1:8000' +
                                   reverse('main:export_key',
                                           kwargs={
                                               'object_id': sample_object.id,
                                               'public_key': 1
                                           }))
        self.assertIsInstance(response, HttpResponse)
        self.assertEqual(response.status_code, 200)
Esempio n. 6
0
    def __init__(self, *args, **kwargs):

        if 'instance' not in kwargs or not kwargs['instance']:
            kwargs['instance'] = models.CryptographyObject()
        instance = kwargs['instance']

        files = kwargs.get('files')
        file_error = False

        if files:
            old_state = copy.deepcopy(instance)
            for field_name in files:
                file_data = next(files[field_name].chunks())
                try:
                    instance.parse_file(file_data)
                except Exception as error:
                    file_error = error
            if not file_error:
                try:
                    instance.full_clean(exclude=['name'])
                except (ValidationError, ValueError) as error:
                    file_error = error
                    instance = old_state
            else:
                instance = old_state

        if 'data' in kwargs:
            kwargs['data'] = copy.deepcopy(kwargs['data'])
            for field in self.base_fields:
                if field not in kwargs['data'] or not kwargs['data'][field]:
                    kwargs['data'][field] = getattr(instance, field)

        super(CryptographyObjectForm, self).__init__(*args, **kwargs)

        if file_error:
            self.add_error(field=None, error=file_error)

        self.fields['key_length'].queryset = models.CipherKeyLengthRelation.objects.filter(cipher_id=self['cipher'].value())

        for visible_field in self.visible_fields():
            visible_field.field.widget.attrs['class'] = 'form-control'
Esempio n. 7
0
    def get_context_data(self, **kwargs):
        context_data = super(IndexView, self).get_context_data(**kwargs)

        symmetric_queryset = models.Cipher.objects.filter(is_asymmetric=False)
        symmetric_cryptography_object_form = forms.CryptographyObjectForm(
            instance=models.CryptographyObject(
                cipher=symmetric_queryset.first()))
        symmetric_cryptography_object_form.fields[
            'cipher'].queryset = symmetric_queryset

        context_data['cryptography_object_form'] = context_data['form']
        context_data['cryptography_object_form'].fields[
            'cipher'].queryset = models.Cipher.objects.filter(
                is_asymmetric=True)
        context_data[
            'symmetric_cryptography_object_form'] = symmetric_cryptography_object_form
        context_data[
            'cryptography_object_list'] = models.CryptographyObject.objects.all(
            )

        return context_data
Esempio n. 8
0
def cipher_defaults(request):
    """
    Returns cipher available key lengths and new random keys for it.
    Used by AJAX when a user changes a cipher in the select element.
    """
    if request.method != "GET" or not request.is_ajax():
        return HttpResponseNotFound()
    cipher_id = int(request.GET.get('cipher_id'))
    cipher = models.Cipher.objects.filter(id=cipher_id).first()
    if not cipher:
        return HttpResponseNotFound('Шифр с данным ID не найден.')
    form = forms.CryptographyObjectForm(instance=models.CryptographyObject(
        cipher=cipher))
    return JsonResponse({
        'key_length':
        str(form['key_length'].label) + ": " + str(form['key_length']),
        'private_key':
        str(form['private_key'].label) + "<br>" + str(form['private_key']),
        'public_key':
        str(form['public_key'].label) + "<br>" + str(form['public_key']),
        'fingerprint':
        str(form.instance.fingerprint)
    })