Exemplo n.º 1
0
 def test_url_with_protection_settings_3(self):
     self.test_svg_url()
     self.test_png_url()
     response = self.client.get(
         make_qr_code_url(TEST_TEXT,
                          url_signature_enabled=False,
                          cache_enabled=False))
     self.assertEqual(response.status_code, 403)
     response = self.client.get(
         make_qr_code_url(TEST_TEXT,
                          url_signature_enabled=True,
                          cache_enabled=False))
     self.assertEqual(response.status_code, 200)
Exemplo n.º 2
0
    def test_reverse_lazy_url(self):
        from django.urls import reverse, reverse_lazy
        options = QRCodeOptions(image_format='svg', size=1)
        url1 = make_qr_code_url(reverse('qr_code:serve_qr_code_image'),
                                options)
        url2 = make_qr_code_url(reverse_lazy('qr_code:serve_qr_code_image'),
                                options)
        self.assertEqual(url1, url2)

        svg1 = make_embedded_qr_code(reverse('qr_code:serve_qr_code_image'),
                                     options)
        svg2 = make_embedded_qr_code(
            reverse_lazy('qr_code:serve_qr_code_image'), options)
        self.assertEqual(svg1, svg2)
Exemplo n.º 3
0
 def test_url_with_wrong_signature_token(self):
     valid_url_with_signature_token_for_size_10 = make_qr_code_url(
         TEST_TEXT, QRCodeOptions(size=10))
     valid_url_with_signature_token_for_size_8 = make_qr_code_url(
         TEST_TEXT, QRCodeOptions(size=8))
     token_regex = re.compile(r"token=([^&]+)")
     match = token_regex.search(valid_url_with_signature_token_for_size_8)
     size_8_token_value = match.group(1)
     match = token_regex.search(valid_url_with_signature_token_for_size_10)
     size_10_token_value = match.group(1)
     url_with_invalid_signature_token = valid_url_with_signature_token_for_size_10.replace(
         size_10_token_value, size_8_token_value)
     response = self.client.get(url_with_invalid_signature_token)
     self.assertEqual(response.status_code, 403)
Exemplo n.º 4
0
 def test_url_with_protection_settings_3(self):
     # We need to clear cache every time we change the QR_CODE_URL_PROTECTION to avoid incidence between tests.
     caches[settings.QR_CODE_CACHE_ALIAS].clear()
     self.test_svg_url()
     self.test_png_url()
     response = self.client.get(
         make_qr_code_url(TEST_TEXT,
                          url_signature_enabled=False,
                          cache_enabled=False))
     self.assertEqual(response.status_code, 403)
     response = self.client.get(
         make_qr_code_url(TEST_TEXT,
                          url_signature_enabled=True,
                          cache_enabled=False))
     self.assertEqual(response.status_code, 200)
Exemplo n.º 5
0
def make_qr_code_url_with_args(text, qr_code_args):
    cache_enabled = qr_code_args.pop('cache_enabled', DEFAULT_CACHE_ENABLED)
    url_signature_enabled = qr_code_args.pop('url_signature_enabled',
                                             DEFAULT_URL_SIGNATURE_ENABLED)
    options = _options_from_args(qr_code_args)
    return make_qr_code_url(text,
                            options,
                            cache_enabled=cache_enabled,
                            url_signature_enabled=url_signature_enabled)
Exemplo n.º 6
0
 def test_write_png(self):
     response = self.client.get(make_qr_code_url(TEST_TEXT))
     image_data = response.content
     write_png_content_to_file(
         TestWriteResourceData.resource_file_base_name, image_data)
     file_path_to_remove = os.path.join(
         get_resources_path(),
         TestWriteResourceData.resource_file_base_name + PNG_REF_SUFFIX)
     os.remove(file_path_to_remove)
Exemplo n.º 7
0
 def test_url_with_protection_settings_1(self):
     self.test_svg_url()
     self.test_png_url()
     response = self.client.get(
         make_qr_code_url(TEST_TEXT,
                          url_signature_enabled=False,
                          cache_enabled=False))
     # Registered users can access the URL externally, but since we are not logged in, we must expect an HTTP 403.
     self.assertEqual(response.status_code, 403)
