test_validation_url.py 1.34 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
2
3
4
from django.test import TestCase
import pytest
from rest_framework.validators import ValidationError as RFValidationError
from django.core.validators import ValidationError as DJValidationError
5
from backend.validators.tag.url import validate_extension, validate_url
Florent Chehab's avatar
Florent Chehab committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45


class ValidationUrlTestCase(TestCase):

    @classmethod
    def setUpTestData(cls):
        cls.good = [
            "https://www.epfl.ch/publ6d4772210.svg",
            "http://google.fr/image.jpg"
        ]

        cls.bad = [
            "google"
        ]
        pass

    def test_validate_extension(self):

        for url in self.good:
            validate_extension(["svg", 'jpg'], url)

        for url in self.good + self.bad:
            with pytest.raises(RFValidationError):
                validate_extension(['png'], url)

    def test_validate_url(self):
        config_1 = {}
        config_2 = {"validators": {"extension": ["jpg", "jpeg", "png", "svg"]}}
        config_3 = {"validators": {"inconnu_au_bataillon": None}}

        for url in self.good:
            validate_url(config_1, url)
            validate_url(config_2, url)
            with pytest.raises(Exception):
                validate_url(config_3, url)

        for url in self.bad:
            with pytest.raises(DJValidationError):
                validate_url(config_1, url)
                validate_url(config_2, url)