Projet

Général

Profil

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

Lauréline Guérin, 04 juin 2020 16:22

Télécharger (8,49 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 |   7 ++-
 tests/test_cells.py  | 107 +++++++++++++++++++++++++++++++------------
 2 files changed, 83 insertions(+), 31 deletions(-)
combo/data/models.py
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 data_url['url'].count('{{') > 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/'}):
212
        with mock.patch('combo.data.models.requests.get') as requests_get:
213
            mock_json = mock.Mock(status_code=200)
214
            requests_get.return_value = mock_json
215
            cell.save()
216
        assert requests_get.call_args_list[0][0][0] == 'http://www.example.net/'
217
        assert ValidityInfo.objects.exists() is False
210
    settings.TEMPLATE_VARS = {'var1': 'foo'}
211
    cell.url = 'http://foo?varone={{var1}}'
212
    with mock.patch('combo.data.models.requests.get') as requests_get:
213
        mock_json = mock.Mock(status_code=200)
214
        requests_get.return_value = mock_json
215
        cell.save()
216
    assert ValidityInfo.objects.exists() is False
218 217

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

  
227 224
    # 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
        with mock.patch('combo.data.models.requests.get') as requests_get:
231
            mock_json = mock.Mock(status_code=200)
232
            requests_get.return_value = mock_json
233
            cell.save()
234
        assert requests_get.call_args_list == []
235
        assert ValidityInfo.objects.exists() is False
225
    settings.TEMPLATE_VARS = {'var1': 'foo', 'var2': 'bar'}
226
    cell.url = 'http://foo?varone={{var1}}&vartwo={{var2}}'
227
    with mock.patch('combo.data.models.requests.get') as requests_get:
228
        mock_json = mock.Mock(status_code=404)
229
        requests_get.return_value = mock_json
230
        cell.save()
231
    assert ValidityInfo.objects.exists() is False
236 232

  
237 233

  
238 234
def test_link_list_cell():
......
562 558
    cell = JsonCell.objects.create(
563 559
        page=page, placeholder='content', order=1,
564 560
        varnames_str='var1, var2, ',
565
        url='http://foo?varone=[var1]&vartwo=[var2]',
566
        template_string='/var1={{var1}}/var2={{var2}}/')
561
        url='http://foo?varone=[var1]&vartwo=[var2]')
567 562

  
568 563
    with mock.patch('combo.data.models.requests.get') as requests_get:
569 564
        cell.get_cell_extra_context(context)
570 565
    assert requests_get.call_args_list == []  # invalid context
571 566
    assert ValidityInfo.objects.exists() is False
572 567

  
573
    context['var1'] = 'foo'
574
    context['var2'] = 'bar'
575 568
    context['synchronous'] = True  # to get fresh content
569
    cell.varnames_str = ''
570
    cell.url = 'http://foo'
576 571
    with mock.patch('combo.utils.requests.get') as requests_get:
577 572
        mock_json = mock.Mock(status_code=404)
578 573
        requests_get.side_effect = [mock_json]
......
601 596
    assert validity_info.invalid_reason_code == 'data_url_invalid'
602 597
    assert validity_info.invalid_since is not None
603 598

  
599
    # url with a single variable
600
    context['var1'] = 'foo'
601
    cell.varnames_str = 'var1'
602
    cell.url = 'http://foo?varone={{var1}}'
603
    with mock.patch('combo.utils.requests.get') as requests_get:
604
        mock_json = mock.Mock(status_code=200)
605
        requests_get.return_value = mock_json
606
        cell.get_cell_extra_context(context)
607
    assert ValidityInfo.objects.exists() is False
608

  
609
    with mock.patch('combo.utils.requests.get') as requests_get:
610
        mock_json = mock.Mock(status_code=404)
611
        requests_get.side_effect = [mock_json]
612
        cell.get_cell_extra_context(context)
613
    assert ValidityInfo.objects.exists() is True
614

  
615
    # url with two variables
616
    context['var2'] = 'bar'
617
    cell.varnames_str = 'var1, var2'
618
    cell.url = 'http://foo?varone={{var1}}&vartwo={{var2}}'
619
    with mock.patch('combo.utils.requests.get') as requests_get:
620
        mock_json = mock.Mock(status_code=404)
621
        requests_get.side_effect = [mock_json]
622
        cell.get_cell_extra_context(context)
623
    assert ValidityInfo.objects.exists() is False
624

  
604 625

  
605 626
def test_config_json_cell():
606 627
    page = Page(title='example page', slug='example-page')
......
747 768
    assert requests_get.call_args_list == []  # invalid context
748 769
    assert ValidityInfo.objects.exists() is False
749 770

  
750
    context['var1'] = 'foo'
751
    context['var2'] = 'bar'
752 771
    context['synchronous'] = True  # to get fresh content
772
    settings.JSON_CELL_TYPES['test-config-json-cell']['url'] = 'http://foo'
773
    settings.JSON_CELL_TYPES['test-config-json-cell']['varnames'] = []
753 774
    with mock.patch('combo.utils.requests.get') as requests_get:
754 775
        mock_json = mock.Mock(status_code=404)
755 776
        requests_get.side_effect = [mock_json]
......
778 799
    assert validity_info.invalid_reason_code == 'data_url_invalid'
779 800
    assert validity_info.invalid_since is not None
780 801

  
802
    # url with a single variable
803
    context['var1'] = 'foo'
804
    settings.JSON_CELL_TYPES['test-config-json-cell']['varnames'] = ['var1']
805
    settings.JSON_CELL_TYPES['test-config-json-cell']['url'] = 'http://foo?varone={{var1}}'
806
    with mock.patch('combo.utils.requests.get') as requests_get:
807
        mock_json = mock.Mock(status_code=200)
808
        requests_get.return_value = mock_json
809
        cell.get_cell_extra_context(context)
810
    assert ValidityInfo.objects.exists() is False
811

  
812
    with mock.patch('combo.utils.requests.get') as requests_get:
813
        mock_json = mock.Mock(status_code=404)
814
        requests_get.side_effect = [mock_json]
815
        cell.get_cell_extra_context(context)
816
    assert ValidityInfo.objects.exists() is True
817

  
818
    # url with two variables
819
    context['var2'] = 'bar'
820
    settings.JSON_CELL_TYPES['test-config-json-cell']['varnames'] = ['var1', 'var2']
821
    settings.JSON_CELL_TYPES['test-config-json-cell']['url'] = 'http://foo?varone={{var1}}&vartwo={{var2}}'
822
    with mock.patch('combo.utils.requests.get') as requests_get:
823
        mock_json = mock.Mock(status_code=404)
824
        requests_get.side_effect = [mock_json]
825
        cell.get_cell_extra_context(context)
826
    assert ValidityInfo.objects.exists() is False
827

  
781 828

  
782 829
def test_json_force_async():
783 830
    cell = JsonCellBase()
784
-