def test_no_context():
    """Unauthenticated requests return a default context."""

    request = mock.Mock(headers={})
    ctx = call_context(request, Settings())

    # It should return a truthy object.
    assert ctx

    # It should have no roles.
    assert not ctx.client.roles
    assert not ctx.user.roles

    # It should not be authenticated.
    assert not ctx.client.authenticated
    assert not ctx.user.authenticated
Exemple #2
0
 def __init__(
     self,
     queue_name,
     db_engine,
     queue_event,
     consumer_id=None,
     prefetch=1,
     master=False,
     settings=None,
 ):
     self.__queue_name = queue_name
     self.__db_engine = db_engine
     self.__consumer_id = consumer_id or uuid.uuid4()
     self.__prefetch = prefetch
     self.__master = master
     self.__settings = settings or Settings()
     self.__queue_event = queue_event
     self.__last_heartbeat = 0
     self.__last_consume = 0
     self.__started = False
Exemple #3
0
    def __init__(self, middleware=None, settings=None):
        super().__init__(middleware=middleware)

        self.__settings = settings or Settings()
        self.__db_engine = db_engine(self.__settings)
        self.__shared_session = ContextVar("shared_session")
        self.__broker_id = uuid.uuid4()
        self.__queue_events = defaultdict(Event)

        # We have some actors using this, so it's always enabled.
        self.add_middleware(CurrentMessage())

        # Enable special handling of actors with 'scheduled=True' in options.
        self.add_middleware(
            SchedulerMiddleware(self.__settings, self.__db_engine))

        self.add_middleware(LocalNotifyMiddleware())

        # This is postgres-specific, so...
        if "postgresql" in str(self.__db_engine.url):
            self.add_middleware(PostgresNotifyMiddleware(self.__db_engine))
Exemple #4
0
def test_commit_publish_prev_completed(mock_commit, fake_publish, db):
    """Ensure commit_publish fails for publishes in invalid state."""

    db.add(fake_publish)
    # Simulate that this publish was published.
    fake_publish.state = schemas.PublishStates.committed
    db.commit()

    with pytest.raises(HTTPException) as exc_info:
        routers.publish.commit_publish(
            env=get_environment("test"),
            publish_id=fake_publish.id,
            db=db,
            settings=Settings(),
        )

    assert exc_info.value.status_code == 409
    assert (exc_info.value.detail ==
            "Publish %s in unexpected state, 'COMMITTED'" % fake_publish.id)

    mock_commit.assert_not_called()
Exemple #5
0
def test_commit_publish(mock_commit, fake_publish, db):
    """Ensure commit_publish delegates to worker correctly and creates task."""

    db.add(fake_publish)
    db.commit()

    publish_task = routers.publish.commit_publish(
        env=get_environment("test"),
        publish_id=fake_publish.id,
        db=db,
        settings=Settings(),
    )

    assert isinstance(publish_task, Task)

    mock_commit.assert_has_calls(calls=[
        mock.call.send(
            publish_id="123e4567-e89b-12d3-a456-426614174000",
            env="test",
            from_date=mock.ANY,
        )
    ], )
 def __init__(self):
     self.settings = Settings()
     self.db = Session(bind=db_engine(self.settings))
     self.now = datetime.now(timezone.utc)
Exemple #7
0
        LOG.warning(
            "Rolling back %d item(s) due to error",
            len(self.rollback_item_ids),
            exc_info=exception,
        )

        chunk_size = self.settings.item_yield_size
        for index in range(0, len(self.rollback_item_ids), chunk_size):
            item_ids = self.rollback_item_ids[index:index + chunk_size]
            if item_ids:
                del_items = self.db.query(Item).filter(Item.id.in_(item_ids))
                write_batches(self.env, del_items, self.from_date, delete=True)


@dramatiq.actor(time_limit=Settings().actor_time_limit)
def commit(publish_id: str, env: str, from_date: str) -> None:
    actor_msg_id = CurrentMessage.get_current_message().message_id
    commit_obj = Commit(publish_id, env, from_date, actor_msg_id)

    if not commit_obj.should_write():
        return

    commit_obj.task.state = TaskStates.in_progress
    commit_obj.db.commit()

    try:
        commit_obj.write_publish_items()
        commit_obj.task.state = TaskStates.complete
        commit_obj.publish.state = PublishStates.committed
        commit_obj.db.commit()