test_validators.py 5.87 KB
Newer Older
1 2
from os.path import join

3 4 5 6 7 8 9 10 11
import pytest
from django.core.exceptions import ValidationError

from backend_app.validation.validators import (
    UsefulLinksValidator,
    PathExtensionValidator,
    TagNameValidator,
    PhotosValidator,
    TaggedItemValidator,
12
    ThemeValidator,
13
    ImageValidator,
14
)
15
from base_app.settings.dir_locations import BACKEND_ROOT_DIR
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


class TestUsefulLinksValidator(object):
    validator = UsefulLinksValidator()

    def test_empty_array_ok(self):
        data = []
        self.validator(data)

    def test_empty_object_not_ok(self):
        data = {}
        with pytest.raises(ValidationError):
            self.validator(data)

    def test_one_ok(self):
        data = [{"description": "Coucou", "url": "https://utc.fr"}]
        self.validator(data)

    def test_fails_with_more_data(self):
        data = [{"description": "Coucou", "url": "https://utc.fr", "lolilol": True}]
        with pytest.raises(ValidationError):
            self.validator(data)

    def test_not_url(self):
        data = [{"description": "Coucou", "url": "utc"}]
        with pytest.raises(ValidationError):
            self.validator(data)


45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
class TestThemeValidator(object):
    validator = ThemeValidator()

    @staticmethod
    def build(mode, ligth_primary, light_secondary, dark_primary, dark_secondary):
        return dict(
            mode=mode,
            light=dict(primary=ligth_primary, secondary=light_secondary),
            dark=dict(primary=dark_primary, secondary=dark_secondary),
        )

    def test_ok(self):
        data = self.build("dark", "#123456", "#123456", "#123456", "#123456")
        self.validator(data)

    def test_ok_2(self):
        data = self.build("light", "#123456", "#123456", "#123456", "#123456")
        self.validator(data)

    def test_not_ok_mode(self):
        data = self.build("darkos", "#123456", "#123456", "#123456", "#123456")
        with pytest.raises(ValidationError):
            self.validator(data)

    def test_not_ok_color(self):
        data = self.build("dark", "#1zzzzz", "#123456", "#123456", "#123456")
        with pytest.raises(ValidationError):
            self.validator(data)

    def test_not_ok_extra_1(self):
        data = self.build("dark", "#123456", "#123456", "#123456", "#123456")
        data["lol"] = "lolilol"
        with pytest.raises(ValidationError):
            self.validator(data)

    def test_not_ok_extra_in_palette(self):
        data = self.build("dark", "#123456", "#123456", "#123456", "#123456")
        data["light"]["lol"] = "lolilol"
        with pytest.raises(ValidationError):
            self.validator(data)


87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
class TestPhotosValidator(object):
    validator = PhotosValidator()

    def test_empty_array_ok(self):
        data = []
        self.validator(data)

    def test_empty_object_not_ok(self):
        data = {}
        with pytest.raises(ValidationError):
            self.validator(data)

    def test_one_ok(self):
        data = [{"title": "Coucou", "url": "https://utc.fr/logo.svg"}]
        self.validator(data)

    def test_not_ok(self):
        data = [{"title": "Coucou", "url": "https://utc.fr/logosvg"}]
        with pytest.raises(ValidationError):
            self.validator(data)

    def test_fails_with_more_data(self):
        data = [{"title": "Coucou", "url": "https://utc.fr/logo.svg", "héhé": "houhou"}]
        with pytest.raises(ValidationError):
            self.validator(data)


class TestTaggedItemValidator(object):
    validator = TaggedItemValidator()

    def test_photos_tag_ok(self):
        data = [{"title": "Coucou", "url": "https://utc.fr/logo.svg"}]
        self.validator("photos", data)

    def test_photos_tag_not_ok(self):
        data = [{"title": "Coucou", "url": "//utc.fr/logo.svg"}]
        with pytest.raises(ValidationError):
            self.validator("photos", data)

    def test_unknown_tag(self):
        data = []
        with pytest.raises(ValidationError):
            self.validator("photossqsdknrncLQSKDJNCZIURYNCCH", data)


class TestPathExtensionValidator(object):
    validator = PathExtensionValidator(["svg"])

    def test_svg_ok(self):
        self.validator("test.svg")

    def test_no_extension(self):
        with pytest.raises(ValidationError):
            self.validator("test")

    def test_multiple(self):
        with pytest.raises(ValidationError):
            self.validator("test.svg.zero")


class TestTagNameValidator(object):
    validator = TagNameValidator()

    def test_on_valid_tagname(self):
        # Might need to change this at one point
        # it transport is no more available
        self.validator("transport")

    def test_on_invalid_tagname(self):
        # ie a tag name that doesn't have a matching schema
        with pytest.raises(ValidationError):
            self.validator("qmlskdjmlqfinzoieuncmlkqsdnkuy")
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200


class TestImageValidator(object):
    validator = ImageValidator()

    # With file path input

    def test_on_valid_png_fp(self):
        self.validator(
            join(BACKEND_ROOT_DIR, "base_app/static/base_app/favicon/favicon-16x16.png")
        )

    def test_on_valid_svg_fp(self):
        self.validator(
            join(BACKEND_ROOT_DIR, "base_app/static/base_app/favicon/favicon.svg")
        )

    def test_on_invalid_file_fp(self):
        with pytest.raises(ValidationError):
            self.validator(__file__)

    # With file input directly

    def test_on_valid_png(self):
        with open(
            join(
                BACKEND_ROOT_DIR, "base_app/static/base_app/favicon/favicon-16x16.png"
            ),
            "rb",
        ) as f:
            self.validator(f)

    def test_on_valid_svg(self):
        with open(
            join(BACKEND_ROOT_DIR, "base_app/static/base_app/favicon/favicon.svg"), "rb"
        ) as f:
            self.validator(f)

    def test_on_invalid_file(self):
        with open(__file__, "rb") as f:
            with pytest.raises(ValidationError):
                self.validator(f)