Project

General

Profile

0001-user_import-make-sure-legacy-utf-8-encoding-doesn-t-.patch

Paul Marillonnet, 23 Jun 2021 03:43 PM

Download (2.67 KB)

View differences:

Subject: [PATCH] user_import: make sure legacy utf-8 encoding doesn't crash
 (#55008)

 src/authentic2/manager/user_views.py |  3 +++
 tests/test_user_manager.py           | 31 ++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)
src/authentic2/manager/user_views.py
842 842
        self.user_import = UserImport(uuid)
843 843
        if not self.user_import.exists():
844 844
            raise Http404
845
        if self.user_import.encoding == 'utf-8':
846
            with self.user_import.meta_update as meta:
847
                meta['encoding'] = 'utf-8-sig'
845 848
        return super(UserImportView, self).dispatch(request, uuid, **kwargs)
846 849

  
847 850
    def get(self, request, uuid, filename=None):
tests/test_user_manager.py
584 584
    app.get('/manage/users/import/%s/%s/' % (_import.uuid, execute.uuid), status=403)
585 585

  
586 586

  
587
def test_user_import_legacy_encoding(transactional_db, app, admin, ou1, admin_ou1):
588
    response = login(app, admin, '/manage/users/')
589
    response = response.click('Import users')
590
    response.form.set(
591
        'import_file',
592
        Upload(
593
            'users.csv',
594
            '''email key verified,first_name,last_name,phone
595
tnoel@entrouvert.com,Thomas,Noël,1234
596
fpeters@entrouvert.com,Frédéric,Péters,5678
597
john.doe@entrouvert.com,John,Doe,9101112
598
x,x,x,x'''.encode(
599
                'utf-8'
600
            ),
601
            'application/octet-stream',
602
        ),
603
    )
604
    response.form.set('encoding', 'utf-8-sig')
605
    response.form.set('ou', str(get_default_ou().pk))
606
    response = response.form.submit()
607

  
608
    imports = [i for i in user_import.UserImport.all()]
609
    # oops, utf-8 used to be supported. now it's utf-8-sig but imports may have
610
    # been created with utf-8 encoding and not executed yet
611
    with imports[0].meta_update as meta:
612
        meta['encoding'] = 'utf-8'  # not supported anymore
613

  
614
    response = response.follow()
615
    response = response.forms['action-form'].submit(name='simulate')
616

  
617

  
587 618
def test_su_permission(app, admin, simple_user):
588 619
    resp = login(app, admin, '/manage/users/%s/' % simple_user.pk)
589 620
    assert len(resp.pyquery('button[name="su"]')) == 0
590
-