Exemplo n.º 1
0
 def setUp(self):
     self.wechat_message_worker = self.mock_coroutine_object(
         "app.proxy.worker.WechatMessageProxyWorker")
     self.token_provider = self.mock_coroutine_object(
         "app.wechat.token.AccessTokenProvider")
     self.wechat_client = self.mock_coroutine_object(
         "app.wechat.client.WechatClient")
     self.message_event_handlers = [
         WechatReplyTextMessageHandler(
             self.wechat_client,
             WechatEventSetting(
                 setting={
                     WechatEventSetting.Subscribe_Reply_Text: "subscribe"
                 }))
     ]
     self.wechat_message_worker.mock_method('proxy_message',
                                            self._proxy_message)
     self.wechat_message_proxy = WechatMessageProxy(
         self.token_provider, WechatMessageParser(),
         self.wechat_message_worker, self.message_event_handlers)
     self.access_token = AccessToken('abcd', 7200)
     self.token_provider.set_method_result('get_access_token',
                                           self.access_token)
     self.wechat_request_validator = WechatMessageRequestValidator("token")
     self.proxy_passed_message = None
     self.url = "/api/v1/message?signature=59d7df0c0961b050088fe2cc833340663d12bd03" \
                "&timestamp=1441077547&nonce=1917417690"
     super(MessageHandlerTestCase, self).setUp()
class EventSettingHandlerTestCase(AsyncHTTPTestCase):
    def setUp(self):
        self.wechat_event_setting = WechatEventSetting(
            setting={
                WechatEventSetting.Subscribe_Reply_Text: 'hahaha'
            }
        )
        self.auth_headers = {
            'Authorization': 'Basic YWRtaW46MTIzNDU2'
        }
        super(EventSettingHandlerTestCase, self).setUp()

    def get_app(self):
        return Application(
            [
                (
                    r"/api/v1/event-setting", EventSettingHandler,
                    dict(
                        auth_compare_func=lambda x, y: x == 'admin' and y == '123456',
                        wechat_event_setting=self.wechat_event_setting
                    )
                )
            ]
        )

    def test_get_setting_without_auth(self):
        response = self.get('/api/v1/event-setting')
        self.assertEqual(401, response.code)

    def test_get_setting(self):
        response = self.get('/api/v1/event-setting', headers=self.auth_headers)
        self.assertEqual(200, response.code)
        self.assertIn('hahaha', response.body)

    def test_update_setting(self):
        response = self.post('/api/v1/event-setting', body='', headers=self.auth_headers)
        self.assertEqual(400, response.code)

        setting = self.wechat_event_setting.get_all()
        setting[WechatEventSetting.Subscribe_Reply_Text] = 'blabla'
        response = self.post(
            '/api/v1/event-setting', body=json.dumps(setting, ensure_ascii=False), headers=self.auth_headers
        )
        self.assertEqual(200, response.code)
        new_setting = self.wechat_event_setting.get(WechatEventSetting.Subscribe_Reply_Text)
        self.assertEqual(new_setting, 'blabla')
 def setUp(self):
     self.wechat_event_setting = WechatEventSetting(
         setting={
             WechatEventSetting.Subscribe_Reply_Text: 'hahaha'
         }
     )
     self.auth_headers = {
         'Authorization': 'Basic YWRtaW46MTIzNDU2'
     }
     super(EventSettingHandlerTestCase, self).setUp()
class EventSettingHandlerTestCase(AsyncHTTPTestCase):
    def setUp(self):
        self.wechat_event_setting = WechatEventSetting(
            setting={WechatEventSetting.Subscribe_Reply_Text: 'hahaha'})
        self.auth_headers = {'Authorization': 'Basic YWRtaW46MTIzNDU2'}
        super(EventSettingHandlerTestCase, self).setUp()

    def get_app(self):
        return Application([
            (r"/api/v1/event-setting", EventSettingHandler,
             dict(
                 auth_compare_func=lambda x, y: x == 'admin' and y == '123456',
                 wechat_event_setting=self.wechat_event_setting))
        ])

    def test_get_setting_without_auth(self):
        response = self.get('/api/v1/event-setting')
        self.assertEqual(401, response.code)

    def test_get_setting(self):
        response = self.get('/api/v1/event-setting', headers=self.auth_headers)
        self.assertEqual(200, response.code)
        self.assertIn('hahaha', response.body)

    def test_update_setting(self):
        response = self.post('/api/v1/event-setting',
                             body='',
                             headers=self.auth_headers)
        self.assertEqual(400, response.code)

        setting = self.wechat_event_setting.get_all()
        setting[WechatEventSetting.Subscribe_Reply_Text] = 'blabla'
        response = self.post('/api/v1/event-setting',
                             body=json.dumps(setting, ensure_ascii=False),
                             headers=self.auth_headers)
        self.assertEqual(200, response.code)
        new_setting = self.wechat_event_setting.get(
            WechatEventSetting.Subscribe_Reply_Text)
        self.assertEqual(new_setting, 'blabla')
 def setUp(self):
     self.wechat_event_setting = WechatEventSetting(
         setting={WechatEventSetting.Subscribe_Reply_Text: 'hahaha'})
     self.auth_headers = {'Authorization': 'Basic YWRtaW46MTIzNDU2'}
     super(EventSettingHandlerTestCase, self).setUp()
Exemplo n.º 6
0
 def wechat_event_setting(self):
     setting_file = self.config.get(
         'event_setting_file',
         '{}/event_setting.cfg'.format(self._base_path))
     return WechatEventSetting(setting_file)