예제 #1
0
 def initial_setup(self):
     if not test_globals:
         test_globals['app'] = application.app
         test_globals['dbfile'] = tempfile.mkstemp(prefix='_db')[1]
         db_url = os.environ.get(
             'DB_URL', 'sqlite:///%s' % test_globals['dbfile'])
         subprocess.Popen(['cp', '.proto.sqlite', test_globals['dbfile']])
         path = os.path.join(os.path.dirname(
             os.path.abspath(__file__)), '..', 'media')
         db_seed_file = os.path.join(path, '..', 'tests', 'data',
                                     'db_fixture.yaml')
         test_globals['redis'] = redis_conn = fakeredis.FakeStrictRedis(
             server=fakeredis.FakeServer())
         initialize.app(
             application, controllers, models, path,
             db_seed_file=db_seed_file, db_url=db_url,
             redis_conn=redis_conn)
         db_session = database.get_session(db_url=db_url)
         database.seed_new_db(db_session)
         db_session.remove()
         self.baseSetup()
     yield test_globals
     try:
         if os.environ.get('DBCLEAN', None) != '0':
             os.remove(test_globals['dbfile'])
     except FileNotFoundError:
         # TODO this is getting called for each test class
         # (thought scope='session' would invoke only once)
         pass
예제 #2
0
def app():
    app = Flask(__name__, instance_relative_config=True)

    server = fakeredis.FakeServer()
    fr = fakeredis.FakeStrictRedis(server=server, decode_responses=True)
    fr.set('next_uid', 1)
    fr.set('next_mid', 1)
    fr.set('next_nid', 1)
    fr.bgsave()

    app.config.from_mapping(
        SECRET_KEY='dev',
        RDSCXN=fr,
    )

    app.register_blueprint(auth.bp)
    app.register_blueprint(profile.bp)
    app.register_blueprint(class_list.bp)
    app.register_blueprint(message.bp)
    app.register_blueprint(recovery.bp)
    app.register_blueprint(match.bp)
    app.register_blueprint(schedule.bp)
    app.register_blueprint(search.bp)
    app.register_blueprint(notification.bp)

    return app
예제 #3
0
    def test_lock_is_removed_after_func_is_finished(self):
        redis_params = make_redis_params(
            file_path='test_dir/test_file.pkl',
            unique_id='123abc',
            redis_host='0.0.0.0',
            redis_port=12345,
        )

        server = fakeredis.FakeServer()

        with patch('gokart.redis_lock.redis.Redis') as redis_mock:
            redis_mock.return_value = fakeredis.FakeRedis(
                server=server,
                host=redis_params.redis_host,
                port=redis_params.redis_port)
            mock_func = MagicMock()
            resulted = with_lock(func=mock_func,
                                 redis_params=redis_params)(123, b='abc')

            mock_func.assert_called_once()
            called_args, called_kwargs = mock_func.call_args
            self.assertTupleEqual(called_args, (123, ))
            self.assertDictEqual(called_kwargs, dict(b='abc'))

            self.assertEqual(resulted, mock_func())

            fake_redis = fakeredis.FakeStrictRedis(server=server)
            with self.assertRaises(KeyError):
                fake_redis[redis_params.redis_key]
예제 #4
0
    def test_refresh_keyset_timeout(self, monkeypatch):
        metrics = pretend.stub(
            increment=pretend.call_recorder(lambda *a, **kw: None))
        service = services.OIDCProviderService(
            provider="example",
            issuer_url="https://example.com",
            cache_url="rediss://fake.example.com",
            metrics=metrics,
        )

        # Create a fake server to provide persistent state through each
        # StrictRedis.from_url context manager.
        server = fakeredis.FakeServer()
        from_url = functools.partial(fakeredis.FakeStrictRedis.from_url,
                                     server=server)
        monkeypatch.setattr(services.redis.StrictRedis, "from_url", from_url)

        keyset = {"fake-key-id": {"foo": "bar"}}
        service._store_keyset(keyset)

        keys = service._refresh_keyset()
        assert keys == keyset
        assert metrics.increment.calls == [
            pretend.call("warehouse.oidc.refresh_keyset.timeout",
                         tags=["provider:example"])
        ]
