Ejemplo n.º 1
0
def test_create_invoices_form(session):
    municipalities = MunicipalityCollection(session)
    adlikon = municipalities.add(name="Adlikon", bfs_number=21)
    aesch = municipalities.add(name="Aesch", bfs_number=241)

    # Test choices
    form = CreateInvoicesForm()
    form.request = Request(session)
    form.on_request()
    assert form.municipality_id.choices == [('-', 'For all municipalities'),
                                            (adlikon.id.hex, 'Adlikon (21)'),
                                            (aesch.id.hex, 'Aesch (241)')]

    # Test update model
    form.from_date.data = date(2019, 1, 1)
    form.to_date.data = date(2019, 1, 31)
    form.cs2_user.data = '123456'
    form.subject.data = 'Rechnungen'
    form.municipality_id.data = aesch.id.hex
    form.accounting_unit.data = '99999'
    form.revenue_account.data = '987654321'

    model = Invoice(session)
    form.update_model(model)
    assert model.from_date == date(2019, 1, 1)
    assert model.to_date == date(2019, 1, 31)
    assert model.cs2_user == '123456'
    assert model.subject == 'Rechnungen'
    assert model.municipality_id == aesch.id.hex
    assert model.accounting_unit == '99999'
    assert model.revenue_account == '987654321'

    form.municipality_id.data = '-'
    form.update_model(model)
    assert model.municipality_id is None
Ejemplo n.º 2
0
def test_municipality_form(session):
    payment_types = PaymentTypeCollection(session)
    payment_types.add(name='normal', _price_per_quantity=700)
    payment_types.add(name='spezial', _price_per_quantity=850)

    municipalities = MunicipalityCollection(session)
    municipality = municipalities.add(name="Boppelsen",
                                      bfs_number=82,
                                      payment_type='normal')

    # Test choices
    form = MunicipalityForm()
    form.request = Request(session)
    form.on_request()
    assert form.payment_type.choices == [('normal', 'Normal'),
                                         ('spezial', 'Spezial')]

    # Test apply / update
    form.apply_model(municipality)
    assert form.name.data == "Boppelsen"
    assert form.bfs_number.data == 82
    assert form.address_supplement.data is None
    assert form.gpn_number.data is None
    assert form.payment_type.data == 'normal'

    form.name.data = "Adlikon"
    form.bfs_number.data = 21
    form.address_supplement.data = "Zusatz"
    form.gpn_number.data = 1122
    form.payment_type.data = 'spezial'

    form.update_model(municipality)
    assert municipality.name == "Adlikon"
    assert municipality.bfs_number == 21
    assert municipality.address_supplement == "Zusatz"
    assert municipality.gpn_number == 1122
    assert municipality.payment_type == 'spezial'

    # Test validation
    form = MunicipalityForm()
    form.request = Request(session)
    form.on_request()
    assert not form.validate()

    form = MunicipalityForm(PostData({
        'bfs_number': '82',
    }))
    form.request = Request(session)
    form.on_request()
    assert not form.validate()

    form = MunicipalityForm(
        PostData({
            'name': "Boppelsen",
            'bfs_number': '82',
            'payment_type': 'normal'
        }))
    form.request = Request(session)
    form.on_request()
    assert form.validate()
Ejemplo n.º 3
0
def test_edit_scan_job_form(session):
    municipalities = MunicipalityCollection(session)
    municipality = municipalities.add(name="Boppelsen", bfs_number=82)
    municipality.pickup_dates.append(PickupDate(date=date(2019, 1, 7)))
    municipality.pickup_dates.append(PickupDate(date=date(2019, 1, 8)))

    # Test apply / update
    model = ScanJob()
    model.municipality_id = municipality.id
    model.type = 'normal'
    model.dispatch_date = date(2019, 1, 8)
    model.dispatch_boxes = 1
    model.dispatch_tax_forms_current_year = 2
    model.dispatch_tax_forms_last_year = 3
    model.dispatch_tax_forms_older = 4
    model.dispatch_single_documents = 5
    model.dispatch_note = 'Note on dispatch'
    model.dispatch_cantonal_tax_office = 6
    model.dispatch_cantonal_scan_center = 7

    form = EditScanJobForm()
    form.request = Request(session, groupid=municipality.id.hex)
    form.apply_model(model)
    assert form.dispatch_boxes.data == 1
    assert form.dispatch_tax_forms_current_year.data == 2
    assert form.dispatch_tax_forms_last_year.data == 3
    assert form.dispatch_tax_forms_older.data == 4
    assert form.dispatch_single_documents.data == 5
    assert form.dispatch_note.data == 'Note on dispatch'
    assert form.dispatch_cantonal_tax_office.data == 6
    assert form.dispatch_cantonal_scan_center.data == 7

    form.dispatch_boxes.data = 10
    form.dispatch_tax_forms_current_year.data = 20
    form.dispatch_tax_forms_last_year.data = 30
    form.dispatch_tax_forms_older.data = 40
    form.dispatch_single_documents.data = 50
    form.dispatch_note.data = 'A note on the dispatch'
    form.dispatch_cantonal_tax_office.data = 60
    form.dispatch_cantonal_scan_center.data = 70

    form.update_model(model)
    assert model.municipality_id == municipality.id
    assert model.type == 'normal'
    assert model.dispatch_date == date(2019, 1, 8)
    assert model.dispatch_boxes == 10
    assert model.dispatch_tax_forms_current_year == 20
    assert model.dispatch_tax_forms_last_year == 30
    assert model.dispatch_tax_forms_older == 40
    assert model.dispatch_single_documents == 50
    assert model.dispatch_note == 'A note on the dispatch'
    assert model.dispatch_cantonal_tax_office == 60
    assert model.dispatch_cantonal_scan_center == 70

    # Test validation
    form = EditScanJobForm()
    form.request = Request(session, groupid=municipality.id.hex)
    assert form.validate()
Ejemplo n.º 4
0
def test_municipality_selection_form(session):
    municipalities = MunicipalityCollection(session)
    municipality = municipalities.add(name="Aesch", bfs_number=82)

    # Test choices
    form = MunicipalityIdSelectionForm()
    form.request = Request(session)
    form.on_request()
    assert [c[1] for c in form.municipality_id.choices] == ["Aesch"]

    form.municipality_id.data = municipality.id
    assert form.municipality == municipality
Ejemplo n.º 5
0
def test_report_selection_form(session):
    municipalities = MunicipalityCollection(session)
    adlikon = municipalities.add(name="Adlikon", bfs_number=21)
    aesch = municipalities.add(name="Aesch", bfs_number=241)

    # Test choices
    form = ReportSelectionForm()
    form.request = Request(session)
    form.on_request()
    assert form.municipality_id.choices == [(adlikon.id.hex, 'Adlikon (21)'),
                                            (aesch.id.hex, 'Aesch (241)')]

    # Test get model
    form.start.data = date(2019, 1, 1)
    form.end.data = date(2019, 1, 31)
    form.scan_job_type.data = 'express'
    form.municipality_id.data = aesch.id.hex

    form.report_type.data = 'boxes'
    model = form.get_model()
    assert model.__class__.__name__ == 'ReportBoxes'
    assert model.start == date(2019, 1, 1)
    assert model.end == date(2019, 1, 31)

    form.report_type.data = 'boxes_and_forms'
    model = form.get_model()
    assert model.__class__.__name__ == 'ReportBoxesAndForms'
    assert model.start == date(2019, 1, 1)
    assert model.end == date(2019, 1, 31)
    assert model.type == 'express'

    form.report_type.data = 'all_forms'
    model = form.get_model()
    assert model.__class__.__name__ == 'ReportFormsAllMunicipalities'
    assert model.start == date(2019, 1, 1)
    assert model.end == date(2019, 1, 31)
    assert model.type == 'express'

    form.report_type.data = 'forms'
    model = form.get_model()
    assert model.__class__.__name__ == 'ReportFormsByMunicipality'
    assert model.start == date(2019, 1, 1)
    assert model.end == date(2019, 1, 31)
    assert model.type == 'express'
    assert model.municipality_id == aesch.id.hex

    form.report_type.data = 'delivery'
    model = form.get_model()
    assert model.__class__.__name__ == 'ReportBoxesAndFormsByDelivery'
    assert model.start == date(2019, 1, 1)
    assert model.end == date(2019, 1, 31)
    assert model.type == 'express'
    assert model.municipality_id == aesch.id.hex
Ejemplo n.º 6
0
def test_delete_municipality_dates_form(session):
    municipalities = MunicipalityCollection(session)
    municipality = municipalities.add(name="Boppelsen", bfs_number=82)

    # Test apply / update
    form = DeleteMunicipalityDatesForm()
    form.request = Request(session)
    form.apply_model(municipality)
    assert form.start.data is None
    assert form.end.data is None

    form.start.data = date(2019, 1, 1)
    form.end.data = date(2019, 12, 31)
    form.update_model(municipality)

    municipality.pickup_dates.append(PickupDate(date=date(2018, 12, 31)))
    municipality.pickup_dates.append(PickupDate(date=date(2019, 1, 1)))
    municipality.pickup_dates.append(PickupDate(date=date(2019, 6, 6)))
    municipality.pickup_dates.append(PickupDate(date=date(2019, 12, 31)))
    municipality.pickup_dates.append(PickupDate(date=date(2020, 1, 1)))

    with freeze_time("2018-01-05"):
        form.apply_model(municipality)
        assert form.start.data == date(2018, 12, 31)
        assert form.end.data == date(2020, 1, 1)

    with freeze_time("2019-01-05"):
        form.apply_model(municipality)
        assert form.start.data == date(2019, 1, 1)
        assert form.end.data == date(2020, 1, 1)

    form.start.data = date(2019, 1, 1)
    form.end.data = date(2019, 12, 31)
    form.update_model(municipality)
    assert [d.date for d in municipality.pickup_dates
            ] == [date(2018, 12, 31), date(2020, 1, 1)]

    # Test validation
    form = DeleteMunicipalityDatesForm()
    assert not form.validate()

    form = DeleteMunicipalityDatesForm(
        PostData({
            'start': "2019-01-01",
            'end': "2019-12-31"
        }))
    assert form.validate()
