Ejemplo n.º 1
0
 def _get_message(self, channel, message_id):
     request_channel_use_case = RequestChannelAPIUseCase(channel)
     message_payload = request_channel_use_case.get_message(message_id)
     message = Message.from_dict(message_payload['message'])
     message.status = "received"
     message.sender_ref = str(uuid.uuid4())
     return message
Ejemplo n.º 2
0
 def setup(self):
     self.channel_config = {
         "Id": "1234",
         "Name": "shared db channel to Australia",
         "Jurisdiction": "AU",
         "Predicate": "UN.CEFACT.",
         "ChannelUrl": "http://channel_api_url:8000",
     }
     self.channel_api_url = 'http://channel_api_url:8000/messages/subscriptions/by_jurisdiction'
     self.use_case = RequestChannelAPIUseCase(
         channel_config=self.channel_config)
Ejemplo n.º 3
0
 def subscribe(self, channel):
     channel_url = self.get_channel_subscribe_endpoint_url(channel)
     now = datetime.datetime.utcnow()
     try:
         callback_url = self.get_callback_url(channel)
         logger.info('Sending subscription request to %s', channel_url)
         RequestChannelAPIUseCase(channel).subscribe_by_jurisdiction(callback_url, env.COUNTRY)
     except (SubscriptionFailure, InvalidSubscriptionParameters) as e:
         logger.error(e)
     else:
         self.last_subscribed_at = now
         logger.info('Successfully subscribed at %s' % self.last_subscribed_at)
Ejemplo n.º 4
0
class TestSubscribeByJurisdiction:
    @pytest.fixture(autouse=True)
    def setup(self):
        self.channel_config = {
            "Id": "1234",
            "Name": "shared db channel to Australia",
            "Jurisdiction": "AU",
            "Predicate": "UN.CEFACT.",
            "ChannelUrl": "http://channel_api_url:8000",
        }
        self.channel_api_url = 'http://channel_api_url:8000/messages/subscriptions/by_jurisdiction'
        self.use_case = RequestChannelAPIUseCase(
            channel_config=self.channel_config)

    def test_use_case__with_missing_params__should_raise_exception(self):
        with pytest.raises(InvalidSubscriptionParameters):
            self.use_case.subscribe_by_jurisdiction(callback_url='',
                                                    jurisdiction='something')
        with pytest.raises(InvalidSubscriptionParameters):
            self.use_case.subscribe_by_jurisdiction(callback_url='something',
                                                    jurisdiction='')

    def test_use_case__when_channel_response_not_ok__should_raise_exception(
            self):
        self.mocked_responses.add(responses.POST,
                                  self.channel_api_url,
                                  status=400)
        with pytest.raises(SubscriptionFailure):
            response = self.use_case.subscribe_by_jurisdiction(
                callback_url='https://callback.url',
                jurisdiction='AU',
                secret='123')
            response.raise_for_status()

    def test_use_case__happy_path(self):
        self.mocked_responses.add(responses.POST,
                                  self.channel_api_url,
                                  status=202)
        self.use_case.subscribe_by_jurisdiction(
            callback_url='https://callback.url',
            jurisdiction='AU',
            secret='123')

        body = ('hub.mode=subscribe&'
                'hub.callback=https%3A%2F%2Fcallback.url&'
                'hub.topic=AU&'
                'hub.secret=123')
        assert self.mocked_responses.calls[0].request.body == body