Exemplo n.º 8
0
 def test_png_error_correction(self):
     base_file_name = 'qrfromtext_error_correction'
     for correction_level in ERROR_CORRECTION_DICT:
         print('Testing PNG URL with error correction: %s' %
               correction_level)
         url1 = make_qr_code_url(COMPLEX_TEST_TEXT,
                                 QRCodeOptions(
                                     error_correction=correction_level,
                                     image_format='png'),
                                 cache_enabled=False)
         url2 = make_qr_code_url(COMPLEX_TEST_TEXT,
                                 QRCodeOptions(
                                     error_correction=correction_level,
                                     image_format='PNG'),
                                 cache_enabled=False)
         url3 = qr_url_from_text(COMPLEX_TEST_TEXT,
                                 error_correction=correction_level,
                                 image_format='png',
                                 cache_enabled=False)
         url4 = qr_url_from_text(COMPLEX_TEST_TEXT,
                                 error_correction=correction_level,
                                 image_format='PNG',
                                 cache_enabled=False)
         url5 = qr_url_from_text(COMPLEX_TEST_TEXT,
                                 options=QRCodeOptions(
                                     error_correction=correction_level,
                                     image_format='PNG'),
                                 cache_enabled=False)
         url = url1
         urls = get_urls_without_token_for_comparison(
             url1, url2, url3, url4, url5)
         self.assertEqual(urls[0], urls[1])
         self.assertEqual(urls[0], urls[2])
         self.assertEqual(urls[0], urls[3])
         self.assertEqual(urls[0], urls[4])
         response = self.client.get(url)
         self.assertEqual(response.status_code, 200)
         source_image_data = response.content
         ref_file_name = '%s_%s' % (base_file_name,
                                    correction_level.lower())
         if REFRESH_REFERENCE_IMAGES:
             write_png_content_to_file(ref_file_name, source_image_data)
         ref_image_data = get_png_content_from_file_name(ref_file_name)
         self.assertEqual(source_image_data, ref_image_data)
Exemplo n.º 9
0
 def test_url_with_protection_settings_4(self):
     self.test_svg_url()
     self.test_png_url()
     # The callable for ALLOWS_EXTERNAL_REQUESTS_FOR_REGISTERED_USER always return True, even for anonymous user.
     # Therefore, we must expect an HTTP 200.
     # We test with different values of url_signature_enabled.
     response = self.client.get(
         make_qr_code_url(TEST_TEXT, cache_enabled=False))
     self.assertEqual(response.status_code, 200)
     response = self.client.get(
         make_qr_code_url(TEST_TEXT,
                          url_signature_enabled=True,
                          cache_enabled=False))
     self.assertEqual(response.status_code, 200)
     response = self.client.get(
         make_qr_code_url(TEST_TEXT,
                          url_signature_enabled=False,
                          cache_enabled=False))
     self.assertEqual(response.status_code, 200)
Exemplo n.º 10
0
def ordemservicos_detail(request, pk):
    msg = messages.get_messages(request)
    ordemservico = get_object_or_404(OrdemServico, pk=pk)

    options_qr = QRCodeOptions(size='t', border=6, error_correction='L')
    qr_url = make_qr_code_url("URL:https://excelencia.online/c/" +
                              str(ordemservico.os_id),
                              qr_code_options=options_qr)

    args = {'ordemservico': ordemservico, 'qr_url': qr_url, 'message': msg}
    return render(request, 'servicos/os/os_detail.html', args)
Exemplo n.º 11
0
 def test_url_with_protection_settings_1(self):
     # We need to clear cache every time we change the QR_CODE_URL_PROTECTION to avoid incidence between tests.
     caches[settings.QR_CODE_CACHE_ALIAS].clear()
     self.test_svg_url()
     self.test_png_url()
     response = self.client.get(
         make_qr_code_url(TEST_TEXT,
                          url_signature_enabled=False,
                          cache_enabled=False))
     # Registered users can access the URL externally, but since we are not logged in, we must expect an HTTP 403.
     self.assertEqual(response.status_code, 403)
Exemplo n.º 12
0
 def test_png_url(self):
     is_first = True
     for url_options in product([True, False, None], [True, False, None]):
         cache_enabled = url_options[0]
         url_signature_enabled = url_options[1]
         url_options_kwargs = dict()
         if cache_enabled is not None:
             url_options_kwargs['cache_enabled'] = cache_enabled
         if url_signature_enabled is not None:
             url_options_kwargs[
                 'url_signature_enabled'] = url_signature_enabled
         url1 = make_qr_code_url(TEST_TEXT,
                                 QRCodeOptions(image_format='png', size=1),
                                 **url_options_kwargs)
         url2 = qr_url_from_text(TEST_TEXT,
                                 image_format='png',
                                 size=1,
                                 **url_options_kwargs)
         url3 = qr_url_from_text(TEST_TEXT,
                                 image_format='PNG',
                                 size=1,
                                 **url_options_kwargs)
         url4 = qr_url_from_text(TEST_TEXT,
                                 options=QRCodeOptions(image_format='PNG',
                                                       size=1),
                                 **url_options_kwargs)
         url = url1
         if url_signature_enabled is not False:
             urls = get_urls_without_token_for_comparison(
                 url1, url2, url3, url4)
         else:
             urls = [url1, url2, url3, url4]
         self.assertEqual(urls[0], urls[1])
         self.assertEqual(urls[0], urls[2])
         self.assertEqual(urls[0], urls[3])
         response = self.client.get(url)
         print("\t - cache_enabled=%s, url_signature_enabled=%s" %
               (cache_enabled, url_signature_enabled))
         expected_status_code = 200
         if url_signature_enabled is False and not allows_external_request_from_user(
                 None):
             expected_status_code = 403
         self.assertEqual(response.status_code, expected_status_code)
         if expected_status_code == 200:
             if is_first and REFRESH_REFERENCE_IMAGES:
                 write_png_content_to_file(
                     TestQRUrlFromTextResult.default_ref_base_file_name,
                     response.content)
                 is_first = False
             self.assertEqual(
                 response.content,
                 TestQRUrlFromTextResult.
                 _get_reference_result_for_default_png())