Ejemplo n.º 7
0
def test_scan_jobs(session):
    municipalities = MunicipalityCollection(session)
    municipality = municipalities.add(name='Boppelsen', bfs_number=82)

    scan_jobs = ScanJobCollection(session)
    for day in (2, 1, 4, 3):
        scan_jobs.add(type='normal',
                      municipality_id=municipality.id,
                      dispatch_date=date(2019, 1, day))

    assert [s.dispatch_date.day for s in scan_jobs.query()] == [4, 3, 2, 1]
    assert [s.delivery_number for s in scan_jobs.query()] == [3, 4, 1, 2]

    municipality = municipalities.add(name='Adlikon', bfs_number=21)
    scan_jobs.add(type='normal',
                  municipality_id=municipality.id,
                  dispatch_date=date(2019, 1, 5))
    assert [s.delivery_number for s in scan_jobs.query()] == [1, 3, 4, 1, 2]

    assert scan_jobs.next_delivery_number(None) == 1
    assert scan_jobs.next_delivery_number(municipality.id) == 2
Ejemplo n.º 8
0
def test_unrestricted_scan_jobs_form(session):
    scan_jobs = ScanJobCollection(session)
    municipalities = MunicipalityCollection(session)
    municipalities.add(name="Adlikon", bfs_number=21)
    municipalities.add(name="Aesch", bfs_number=241)

    # Test on request / popluate
    form = UnrestrictedScanJobsForm()
    form.request = Request(session)
    form.on_request()
    assert [m[1] for m in form.municipality_id.choices
            ] == ['Adlikon (21)', 'Aesch (241)']

    # Test apply
    form.apply_model(scan_jobs)
    assert form.sort_by.data is None
    assert form.sort_order.data is None
    assert form.from_date.data is None
    assert form.to_date.data is None
    assert form.type.data == ['normal', 'express']
    assert form.municipality_id.data is None
    assert form.term.data is None

    scan_jobs.sort_by = 'delivery_number'
    scan_jobs.sort_order = 'ascending'
    scan_jobs.from_date = date(2010, 1, 1)
    scan_jobs.to_date = date(2010, 12, 31)
    scan_jobs.type = ['express']
    scan_jobs.term = 'term'
    scan_jobs.municipality_id = [municipalities.query().first().id]

    form.apply_model(scan_jobs)

    assert form.sort_by.data == 'delivery_number'
    assert form.sort_order.data == 'ascending'
    assert form.from_date.data == date(2010, 1, 1)
    assert form.to_date.data == date(2010, 12, 31)
    assert form.type.data == ['express']
    assert form.term.data == 'term'
    assert form.municipality_id.data == [municipalities.query().first().id]

    # Test validation
    form = UnrestrictedScanJobsForm()
    assert form.validate()
Ejemplo n.º 9
0
    def top_navigation(self):
        has_permission = self.request.has_permission
        session = self.request.session
        result = []
        if has_permission(ScanJobCollection(session), ViewModel):
            result.append(Link(_("Scan jobs"), self.scan_jobs_url))
        if has_permission(DailyList(), ViewModel):
            result.append(Link(_("Daily list"), self.daily_list_url))
        if has_permission(Report(session), ViewModel):
            result.append(Link(_("Report"), self.report_url))
        if has_permission(Invoice(session), ViewModel):
            result.append(Link(_("Invoices"), self.invoices_url))
        if has_permission(UserCollection(session), ViewModel):
            result.append(Link(_("Users"), self.users_url))
        if has_permission(MunicipalityCollection(session), ViewModel):
            result.append(Link(_("Municipalities"), self.municipalities_url))
        if has_permission(NotificationCollection(session), ViewModel):
            result.append(Link(_("Notifications"), self.notifications_url))
        if has_permission(UserManual(self.app), ViewModel):
            result.append(Link(_("User manual"), self.user_manual_url))

        return result
Ejemplo n.º 10
0
def test_scan_jobs_pagination(session):
    municipality_id = uuid4()
    MunicipalityCollection(session).add(id=municipality_id,
                                        name='Adlikon',
                                        bfs_number=21)

    scan_jobs = ScanJobCollection(session)

    assert scan_jobs.pages_count == 0
    assert scan_jobs.batch == []
    assert scan_jobs.page_index == 0
    assert scan_jobs.offset == 0
    assert scan_jobs.previous is None
    assert scan_jobs.next is None
    assert scan_jobs.page_by_index(0) == scan_jobs

    for day in range(1, 27):
        scan_jobs.add(
            type='express',
            dispatch_date=date(2019, 1, day),
            municipality_id=municipality_id,
        )

    scan_jobs = ScanJobCollection(session)
    assert scan_jobs.query().count() == 26
    assert scan_jobs.subset_count == 26
    assert scan_jobs.pages_count == 2
    assert len(scan_jobs.batch) == 20
    assert scan_jobs.page_index == 0
    assert scan_jobs.offset == 0
    assert scan_jobs.previous is None
    assert scan_jobs.next == scan_jobs.page_by_index(1)
    assert scan_jobs.page_by_index(0) == scan_jobs

    assert scan_jobs.next.page_index == 1
    assert len(scan_jobs.next.batch) == 6
    assert scan_jobs.next.previous == scan_jobs
Ejemplo n.º 11
0
def test_import_municipality_data_form(session):
    municipalities = MunicipalityCollection(session)
    municipalities.add(name="Boppelsen", bfs_number=82)
    municipalities.add(name="Adlikon", bfs_number=21)

    # Test apply
    form = ImportMunicipalityDataForm()
    form.request = Request(session)

    form.file.data = {
        21: {
            'dates': [date(2019, 1, 1), date(2019, 1, 7)]
        },
        241: {
            'dates': [date(2019, 1, 3), date(2019, 1, 9)]
        },
        82: {
            'dates': [date(2019, 1, 4), date(2019, 1, 10)]
        }
    }
    form.update_model(municipalities)
    assert [(m.bfs_number, [d.date for d in m.pickup_dates])
            for m in municipalities.query()
            ] == [(21, [date(2019, 1, 1), date(2019, 1, 7)]),
                  (82, [date(2019, 1, 4), date(2019, 1, 10)])]

    # Test validation
    form = ImportMunicipalityDataForm()
    form.request = Request(session)
    assert not form.validate()

    field_storage = FieldStorage()
    field_storage.file = BytesIO(
        "Adlikon;21;-1;Normal;12.2.2015".encode('cp1252'))
    field_storage.type = 'text/csv'
    field_storage.filename = 'test.csv'
    form.file.process(PostData({'file': field_storage}))

    assert form.validate()
Ejemplo n.º 12
0
 def municipalities_url(self):
     return self.request.link(MunicipalityCollection(self.request.session))
Ejemplo n.º 13
0
def get_municipality(request, id):
    return MunicipalityCollection(request.session).by_id(id)
Ejemplo n.º 14
0
def get_municipalities(request):
    return MunicipalityCollection(request.session)
Ejemplo n.º 15
0
def delete_municipality(self, request):
    """ Delete a municipality. """

    request.assert_valid_csrf_token()
    MunicipalityCollection(request.session).delete(self)
    request.message(_("Municipality deleted."), 'success')