예제 #5
0
def test_move_happy_path(mocker, user):
    app = application.test_client()
    app.testing = True
    session_id = '0x5dc99a053b9a2ca01da214a3a159e4c2da7be0ba7ff4e073551e023e31c5a332'  # noqa: E501

    # Move
    data = {
        'session_id': session_id,
        'direction': 1,
    }

    # Session
    gamestate = {
        'board': [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [2, 2, 0, 0]],
        'score': 0,
        'gameover': False,
    }
    paid_session = {
        'paid': True,
        'gamestate': gamestate,
        'user': user.address,
    }

    server = fakeredis.FakeServer()
    redis = fakeredis.FakeStrictRedis(server=server)
    redis.set(session_id, json.dumps(paid_session))
    mocker.patch('webserver.move.redis.Redis.from_url').return_value = redis

    response = app.post('/api/v1/move',
                        data=json.dumps(data),
                        content_type='application/json')
    assert response.status_code == 200
예제 #6
0
def test_happy_path(user, mocker):
    # Move
    session_id = new_session()
    move_payload = {
        'session_id': session_id,
        'direction': 1,
    }

    # PaidSession
    gamestate = {
        'board': [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [2, 2, 0, 0]],
        'score': 0,
        'gameover': False,
    }
    paid_session = {
        'paid': True,
        'gamestate': gamestate,
        'user': user.address,
    }
    server = fakeredis.FakeServer()
    redis = fakeredis.FakeStrictRedis(server=server)
    redis.set(session_id, json.dumps(paid_session))
    mocker.patch('webserver.move.redis.Redis.from_url').return_value = redis

    # Run
    signed_gamestate = json.loads(post_move(move_payload))
    session = json.loads(redis.get(session_id))

    # Test
    errors = SignedGamestateResponse().validate(signed_gamestate)
    assert not errors
    errors = PaidSession().validate(session)
    assert not errors
예제 #7
0
 def setUp(self):
     server = fakeredis.FakeServer()
     self.mockclient = fakeredis.FakeStrictRedis(server=server)
     config = {
     }
     mock_client = Redis(host='localhost', test_client=self.mockclient)
     self.interface = Status(config, mock_client)
예제 #8
0
def test_confirm_payment_happy_path(mocker, user, monkeypatch):
    app = application.test_client()
    app.testing = True
    session_id = '0x5dc99a053b9a2ca01da214a3a159e4c2da7be0ba7ff4e073551e023e31c5a332'  # noqa: E501
    payment_code = '0x1c3103ad99a02bea73d377c82a490eef72fea445b39a2ec8e4b6e671b534b13b'  # noqa: E501
    payload = {
        'session_id': session_id,
        'user': user.address,
    }

    # Session
    unpaid_session = {
        'paid': False,
        'payment_code': payment_code,
    }
    server = fakeredis.FakeServer()
    redis = fakeredis.FakeStrictRedis(server=server)
    redis.set(session_id, json.dumps(unpaid_session))
    mocker.patch('webserver.payment_confirmation.redis.Redis.from_url',
                 ).return_value = redis

    # Contract
    monkeypatch.setattr("webserver.payment_confirmation.ADDRESS", ADDRESS)
    contract = mocker.patch(
        'webserver.payment_confirmation.arcade_protocol.Contract',
    ).return_value
    contract.get_payment_code.return_value = payment_code
    contract.address = ADDRESS
    contract.game_id = '0x4a3c9000acbe7d73d0d6dcea6abd664006dadbd4d7c37c7095635c9c47ca1d4e'  # noqa: E501

    response = app.get('/api/v1/payment_confirmation',
                       query_string=payload,
                       content_type='application/json')
    assert response.status_code == 200
예제 #9
0
def app():
    app = Flask(__name__)
    server = fakeredis.FakeServer()
    server.connected = True
    app.redis = fakeredis.FakeStrictRedis(server=server)
    app.app_context().push()
    yield app
예제 #10
0
def redis_server(settings):
    settings.OBSERVABILITY_BROKER_URL = BROKER_URL
    settings.OBSERVABILITY_BUFFER_SIZE_LIMIT = MAX_SIZE
    settings.OBSERVABILITY_BUFFER_BATCH_SIZE = BATCH_SIZE
    server = fakeredis.FakeServer()
    server.connected = True
    return server
