Exemplo n.º 1
0
def create_new_user(user_data):
    try:
        account = AccountAPI()
        verification = VerificationAPI()
        app.logger.info('Creating user in ldap for {}'.format(
            user_data['email']))
        response = account.create(user_data)
        user_data['ldap_id'] = response['user_id']

        user_data = _process_new_user_data(user_data)
        app.logger.info('Adding user {} to the ulapd database...'.format(
            user_data['email']))
        new_user_details = UserDetails(user_data)
        db.session.add(new_user_details)
        db.session.commit()
        user_data['user_details_id'] = new_user_details.user_details_id

        if user_data['contactable']:
            app.logger.info(
                'Inserting the contact preferences for {}...'.format(
                    user_data['email']))
            for preference in user_data['contact_preferences']:
                contact = {
                    'user_details_id': user_data['user_details_id'],
                    'contact_type': preference
                }
                contact_preference = Contact(contact)
                db.session.add(contact_preference)
        db.session.commit()
        app.logger.info(
            'Finished adding user {} to the ulapd database...'.format(
                user_data['email']))

        # Add the user details to verification database for DST
        app.logger.info('Adding user {} to verification db...'.format(
            user_data['email']))
        verification.create(user_data)

        # Call to account-api to activate or acknowledge user
        is_uk_org = 'organisation-uk' in user_data['user_type']

        if is_uk_org:
            app.logger.info('Acknowledging user')
            account.acknowledge(user_data['ldap_id'])
        else:
            app.logger.info('Activating user')
            account.activate(user_data['ldap_id'])

        db.session.close()
        return user_data
    except Exception as e:
        app.logger.error('Failed to create user with error - {}'.format(
            str(e)))
        db.session.rollback()
        _delete_user_data(account, user_data)
        db.session.close()
        raise ApplicationError(*errors.get('ulapd_api',
                                           'CREATE_USER_ERROR',
                                           filler=e),
                               http_code=400)
 def test_activate_with_timeout(self, mock_post):
     mock_post.side_effect = Timeout(self.error_msg)
     with app.app_context() as ac:
         ac.g.trace_id = None
         ac.g.requests = requests.Session()
         with app.test_request_context():
             with self.assertRaises(ApplicationError) as context:
                 AccountAPI.activate(self, '1234-567-890')
                 self.assertTrue(ApplicationError in str(context.exception))
             self.assertEqual(
                 context.exception.message,
                 'Connection to account_api timed out: {}'.format(
                     self.error_msg))
             self.assertEqual(context.exception.code, 'E711')
             self.assertEqual(context.exception.http_code, 500)
    def test_activate_connection_error(self, mock_post):
        with app.app_context() as ac:
            ac.g.trace_id = None
            ac.g.requests = requests.Session()
            with app.test_request_context():
                mock_post.side_effect = ConnectionError(self.error_msg)

                with self.assertRaises(ApplicationError) as context:
                    AccountAPI.activate(self, '1234-567-890')

                self.assertEqual(
                    context.exception.message,
                    'Encountered an error connecting to account_api: {}'.
                    format(self.error_msg))
                self.assertEqual(context.exception.code, 'E710')
                self.assertEqual(context.exception.http_code, 500)
    def test_activate_http_error(self, mock_post):
        with app.app_context() as ac:
            ac.g.trace_id = None
            ac.g.requests = requests.Session()
            with app.test_request_context():
                mock_post.side_effect = HTTPError(self.error_msg)

                with self.assertRaises(ApplicationError) as context:
                    AccountAPI.activate(self, '1234-567-890')

                self.assertEqual(
                    context.exception.message,
                    'Received the following response from account_api: {}'.
                    format(self.error_msg))
                self.assertEqual(context.exception.code, 'E709')
                self.assertEqual(context.exception.http_code, 500)
 def test_activate(self, mock_post):
     with app.app_context() as ac:
         ac.g.trace_id = None
         ac.g.requests = requests.Session()
         with app.test_request_context():
             mock_post.return_value.text = 'Success'
             mock_post.return_value.status_code = 200
             response = AccountAPI.activate(self, '1234-567-890')
             assert response == {'message': 'activated'}