Ejemplo n.º 16
0
def test_report_layouts(session):
    municipality = MunicipalityCollection(session).add(
        name="Adlikon",
        bfs_number=21,
    )

    request = DummyRequest()

    model = Report(session)
    layout = ReportLayout(model, request)
    assert layout.title == 'Report'
    assert layout.editbar_links == []
    assert path(layout.breadcrumbs) == 'DummyPrincipal/Report'
    assert layout.cancel_url == ''
    assert layout.success_url == ''

    model = ReportBoxes(
        session,
        start=date(2019, 1, 1),
        end=date(2019, 1, 31)
    )
    layout = ReportBoxesLayout(model, request)
    assert layout.title == 'Report boxes'
    assert layout.subtitle == '01.01.2019-31.01.2019'
    assert list(hrefs(layout.editbar_links)) == ['#']
    assert path(layout.breadcrumbs) == 'DummyPrincipal/Report/#/ReportBoxes'
    assert layout.cancel_url == ''
    assert layout.success_url == ''

    model = ReportBoxesAndForms(
        session,
        start=date(2019, 1, 1),
        end=date(2019, 1, 31)
    )
    layout = ReportBoxesAndFormsLayout(model, request)
    assert layout.title == 'Report boxes and forms'
    assert layout.subtitle == '01.01.2019-31.01.2019'
    assert list(hrefs(layout.editbar_links)) == ['#']
    assert path(layout.breadcrumbs) == (
        'DummyPrincipal/Report/#/ReportBoxesAndForms'
    )
    assert layout.cancel_url == ''
    assert layout.success_url == ''

    model = ReportFormsAllMunicipalities(
        session,
        start=date(2019, 1, 1),
        end=date(2019, 1, 31),
    )
    layout = ReportFormsAllMunicipalitiesLayout(model, request)
    assert layout.title == 'Report forms of all municipalities'
    assert layout.subtitle == '01.01.2019-31.01.2019'
    assert list(hrefs(layout.editbar_links)) == ['#']
    assert path(layout.breadcrumbs) == (
        'DummyPrincipal/Report/#/ReportFormsAllMunicipalities'
    )
    assert layout.cancel_url == ''
    assert layout.success_url == ''

    model = ReportFormsByMunicipality(
        session,
        start=date(2019, 1, 1),
        end=date(2019, 1, 31),
        municipality_id=municipality.id
    )
    layout = ReportFormsByMunicipalityLayout(model, request)
    assert layout.title == 'Report forms'
    assert layout.subtitle == 'Adlikon 01.01.2019-31.01.2019'
    assert list(hrefs(layout.editbar_links)) == ['#']
    assert path(layout.breadcrumbs) == (
        'DummyPrincipal/Report/#/ReportFormsByMunicipality'
    )
    assert layout.cancel_url == ''
    assert layout.success_url == ''

    model = ReportBoxesAndFormsByDelivery(
        session,
        start=date(2019, 1, 1),
        end=date(2019, 1, 31),
        type='normal',
        municipality_id=municipality.id
    )
    layout = ReportBoxesAndFormsByDeliveryLayout(model, request)
    assert layout.title == 'Report boxes and forms by delivery'
    assert layout.subtitle == 'Adlikon (21) 01.01.2019-31.01.2019'
    assert list(hrefs(layout.editbar_links)) == ['#']
    assert path(layout.breadcrumbs) == (
        'DummyPrincipal/Report/#/ReportBoxesAndFormsByDelivery'
    )
    assert layout.cancel_url == ''
    assert layout.success_url == ''
Ejemplo n.º 17
0
def test_scan_jobs_order(session):
    scan_jobs = ScanJobCollection(session)
    for day, name in ((3, 'First'), (1, 'Śecond'), (2, 'Phirrrrd')):
        municipality = MunicipalityCollection(session).add(
            name=name,
            bfs_number=day,
        )
        scan_jobs.add(
            dispatch_date=date(2019, 1, day),
            type='normal',
            municipality_id=municipality.id,
        )
    scan_jobs.add(
        dispatch_date=date(2019, 1, 4),
        type='normal',
        municipality_id=municipality.id,
    )

    def collection(sort_by=None, sort_order=None):
        return ScanJobCollection(session,
                                 sort_by=sort_by,
                                 sort_order=sort_order)

    scan_jobs = collection()
    assert scan_jobs.sort_order_by_key('dispatch_date') == 'descending'
    assert scan_jobs.sort_order_by_key('delivery_number') == 'unsorted'
    assert scan_jobs.sort_order_by_key('municipality_id') == 'unsorted'
    assert scan_jobs.sort_order_by_key('invalid') == 'unsorted'

    scan_jobs = collection('', '')
    assert scan_jobs.current_sort_by == 'dispatch_date'
    assert scan_jobs.current_sort_order == 'descending'

    scan_jobs = collection('xx', 'yy')
    assert scan_jobs.current_sort_by == 'dispatch_date'
    assert scan_jobs.current_sort_order == 'descending'

    scan_jobs = collection('dispatch_date', 'yy')
    assert scan_jobs.current_sort_by == 'dispatch_date'
    assert scan_jobs.current_sort_order == 'descending'

    scan_jobs = collection('xx', 'ascending')
    assert scan_jobs.current_sort_by == 'dispatch_date'
    assert scan_jobs.current_sort_order == 'descending'

    scan_jobs = collection('delivery_number', 'yy')
    assert scan_jobs.current_sort_by == 'delivery_number'
    assert scan_jobs.current_sort_order == 'ascending'

    scan_jobs = collection()
    assert scan_jobs.current_sort_by == 'dispatch_date'
    assert scan_jobs.current_sort_order == 'descending'
    assert 'dispatch_date' in str(scan_jobs.order_by)
    assert 'DESC' in str(scan_jobs.order_by)
    assert [s.dispatch_date.day for s in scan_jobs.query()] == [4, 3, 2, 1]

    scan_jobs = scan_jobs.by_order('dispatch_date')
    assert scan_jobs.current_sort_by == 'dispatch_date'
    assert scan_jobs.current_sort_order == 'ascending'
    assert 'dispatch_date' in str(scan_jobs.order_by)
    assert [s.dispatch_date.day for s in scan_jobs.query()] == [1, 2, 3, 4]

    scan_jobs = scan_jobs.by_order('delivery_number')
    assert scan_jobs.current_sort_by == 'delivery_number'
    assert scan_jobs.current_sort_order == 'ascending'
    assert 'delivery_number' in str(scan_jobs.order_by)
    assert [s.delivery_number for s in scan_jobs.query()] == [1, 1, 1, 2]

    scan_jobs = scan_jobs.by_order('delivery_number')
    assert scan_jobs.current_sort_by == 'delivery_number'
    assert scan_jobs.current_sort_order == 'descending'
    assert 'delivery_number' in str(scan_jobs.order_by)
    assert 'DESC' in str(scan_jobs.order_by)
    assert [s.delivery_number for s in scan_jobs.query()] == [2, 1, 1, 1]

    scan_jobs = scan_jobs.by_order('municipality_id')
    assert scan_jobs.current_sort_by == 'municipality_id'
    assert scan_jobs.current_sort_order == 'ascending'
    assert 'name' in str(scan_jobs.order_by)
    assert [s.municipality.name for s in scan_jobs.query()
            ] == ['First', 'Phirrrrd', 'Phirrrrd', 'Śecond']

    scan_jobs = scan_jobs.by_order('municipality_id')
    assert scan_jobs.current_sort_by == 'municipality_id'
    assert scan_jobs.current_sort_order == 'descending'
    assert 'name' in str(scan_jobs.order_by)
    assert 'DESC' in str(scan_jobs.order_by)
    assert [s.municipality.name for s in scan_jobs.query()
            ] == ['Śecond', 'Phirrrrd', 'Phirrrrd', 'First']

    scan_jobs = scan_jobs.by_order(None)
    assert scan_jobs.current_sort_by == 'dispatch_date'
    assert scan_jobs.current_sort_order == 'descending'

    scan_jobs = scan_jobs.by_order('')
    assert scan_jobs.current_sort_by == 'dispatch_date'
    assert scan_jobs.current_sort_order == 'descending'

    scan_jobs = scan_jobs.by_order('xxx')
    assert scan_jobs.current_sort_by == 'dispatch_date'
    assert scan_jobs.current_sort_order == 'descending'
Ejemplo n.º 18
0
def test_scan_jobs_query(session):
    municipalities = MunicipalityCollection(session)
    municipalities = {
        'Adlikon': municipalities.add(name='Adlikon', bfs_number=21),
        'Aesch': municipalities.add(name='Aesch', bfs_number=241)
    }

    def add(name, dispatch_date, type, dispatch_note, return_note):
        ScanJobCollection(session).add(
            dispatch_date=dispatch_date,
            type=type,
            municipality_id=municipalities[name].id,
            dispatch_note=dispatch_note,
            return_note=return_note,
        )

    add('Adlikon', date(2019, 1, 6), 'normal', None, 'bc')
    add('Adlikon', date(2019, 1, 5), 'express', 'abc', None)
    add('Adlikon', date(2019, 1, 4), 'express', '', '')
    add('Aesch', date(2019, 1, 4), 'normal', None, 'Lorem ipsum')
    add('Aesch', date(2019, 1, 3), 'express', 'Lorem Ipsum', None)
    add('Aesch', date(2019, 1, 2), 'express', None, 'loremipsum')

    def count(**kwargs):
        return ScanJobCollection(session, **kwargs).query().count()

    assert count() == 6

    assert count(from_date=date(2019, 1, 1)) == 6
    assert count(from_date=date(2019, 1, 2)) == 6
    assert count(from_date=date(2019, 1, 3)) == 5
    assert count(from_date=date(2019, 1, 5)) == 2
    assert count(from_date=date(2019, 1, 6)) == 1
    assert count(from_date=date(2019, 1, 7)) == 0

    assert count(to_date=date(2019, 1, 1)) == 0
    assert count(to_date=date(2019, 1, 2)) == 1
    assert count(to_date=date(2019, 1, 3)) == 2
    assert count(to_date=date(2019, 1, 5)) == 5
    assert count(to_date=date(2019, 1, 6)) == 6
    assert count(to_date=date(2019, 1, 7)) == 6

    assert count(from_date=date(2019, 1, 3), to_date=date(2019, 1, 4)) == 3
    assert count(from_date=date(2019, 1, 4), to_date=date(2019, 1, 3)) == 0

    assert count(type=[]) == 6
    assert count(type=['normal']) == 2
    assert count(type=['express']) == 4
    assert count(type=['normal', 'express']) == 6

    assert count(municipality_id=[]) == 6
    assert count(municipality_id=[municipalities['Adlikon'].id]) == 3
    assert count(municipality_id=[
        municipalities['Adlikon'].id, municipalities['Aesch'].id
    ]) == 6

    assert count(term='abc') == 1
    assert count(term='bc') == 2
    assert count(term='C') == 2
    assert count(term='LOREM IPSUM') == 2
    assert count(term='ips') == 3

    assert count(municipality_id=[municipalities['Adlikon'].id],
                 type=['express'],
                 from_date=date(2019, 1, 4),
                 term='a') == 1
