Projet

Général

Profil

0001-cells-don-t-check-validity-for-json-cells-with-multi.patch

Lauréline Guérin, 04 juin 2020 14:56

Télécharger (9,13 ko)

Voir les différences:

Subject: [PATCH] cells: don't check validity for json cells with multiple
 variables in url (#43604)

 combo/data/models.py |   9 +++-
 tests/test_cells.py  | 112 ++++++++++++++++++++++++++++++++++++-------
 2 files changed, 103 insertions(+), 18 deletions(-)
combo/data/models.py
1228 1228
            self.mark_as_invalid('data_url_not_defined')
1229 1229
            return
1230 1230

  
1231
        if self.url.count('{{') > 1:
1231
        if sum(map(lambda a: self.url.count(a), ['{{', '['])) > 1:
1232 1232
            self.mark_as_valid()
1233 1233
            return
1234 1234

  
......
1565 1565
            context[data_key] = extra_context[data_key]
1566 1566

  
1567 1567
        if not self._meta.abstract:
1568
            returns = [extra_context.get(d['key'] + '_status') for d in data_urls]
1568
            returns = []
1569
            for data_url in data_urls:
1570
                if sum(map(lambda a: data_url['url'].count(a), ['{{', '['])) > 1:
1571
                    # ignore returns of url with more than one variable
1572
                    continue
1573
                returns.append(extra_context.get(data_url['key'] + '_status'))
1569 1574
            returns = set([s for s in returns if s is not None])
1570 1575
            if returns and 200 not in returns:  # not a single valid answer
1571 1576
                if 404 in returns:
tests/test_cells.py
146 146
    assert cell.render(ctx).strip() == '<a href="http://example.net/#anchor">altertitle</a>'
147 147

  
148 148

  
149
def test_link_cell_validity():
149
def test_link_cell_validity(settings):
150 150
    page = Page.objects.create(title='example page', slug='example-page')
151 151
    cell = LinkCell.objects.create(
152 152
        page=page,
......
207 207
    # external link with a single variable
208 208
    cell.link_page = None
209 209
    cell.anchor = ''
210
    cell.url = '{{test_url}}'
211
    with override_settings(TEMPLATE_VARS={'test_url': 'http://www.example.net/'}):
210
    settings.TEMPLATE_VARS = {'var1': 'foo'}
211
    test_urls = [
212
        'http://foo?varone=[var1]',
213
        'http://foo?varone={{var1}}',
214
    ]
215
    for test_url in test_urls:
216
        cell.url = test_url
212 217
        with mock.patch('combo.data.models.requests.get') as requests_get:
213 218
            mock_json = mock.Mock(status_code=200)
214 219
            requests_get.return_value = mock_json
215 220
            cell.save()
216
        assert requests_get.call_args_list[0][0][0] == 'http://www.example.net/'
217 221
        assert ValidityInfo.objects.exists() is False
218 222

  
219
    with override_settings(TEMPLATE_VARS={'test_url': 'http://www.example.net/'}):
220 223
        with mock.patch('combo.data.models.requests.get') as requests_get:
221 224
            mock_json = mock.Mock(status_code=404)
222 225
            requests_get.return_value = mock_json
223 226
            cell.save()
224
        assert requests_get.call_args_list[0][0][0] == 'http://www.example.net/'
225 227
        assert ValidityInfo.objects.exists() is True
226 228

  
227 229
    # external link with two variables
228
    cell.url = '{{test_url}}/path/{{other}}'
229
    with override_settings(TEMPLATE_VARS={'test_url': 'http://www.example.net/'}):
230
    settings.TEMPLATE_VARS = {'var1': 'foo', 'var2': 'bar'}
231
    test_urls = [
232
        'http://foo?varone=[var1]&vartwo=[var2]',
233
        'http://foo?varone={{var1}}&vartwo={{var2}}',
234
        'http://foo?varone=[var1]&vartwo={{var2}}',
235
    ]
236
    for test_url in test_urls:
237
        cell.url = test_url
230 238
        with mock.patch('combo.data.models.requests.get') as requests_get:
231
            mock_json = mock.Mock(status_code=200)
239
            mock_json = mock.Mock(status_code=404)
232 240
            requests_get.return_value = mock_json
233 241
            cell.save()
234
        assert requests_get.call_args_list == []
235 242
        assert ValidityInfo.objects.exists() is False
236 243

  
237 244

  
......
562 569
    cell = JsonCell.objects.create(
563 570
        page=page, placeholder='content', order=1,
564 571
        varnames_str='var1, var2, ',
565
        url='http://foo?varone=[var1]&vartwo=[var2]',
566
        template_string='/var1={{var1}}/var2={{var2}}/')
572
        url='http://foo?varone=[var1]&vartwo=[var2]')
567 573

  
568 574
    with mock.patch('combo.data.models.requests.get') as requests_get:
569 575
        cell.get_cell_extra_context(context)
570 576
    assert requests_get.call_args_list == []  # invalid context
571 577
    assert ValidityInfo.objects.exists() is False
572 578

  
573
    context['var1'] = 'foo'
574
    context['var2'] = 'bar'
575 579
    context['synchronous'] = True  # to get fresh content
580
    cell.varnames_str = ''
581
    cell.url = 'http://foo'
576 582
    with mock.patch('combo.utils.requests.get') as requests_get:
577 583
        mock_json = mock.Mock(status_code=404)
578 584
        requests_get.side_effect = [mock_json]
......
601 607
    assert validity_info.invalid_reason_code == 'data_url_invalid'
602 608
    assert validity_info.invalid_since is not None
603 609

  
610
    # url with a single variable
611
    context['var1'] = 'foo'
612
    cell.varnames_str = 'var1'
613
    test_urls = [
614
        'http://foo?varone=[var1]',
615
        'http://foo?varone={{var1}}',
616
    ]
617
    for test_url in test_urls:
618
        cell.url = test_url
619
        with mock.patch('combo.utils.requests.get') as requests_get:
620
            mock_json = mock.Mock(status_code=200)
621
            requests_get.return_value = mock_json
622
            cell.get_cell_extra_context(context)
623
        assert ValidityInfo.objects.exists() is False
624

  
625
        with mock.patch('combo.utils.requests.get') as requests_get:
626
            mock_json = mock.Mock(status_code=404)
627
            requests_get.side_effect = [mock_json]
628
            cell.get_cell_extra_context(context)
629
        assert ValidityInfo.objects.exists() is True
630

  
631
    # url with two variables
632
    context['var2'] = 'bar'
633
    cell.varnames_str = 'var1, var2'
634
    test_urls = [
635
        'http://foo?varone=[var1]&vartwo=[var2]',
636
        'http://foo?varone={{var1}}&vartwo={{var2}}',
637
        'http://foo?varone=[var1]&vartwo={{var2}}',
638
    ]
639
    for test_url in test_urls:
640
        cell.url = test_url
641
        with mock.patch('combo.utils.requests.get') as requests_get:
642
            mock_json = mock.Mock(status_code=404)
643
            requests_get.side_effect = [mock_json]
644
            cell.get_cell_extra_context(context)
645
        assert ValidityInfo.objects.exists() is False
646

  
604 647

  
605 648
def test_config_json_cell():
606 649
    page = Page(title='example page', slug='example-page')
......
747 790
    assert requests_get.call_args_list == []  # invalid context
748 791
    assert ValidityInfo.objects.exists() is False
749 792

  
750
    context['var1'] = 'foo'
751
    context['var2'] = 'bar'
752 793
    context['synchronous'] = True  # to get fresh content
794
    settings.JSON_CELL_TYPES['test-config-json-cell']['url'] = 'http://foo'
795
    settings.JSON_CELL_TYPES['test-config-json-cell']['varnames'] = []
753 796
    with mock.patch('combo.utils.requests.get') as requests_get:
754 797
        mock_json = mock.Mock(status_code=404)
755 798
        requests_get.side_effect = [mock_json]
......
778 821
    assert validity_info.invalid_reason_code == 'data_url_invalid'
779 822
    assert validity_info.invalid_since is not None
780 823

  
824
    # url with a single variable
825
    context['var1'] = 'foo'
826
    test_urls = [
827
        'http://foo?varone=[var1]',
828
        'http://foo?varone={{var1}}',
829
    ]
830
    settings.JSON_CELL_TYPES['test-config-json-cell']['varnames'] = ['var1']
831
    for test_url in test_urls:
832
        settings.JSON_CELL_TYPES['test-config-json-cell']['url'] = test_url
833
        with mock.patch('combo.utils.requests.get') as requests_get:
834
            mock_json = mock.Mock(status_code=200)
835
            requests_get.return_value = mock_json
836
            cell.get_cell_extra_context(context)
837
        assert ValidityInfo.objects.exists() is False
838

  
839
        with mock.patch('combo.utils.requests.get') as requests_get:
840
            mock_json = mock.Mock(status_code=404)
841
            requests_get.side_effect = [mock_json]
842
            cell.get_cell_extra_context(context)
843
        assert ValidityInfo.objects.exists() is True
844

  
845
    # url with two variables
846
    context['var2'] = 'bar'
847
    settings.JSON_CELL_TYPES['test-config-json-cell']['varnames'] = ['var1', 'var2']
848
    test_urls = [
849
        'http://foo?varone=[var1]&vartwo=[var2]',
850
        'http://foo?varone={{var1}}&vartwo={{var2}}',
851
        'http://foo?varone=[var1]&vartwo={{var2}}',
852
    ]
853
    for test_url in test_urls:
854
        settings.JSON_CELL_TYPES['test-config-json-cell']['url'] = test_url
855
        with mock.patch('combo.utils.requests.get') as requests_get:
856
            mock_json = mock.Mock(status_code=404)
857
            requests_get.side_effect = [mock_json]
858
            cell.get_cell_extra_context(context)
859
        assert ValidityInfo.objects.exists() is False
860

  
781 861

  
782 862
def test_json_force_async():
783 863
    cell = JsonCellBase()
784
-