Exemplo n.º 13
0
 def test_url_with_protection_settings_4(self):
     # We need to clear cache every time we change the QR_CODE_URL_PROTECTION to avoid incidence between tests.
     caches[settings.QR_CODE_CACHE_ALIAS].clear()
     self.test_svg_url()
     self.test_png_url()
     # The callable for ALLOWS_EXTERNAL_REQUESTS_FOR_REGISTERED_USER always return True, even for anonymous user.
     # Therefore, we must expect an HTTP 200.
     # We test with different values of url_signature_enabled.
     response = self.client.get(
         make_qr_code_url(TEST_TEXT, cache_enabled=False))
     self.assertEqual(response.status_code, 200)
     response = self.client.get(
         make_qr_code_url(TEST_TEXT,
                          url_signature_enabled=True,
                          cache_enabled=False))
     self.assertEqual(response.status_code, 200)
     response = self.client.get(
         make_qr_code_url(TEST_TEXT,
                          url_signature_enabled=False,
                          cache_enabled=False))
     self.assertEqual(response.status_code, 200)
Exemplo n.º 14
0
 def test_svg_error_correction(self):
     base_file_name = 'qrfromtext_error_correction'
     for correction_level in ERROR_CORRECTION_DICT:
         print('Testing SVG URL with error correction: %s' %
               correction_level)
         url1 = make_qr_code_url(
             COMPLEX_TEST_TEXT,
             QRCodeOptions(error_correction=correction_level),
             cache_enabled=False)
         url2 = qr_url_from_text(COMPLEX_TEST_TEXT,
                                 error_correction=correction_level,
                                 cache_enabled=False)
         url3 = qr_url_from_text(COMPLEX_TEST_TEXT,
                                 error_correction=correction_level,
                                 image_format='svg',
                                 cache_enabled=False)
         url4 = qr_url_from_text(COMPLEX_TEST_TEXT,
                                 error_correction=correction_level,
                                 image_format='SVG',
                                 cache_enabled=False)
         url5 = qr_url_from_text(COMPLEX_TEST_TEXT,
                                 options=QRCodeOptions(
                                     error_correction=correction_level,
                                     image_format='SVG'),
                                 cache_enabled=False)
         # Using an invalid image format should fallback to SVG.
         url6 = qr_url_from_text(COMPLEX_TEST_TEXT,
                                 error_correction=correction_level,
                                 image_format='invalid-format-name',
                                 cache_enabled=False)
         url = url1
         urls = get_urls_without_token_for_comparison(
             url1, url2, url3, url4, url5, url6)
         self.assertEqual(urls[0], urls[1])
         self.assertEqual(urls[0], urls[2])
         self.assertEqual(urls[0], urls[3])
         self.assertEqual(urls[0], urls[4])
         self.assertEqual(urls[0], urls[5])
         response = self.client.get(url)
         self.assertEqual(response.status_code, 200)
         source_image_data = response.content.decode('utf-8')
         # Skip header and adjust tag format.
         source_image_data = source_image_data[source_image_data.
                                               index('\n') + 1:]
         source_image_data = _make_closing_path_tag(source_image_data)
         ref_file_name = '%s_%s' % (base_file_name,
                                    correction_level.lower())
         if REFRESH_REFERENCE_IMAGES:
             write_svg_content_to_file(ref_file_name, source_image_data)
         ref_image_data = get_svg_content_from_file_name(ref_file_name)
         self.assertEqual(source_image_data, ref_image_data)