예제 #11
0
def _patch_redis_adapter():
    """Allows testing of Corva apps without running a real Redis server.

    Internally Corva uses Redis as cache. This function patches RedisAdapter to use
    fakeredis instead of redis. fakeredis is a library that simulates talking to a
    real Redis server. This way the function allows testing Corva apps without running
    a real Redis server.

    Patch steps:
      1. patch RedisAdapter.__bases__ to use fakeredis.FakeRedis instead of redis.Redis
      2. patch redis.from_url with fakeredis.FakeRedis.from_url
    """

    redis_adapter_path = 'corva.state.redis_adapter'

    redis_adapter_patcher = mock.patch(
        f'{redis_adapter_path}.RedisAdapter.__bases__',
        (fakeredis.FakeRedis, ))

    server = (
        fakeredis.FakeServer()
    )  # use FakeServer to share cache between different instances of RedisState
    from_url_patcher = mock.patch(
        f'{redis_adapter_path}.from_url',
        side_effect=functools.partial(fakeredis.FakeRedis.from_url,
                                      server=server),
    )

    with redis_adapter_patcher, from_url_patcher:
        # stops mock.patch from trying to call delattr when reversing the patch
        redis_adapter_patcher.is_local = True

        yield
예제 #12
0
def test_user_grabs_someone_elses_payment_code(user, mocker):
    honest_user = user

    actual_payment_code_the_user_got = GameInterface.new_payment_code()
    session_id = new_session()

    # Liar pretends to be honest
    payment_locator_payload = {
        'session_id': session_id,
        'user': honest_user.address,
    }

    # UnpaidSession
    unpaid_session = {
        'paid': False,
        'payment_code': actual_payment_code_the_user_got,
    }
    server = fakeredis.FakeServer()
    redis = fakeredis.FakeStrictRedis(server=server)
    redis.set(session_id, json.dumps(unpaid_session))
    mocker.patch('webserver.payment_confirmation.redis.Redis.from_url',
                 ).return_value = redis

    # Contract
    some_other_payment_code = GameInterface.new_payment_code()
    contract = mocker.patch(
        'webserver.payment_confirmation.arcade_protocol.Contract',
    ).return_value
    contract.get_payment_code.return_value = some_other_payment_code

    # Run / Test
    with pytest.raises(PaymentError):
        get_payment_confirmation(payment_locator_payload)
예제 #13
0
def test_payment_doesnt_confirm(user, mocker, monkeypatch):
    payment_code = GameInterface.new_payment_code()
    session_id = new_session()
    payment_locator_payload = {
        'session_id': session_id,
        'user': user.address,
    }

    # UnpaidSession
    unpaid_session = {
        'paid': False,
        'payment_code': payment_code,
    }
    server = fakeredis.FakeServer()
    redis = fakeredis.FakeStrictRedis(server=server)
    redis.set(session_id, json.dumps(unpaid_session))
    mocker.patch('webserver.payment_confirmation.redis.Redis.from_url',
                 ).return_value = redis

    # Contract
    empty_payment_code = '0x0'
    contract = mocker.patch(
        'webserver.payment_confirmation.arcade_protocol.Contract',
    ).return_value
    contract.get_payment_code.return_value = empty_payment_code

    # Run
    with pytest.raises(PaymentError):
        get_payment_confirmation(payment_locator_payload)
예제 #14
0
    def __init__(self,
                 db=0,
                 charset='utf-8',
                 errors='strict',
                 decode_responses=False,
                 connected=True,
                 **kwargs):

        db = int(kwargs.get("port", 0))
        if db not in port_2_fake_server:
            port_2_fake_server[db] = server = fakeredis.FakeServer()
        else:
            server = port_2_fake_server[db]

        if 'connection_pool' in kwargs:
            kwargs.pop('connection_pool')
        # 新版的fakeredis的不同的存储是通过server来控制的,server相同则存储相同
        FakeStrictRedis.__init__(self,
                                 db=0,
                                 charset=charset,
                                 errors=errors,
                                 decode_responses=decode_responses,
                                 connected=connected,
                                 server=server,
                                 **kwargs)
