Пример #1
0
    def test_person_by_osu_id(self):
        person_res = utils.get_person_by_osu_id(osu_id)

        # expect 200 if osuID is valid
        self.assertEqual(person_res.status_code, 200)

        # osuID and return data should matched
        self.assertIsNotNone(person_res.json()['data'])
        self.assertEqual(person_res.json()['data']['type'], 'person')
        self.assertEqual(person_res.json()['data']['id'], osu_id)

        # expect 404 if osuID is not valid
        self.assertEqual(
            utils.get_person_by_osu_id(not_valid_osu_id).status_code, 404)
Пример #2
0
    def validate_current_employee(self, employee_osu_id, is_current_employee):
        employee_response = utils.get_person_by_osu_id(employee_osu_id)
        self.assertEqual(employee_response.status_code, 200)

        current_employee_from_response = employee_response.json(
        )['data']['attributes']['currentEmployee']
        self.assertEqual(current_employee_from_response, is_current_employee)
Пример #3
0
    def test_bad_phones(self):
        long_phone_person = utils.get_person_by_osu_id(long_phone_osu_id)
        self.assertEqual(long_phone_person.status_code, 200)
        long_home_phone = long_phone_person.json(
        )['data']['attributes']['homePhone']

        with self.assertRaises(
                phonenumbers.phonenumberutil.NumberParseException):
            self.__parse_phone_number(long_home_phone)
Пример #4
0
    def test_image_by_osu_id(self):
        image_res = utils.get_image_by_osu_id(osu_id)
        bad_res = utils.get_image_by_osu_id(osu_id, {'width': 999999})
        not_found_res = utils.get_person_by_osu_id(not_valid_osu_id)

        # expect 200 if osuID is valid
        self.assertEqual(image_res.status_code, 200)

        # test default image
        expected_image = Image.open('images/defaultimage.jpg')
        response_image = Image.open(BytesIO(image_res.content))
        h1 = expected_image.histogram()
        h2 = response_image.histogram()
        rms = math.sqrt(
            reduce(operator.add, map(lambda x, y:
                                     (x - y)**2, h1, h2)) / len(h1))
        self.assertLess(rms, 100)

        # expect 400 if width out of range (1 - 2000)
        self.assertEqual(
            utils.get_image_by_osu_id(osu_id, {
                'width': 0
            }).status_code, 400)
        self.assertEqual(
            utils.get_image_by_osu_id(osu_id, {
                'width': 2001
            }).status_code, 400)

        # test image resize
        resize_width = 500
        resize_res = utils.get_image_by_osu_id(osu_id, {'width': resize_width})
        resized_image = Image.open(BytesIO(resize_res.content))

        original_res = utils.get_image_by_osu_id(osu_id)
        original_image = Image.open(BytesIO(original_res.content))

        expected_height = ((original_image.height * resize_width) /
                           original_image.width)
        self.assertEqual(resize_res.status_code, 200)
        self.assertEqual(expected_height, resized_image.height)
        self.assertEqual(resize_width, resized_image.width)

        # expect 404 if osuID is not valid
        self.assertEqual(not_found_res.status_code, 404)

        # test content type
        self.assertEqual(image_res.headers['Content-Type'], 'image/jpeg')
        self.assertEqual(not_found_res.headers['Content-Type'],
                         'application/json')
        self.assertEqual(bad_res.headers['Content-Type'], 'application/json')
Пример #5
0
    def test_date(self):
        iso_8601_full_date_pattern = re.compile(r'^\d{4}-\d{2}-\d{2}')
        person_res = utils.get_person_by_osu_id(osu_id)
        jobs_res = utils.get_jobs_by_osu_id(jobs_osu_id)

        dates = [person_res.json()['data']['attributes']['birthDate']]
        for job in jobs_res.json()['data']:
            attributes = job['attributes']
            dates += [attributes['beginDate'], attributes['endDate']]

        for date in filter(None, dates):
            # validate ISO 8601 full-date format
            self.assertIsNotNone(
                iso_8601_full_date_pattern.match(date).group(0))
Пример #6
0
    def test_phones(self):
        phones_res = utils.get_person_by_osu_id(phones_osu_id)
        phones_res_attributes = phones_res.json()['data']['attributes']

        home_phone = phones_res_attributes['homePhone']
        alternate_phone = phones_res_attributes['alternatePhone']
        primary_phone = phones_res_attributes['primaryPhone']
        mobile_phone = phones_res_attributes['mobilePhone']

        phones = filter(
            None, [home_phone, alternate_phone, primary_phone, mobile_phone])
        for phone in phones:
            # validate E.164 phone format
            parsed_phone = self.__parse_phone_number(phone)
            e164_phone = phonenumbers.format_number(
                parsed_phone, phonenumbers.PhoneNumberFormat.E164)
            self.assertEqual(phone, e164_phone)
Пример #7
0
    def test_self_link(self):
        person_ids_res = utils.get_person_by_ids({'osuID': osu_id})
        for person in person_ids_res.json()['data']:
            self.assertEqual(person['links']['self'], api_url + osu_id)

        person_id_res = utils.get_person_by_osu_id(osu_id)
        self.assertEqual(person_id_res.json()['data']['links']['self'],
                         api_url + osu_id)

        jobs_res = utils.get_jobs_by_osu_id(jobs_osu_id)
        for job in jobs_res.json()['data']:
            attributes = job['attributes']
            position_number = attributes['positionNumber']
            suffix = attributes['suffix']
            expected_url = "{}{}/jobs?positionNumber={}&suffix={}".format(
                api_url, osu_id, position_number, suffix)

            self.assertEqual(job['links']['self'], expected_url)
Пример #8
0
    def test_get_jobs_by_osu_id(self):
        jobs_res = utils.get_jobs_by_osu_id(jobs_osu_id)
        no_job_res = utils.get_jobs_by_osu_id(no_job_osu_id)

        # expect 200 if osuID is valid
        for res, res_osu_id in [(jobs_res, jobs_osu_id),
                                (no_job_res, no_job_osu_id)]:
            self.assertEqual(res.status_code, 200)

        # test person with jobs
        self.assertGreater(self.length_of_response(jobs_res), 0)

        # test person without job
        self.assertEqual(self.length_of_response(no_job_res), 0)

        # expect 404 if osuID is not valid
        self.assertEqual(
            utils.get_person_by_osu_id(not_valid_osu_id).status_code, 404)