Ejemplo n.º 19
0
def test_municipalities(session):
    municipalities = MunicipalityCollection(session)
    municipalities.add(
        name='Boppelsen',
        bfs_number=82,
    )
    municipalities.add(
        name='Adlikon',
        bfs_number=21,
    )

    assert [(m.name, m.bfs_number)
            for m in municipalities.query()] == [('Adlikon', 21),
                                                 ('Boppelsen', 82)]

    # Import data
    data = {
        21: {
            'dates': [date(2019, 1, 1), date(2019, 1, 7)]
        },
        241: {
            'dates': [date(2019, 1, 2), date(2019, 1, 8)]
        },
        82: {
            'dates': [date(2019, 1, 4), date(2019, 1, 10)]
        }
    }
    municipalities.import_data(data)
    assert [(m.bfs_number, [d.date for d in m.pickup_dates])
            for m in municipalities.query()
            ] == [(21, [date(2019, 1, 1), date(2019, 1, 7)]),
                  (82, [date(2019, 1, 4), date(2019, 1, 10)])]

    municipalities.add(
        name='Aesch',
        bfs_number=241,
    )
    data = {
        21: {
            'dates': [date(2019, 1, 7), date(2019, 1, 8)]
        },
        241: {
            'dates': [date(2019, 1, 2), date(2019, 1, 8)]
        },
        82: {
            'dates': []
        }
    }
    municipalities.import_data(data)
    assert [(m.bfs_number, [d.date for d in m.pickup_dates])
            for m in municipalities.query()
            ] == [(21, [date(2019, 1, 1),
                        date(2019, 1, 7),
                        date(2019, 1, 8)]),
                  (241, [date(2019, 1, 2), date(2019, 1, 8)]),
                  (82, [date(2019, 1, 4), date(2019, 1, 10)])]
Ejemplo n.º 20
0
def test_scan_job_layouts(session):
    request = DummyRequest()
    request_admin = DummyRequest(roles=['admin'])
    request_editor = DummyRequest(roles=['editor'])

    municipalities = MunicipalityCollection(session)
    municipality = municipalities.add(name='Boppelsen', bfs_number=82)

    # ScanJob collection
    model = ScanJobCollection(None)
    layout = ScanJobsLayout(model, request)
    assert layout.title == 'Scan jobs'
    assert layout.editbar_links == []
    assert path(layout.breadcrumbs) == 'DummyPrincipal/ScanJobCollection'
    assert layout.cancel_url == ''
    assert layout.success_url == ''

    layout = ScanJobsLayout(model, request_admin)
    assert list(hrefs(layout.editbar_links)) == [
        'ScanJobCollection/add-unrestricted'
    ]

    layout = ScanJobsLayout(model, request_editor)
    assert list(hrefs(layout.editbar_links)) == ['ScanJobCollection/add']

    # .. add
    layout = AddScanJobLayout(model, request)
    assert layout.title == 'Add scan job'
    assert layout.editbar_links == []
    assert path(layout.breadcrumbs) == 'DummyPrincipal/ScanJobCollection/#'
    assert layout.cancel_url == 'ScanJobCollection/'
    assert layout.success_url == 'ScanJobCollection/'

    layout = AddScanJobLayout(model, request_admin)
    assert list(hrefs(layout.editbar_links)) == []

    layout = AddScanJobLayout(model, request_editor)
    assert list(hrefs(layout.editbar_links)) == []

    # ScanJob
    model = ScanJobCollection(session).add(
        type='normal',
        dispatch_date=date(2019, 1, 1),
        municipality_id=municipality.id
    )
    layout = ScanJobLayout(model, request)
    assert layout.title.interpolate() == 'Scan job no. 1'
    assert list(hrefs(layout.editbar_links)) == ['ScanJob/delivery-note']
    assert path(layout.breadcrumbs) == 'DummyPrincipal/ScanJobCollection/#'
    assert layout.cancel_url == ''
    assert layout.success_url == ''

    layout = ScanJobLayout(model, request_admin)
    assert list(hrefs(layout.editbar_links)) == [
        'ScanJob/delivery-note',
        'ScanJob/edit-unrestricted',
        'ScanJob/?csrf-token=x'
    ]

    layout = ScanJobLayout(model, request_editor)
    assert list(hrefs(layout.editbar_links)) == [
        'ScanJob/delivery-note',
        'ScanJob/edit',
        'ScanJob/?csrf-token=x'
    ]

    # ... edit
    layout = EditScanJobLayout(model, request)
    assert layout.title == 'Edit scan job'
    assert layout.editbar_links == []
    assert path(layout.breadcrumbs) == (
        'DummyPrincipal/ScanJobCollection/ScanJob/#'
    )
    assert layout.cancel_url == 'ScanJob/'
    assert layout.success_url == 'ScanJob/'

    layout = EditScanJobLayout(model, request_admin)
    assert list(hrefs(layout.editbar_links)) == []

    layout = EditScanJobLayout(model, request_editor)
    assert list(hrefs(layout.editbar_links)) == []

    # ... delivery note
    layout = DeliveryNoteLayout(model, request)
    assert layout.title == 'Delivery note'
    assert list(hrefs(layout.editbar_links)) == ['#']
    assert path(layout.breadcrumbs) == (
        'DummyPrincipal/ScanJobCollection/ScanJob/#'
    )
Ejemplo n.º 21
0
def test_unrestricted_scan_job_form(session):
    municipalities = MunicipalityCollection(session)
    municipality_1 = municipalities.add(name="Boppelsen", bfs_number=82)
    municipality_1.pickup_dates.append(PickupDate(date=date(2019, 1, 7)))
    municipality_1.pickup_dates.append(PickupDate(date=date(2019, 1, 8)))

    municipality_2 = municipalities.add(name="Adlikon", bfs_number=21)
    municipality_2.pickup_dates.append(PickupDate(date=date(2019, 1, 17)))
    municipality_2.pickup_dates.append(PickupDate(date=date(2019, 1, 18)))

    # Test on request
    form = UnrestrictedScanJobForm()
    form.request = Request(session, groupid=municipality_1.id.hex)
    with freeze_time("2019-01-05"):
        form.on_request()
        assert form.type.choices == [('normal', 'Regular shipment'),
                                     ('express', 'Express shipment')]
        assert form.municipality_id.choices == [
            (municipality_2.id.hex, 'Adlikon (21)'),
            (municipality_1.id.hex, 'Boppelsen (82)')
        ]

    # Test apply / update
    model = ScanJob()
    model.municipality_id = municipality_1.id
    model.type = 'normal'
    model.dispatch_date = date(2019, 1, 8)
    model.dispatch_boxes = 1
    model.dispatch_tax_forms_current_year = 2
    model.dispatch_tax_forms_last_year = 3
    model.dispatch_tax_forms_older = 4
    model.dispatch_single_documents = 5
    model.dispatch_note = 'Note on dispatch'
    model.dispatch_cantonal_tax_office = 6
    model.dispatch_cantonal_scan_center = 7
    model.return_date = date(2019, 1, 10)
    model.return_boxes = 8
    model.return_tax_forms_current_year = 9
    model.return_tax_forms_last_year = 10
    model.return_tax_forms_older = 11
    model.return_single_documents = 12
    model.return_unscanned_tax_forms_current_year = 13
    model.return_unscanned_tax_forms_last_year = 14
    model.return_unscanned_tax_forms_older = 15
    model.return_unscanned_single_documents = 16
    model.return_note = 'Note on return'

    form.apply_model(model)
    assert form.type.data == 'normal'
    assert form.municipality_id.data == municipality_1.id.hex
    assert form.dispatch_date.data == date(2019, 1, 8)
    assert form.dispatch_boxes.data == 1
    assert form.dispatch_tax_forms_current_year.data == 2
    assert form.dispatch_tax_forms_last_year.data == 3
    assert form.dispatch_tax_forms_older.data == 4
    assert form.dispatch_single_documents.data == 5
    assert form.dispatch_note.data == 'Note on dispatch'
    assert form.dispatch_cantonal_tax_office.data == 6
    assert form.dispatch_cantonal_scan_center.data == 7
    assert form.return_date.data == date(2019, 1, 10)
    assert form.return_boxes.data == 8
    assert form.return_tax_forms_current_year.data == 9
    assert form.return_tax_forms_last_year.data == 10
    assert form.return_tax_forms_older.data == 11
    assert form.return_single_documents.data == 12
    assert form.return_unscanned_tax_forms_current_year.data == 13
    assert form.return_unscanned_tax_forms_last_year.data == 14
    assert form.return_unscanned_tax_forms_older.data == 15
    assert form.return_unscanned_single_documents.data == 16
    assert form.return_note.data == 'Note on return'

    form.type.data = 'express'
    form.municipality_id.data = municipality_2.id.hex
    form.dispatch_date.data = date(2019, 1, 6)
    form.dispatch_boxes.data = 10
    form.dispatch_tax_forms_current_year.data = 20
    form.dispatch_tax_forms_last_year.data = 30
    form.dispatch_tax_forms_older.data = 40
    form.dispatch_single_documents.data = 50
    form.dispatch_note.data = 'A note on the dispatch'
    form.dispatch_cantonal_tax_office.data = 60
    form.dispatch_cantonal_scan_center.data = 70
    form.return_date.data = date(2019, 1, 10)
    form.return_boxes.data = 80
    form.return_tax_forms_current_year.data = 90
    form.return_tax_forms_last_year.data = 100
    form.return_tax_forms_older.data = 110
    form.return_single_documents.data = 120
    form.return_unscanned_tax_forms_current_year.data = 130
    form.return_unscanned_tax_forms_last_year.data = 140
    form.return_unscanned_tax_forms_older.data = 150
    form.return_unscanned_single_documents.data = 160
    form.return_note.data = 'A note on the return'

    form.update_model(model)
    assert model.municipality_id == municipality_2.id.hex
    assert model.type == 'express'
    assert model.dispatch_date == date(2019, 1, 6)
    assert model.dispatch_boxes == 10
    assert model.dispatch_tax_forms_current_year == 20
    assert model.dispatch_tax_forms_last_year == 30
    assert model.dispatch_tax_forms_older == 40
    assert model.dispatch_single_documents == 50
    assert model.dispatch_note == 'A note on the dispatch'
    assert model.dispatch_cantonal_tax_office == 60
    assert model.dispatch_cantonal_scan_center == 70
    assert model.return_date == date(2019, 1, 10)
    assert model.return_boxes == 80
    assert model.return_tax_forms_current_year == 90
    assert model.return_tax_forms_last_year == 100
    assert model.return_tax_forms_older == 110
    assert model.return_single_documents == 120
    assert model.return_unscanned_tax_forms_current_year == 130
    assert model.return_unscanned_tax_forms_last_year == 140
    assert model.return_unscanned_tax_forms_older == 150
    assert model.return_unscanned_single_documents == 160
    assert model.return_note == 'A note on the return'

    # Test validation
    with freeze_time("2019-01-05"):
        form = UnrestrictedScanJobForm()
        form.request = Request(session, groupid=municipality_1.id.hex)
        form.on_request()
        assert not form.validate()

        form = UnrestrictedScanJobForm(
            PostData({
                'municipality_id': municipality_1.id.hex,
                'type': 'normal',
                'dispatch_date': '2019-01-18'
            }))
        form.request = Request(session, groupid=municipality_1.id.hex)
        form.on_request()