예제 #15
0
    def test_refresh_keyset_successful(self, monkeypatch):
        metrics = pretend.stub(
            increment=pretend.call_recorder(lambda *a, **kw: None))
        service = services.OIDCProviderService(
            provider="example",
            issuer_url="https://example.com",
            cache_url="rediss://fake.example.com",
            metrics=metrics,
        )

        # Create a fake server to provide persistent state through each
        # StrictRedis.from_url context manager.
        server = fakeredis.FakeServer()
        from_url = functools.partial(fakeredis.FakeStrictRedis.from_url,
                                     server=server)
        monkeypatch.setattr(services.redis.StrictRedis, "from_url", from_url)

        openid_resp = pretend.stub(
            ok=True,
            json=lambda: {
                "jwks_uri": "https://example.com/.well-known/jwks.json",
            },
        )
        jwks_resp = pretend.stub(
            ok=True,
            json=lambda: {"keys": [{
                "kid": "fake-key-id",
                "foo": "bar"
            }]})

        def get(url):
            if url == "https://example.com/.well-known/jwks.json":
                return jwks_resp
            else:
                return openid_resp

        requests = pretend.stub(get=pretend.call_recorder(get))
        sentry_sdk = pretend.stub(
            capture_message=pretend.call_recorder(lambda msg: pretend.stub()))
        monkeypatch.setattr(services, "requests", requests)
        monkeypatch.setattr(services, "sentry_sdk", sentry_sdk)

        keys = service._refresh_keyset()

        assert keys == {"fake-key-id": {"kid": "fake-key-id", "foo": "bar"}}
        assert metrics.increment.calls == []
        assert requests.get.calls == [
            pretend.call(
                "https://example.com/.well-known/openid-configuration"),
            pretend.call("https://example.com/.well-known/jwks.json"),
        ]
        assert sentry_sdk.capture_message.calls == []

        # Ensure that we also cached the updated keyset as part of refreshing.
        keys, timeout = service._get_keyset()
        assert keys == {"fake-key-id": {"kid": "fake-key-id", "foo": "bar"}}
        assert timeout is True
예제 #16
0
def get_connection(config=None, retry=False):
    host = config.REDIS_HOST
    port = config.REDIS_PORT
    # connection = redis.Redis(
    #     host=host,
    #     port=port
    # )
    server = fakeredis.FakeServer()
    connection = fakeredis.FakeStrictRedis(server=server)
    test = connection.set('connection', 'true')
    return connection
예제 #17
0
def app(mocker):
    """
    Set up a Flask App instance for testing.
    Create a Redis mock to abstract the tests from the actual Redis instance.
    """
    server = fakeredis.FakeServer()
    fake_redis = fakeredis.FakeStrictRedis(server=server)
    mocker.patch('redis.StrictRedis', return_value=fake_redis)

    flask_app = CanvasAPI.create_app('config.TestingConfig')
    yield flask_app
def test_check_cache_available():
    fake_redis_server = fakeredis.FakeServer()
    test_redis_handler = fakeredis.FakeStrictRedis(charset="utf-8",
                                                   decode_responses=True,
                                                   server=fake_redis_server)
    test_sirius_service = SiriusService(config_params=SiriusServiceTestConfig,
                                        cache=test_redis_handler)

    result = test_sirius_service.check_cache_available()

    assert result is True
예제 #19
0
    def wrapper(*args, **kwargs):
        server = fakeredis.FakeServer()

        def from_url(url, **kwargs):
            if expect_url is not None:
                assert url == expect_url

            return fakeredis.FakeStrictRedis(server=server)  # type: ignore

        with patch('redis.Redis.from_url', side_effect=from_url):
            return f(*args, **kwargs)
def redis(monkeypatch):
    server = fakeredis.FakeServer()
    conn = fakeredis.FakeStrictRedis(server=server)

    monkeypatch.setattr("passari_workflow.queue.queues.get_redis_connection",
                        lambda: conn)
    monkeypatch.setattr("passari_workflow.jobs.utils.get_redis_connection",
                        lambda: conn)
    monkeypatch.setattr("passari_workflow.heartbeat.get_redis_connection",
                        lambda: conn)

    yield conn
예제 #21
0
def test_payment_code_happy_path(mocker):
    server = fakeredis.FakeServer()
    redis = fakeredis.FakeStrictRedis(server=server)
    mocker.patch(
        'webserver.payment_code.redis.Redis.from_url', ).return_value = redis

    app = application.test_client()
    app.testing = True
    data = {}
    response = app.get('/api/v1/payment_code',
                       data=json.dumps(data),
                       content_type='application/json')
    assert response.status_code == 200
예제 #22
0
def redis_handle():
    settings = Settings().dict()
    if settings['testing']:
        import fakeredis
        global FAKE_REDIS_SERVER
        if not FAKE_REDIS_SERVER:
            FAKE_REDIS_SERVER = fakeredis.FakeServer()
        r = fakeredis.FakeRedis(server=FAKE_REDIS_SERVER)
    else:
        r = redis.Redis(
            host=settings['redis_host'],
            port=settings['redis_port'],
        )
    return r
