示例#1
0
    def test_update_interface(self):
        user = User(id=2, email='*****@*****.**', password=hash_pass('prova'), first_name='prova', last_name='prova')
        add_user(user)
        modify_user(User(id=2, email='*****@*****.**', password=hash_pass('prova'), first_name='test', last_name='prova'))

        with session_scope(self.DBSession) as session:
            self.assertEqual(session.query(User).filter(User.email == '*****@*****.**').first().first_name, 'test')

        delete_user(User(id=2))

        with session_scope(self.DBSession) as session:
            self.assertIsNone(session.query(User).filter(User.id == 2).first())
 def test_add_event(self):
     with session_scope(self.EventStoreSession) as session:
         event = UserCreatedEvent(1, '*****@*****.**', hash_pass('test_password'), 'test_first_name',
                                  'test_last_name')
         j_event = event.toJSON()
         session.add(UserEvent(uuid=event.uuid, event=j_event))
         q = session.query(UserEvent).first()
         self.assertEqual(q.event, j_event)
         self.assertEqual(json.loads(q.event)['type'], USER_CREATED_EVENT)
示例#3
0
    def test_messages(self):
        event = UserCreatedEvent(1, '*****@*****.**',
                                 hash_pass('test_password'), 'test_first_name',
                                 'test_last_name')
        publish_event(self.app.sending_channel, event, 'user.created')
        event = UserModifiedEvent(1, '*****@*****.**', hash_pass('test_password'),
                                  'test_first_name', 'test_last_name')
        publish_event(self.app.sending_channel, event, USER_MODIFIED)
        self.simulate_eventual_consistency()

        with session_scope(self.DBSession) as session:
            self.assertEqual(
                session.query(User).filter(User.id == 1).first().email,
                '*****@*****.**')

        event = UserDeletedEvent(1)
        publish_event(self.app.sending_channel, event, USER_DELETED)
        self.simulate_eventual_consistency()

        with session_scope(self.DBSession) as session:
            self.assertIsNone(session.query(User).filter(User.id == 1).first())
    def test_build_aggregate(self):
        event1 = UserCreatedEvent(1, '*****@*****.**',
                                  hash_pass('test1_password'),
                                  'test_first_name', 'test_last_name')
        event2 = UserModifiedEvent(1, '*****@*****.**',
                                   hash_pass('test2_password'),
                                   'test_first_name', 'test_last_name')
        event3 = UserCreatedEvent(2, '*****@*****.**',
                                  hash_pass('test1_password'),
                                  'prova_first_name', 'prova_last_name')
        event4 = UserDeletedEvent(1)

        UserEvent.append_event(event1)
        UserEvent.append_event(event2)
        UserEvent.append_event(event3)
        UserEvent.append_event(event4)

        aggregate_status = build_user_aggregate()
        expected_status = {}
        event3 = json.loads(event3.toJSON())
        expected_status[str(event3['event_info']['id'])] = event3['event_info']
        self.assertEqual(aggregate_status, expected_status)
示例#5
0
 def test_user_db(self):
     with session_scope(self.DBSession) as session:
         session.add(User(id=1, email='*****@*****.**', password=hash_pass('test2'), first_name='test2', last_name='test2'))
         user = session.query(User).filter(User.email == '*****@*****.**').first()
         self.assertTrue(check_pass('test2', user.password))
示例#6
0
def handle_create():
    """
    This is the create user endpoint. It creates a new user resource.
    """
    data = request.get_json()

    aggregate_status = build_user_aggregate()
    if aggregate_status == -1:
        logger.error(f'Could not build user aggregate for request: {request}')
        return ko(jsonify(dict(error='cannot build user aggregate')))

    if validate_request_payload(data):  # checks the data in the payload is formed correctly
        email = data['email']
        password = data['password']
        first_name = data['first_name']
        last_name = data['last_name']
        logger.info("Validated request payload")
        if validate_user_data(email, password, first_name, last_name):
            logger.info("Validated user data")

            # checks if the account can be created given the current aggregate status
            if is_unique_email(email, aggregate_status):
                logger.info("Email is indeed unique")
                if aggregate_status:
                    new_id = int(max(aggregate_status, key=int)) + 1
                else:
                    new_id = 1

                data['id'] = new_id
                data['password'] = hash_pass(password)

                event = UserCreatedEvent(**data)

                session = EventStoreSession()

                logger.info(data)
                try:

                    # both of these are wrapped inside a unique try catch because this must be an atomic operation.
                    UserEvent.append_event(event)
                    event_published = publish_event(get_sending_channel(), event, USER_CREATED)

                    # create a new consumer inside kong
                    kong_request = requests.post(KONG_CONSUMER_API, data={'username': email})
                    if not event_published or not kong_request.status_code == 201:
                        logger.error(f'Could not publish event on event bus for request: {request}')
                        session.rollback()
                        return ko(jsonify(dict(error='could not publish event on event bus')))

                    else:
                        session.commit()
                        logger.info(f'Created user with id: {new_id}')
                        logger.info(f"Added new consumer to kong with username {email}")
                        return created(jsonify(dict(id=new_id)))

                except SQLAlchemyError:
                    session.rollback()
                    logger.error(f'Could not append to event store for request: {request}')
                    return ko(jsonify(dict(error='could not append to event store')))

                except Exception as e:
                    logger.error(e)
                finally:
                    session.close()

    return bad_request(jsonify(dict(error='invalid data')))