Ejemplo n.º 22
0
def test_user_form(session):
    municipalities = MunicipalityCollection(session)
    municipality = municipalities.add(name="Aesch", bfs_number=82)

    # Test apply / update
    form = UserForm()
    form.request = Request(session, groupid=municipality.id.hex)
    form.request.identity.userid = '*****@*****.**'
    form.request.identity.role = 'editor'

    user = User()
    form.realname.data = "Petra Muster"
    form.username.data = "*****@*****.**"
    form.contact.data = False
    logged_out = form.update_model(user)
    assert user.realname == "Petra Muster"
    assert user.username == "*****@*****.**"
    assert user.group_id == municipality.id.hex
    assert user.role == 'member'
    assert user.data['contact'] is False
    assert user.password_hash
    assert user.modified
    assert logged_out is False

    users = UserCollection(session)
    user = users.add(
        realname="Hans Muster",
        username="******",
        role='invalid',
        password='******',
    )
    user.logout_all_sessions = MagicMock()
    password_hash = user.password_hash

    form.apply_model(user)
    assert form.realname.data == "Hans Muster"
    assert form.username.data == "*****@*****.**"
    assert form.contact.data is False

    form.realname.data = "Hans-Peter Muster"
    form.username.data = "*****@*****.**"
    form.contact.data = True
    logged_out = form.update_model(user)
    assert user.realname == "Hans-Peter Muster"
    assert user.username == "*****@*****.**"
    assert user.group_id == municipality.id.hex
    assert user.role == 'member'
    assert user.data['contact'] is True
    assert user.password_hash == password_hash
    assert user.logout_all_sessions.called is True
    assert logged_out is False

    form.request.identity.userid = "*****@*****.**"
    logged_out = form.update_model(user)
    assert user.realname == "Hans-Peter Muster"
    assert user.username == "*****@*****.**"
    assert user.group_id == municipality.id.hex
    assert user.role == 'editor'
    assert user.data['contact'] is True
    assert user.password_hash == password_hash
    assert user.logout_all_sessions.called is True
    assert logged_out is True

    # Test validation
    form = UserForm()
    form.request = Request(session, groupid=municipality.id.hex)
    assert not form.validate()

    form = UserForm(
        PostData({
            'realname': "Hans-Peter Muster",
            'username': "******"
        }))
    form.request = Request(session, groupid=municipality.id.hex)
    assert not form.validate()
    assert form.errors == {'username': ['This value already exists.']}

    form = UserForm(
        PostData({
            'realname': "Hans Muster",
            'username': "******"
        }))
    form.request = Request(session, groupid=municipality.id.hex)
    assert form.validate()
Ejemplo n.º 23
0
def test_add_scan_job_form(session):
    municipalities = MunicipalityCollection(session)
    municipality = municipalities.add(name="Boppelsen", bfs_number=82)
    municipality.pickup_dates.append(PickupDate(date=date(2019, 1, 1)))
    municipality.pickup_dates.append(PickupDate(date=date(2019, 1, 8)))
    municipality.pickup_dates.append(PickupDate(date=date(2019, 1, 15)))

    # Test on request
    form = AddScanJobForm()
    form.request = Request(session, groupid=municipality.id.hex)
    assert form.municipality_id == municipality.id.hex
    with freeze_time("2019-01-05"):
        form.on_request()
        assert form.type.choices == [('normal', 'Regular shipment')]
        assert form.dispatch_date_normal.choices == [(date(2019, 1,
                                                           8), '08.01.2019'),
                                                     (date(2019, 1,
                                                           15), '15.01.2019')]
    form.request = Request(session,
                           groupid=municipality.id.hex,
                           roles=['editor'])
    with freeze_time("2019-01-05"):
        form.on_request()
        assert form.type.choices == [('normal', 'Regular shipment'),
                                     ('express', 'Express shipment')]
        assert form.dispatch_date_normal.choices == [(date(2019, 1,
                                                           8), '08.01.2019'),
                                                     (date(2019, 1,
                                                           15), '15.01.2019')]

    # Test update
    form.type.data = 'normal'
    form.dispatch_date_normal.data = date(2019, 1, 8)
    form.dispatch_date_express.data = date(2019, 1, 6)
    form.dispatch_boxes.data = 1
    form.dispatch_tax_forms_current_year.data = 2
    form.dispatch_tax_forms_last_year.data = 3
    form.dispatch_tax_forms_older.data = 4
    form.dispatch_single_documents.data = 5
    form.dispatch_note.data = 'Note on dispatch'
    form.dispatch_cantonal_tax_office.data = 6
    form.dispatch_cantonal_scan_center.data = 7

    model = ScanJob()
    form.update_model(model)
    assert model.municipality_id == municipality.id.hex
    assert model.type == 'normal'
    assert model.dispatch_date == date(2019, 1, 8)
    assert model.return_date == date(2019, 1, 15)
    assert model.dispatch_boxes == 1
    assert model.dispatch_tax_forms_current_year == 2
    assert model.dispatch_tax_forms_last_year == 3
    assert model.dispatch_tax_forms_older == 4
    assert model.dispatch_single_documents == 5
    assert model.dispatch_note == 'Note on dispatch'
    assert model.dispatch_cantonal_tax_office == 6
    assert model.dispatch_cantonal_scan_center == 7

    form.type.data = 'express'
    form.update_model(model)
    assert model.dispatch_date == date(2019, 1, 6)
    assert model.return_date is None

    # Test validation
    with freeze_time("2019-01-05"):
        form = AddScanJobForm()
        form.request = Request(session, groupid=municipality.id.hex)
        form.on_request()
        assert not form.validate()

        form = AddScanJobForm(
            PostData({
                'municipality_id': municipality.id.hex,
                'type': 'normal',
                'dispatch_date_normal': '2019-01-08',
                'dispatch_boxes': 1,
            }))
        form.request = Request(session, groupid=municipality.id.hex)
        form.on_request()
        assert form.validate()
