Пример #1
0
    def test_login_successful(self):
        params = {'username': '******', 'password': '******'}
        resp = self.client.post('/login',
                                data=json.dumps(params),
                                headers={'content-type': 'application/json'})
        self.assertEqual(resp.status_code, 200)

        token = resp.json['token']
        self.assertEqual(len(token), 36)
        self.assertTrue(isinstance(token, str))

        result = dbs.read_user_info(self.admin_token, {})
        expected = [{
            'username': '******',
            'forename': 'Yilu',
            'surname': 'Zhu',
            'email': '*****@*****.**',
            'role': 'admin',
            'token': self.admin_token
        }, {
            'username': '******',
            'forename': 'Tony',
            'surname': 'Foltz',
            'email': '*****@*****.**',
            'role': 'user',
            'token': self.user_token
        }, {
            'username': '******',
            'forename': 'Jeffrey',
            'surname': 'Wood',
            'email': '*****@*****.**',
            'role': 'user',
            'token': token
        }]
        self.assertEqual(result, expected)
Пример #2
0
    def test_logout(self):
        resp = self.client.get('/logout',
                               headers={
                                   'content-type': 'application/json',
                                   'Authorization': self.user_token
                               })
        self.assertEqual(resp.status_code, 200)

        result = dbs.read_user_info(self.admin_token, {})
        expected = [{
            'username': '******',
            'forename': 'Yilu',
            'surname': 'Zhu',
            'email': '*****@*****.**',
            'role': 'admin',
            'token': self.admin_token
        }, {
            'username': '******',
            'forename': 'Tony',
            'surname': 'Foltz',
            'email': '*****@*****.**',
            'role': 'user',
            'token': ''
        }, {
            'username': '******',
            'forename': 'Jeffrey',
            'surname': 'Wood',
            'email': '*****@*****.**',
            'role': 'user',
            'token': ''
        }]
        self.assertEqual(result, expected)
Пример #3
0
    def test_create_user_staff_role(self):
        resp = self.client.put('/user', data=json.dumps(self.params), headers={'content-type': 'application/json', 'Authorization': self.staff_token})
        self.assertEqual(resp.status_code, 200)

        result = dbs.read_user_info(self.admin_token, {})
        expected = [
            {'email': '*****@*****.**', 'forename': 'Yilu', 'surname': 'Zhu', 'username': '******', 'role': 'admin', 'token': self.admin_token},
            {'email': '*****@*****.**', 'forename': 'Alex', 'surname': 'Zhu', 'username': '******', 'role': 'staff', 'token': self.staff_token},
            {'email': '*****@*****.**', 'forename': 'Tony', 'surname': 'Foltz', 'username': '******', 'role': 'user', 'token': self.user_token},
            {'email': '*****@*****.**', 'forename': 'Jeffrey', 'surname': 'Wood', 'username': '******', 'role': 'user', 'token': ''},
        ]
        self.assertEqual(result, expected)
Пример #4
0
def read_user_info():
    try:
        page = int(request.args.get('page', DEFAULT_PAGE_NUM))
        page_size = int(request.args.get('pagesize', DEFAULT_PAGE_SIZE))

    except Exception as e:
        return jsonify(status=1, msg=f'Invalid page or pagesize: {e}'), 400

    try:
        filter_str = request.args.get('filter')
        filters = convert_str_to_filters(filter_str)

    except Exception as e:
        traceback.print_exc()
        return jsonify(status=1, msg=f'Invalid filter {filter_str}: {e}'), 400

    token = request.headers.get('Authorization')

    try:
        logger.debug(
            f'Token: {token}. Filters for string {filter_str}: {filters}')
        data = dbs.read_user_info(token, filters, page, page_size)
        return jsonify(status=0, msg='OK', data=data)

    except exceptions.UnauthenticatedError as e:
        return jsonify(status=1, msg=f'ERROR: {e}'), 401

    except exceptions.NoAccessError as e:
        return jsonify(status=1, msg=f'ERROR: {e}'), 403

    except Exception as e:
        traceback.print_exc()
        logger.error(f'Failed to read user information: {e}')
        return jsonify(status=1, msg=f'ERROR: {e}'), 500

    finally:
        dbs.session.rollback()