예제 #23
0
def test_happy_path(mocker):
    server = fakeredis.FakeServer()
    redis = fakeredis.FakeStrictRedis(server=server)
    mocker.patch(
        'webserver.payment_code.redis.Redis.from_url', ).return_value = redis

    payment_code = json.loads(get_payment_code())
    session_id = payment_code['session_id']
    session = json.loads(redis.get(session_id))

    errors = UnpaidSession().validate(session)
    assert not errors
    errors = PaymentCodeResponse().validate(payment_code)
    assert not errors
예제 #24
0
def flask_client(context, *args, **kwargs):
    """Start a new Flask Test Client.

    This fixture will start a new Flask Client, also configuring it to
    run as test in order to make it easier to run our requests on it.

    """
    app = create_app(env='TESTING')
    app.testing = True

    fake_redis = fakeredis.FakeServer()
    redis.instance = fakeredis.FakeStrictRedis(server=fake_redis)

    context.client = app.test_client()
예제 #25
0
def test_user_has_no_session_id_in_redis(user, mocker):
    # Move
    session_id = new_session()
    move_payload = {
        'session_id': session_id,
        'direction': 1,
    }
    server = fakeredis.FakeServer()
    redis = fakeredis.FakeStrictRedis(server=server)
    mocker.patch('webserver.move.redis.Redis.from_url').return_value = redis

    # Run / Test
    with pytest.raises(PaidSessionValidationError):
        post_move(move_payload)
예제 #26
0
def test_user_has_no_session_id_in_redis(user, mocker):
    session_id = new_session()
    payment_locator_payload = {
        'session_id': session_id,
        'user': user.address,
    }
    server = fakeredis.FakeServer()
    redis = fakeredis.FakeStrictRedis(server=server)
    mocker.patch('webserver.payment_confirmation.redis.Redis.from_url'
                 ).return_value = redis

    # Run / Test
    with pytest.raises(UnpaidSessionValidationError):
        get_payment_confirmation(payment_locator_payload)
def redis(monkeypatch):
    """
    Fixture for a fake Redis server
    """
    server = fakeredis.FakeServer()
    conn = fakeredis.FakeStrictRedis(server=server)

    monkeypatch.setattr("passari_workflow.queue.queues.get_redis_connection",
                        lambda: conn)
    monkeypatch.setattr("passari_web_ui.api.views.get_redis_connection",
                        lambda: conn)
    monkeypatch.setattr("passari_workflow.heartbeat.get_redis_connection",
                        lambda: conn)

    yield conn
예제 #28
0
def get_redis_client():
    # Only import fakeredis in testing environment
    if config['TESTING'] == True:
        import fakeredis
        global server
        if server is None:
            server = fakeredis.FakeServer()
        return fakeredis.FakeStrictRedis(server=server)
    else:
        # Create connection pool
        global redis_pool
        if redis_pool is None:
            redis_pool = redis.ConnectionPool(
                host=config['REDIS']['HOST'],
                port=config['REDIS']['PORT'],
                db=0,
            )
        return redis.Redis(connection_pool=redis_pool)
예제 #29
0
def test_put_sirius_data_in_cache_broken(caplog):

    test_key = "test_key"
    test_data = {"test": "data"}

    fake_redis_server = fakeredis.FakeServer()
    fake_redis_server.connected = False
    test_redis_handler = fakeredis.FakeStrictRedis(charset="utf-8",
                                                   decode_responses=True,
                                                   server=fake_redis_server)
    test_sirius_service = SiriusService(config_params=SiriusServiceTestConfig,
                                        cache=test_redis_handler)

    test_sirius_service._put_sirius_data_in_cache(key=test_key,
                                                  data=json.dumps(test_data))

    with caplog.at_level("ERROR"):
        assert "Unable to set cache" in caplog.text
    def connect(self, redis_url=None):

        if self.type == 'DEV':
            return redis.Redis()
        if self.type == 'TEST':
            return fakeredis.FakeStrictRedis(server=fakeredis.FakeServer())

        if redis_url:
            self.redis_url = redis_url

        if not self.redis_url:
            raise Exception(
                'You need to set REDIS_URL environment or set TYPE environment to [DEV || TEST] or '
                'set the parameter redis_url in connect function.')

        hosts = self.get_hosts_from_redis_url()

        return Sentinel([(host.split(':')[0], self.extract_port_from_host(host)) for host in hosts]).\
            master_for(self.get_service_name(), password=self.get_password())