Ejemplo n.º 24
0
def test_unrestricted_user_form(session):
    municipalities = MunicipalityCollection(session)
    municipality_1 = municipalities.add(name="Aesch", bfs_number=82)
    municipality_2 = municipalities.add(name="Adlikon", bfs_number=21)

    # Test choices
    form = UnrestrictedUserForm()
    form.request = Request(session)
    form.request.identity.userid = '*****@*****.**'
    form.request.identity.role = 'admin'
    form.on_request()
    assert [c[1] for c in form.municipality_id.choices
            ] == ["- none -", "Adlikon (21)", "Aesch (82)"]
    assert form.role.choices == [('admin', "Admin"), ('editor', "Editor"),
                                 ('member', "Member")]

    # Test apply / update
    user = User()
    form.role.data = "member"
    form.municipality_id.data = None
    form.realname.data = "Petra Muster"
    form.username.data = "*****@*****.**"
    form.contact.data = False
    logged_out = form.update_model(user)
    assert user.role == 'member'
    assert user.realname == "Petra Muster"
    assert user.username == "*****@*****.**"
    assert user.group_id is None
    assert user.data['contact'] is False
    assert user.password_hash
    assert user.modified
    assert logged_out is False

    users = UserCollection(session)
    user = users.add(
        realname="Hans Muster",
        username="******",
        role='member',
        password='******',
    )
    user.group_id = municipality_1.id
    user.logout_all_sessions = MagicMock()
    password_hash = user.password_hash

    form.apply_model(user)
    assert form.role.data == 'member'
    assert form.municipality_id.data == municipality_1.id.hex
    assert form.realname.data == "Hans Muster"
    assert form.username.data == "*****@*****.**"
    assert form.contact.data is False

    form.role.data = 'admin'
    form.municipality_id.data = municipality_2.id.hex
    form.realname.data = "Hans-Peter Muster"
    form.username.data = "*****@*****.**"
    form.contact.data = True
    logged_out = form.update_model(user)
    assert user.realname == "Hans-Peter Muster"
    assert user.username == "*****@*****.**"
    assert user.group_id == municipality_2.id.hex
    assert user.data['contact'] is True
    assert user.password_hash == password_hash
    assert user.logout_all_sessions.called is True
    assert logged_out is False

    form.request.identity.userid = "*****@*****.**"
    logged_out = form.update_model(user)
    assert user.realname == "Hans-Peter Muster"
    assert user.username == "*****@*****.**"
    assert user.group_id == municipality_2.id.hex
    assert user.data['contact'] is True
    assert user.password_hash == password_hash
    assert user.logout_all_sessions.called is True
    assert logged_out is True

    # Test validation
    form = UnrestrictedUserForm()
    form.request = Request(session, groupid=municipality_1.id.hex)
    form.on_request()
    assert not form.validate()

    form = UnrestrictedUserForm(
        PostData({
            'role': 'member',
            'realname': "Hans-Peter Muster",
            'username': "******"
        }))
    form.request = Request(session, groupid=municipality_1.id.hex)
    form.on_request()
    assert not form.validate()
    assert form.errors == {'username': ['This value already exists.']}

    form = UnrestrictedUserForm(
        PostData({
            'role': 'member',
            'realname': "Hans Muster",
            'username': "******"
        }))
    form.request = Request(session, groupid=municipality_1.id.hex)
    form.on_request()
    assert form.validate()

    form = UnrestrictedUserForm(
        PostData({
            'role': 'editor',
            'municipality_id': municipality_2.id.hex,
            'realname': "Hans Muster",
            'username': "******"
        }))
    form.request = Request(session, groupid=municipality_1.id.hex)
    form.on_request()
    assert form.validate()
