Repository: Yoctol/bottender
Pull request #121 (2017-12-25 06:16:14)

Fix Telegram CLI bug


Patch:
diff --git a/src/cli/providers/telegram/webhook.js b/src/cli/providers/telegram/webhook.js
index 8fd87042..8124c87c 100644
--- a/src/cli/providers/telegram/webhook.js
+++ b/src/cli/providers/telegram/webhook.js
@@ -24,7 +24,7 @@ export async function getWebhook() {
     invariant(accessToken, '`accessToken` is not found in config file');
 
     const client = TelegramClient.connect(accessToken);
-    const { data: { ok, result } } = await client.getWebhookInfo();
+    const { ok, result } = await client.getWebhookInfo();
     invariant(ok, 'Getting Telegram webhook is failed');
 
     Object.keys(result).forEach(key => print(`${key}: ${result[key]}`));
@@ -56,7 +56,7 @@ export async function setWebhook(_webhook) {
       '`webhook` is required but not found. Use -w <webhook> to setup or make sure you are running ngrok server.'
     );
 
-    const { data: { ok } } = await client.setWebhook(webhook);
+    const { ok } = await client.setWebhook(webhook);
     invariant(ok, 'Setting for webhook is failed');
 
     print('Successfully set Telegram webhook callback URL');
@@ -81,7 +81,7 @@ export async function deleteWebhook() {
     invariant(accessToken, '`accessToken` is not found in config file');
 
     const client = TelegramClient.connect(accessToken);
-    const { data: { ok } } = await client.deleteWebhook();
+    const { ok } = await client.deleteWebhook();
     invariant(ok, 'Deleting Telegram webhook is failed');
 
     print('Successfully delete Telegram webhook');


Test patch:
diff --git a/src/cli/providers/telegram/__tests__/deleteWebhook.spec.js b/src/cli/providers/telegram/__tests__/deleteWebhook.spec.js
index 385cfd8e..26f15f90 100644
--- a/src/cli/providers/telegram/__tests__/deleteWebhook.spec.js
+++ b/src/cli/providers/telegram/__tests__/deleteWebhook.spec.js
@@ -21,13 +21,9 @@ beforeEach(() => {
   process.exit = jest.fn();
   getConfig.mockReturnValue(MOCK_FILE_WITH_PLATFORM.telegram);
 
-  log.print = jest.fn();
-  log.error = jest.fn();
-  log.bold = jest.fn(s => s);
-
   TelegramClient.connect.mockReturnValue({
-    deleteWebhook: jest.fn(() => ({
-      data: {
+    deleteWebhook: jest.fn(() =>
+      Promise.resolve({
         ok: true,
         result: {
           url: 'https://4a16faff.ngrok.io/',
@@ -35,8 +31,8 @@ beforeEach(() => {
           pending_update_count: 0,
           max_connections: 40,
         },
-      },
-    })),
+      })
+    ),
   });
 });
 
@@ -60,11 +56,11 @@ describe('resolve', () => {
 
 describe('reject', () => {
   it('reject when Telegram return not success', async () => {
-    TelegramClient.connect().deleteWebhook.mockReturnValueOnce({
-      data: {
+    TelegramClient.connect().deleteWebhook.mockImplementationOnce(
+      Promise.resolve({
         ok: false,
-      },
-    });
+      })
+    );
     await deleteWebhook();
     expect(deleteWebhook().then).toThrow();
   });
diff --git a/src/cli/providers/telegram/__tests__/getWebhook.spec.js b/src/cli/providers/telegram/__tests__/getWebhook.spec.js
index 9d1c3ef5..de67de72 100644
--- a/src/cli/providers/telegram/__tests__/getWebhook.spec.js
+++ b/src/cli/providers/telegram/__tests__/getWebhook.spec.js
@@ -21,13 +21,9 @@ beforeEach(() => {
   process.exit = jest.fn();
   getConfig.mockReturnValue(MOCK_FILE_WITH_PLATFORM.telegram);
 
-  log.print = jest.fn();
-  log.error = jest.fn();
-  log.bold = jest.fn(s => s);
-
   TelegramClient.connect.mockReturnValue({
-    getWebhookInfo: jest.fn(() => ({
-      data: {
+    getWebhookInfo: jest.fn(() =>
+      Promise.resolve({
         ok: true,
         result: {
           url: 'https://4a16faff.ngrok.io/',
@@ -35,8 +31,8 @@ beforeEach(() => {
           pending_update_count: 0,
           max_connections: 40,
         },
-      },
-    })),
+      })
+    ),
   });
 });
 
@@ -59,11 +55,11 @@ describe('resolve', () => {
 
 describe('reject', () => {
   it('reject when Telegram return not success', async () => {
-    TelegramClient.connect().getWebhookInfo.mockReturnValueOnce({
-      data: {
+    TelegramClient.connect().getWebhookInfo.mockImplementationOnce(
+      Promise.resolve({
         ok: false,
-      },
-    });
+      })
+    );
     await getWebhook();
     expect(getWebhook().then).toThrow();
   });
diff --git a/src/cli/providers/telegram/__tests__/setWebhook.spec.js b/src/cli/providers/telegram/__tests__/setWebhook.spec.js
index 774d14cf..ab7fc0d7 100644
--- a/src/cli/providers/telegram/__tests__/setWebhook.spec.js
+++ b/src/cli/providers/telegram/__tests__/setWebhook.spec.js
@@ -45,13 +45,9 @@ beforeEach(() => {
     ],
   });
 
-  log.print = jest.fn();
-  log.error = jest.fn();
-  log.bold = jest.fn(s => s);
-
   TelegramClient.connect.mockReturnValue({
-    setWebhook: jest.fn(() => ({
-      data: {
+    setWebhook: jest.fn(() =>
+      Promise.resolve({
         ok: true,
         result: {
           url: 'https://4a16faff.ngrok.io/',
@@ -59,8 +55,8 @@ beforeEach(() => {
           pending_update_count: 0,
           max_connections: 40,
         },
-      },
-    })),
+      })
+    ),
   });
 });
 
@@ -107,11 +103,11 @@ describe('reject', () => {
 
   it('reject when telegram return not success', () => {
     const { webhook } = setup();
-    TelegramClient.connect().setWebhook.mockReturnValueOnce({
-      data: {
+    TelegramClient.connect().setWebhook.mockImplementationOnce(
+      Promise.resolve({
         ok: false,
-      },
-    });
+      })
+    );
     expect(setWebhook(webhook).then).toThrow();
   });
 });
diff --git a/src/cli/providers/viber/__tests__/deleteWebhook.spec.js b/src/cli/providers/viber/__tests__/deleteWebhook.spec.js
index 6a70e4a0..3bd4a282 100644
--- a/src/cli/providers/viber/__tests__/deleteWebhook.spec.js
+++ b/src/cli/providers/viber/__tests__/deleteWebhook.spec.js
@@ -22,10 +22,8 @@ beforeEach(() => {
 
   ViberClient.connect.mockReturnValue({
     removeWebhook: jest.fn(() => ({
-      data: {
-        status: 0,
-        status_message: 'ok',
-      },
+      status: 0,
+      status_message: 'ok',
     })),
   });
 });
@@ -50,7 +48,7 @@ describe('resolve', () => {
 describe('reject', () => {
   it('reject when Viber return not success', async () => {
     ViberClient.connect().removeWebhook.mockImplementation(() =>
-      Promise.reject()
+      Promise.reject(new Error('removeWebhook failed'))
     );
 
     expect(deleteWebhook().then).toThrow();
diff --git a/src/cli/providers/viber/__tests__/setWebhook.spec.js b/src/cli/providers/viber/__tests__/setWebhook.spec.js
index 0fec486a..08508e09 100644
--- a/src/cli/providers/viber/__tests__/setWebhook.spec.js
+++ b/src/cli/providers/viber/__tests__/setWebhook.spec.js
@@ -48,18 +48,16 @@ beforeEach(() => {
 
   ViberClient.connect.mockReturnValue({
     setWebhook: jest.fn(() => ({
-      data: {
-        status: 0,
-        status_message: 'ok',
-        event_types: [
-          'delivered',
-          'seen',
-          'failed',
-          'subscribed',
-          'unsubscribed',
-          'conversation_started',
-        ],
-      },
+      status: 0,
+      status_message: 'ok',
+      event_types: [
+        'delivered',
+        'seen',
+        'failed',
+        'subscribed',
+        'unsubscribed',
+        'conversation_started',
+      ],
     })),
   });
 });