Exemplo n.º 15
0
def make_qr_code(text, qr_code_args, embedded):
    cache_enabled = DEFAULT_CACHE_ENABLED
    if 'cache_enabled' in qr_code_args:
        cache_enabled = qr_code_args.pop('cache_enabled')
    options = qr_code_args.get('options')
    if options:
        if not isinstance(options, QRCodeOptions):
            raise TypeError('The options argument must be of type QRCodeOptions.')
    else:
        options = QRCodeOptions(**qr_code_args)
    if embedded:
        return make_embedded_qr_code(text, options)
    else:
        return make_qr_code_url(text, options, cache_enabled=cache_enabled)
Exemplo n.º 16
0
def make_qr_code_url_with_args(text, qr_code_args):
    cache_enabled = DEFAULT_CACHE_ENABLED
    if 'cache_enabled' in qr_code_args:
        cache_enabled = qr_code_args.pop('cache_enabled')
    url_signature_enabled = DEFAULT_URL_SIGNATURE_ENABLED
    if 'url_signature_enabled' in qr_code_args:
        url_signature_enabled = qr_code_args.pop('url_signature_enabled')
    options = qr_code_args.get('options')
    if options:
        if not isinstance(options, QRCodeOptions):
            raise TypeError('The options argument must be of type QRCodeOptions.')
    else:
        options = QRCodeOptions(**qr_code_args)
    return make_qr_code_url(text, options, cache_enabled=cache_enabled, url_signature_enabled=url_signature_enabled)
Exemplo n.º 17
0
 def test_svg_url(self):
     is_first = True
     users = [None, AnonymousUser(), User(username='******')]
     for url_options in product([True, False, None], [True, False, None],
                                users):
         cache_enabled = url_options[0]
         url_signature_enabled = url_options[1]
         user = url_options[2]
         print("\t - cache_enabled=%s, url_signature_enabled=%s, user=%s" %
               (cache_enabled, url_signature_enabled, user))
         url_options_kwargs = dict()
         url0 = make_qr_code_url(
             TEST_TEXT, QRCodeOptions(size=1),
             **dict(**url_options_kwargs,
                    cache_enabled=cache_enabled,
                    url_signature_enabled=url_signature_enabled))
         if cache_enabled is not None:
             url_options_kwargs['cache_enabled'] = cache_enabled
         url1 = make_qr_code_url(
             TEST_TEXT, QRCodeOptions(size=1),
             **dict(**url_options_kwargs,
                    url_signature_enabled=url_signature_enabled))
         if url_signature_enabled is not None:
             url_options_kwargs[
                 'url_signature_enabled'] = url_signature_enabled
         url2 = qr_url_from_text(TEST_TEXT, size=1, **url_options_kwargs)
         url3 = qr_url_from_text(TEST_TEXT,
                                 image_format='svg',
                                 size=1,
                                 **url_options_kwargs)
         url4 = qr_url_from_text(TEST_TEXT,
                                 image_format='SVG',
                                 size=1,
                                 **url_options_kwargs)
         url5 = qr_url_from_text(TEST_TEXT,
                                 options=QRCodeOptions(image_format='SVG',
                                                       size=1),
                                 **url_options_kwargs)
         # Using an invalid image format should fallback to SVG.
         url6 = qr_url_from_text(TEST_TEXT,
                                 image_format='invalid-format-name',
                                 size=1,
                                 **url_options_kwargs)
         url = url1
         if url_signature_enabled is not False:
             urls = get_urls_without_token_for_comparison(
                 url0, url1, url2, url3, url4, url5, url6)
         else:
             urls = [url0, url1, url2, url3, url4, url5, url6]
         self.assertEqual(urls[0], urls[1])
         self.assertEqual(urls[0], urls[2])
         self.assertEqual(urls[0], urls[3])
         self.assertEqual(urls[0], urls[4])
         self.assertEqual(urls[0], urls[5])
         response = self.client.get(url)
         expected_status_code = 200
         if url_signature_enabled is False and not allows_external_request_from_user(
                 user):
             expected_status_code = 403
         self.assertEqual(response.status_code, expected_status_code)
         image_data = response.content.decode('utf-8')
         if expected_status_code == 200:
             if is_first and REFRESH_REFERENCE_IMAGES:
                 write_svg_content_to_file(
                     TestQRUrlFromTextResult.default_ref_base_file_name,
                     image_data)
                 is_first = False
             self.assertEqual(
                 image_data,
                 TestQRUrlFromTextResult.
                 _get_reference_result_for_default_svg())
Exemplo n.º 18
0
 def test_url_with_invalid_signature_token(self):
     valid_url_with_signature_token = make_qr_code_url(TEST_TEXT)
     url_with_invalid_signature_token = valid_url_with_signature_token.replace(
         'token=', 'token=some-front-padding')
     response = self.client.get(url_with_invalid_signature_token)
     self.assertEqual(response.status_code, 403)