Ejemplo n.º 25
0
def test_permissions(wtfs_app, wtfs_password):
    session = wtfs_app.session()

    def permits(user, model, permission):
        return permits_by_app(wtfs_app, user, model, permission)

    # Remove existing users and group
    session.query(User).filter_by(realname='Editor').one().group_id = None
    session.query(User).filter_by(realname='Member').one().group_id = None
    session.query(Municipality).delete()

    # Add new
    municipality_a_id = uuid4()
    session.add(Municipality(
        id=municipality_a_id,
        name='Municipality A',
        bfs_number=10,
    ))
    session.add(PickupDate(
        date=date.today(),
        municipality_id=municipality_a_id,
    ))
    session.add(ScanJob(
        type='normal',
        municipality_id=municipality_a_id,
        delivery_number=1,
        dispatch_date=date(2019, 1, 1))
    )
    session.add(User(
        realname='Admin A',
        username='******',
        password_hash=wtfs_password,
        role='admin',
        group_id=municipality_a_id
    ))
    session.add(User(
        realname='Editor A',
        username='******',
        password_hash=wtfs_password,
        role='editor',
        group_id=municipality_a_id
    ))
    session.add(User(
        realname='Member A',
        username='******',
        password_hash=wtfs_password,
        role='member',
        group_id=municipality_a_id
    ))

    municipality_b_id = uuid4()
    session.add(Municipality(
        id=municipality_b_id,
        name='Municipality B',
        bfs_number=20,
    ))
    session.add(User(
        realname='Admin B',
        username='******',
        password_hash=wtfs_password,
        role='admin',
        group_id=municipality_b_id
    ))
    session.add(User(
        realname='Editor B',
        username='******',
        password_hash=wtfs_password,
        role='editor',
        group_id=municipality_b_id
    ))
    session.add(User(
        realname='Member B',
        username='******',
        password_hash=wtfs_password,
        role='member',
        group_id=municipality_b_id
    ))

    query = session.query
    admin = query(User).filter_by(realname='Admin').one()
    admin_a = query(User).filter_by(realname='Admin A').one()
    admin_b = query(User).filter_by(realname='Admin B').one()
    editor = query(User).filter_by(realname='Editor').one()
    editor_a = query(User).filter_by(realname='Editor A').one()
    editor_b = query(User).filter_by(realname='Editor B').one()
    member = query(User).filter_by(realname='Member').one()
    member_a = query(User).filter_by(realname='Member A').one()
    member_b = query(User).filter_by(realname='Member B').one()
    group = UserGroup()
    group_a = query(UserGroup).filter_by(name='Municipality A').one()
    group_b = query(UserGroup).filter_by(name='Municipality B').one()
    municipality = Municipality()
    municipality_a = query(Municipality).filter_by(name='Municipality A').one()
    municipality_b = query(Municipality).filter_by(name='Municipality B').one()
    scan_job = query(ScanJob).one()

    # General
    model = object()
    for user in (admin, admin_a, admin_b):
        assert permits(user, model, Public)
        assert permits(user, model, AddModel)
        assert permits(user, model, AddModelUnrestricted)
        assert permits(user, model, EditModel)
        assert permits(user, model, EditModelUnrestricted)
        assert permits(user, model, DeleteModel)
        assert permits(user, model, ViewModel)
        assert permits(user, model, ViewModelUnrestricted)
    for user in (editor, editor_a, editor_b, member, member_a, member_b):
        assert permits(user, model, Public)
        assert not permits(user, model, AddModel)
        assert not permits(user, model, AddModelUnrestricted)
        assert not permits(user, model, EditModel)
        assert not permits(user, model, EditModelUnrestricted)
        assert not permits(user, model, DeleteModel)
        assert not permits(user, model, ViewModel)
        assert not permits(user, model, ViewModelUnrestricted)

    # UserGroupCollection / MunicipalityCollection
    # MunicipalityCollection
    for model in (
        MunicipalityCollection(session),
        UserGroupCollection(session)
    ):
        for user in (admin, admin_a, admin_b):
            assert permits(user, model, Public)
            assert permits(user, model, AddModel)
            assert permits(user, model, AddModelUnrestricted)
            assert permits(user, model, EditModel)
            assert permits(user, model, EditModelUnrestricted)
            assert permits(user, model, DeleteModel)
            assert permits(user, model, ViewModel)
            assert permits(user, model, ViewModelUnrestricted)
        for user in (editor, editor_a, editor_b, member, member_a, member_b):
            assert permits(user, model, Public)
            assert not permits(user, model, AddModel)
            assert not permits(user, model, AddModelUnrestricted)
            assert not permits(user, model, EditModel)
            assert not permits(user, model, EditModelUnrestricted)
            assert not permits(user, model, DeleteModel)
            assert not permits(user, model, ViewModel)
            assert not permits(user, model, ViewModelUnrestricted)

    # UserGroup / Municipality
    for user in (admin, admin_a, admin_b):
        for model in (group_a, group_b, municipality_a, municipality_b):
            assert permits(user, model, Public)
            assert permits(user, model, AddModel)
            assert permits(user, model, AddModelUnrestricted)
            assert permits(user, model, EditModel)
            assert permits(user, model, EditModelUnrestricted)
            assert not permits(user, model, DeleteModel)
            assert permits(user, model, ViewModel)
            assert permits(user, model, ViewModelUnrestricted)
        for model in (group, municipality):
            assert permits(user, model, Public)
            assert permits(user, model, AddModel)
            assert permits(user, model, AddModelUnrestricted)
            assert permits(user, model, EditModel)
            assert permits(user, model, EditModelUnrestricted)
            assert permits(user, model, DeleteModel)
            assert permits(user, model, ViewModel)
            assert permits(user, model, ViewModelUnrestricted)
    for user in (editor, editor_a, editor_b, member, member_a, member_b):
        for model in (
            group_a, group_b, group,
            municipality_a, municipality_b, municipality
        ):
            assert permits(user, model, Public)
            assert not permits(user, model, AddModel)
            assert not permits(user, model, AddModelUnrestricted)
            assert not permits(user, model, EditModel)
            assert not permits(user, model, EditModelUnrestricted)
            assert not permits(user, model, DeleteModel)
            assert not permits(user, model, ViewModel)
            assert not permits(user, model, ViewModelUnrestricted)

    # UserCollection
    model = UserCollection(session)
    for user in (admin, admin_a, admin_b):
        assert permits(user, model, Public)
        assert permits(user, model, AddModel)
        assert permits(user, model, AddModelUnrestricted)
        assert permits(user, model, EditModel)
        assert permits(user, model, EditModelUnrestricted)
        assert permits(user, model, DeleteModel)
        assert permits(user, model, ViewModel)
        assert permits(user, model, ViewModelUnrestricted)
    for user in (editor, editor_a, editor_b):
        assert permits(user, model, Public)
        assert permits(user, model, AddModel)
        assert not permits(user, model, AddModelUnrestricted)
        assert not permits(user, model, EditModel)
        assert not permits(user, model, EditModelUnrestricted)
        assert not permits(user, model, DeleteModel)
        assert permits(user, model, ViewModel)
        assert not permits(user, model, ViewModelUnrestricted)
    for user in (member, member_a, member_b):
        assert permits(user, model, Public)
        assert not permits(user, model, AddModel)
        assert not permits(user, model, AddModelUnrestricted)
        assert not permits(user, model, EditModel)
        assert not permits(user, model, EditModelUnrestricted)
        assert not permits(user, model, DeleteModel)
        assert not permits(user, model, ViewModel)
        assert not permits(user, model, ViewModelUnrestricted)

    # User
    for user in (admin, admin_a, admin_b):
        for model in (admin, admin_a, admin_b):
            assert permits(user, model, Public)
            assert permits(user, model, AddModel)
            assert permits(user, model, AddModelUnrestricted)
            assert permits(user, model, EditModel)
            assert permits(user, model, EditModelUnrestricted)
            if user == model:
                assert not permits(user, model, DeleteModel)
            else:
                assert permits(user, model, DeleteModel)
            assert permits(user, model, ViewModel)
            assert permits(user, model, ViewModelUnrestricted)
        for model in (editor, editor_a, editor_b, member, member_a, member_b):
            assert permits(user, model, Public)
            assert permits(user, model, AddModel)
            assert permits(user, model, AddModelUnrestricted)
            assert permits(user, model, EditModel)
            assert permits(user, model, EditModelUnrestricted)
            assert permits(user, model, DeleteModel)
            assert permits(user, model, ViewModel)
            assert permits(user, model, ViewModelUnrestricted)
    for user in (editor, ):
        for model in (admin, admin_a, admin_b, editor_a, editor_b,
                      member, member_a, member_b):
            assert permits(user, model, Public)
            assert not permits(user, model, AddModel)
            assert not permits(user, model, AddModelUnrestricted)
            assert not permits(user, model, EditModel)
            assert not permits(user, model, EditModelUnrestricted)
            assert not permits(user, model, DeleteModel)
            assert not permits(user, model, ViewModel)
            assert not permits(user, model, ViewModelUnrestricted)
    for user in (editor, ):
        for model in (editor, ):
            assert permits(user, model, Public)
            assert not permits(user, model, AddModel)
            assert not permits(user, model, AddModelUnrestricted)
            assert permits(user, model, EditModel)
            assert not permits(user, model, EditModelUnrestricted)
            assert not permits(user, model, DeleteModel)
            assert permits(user, model, ViewModel)
            assert not permits(user, model, ViewModelUnrestricted)
    for user in (editor_a, ):
        for model in (admin, admin_a, admin_b, editor, editor_b,
                      member, member_b):
            assert permits(user, model, Public)
            assert not permits(user, model, AddModel)
            assert not permits(user, model, AddModelUnrestricted)
            assert not permits(user, model, EditModel)
            assert not permits(user, model, EditModelUnrestricted)
            assert not permits(user, model, DeleteModel)
            assert not permits(user, model, ViewModel)
            assert not permits(user, model, ViewModelUnrestricted)
        for model in (editor_a, ):
            assert permits(user, model, Public)
            assert not permits(user, model, AddModel)
            assert not permits(user, model, AddModelUnrestricted)
            assert permits(user, model, EditModel)
            assert not permits(user, model, EditModelUnrestricted)
            assert not permits(user, model, DeleteModel)
            assert permits(user, model, ViewModel)
            assert not permits(user, model, ViewModelUnrestricted)
        for model in (member_a,):
            assert permits(user, model, Public)
            assert not permits(user, model, AddModel)
            assert not permits(user, model, AddModelUnrestricted)
            assert permits(user, model, EditModel)
            assert not permits(user, model, EditModelUnrestricted)
            assert permits(user, model, DeleteModel)
            assert permits(user, model, ViewModel)
            assert not permits(user, model, ViewModelUnrestricted)
    for user in (editor_b, ):
        for model in (admin, admin_a, admin_b, editor, editor_a,
                      member, member_a):
            assert permits(user, model, Public)
            assert not permits(user, model, AddModel)
            assert not permits(user, model, AddModelUnrestricted)
            assert not permits(user, model, EditModel)
            assert not permits(user, model, EditModelUnrestricted)
            assert not permits(user, model, DeleteModel)
            assert not permits(user, model, ViewModel)
            assert not permits(user, model, ViewModelUnrestricted)
        for model in (editor_b, ):
            assert permits(user, model, Public)
            assert not permits(user, model, AddModel)
            assert not permits(user, model, AddModelUnrestricted)
            assert permits(user, model, EditModel)
            assert not permits(user, model, EditModelUnrestricted)
            assert not permits(user, model, DeleteModel)
            assert permits(user, model, ViewModel)
            assert not permits(user, model, ViewModelUnrestricted)
        for model in (member_b,):
            assert permits(user, model, Public)
            assert not permits(user, model, AddModel)
            assert not permits(user, model, AddModelUnrestricted)
            assert permits(user, model, EditModel)
            assert not permits(user, model, EditModelUnrestricted)
            assert permits(user, model, DeleteModel)
            assert permits(user, model, ViewModel)
            assert not permits(user, model, ViewModelUnrestricted)
    for user in (member, member_a, member_b):
        for model in (admin, admin_a, admin_b, editor, editor_a, editor_b,
                      member, member_a, member_b):
            assert permits(user, model, Public)
            assert not permits(user, model, AddModel)
            assert not permits(user, model, AddModelUnrestricted)
            assert not permits(user, model, EditModel)
            assert not permits(user, model, EditModelUnrestricted)
            assert not permits(user, model, DeleteModel)
            assert not permits(user, model, ViewModel)
            assert not permits(user, model, ViewModelUnrestricted)

    # ScanJobCollection
    model = ScanJobCollection(session)
    for user in (admin, admin_a, admin_b):
        assert permits(user, model, Public)
        assert permits(user, model, AddModel)
        assert permits(user, model, AddModelUnrestricted)
        assert permits(user, model, EditModel)
        assert permits(user, model, EditModelUnrestricted)
        assert permits(user, model, DeleteModel)
        assert permits(user, model, ViewModel)
        assert permits(user, model, ViewModelUnrestricted)
    for user in (editor_a, editor_b, member_a, member_b):
        assert permits(user, model, Public)
        assert permits(user, model, AddModel)
        assert not permits(user, model, AddModelUnrestricted)
        assert not permits(user, model, EditModel)
        assert not permits(user, model, EditModelUnrestricted)
        assert not permits(user, model, DeleteModel)
        assert permits(user, model, ViewModel)
        assert not permits(user, model, ViewModelUnrestricted)
    for user in (editor, member):
        assert permits(user, model, Public)
        assert not permits(user, model, AddModel)
        assert not permits(user, model, AddModelUnrestricted)
        assert not permits(user, model, EditModel)
        assert not permits(user, model, EditModelUnrestricted)
        assert not permits(user, model, DeleteModel)
        assert not permits(user, model, ViewModel)
        assert not permits(user, model, ViewModelUnrestricted)

    # ScanJob
    model = scan_job
    for user in (admin, admin_a, admin_b):
        assert permits(user, model, Public)
        assert permits(user, model, AddModel)
        assert permits(user, model, AddModelUnrestricted)
        assert permits(user, model, EditModel)
        assert permits(user, model, EditModelUnrestricted)
        assert permits(user, model, DeleteModel)
        assert permits(user, model, ViewModel)
        assert permits(user, model, ViewModelUnrestricted)
    for user in (editor, editor_b, member, member_b):
        assert permits(user, model, Public)
        assert not permits(user, model, AddModel)
        assert not permits(user, model, AddModelUnrestricted)
        assert not permits(user, model, EditModel)
        assert not permits(user, model, EditModelUnrestricted)
        assert not permits(user, model, DeleteModel)
        assert not permits(user, model, ViewModel)
        assert not permits(user, model, ViewModelUnrestricted)
    for user in (editor_a, member_a):
        assert permits(user, model, Public)
        assert not permits(user, model, AddModel)
        assert not permits(user, model, AddModelUnrestricted)
        assert permits(user, model, EditModel)
        assert not permits(user, model, EditModelUnrestricted)
        assert not permits(user, model, DeleteModel)
        assert permits(user, model, ViewModel)
        assert not permits(user, model, ViewModelUnrestricted)

    # DailyList
    for model in (DailyList(), DailyListBoxes(session)):
        for user in (admin, admin_a, admin_b):
            assert permits(user, model, Public)
            assert permits(user, model, AddModel)
            assert permits(user, model, AddModelUnrestricted)
            assert permits(user, model, EditModel)
            assert permits(user, model, EditModelUnrestricted)
            assert permits(user, model, DeleteModel)
            assert permits(user, model, ViewModel)
            assert permits(user, model, ViewModelUnrestricted)
        for user in (editor, editor_a, editor_b, member_a, member_b):
            assert permits(user, model, Public)
            assert not permits(user, model, AddModel)
            assert not permits(user, model, AddModelUnrestricted)
            assert not permits(user, model, EditModel)
            assert not permits(user, model, EditModelUnrestricted)
            assert not permits(user, model, DeleteModel)
            assert not permits(user, model, ViewModel)
            assert not permits(user, model, ViewModelUnrestricted)
        for user in (member,):
            assert permits(user, model, Public)
            assert not permits(user, model, AddModel)
            assert not permits(user, model, AddModelUnrestricted)
            assert not permits(user, model, EditModel)
            assert not permits(user, model, EditModelUnrestricted)
            assert not permits(user, model, DeleteModel)
            assert permits(user, model, ViewModel)
            assert not permits(user, model, ViewModelUnrestricted)
    for model in (DailyListBoxesAndForms(session), ):
        for user in (admin, admin_a, admin_b):
            assert permits(user, model, Public)
            assert permits(user, model, AddModel)
            assert permits(user, model, AddModelUnrestricted)
            assert permits(user, model, EditModel)
            assert permits(user, model, EditModelUnrestricted)
            assert permits(user, model, DeleteModel)
            assert permits(user, model, ViewModel)
            assert permits(user, model, ViewModelUnrestricted)
        for user in (editor, editor_a, editor_b, member, member_a, member_b):
            assert permits(user, model, Public)
            assert not permits(user, model, AddModel)
            assert not permits(user, model, AddModelUnrestricted)
            assert not permits(user, model, EditModel)
            assert not permits(user, model, EditModelUnrestricted)
            assert not permits(user, model, DeleteModel)
            assert not permits(user, model, ViewModel)
            assert not permits(user, model, ViewModelUnrestricted)

    # Report
    for model in (
        Report(session),
        ReportBoxes(session),
        ReportBoxesAndForms(session),
        ReportFormsByMunicipality(session)
    ):
        for user in (admin, admin_a, admin_b):
            assert permits(user, model, Public)
            assert permits(user, model, AddModel)
            assert permits(user, model, AddModelUnrestricted)
            assert permits(user, model, EditModel)
            assert permits(user, model, EditModelUnrestricted)
            assert permits(user, model, DeleteModel)
            assert permits(user, model, ViewModel)
            assert permits(user, model, ViewModelUnrestricted)
        for user in (editor, editor_a, editor_b, member, member_a, member_b):
            assert permits(user, model, Public)
            assert not permits(user, model, AddModel)
            assert not permits(user, model, AddModelUnrestricted)
            assert not permits(user, model, EditModel)
            assert not permits(user, model, EditModelUnrestricted)
            assert not permits(user, model, DeleteModel)
            assert not permits(user, model, ViewModel)
            assert not permits(user, model, ViewModelUnrestricted)

    # Notification
    model = Notification()
    for user in (admin, admin_a, admin_b):
        assert permits(user, model, Public)
        assert permits(user, model, AddModel)
        assert permits(user, model, AddModelUnrestricted)
        assert permits(user, model, EditModel)
        assert permits(user, model, EditModelUnrestricted)
        assert permits(user, model, DeleteModel)
        assert permits(user, model, ViewModel)
        assert permits(user, model, ViewModelUnrestricted)
    for user in (editor, editor_a, editor_b, member, member_a, member_b):
        assert permits(user, model, Public)
        assert not permits(user, model, AddModel)
        assert not permits(user, model, AddModelUnrestricted)
        assert not permits(user, model, EditModel)
        assert not permits(user, model, EditModelUnrestricted)
        assert not permits(user, model, DeleteModel)
        assert permits(user, model, ViewModel)
        assert not permits(user, model, ViewModelUnrestricted)

    # Invoice
    model = Invoice(session)
    for user in (admin, admin_a, admin_b):
        assert permits(user, model, Public)
        assert permits(user, model, AddModel)
        assert permits(user, model, AddModelUnrestricted)
        assert permits(user, model, EditModel)
        assert permits(user, model, EditModelUnrestricted)
        assert permits(user, model, DeleteModel)
        assert permits(user, model, ViewModel)
        assert permits(user, model, ViewModelUnrestricted)
    for user in (editor, editor_a, editor_b, member, member_a, member_b):
        assert permits(user, model, Public)
        assert not permits(user, model, AddModel)
        assert not permits(user, model, AddModelUnrestricted)
        assert not permits(user, model, EditModel)
        assert not permits(user, model, EditModelUnrestricted)
        assert not permits(user, model, DeleteModel)
        assert not permits(user, model, ViewModel)
        assert not permits(user, model, ViewModelUnrestricted)

    # PaymentType
    model = PaymentType()
    for user in (admin, admin_a, admin_b):
        assert permits(user, model, Public)
        assert permits(user, model, AddModel)
        assert permits(user, model, AddModelUnrestricted)
        assert permits(user, model, EditModel)
        assert permits(user, model, EditModelUnrestricted)
        assert permits(user, model, DeleteModel)
        assert permits(user, model, ViewModel)
        assert permits(user, model, ViewModelUnrestricted)
    for user in (editor, editor_a, editor_b, member, member_a, member_b):
        assert permits(user, model, Public)
        assert not permits(user, model, AddModel)
        assert not permits(user, model, AddModelUnrestricted)
        assert not permits(user, model, EditModel)
        assert not permits(user, model, EditModelUnrestricted)
        assert not permits(user, model, DeleteModel)
        assert not permits(user, model, ViewModel)
        assert not permits(user, model, ViewModelUnrestricted)

    # PaymentTypeCollection
    model = PaymentTypeCollection(session)
    for user in (admin, admin_a, admin_b):
        assert permits(user, model, Public)
        assert permits(user, model, AddModel)
        assert permits(user, model, AddModelUnrestricted)
        assert permits(user, model, EditModel)
        assert permits(user, model, EditModelUnrestricted)
        assert permits(user, model, DeleteModel)
        assert permits(user, model, ViewModel)
        assert permits(user, model, ViewModelUnrestricted)
    for user in (editor, editor_a, editor_b, member, member_a, member_b):
        assert permits(user, model, Public)
        assert not permits(user, model, AddModel)
        assert not permits(user, model, AddModelUnrestricted)
        assert not permits(user, model, EditModel)
        assert not permits(user, model, EditModelUnrestricted)
        assert not permits(user, model, DeleteModel)
        assert not permits(user, model, ViewModel)
        assert not permits(user, model, ViewModelUnrestricted)

    # UserManual
    model = UserManual(wtfs_app)
    for user in (admin, admin_a, admin_b):
        assert permits(user, model, Public)
        assert permits(user, model, AddModel)
        assert permits(user, model, AddModelUnrestricted)
        assert permits(user, model, EditModel)
        assert permits(user, model, EditModelUnrestricted)
        assert permits(user, model, DeleteModel)
        assert permits(user, model, ViewModel)
        assert permits(user, model, ViewModelUnrestricted)
    for user in (editor, editor_a, editor_b, member, member_a, member_b):
        assert permits(user, model, Public)
        assert not permits(user, model, AddModel)
        assert not permits(user, model, AddModelUnrestricted)
        assert not permits(user, model, EditModel)
        assert not permits(user, model, EditModelUnrestricted)
        assert not permits(user, model, DeleteModel)
        assert permits(user, model, ViewModel)
        assert not permits(user, model, ViewModelUnrestricted)
