test_validation_url.py 1.32 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_app.validators.tags import validate_extension, validate_url
Florent Chehab's avatar
Florent Chehab committed
6
7
8
9
10
11
12


class ValidationUrlTestCase(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.good = [
            "https://www.epfl.ch/publ6d4772210.svg",
13
            "http://google.fr/image.jpg",
Florent Chehab's avatar
Florent Chehab committed
14
15
        ]

16
        cls.bad = ["google"]
Florent Chehab's avatar
Florent Chehab committed
17
18
19
20
21
        pass

    def test_validate_extension(self):

        for url in self.good:
22
            validate_extension(["svg", "jpg"], url)
Florent Chehab's avatar
Florent Chehab committed
23
24
25

        for url in self.good + self.bad:
            with pytest.raises(RFValidationError):
26
                validate_extension(["png"], url)
Florent Chehab's avatar
Florent Chehab committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

    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)