def check(expect, validities):
            with requests_mock.mock() as m:
                m.get(
                    URL,
                    complete_qs=True,
                    json={
                        "results": [[{
                            "id":
                            "00000000-0000-0000-0000-000000000000",
                            "registreringer": [{
                                "tilstande": {
                                    "organisationenhedgyldighed": [{
                                        "gyldighed":
                                        v,
                                        "virkning": {
                                            "from":
                                            mora_util.to_lora_time(t1, ),
                                            "from_included": True,
                                            "to": mora_util.to_lora_time(t2, ),
                                            "to_included": False
                                        }
                                    } for t1, t2, v in validities]
                                },
                            }]
                        }]]
                    },
                )

                self.assertIs(
                    expect,
                    validator._is_date_range_valid(
                        '00000000-0000-0000-0000-000000000000',
                        util.parsedatetime('01-01-2000'),
                        util.parsedatetime('01-01-3000'), c,
                        'organisationenhedgyldighed'))
Exemple #2
0
    def test_should_return_true_when_interval_contained3(self):
        """
        [------ super ------)
          [------ sub ------)
        """
        self.expire_org_unit(self.PARENT)

        startdate = '01-02-2017'
        enddate = '01-01-2018'

        validator.is_date_range_in_org_unit_range(
            {'uuid': self.PARENT}, mora_util.parsedatetime(startdate),
            mora_util.parsedatetime(enddate))
Exemple #3
0
    def test_should_return_false_when_interval_not_contained2(self):
        """
        [------ super ------)
          [---- sub -----------)
        """
        self.expire_org_unit(self.PARENT)

        startdate = '01-02-2017'
        enddate = '01-06-2019'

        with self.assertRaises(exceptions.HTTPException):
            validator.is_date_range_in_org_unit_range(
                {'uuid': self.PARENT}, mora_util.parsedatetime(startdate),
                mora_util.parsedatetime(enddate))
Exemple #4
0
    def test_is_date_range_in_employee_valid_raises_outside_range(self):
        """Assert that a validation error is raised when the range exceeds
        employee range """

        # Arrange
        self.load_sample_structures()
        employee_uuid = '53181ed2-f1de-4c4a-a8fd-ab358c2c454a'  # Anders And
        valid_from = mora_util.parsedatetime("1910-01-01")
        valid_to = mora_util.parsedatetime("2040-01-01")
        employee = {'uuid': employee_uuid}

        # Act & Assert
        with self.assertRaises(exceptions.HTTPException):
            validator.is_date_range_in_employee_range(employee, valid_from,
                                                      valid_to)
Exemple #5
0
    def test_is_date_range_in_employee_valid_inside_range(self):
        """Assert that a validation error is not raised when the range is
        inside employee range"""

        # Arrange
        self.load_sample_structures()
        employee_uuid = '53181ed2-f1de-4c4a-a8fd-ab358c2c454a'  # Anders And
        valid_from = mora_util.parsedatetime("2020-01-01")
        valid_to = mora_util.parsedatetime("2040-01-01")
        employee = {'uuid': employee_uuid}

        # Act & Assert
        # Should be callable without raising exception
        validator.is_date_range_in_employee_range(employee, valid_from,
                                                  valid_to)
Exemple #6
0
    def test_existing_associations(self, mock):
        person_uuid = "cc1fc948-d3f6-4bbc-9faf-288e0f956135"
        org_unit_uuid = "f4f28810-cdd9-4ff5-821e-427378ab4bf7"
        from_date = "2000-01-01"

        association_uuid = "7cd87e2a-e41a-4b68-baca-ff69426be753"
        payload = {
            "person": {
                "uuid": person_uuid
            },
            "org_unit": {
                "uuid": org_unit_uuid
            },
            "validity": {
                "from": from_date,
                "to": None
            },
            "uuid": association_uuid
        }

        self.client.post('/service/validate/existing-associations/',
                         json=payload)

        mock.assert_called_with(person_uuid, org_unit_uuid,
                                mora_util.parsedatetime(from_date),
                                association_uuid)