Ejemplo n.º 26
0
def test_municipality_layouts():
    request = DummyRequest()
    request_admin = DummyRequest(roles=['admin'])

    # Municipality collection
    model = MunicipalityCollection(None)
    layout = MunicipalitiesLayout(model, request)
    assert layout.title == 'Municipalities'
    assert layout.editbar_links == []
    assert path(layout.breadcrumbs) == 'DummyPrincipal/MunicipalityCollection'
    assert layout.cancel_url == ''
    assert layout.success_url == ''

    layout = MunicipalitiesLayout(model, request_admin)
    assert list(hrefs(layout.editbar_links)) == [
        'MunicipalityCollection/import-data',
        'MunicipalityCollection/add'
    ]

    # ... add
    layout = AddMunicipalityLayout(model, request)
    assert layout.title == 'Add municipality'
    assert layout.editbar_links == []
    assert path(layout.breadcrumbs) == (
        'DummyPrincipal/MunicipalityCollection/#'
    )
    assert layout.cancel_url == 'MunicipalityCollection/'
    assert layout.success_url == 'MunicipalityCollection/'

    layout = AddMunicipalityLayout(model, request_admin)
    assert list(hrefs(layout.editbar_links)) == []

    # ... import data
    layout = ImportMunicipalityDataLayout(model, request)
    assert layout.title == 'Import data'
    assert layout.editbar_links == []
    assert path(layout.breadcrumbs) == (
        'DummyPrincipal/MunicipalityCollection/#'
    )
    assert layout.cancel_url == 'MunicipalityCollection/'
    assert layout.success_url == 'MunicipalityCollection/'

    layout = ImportMunicipalityDataLayout(model, request_admin)
    assert list(hrefs(layout.editbar_links)) == []

    # Municipality
    model = Municipality(name='Boppelsen')
    layout = MunicipalityLayout(model, request)
    assert layout.title == 'Boppelsen'
    assert layout.editbar_links == []
    assert path(layout.breadcrumbs) == (
        'DummyPrincipal/MunicipalityCollection/#'
    )
    assert layout.cancel_url == ''
    assert layout.success_url == ''

    layout = MunicipalityLayout(model, request_admin)
    assert list(hrefs(layout.editbar_links)) == [
        'Municipality/edit',
        'Municipality/delete-dates',
        'Municipality/?csrf-token=x'
    ]

    # ... edit
    layout = EditMunicipalityLayout(model, request)
    assert layout.title == 'Edit municipality'
    assert layout.editbar_links == []
    assert path(layout.breadcrumbs) == (
        'DummyPrincipal/MunicipalityCollection/Municipality/#'
    )
    assert layout.cancel_url == 'Municipality/'
    assert layout.success_url == 'MunicipalityCollection/'

    layout = EditMunicipalityLayout(model, request_admin)
    assert list(hrefs(layout.editbar_links)) == []

    # ... delete dates
    layout = DeleteMunicipalityDatesLayout(model, request)
    assert layout.title == 'Delete pick-up dates'
    assert layout.editbar_links == []
    assert path(layout.breadcrumbs) == (
        'DummyPrincipal/MunicipalityCollection/Municipality/#'
    )
    assert layout.cancel_url == 'Municipality/'
    assert layout.success_url == 'Municipality/'

    layout = DeleteMunicipalityDatesLayout(model, request_admin)
    assert list(hrefs(layout.editbar_links)) == []