def test_get_subscriptions_by_pattern__should_return_subscriptions(self):
        repo = SubscriptionsRepo(connection_data=self.connection_data)
        self.client.list_objects.side_effect = [{
            'Contents': []
        }, {
            'Contents': [{
                'Key': 'AA/BB/ff0d1111f6636c354cf92c7137f1b5e6'
            }]
        }]
        self.client.get_object.return_value = {
            'Body':
            BytesIO(
                b'{"c": "http://callback.url/1", "e": "2020-05-18T15:08:00"}'),
            'Bucket':
            'subscriptions',
            'ContentLength':
            39,
            'Key':
            'AA',
        }

        subscriptions = repo.get_subscriptions_by_pattern(Pattern('aa.bb'))
        assert len(list(subscriptions)) == 1
        assert list(subscriptions)[0].callback_url == 'http://callback.url/1'
        assert self.client.list_objects.mock_calls == [
            mock.call(Bucket='subscriptions', Prefix='AA/', Delimiter='/'),
            mock.call(Bucket='subscriptions', Prefix='AA/BB/', Delimiter='/')
        ]
        self.client.get_object.assert_called_once_with(
            Bucket='subscriptions',
            Key='AA/BB/ff0d1111f6636c354cf92c7137f1b5e6')
    def test_get_subscriptions_by_id__should_return_subscriptions(self):
        repo = SubscriptionsRepo(connection_data=self.connection_data)
        self.client.list_objects.return_value = {
            'Contents': [{
                'Key': 'some_ref/ff0d1111f6636c354cf92c7137f1b5e6'
            }]
        }
        self.client.get_object.return_value = {
            'Body':
            BytesIO(
                b'{"c": "http://callback.url/1", "e": "2020-05-18T15:08:00"}'),
            'Bucket':
            'subscriptions',
            'ContentLength':
            39,
            'Key':
            'some_ref/ff0d1111f6636c354cf92c7137f1b5e6',
        }

        subscriptions = repo.get_subscriptions_by_id(Id('some_ref'))

        assert list(subscriptions)[0].callback_url == 'http://callback.url/1'
        self.client.list_objects.assert_called_once_with(
            Bucket='subscriptions', Prefix='some_ref', Delimiter='/')
        self.client.get_object.assert_called_once_with(
            Bucket='subscriptions',
            Key='some_ref/ff0d1111f6636c354cf92c7137f1b5e6')
 def test_subscribe_by_pattern__with_missing_expiration__should_return_error(
         self):
     repo = SubscriptionsRepo(connection_data=self.connection_data)
     pattern = Pattern('aaa.bbb.ccc')
     with self.assertRaises(SubscriptionMissingExpiration):
         repo.subscribe_by_pattern(pattern,
                                   'http://callback.url/1',
                                   expiration_seconds=0)
 def test_subscribe_by_id__with_missing_expiration__should_return_error(
         self):
     repo = SubscriptionsRepo(connection_data=self.connection_data)
     id = Id('some_ref')
     with self.assertRaises(SubscriptionMissingExpiration):
         repo.subscribe_by_id(id,
                              'http://callback.url/1',
                              expiration_seconds=0)
    def test_subscribe_by_pattern__should_put_object_into_repo(self):
        repo = SubscriptionsRepo(connection_data=self.connection_data)
        pattern = Pattern('aaa.bbb.ccc')

        repo.subscribe_by_pattern(pattern,
                                  'http://callback.url/1',
                                  expiration_seconds=3600)
        self.client.put_object.assert_called_once()
        args, kwargs = self.client.put_object.call_args

        assert kwargs['Bucket'] == 'subscriptions'
        assert kwargs['Key'] == 'AAA/BBB/CCC/ff0d1111f6636c354cf92c7137f1b5e6'
        assert kwargs['Body'].read(
        ) == b'{"c": "http://callback.url/1", "e": "2020-05-18T15:08:00"}'
    def test_subscribe_by_id__should_put_object_into_repo(self):
        repo = SubscriptionsRepo(connection_data=self.connection_data)
        id = Id('some_ref')

        repo.subscribe_by_id(id,
                             'http://callback.url/1',
                             expiration_seconds=3600)
        self.client.put_object.assert_called_once()
        args, kwargs = self.client.put_object.call_args

        assert kwargs['Bucket'] == 'subscriptions'
        assert kwargs['Key'] == 'some_ref/ff0d1111f6636c354cf92c7137f1b5e6'
        assert kwargs['Body'].read(
        ) == b'{"c": "http://callback.url/1", "e": "2020-05-18T15:08:00"}'
示例#7
0
def _deregister_subscription(form):
    repo = SubscriptionsRepo(Config.SUBSCR_REPO_CONF)
    use_case = SubscriptionDeregisterUseCase(repo)
    try:
        use_case.execute(form[CALLBACK_ATTR_KEY], form[TOPIC_ATTR_KEY])
    except SubscriptionNotFound as e:
        raise SubscriptionNotFoundError() from e
    return Response(status=StatusCode.ACCEPTED)
    def test_get_subscriptions_by_pattern__when_same_callback__should_return_one(
            self):
        repo = SubscriptionsRepo(connection_data=self.connection_data)
        self.client.list_objects.side_effect = [
            {
                'Contents': [{
                    'Key': 'AA/ff0d1111f6636c354cf92c7137f1b5e6'
                }]
            },
            {
                'Contents': [{
                    'Key': 'AA/BB/ff0d1111f6636c354cf92c7137f1b5e6'
                }]
            },
        ]
        self.client.get_object.side_effect = [
            {
                'Body':
                BytesIO(
                    b'{"c": "http://callback.url/1", "e": "2020-05-18T15:08:00"}'
                ),
                'Bucket':
                'subscriptions',
                'ContentLength':
                39,
                'Key':
                'AA',
            },
            {
                'Body':
                BytesIO(
                    b'{"c": "http://callback.url/1", "e": "2020-05-18T15:08:00"}'
                ),
                'Bucket':
                'subscriptions',
                'ContentLength':
                39,
                'Key':
                'AA/BB',
            },
        ]

        subscriptions = repo.get_subscriptions_by_pattern(Pattern('aa'))
        assert len(subscriptions) == 1
示例#9
0
def _register_subscription(form):
    repo = SubscriptionsRepo(Config.SUBSCR_REPO_CONF)
    use_case = SubscriptionRegisterUseCase(repo)
    result = use_case.execute(form[CALLBACK_ATTR_KEY], form[TOPIC_ATTR_KEY],
                              form[LEASE_SECONDS_ATTR_KEY])
    if result is None:
        raise UnableToPostSubscriptionError()
    elif not result:
        raise SubscriptionExistsError()
    return Response(status=StatusCode.ACCEPTED)
示例#10
0
def clean_subscriptions_repo(app, request):
    repo = SubscriptionsRepo(app.config['SUBSCRIPTIONS_REPO_CONF'])
    repo._unsafe_method__clear()
    if request.cls is not None:
        request.cls.subscriptions_repo = repo
    yield repo
    repo._unsafe_method__clear()
示例#11
0
 def _prepare_subscriptions_repo(self, conf):
     subscriptions_repo_conf = env_s3_config('PROC_SUB_REPO')
     if conf:
         subscriptions_repo_conf.update(conf)
     self.subscriptions_repo = SubscriptionsRepo(subscriptions_repo_conf)
示例#12
0
 def _get_repo(self):
     return SubscriptionsRepo(
         current_app.config.get('SUBSCRIPTIONS_REPO_CONF'))