Exemple #7
0
    def test_to_lora_time(self):
        tests = {
            self.today: '2015-06-01T00:00:00+02:00',
            self.now: '2015-06-01T01:10:00+02:00',
            '01-06-2017': '2017-06-01T00:00:00+02:00',
            '31-12-2017': '2017-12-31T00:00:00+01:00',
            'infinity': 'infinity',
            '-infinity': '-infinity',
            '2017-07-31T22:00:00+00:00': '2017-08-01T00:00:00+02:00',

            # the frontend doesn't escape the 'plus' in ISO 8601 dates, so
            # we get it as a space
            '2017-07-31T22:00:00 00:00': '2017-08-01T00:00:00+02:00',
            datetime.date(2015, 6, 1): '2015-06-01T00:00:00+02:00',

            # check parsing of raw dates
            '2018-01-01': '2018-01-01T00:00:00+01:00',
            '2018-06-01': '2018-06-01T00:00:00+02:00',
        }

        for value, expected in tests.items():
            self.assertEqual(expected, util.to_lora_time(value),
                             'failed to parse {!r}'.format(value))

        # NB: this test used to work, but we now use dateutil,
        # which tries it best to make of the inputs from the
        # user...
        if False:
            # 15 is not a valid month
            self.assertRaises(exceptions.HTTPException, util.to_lora_time,
                              '1999-15-11 00:00:00+01')

        # make sure we can round-trip the edge cases correctly
        self.assertEqual(util.parsedatetime(util.NEGATIVE_INFINITY),
                         util.NEGATIVE_INFINITY)

        self.assertEqual(util.parsedatetime(util.POSITIVE_INFINITY),
                         util.POSITIVE_INFINITY)

        # we frequently get these dates in spreadsheets
        self.assertEqual(util.parsedatetime('31-12-9999'),
                         util.POSITIVE_INFINITY)

        # test fallback
        self.assertEqual(util.parsedatetime('blyf', 'flaf'), 'flaf')
Exemple #8
0
    def test_candidate_employee(self, mock):
        from_date = "2000-01-01"

        person = {"uuid": "be0df80c-7eed-4a2e-a682-e36be4e4877e"}
        payload = {
            "person": person,
            "validity": {
                "from": from_date,
                "to": None
            }
        }

        self.client.post('/service/validate/employee/', json=payload)

        mock.assert_called_with(person, mora_util.parsedatetime(from_date),
                                mora_util.POSITIVE_INFINITY)
Exemple #9
0
    def test_candidate_org_unit(self, mock):
        from_date = "2000-01-01"

        org_unit = {"uuid": "be0df80c-7eed-4a2e-a682-e36be4e4877e"}
        payload = {
            "org_unit": org_unit,
            "validity": {
                "from": from_date,
                "to": None
            }
        }

        self.client.post('/service/validate/org-unit/', json=payload)

        mock.assert_called_with(org_unit, mora_util.parsedatetime(from_date),
                                mora_util.POSITIVE_INFINITY)
Exemple #10
0
    def test_employee_engagements(self, mock):
        person_uuid = "cc1fc948-d3f6-4bbc-9faf-288e0f956135"
        from_date = "2000-01-01"

        payload = {
            "person": {
                "uuid": person_uuid
            },
            "validity": {
                "from": from_date,
                "to": None
            }
        }

        self.client.post('/service/validate/active-engagements/', json=payload)

        mock.assert_called_with(
            person_uuid,
            mora_util.parsedatetime(from_date),
            mora_util.POSITIVE_INFINITY,
        )
Exemple #11
0
    def test_parent_org_unit(self, mock):
        from_date = "2000-01-01"
        org_unit_uuid = "f4f28810-cdd9-4ff5-821e-427378ab4bf7"
        parent_uuid = "cc1fc948-d3f6-4bbc-9faf-288e0f956135"

        payload = {
            "org_unit": {
                "uuid": org_unit_uuid
            },
            "parent": {
                "uuid": parent_uuid
            },
            "validity": {
                "from": from_date,
            }
        }

        self.client.post('/service/validate/candidate-parent-org-unit/',
                         json=payload)

        mock.assert_called_with(org_unit_uuid, parent_uuid,
                                mora_util.parsedatetime(from_date))