Projet

Général

Profil

0005-wcs-check-related_card_path-validity-58833.patch

Lauréline Guérin, 09 décembre 2021 14:57

Télécharger (3,63 ko)

Voir les différences:

Subject: [PATCH 5/5] wcs: check related_card_path validity (#58833)

 combo/apps/wcs/models.py | 10 ++++++++++
 tests/test_wcs.py        | 42 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)
combo/apps/wcs/models.py
41 41
    'wcs_form_not_found': _('Invalid form'),
42 42
    'wcs_card_not_defined': _('No card model set'),
43 43
    'wcs_card_not_found': _('Invalid card model'),
44
    'wcs_card_relation_not_found': _('Invalid Card Identifier'),
44 45
    'wcs_category_not_defined': _('No category set'),
45 46
    'wcs_category_not_found': _('Invalid category'),
46 47
    'wcs_site_not_found': _('Invalid site'),
......
963 964

  
964 965
        populate_cache()
965 966

  
967
    def check_validity(self):
968
        if self.related_card_path:
969
            relations = [r[0] for r in self.get_related_card_paths()]
970
            if self.related_card_path not in relations:
971
                self.mark_as_invalid('wcs_card_relation_not_found')
972
                return
973

  
974
        self.mark_as_valid()
975

  
966 976
    @property
967 977
    def global_context_key(self):
968 978
        return '%s-card-ids' % self.get_reference()
tests/test_wcs.py
1917 1917
    assert validity_info.invalid_since is not None
1918 1918

  
1919 1919

  
1920
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
1921
def test_card_cell_check_validity(mock_send):
1922
    page = Page.objects.create(title='xxx', template_name='standard')
1923
    cell = WcsCardInfosCell.objects.create(
1924
        page=page,
1925
        placeholder='content',
1926
        order=0,
1927
        carddef_reference='default:card_a',
1928
        card_ids='1',
1929
    )
1930
    cell2 = WcsCardInfosCell.objects.create(
1931
        page=page, placeholder='content', order=1, carddef_reference='default:card_b'
1932
    )
1933

  
1934
    # no related_card_path
1935
    cell2.check_validity()
1936
    assert ValidityInfo.objects.exists() is False
1937

  
1938
    # correct related_card_path but sluga is not defined
1939
    cell2.related_card_path = 'sluga/cardb'
1940
    cell2.save()
1941
    cell2.check_validity()
1942
    validity_info = ValidityInfo.objects.latest('pk')
1943
    assert validity_info.invalid_reason_code == 'wcs_card_relation_not_found'
1944
    assert validity_info.invalid_since is not None
1945

  
1946
    # sluga is now defined
1947
    cell.slug = 'sluga'
1948
    cell.save()
1949
    cell2.check_validity()
1950
    assert ValidityInfo.objects.exists() is False
1951

  
1952
    # bad related_card_path
1953
    cell2.related_card_path = 'sluga/foobar'
1954
    cell2.save()
1955
    cell2.check_validity()
1956
    validity_info = ValidityInfo.objects.latest('pk')
1957
    assert validity_info.invalid_reason_code == 'wcs_card_relation_not_found'
1958
    assert validity_info.invalid_since is not None
1959

  
1960

  
1920 1961
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
1921 1962
def test_manager_card_cell(mock_send, app, admin_user):
1922 1963
    page = Page.objects.create(title='xxx', slug='test_cards', template_name='standard')
......
3423 3464
            WcsCurrentFormsCell,
3424 3465
            WcsCurrentDraftsCell,
3425 3466
            WcsFormsOfCategoryCell,
3467
            WcsCardInfosCell,
3426 3468
            BackofficeSubmissionCell,
3427 3469
            CategoriesCell,
3428 3470
            TrackingCodeInputCell,
3429
-