Skip to content
This repository has been archived by the owner on Nov 21, 2020. It is now read-only.

Commit

Permalink
refetch acces token facebook
Browse files Browse the repository at this point in the history
  • Loading branch information
Enkhtuvshin0513 committed Aug 24, 2020
1 parent 0a06b5e commit c313e27
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 21 deletions.
82 changes: 66 additions & 16 deletions src/__tests__/facebook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Comments, ConversationMessages, Conversations, Customers, Posts } from
import receiveMessage from '../facebook/receiveMessage';
import receivePost from '../facebook/receivePost';
import * as store from '../facebook/store';
import * as utils from '../facebook/utils';
import {
accountFactory,
facebookCommentFactory,
Expand Down Expand Up @@ -105,13 +106,13 @@ describe('Facebook test', () => {
});

afterEach(async () => {
await Comments.remove({});
await Posts.remove({});
await Accounts.remove({});
await Integrations.remove({});
await Customers.remove({});
await Conversations.remove({});
await ConversationMessages.remove({});
await Comments.deleteMany({});
await Posts.deleteMany({});
await Accounts.deleteMany({});
await Integrations.deleteMany({});
await Customers.deleteMany({});
await Conversations.deleteMany({});
await ConversationMessages.deleteMany({});
});

// store test
Expand All @@ -121,6 +122,15 @@ describe('Facebook test', () => {
return Promise.resolve({ _id: '123456789' });
});

const getFacebookUser = sinon.stub(utils, 'getFacebookUser').callsFake(() => Promise.resolve({}));
const getFacebookUserProfilePic = sinon
.stub(utils, 'getFacebookUserProfilePic')
.callsFake(() => Promise.resolve('picture'));

const getPageAccessToken = sinon.stub(utils, 'getPageAccessToken').callsFake(() => {
return Promise.resolve('token');
});

try {
await Promise.all([
store.getOrCreateCustomer(pageId, '123', 'facebook-messenger'),
Expand All @@ -131,21 +141,61 @@ describe('Facebook test', () => {
expect(await Customers.find({}).countDocuments()).toBe(2);
}

mock.restore();
await mock.restore();
await getFacebookUser.restore();
await getFacebookUserProfilePic.restore();
await getPageAccessToken.restore();
});

test('Create or get customer with get user error', async () => {
const mock = sinon.stub(message, 'sendRPCMessage').callsFake(() => {
return Promise.resolve({ _id: '12345678910' });
});

const getFacebookUser = sinon.stub(utils, 'getFacebookUser').callsFake(() => {
throw new Error();
});

const getFacebookUserProfilePic = sinon
.stub(utils, 'getFacebookUserProfilePic')
.callsFake(() => Promise.resolve('picture'));

const getPageAccessToken = sinon.stub(utils, 'getPageAccessToken').callsFake(() => {
return Promise.resolve('token');
});

const customer = await store.getOrCreateCustomer(pageId, '12345', 'facebook-messenger');

expect(customer.userId).toBe('12345');

await getFacebookUser.restore();
await mock.restore();
await getPageAccessToken.restore();
await getFacebookUserProfilePic.restore();
});

test('Create or get customer with rabbitmq error', async () => {
const mock = sinon.stub(message, 'sendRPCMessage').callsFake(() => {
throw new Error();
});

const getFacebookUserProfilePic = sinon
.stub(utils, 'getFacebookUserProfilePic')
.callsFake(() => Promise.resolve('picture'));

const getPageAccessToken = sinon.stub(utils, 'getPageAccessToken').callsFake(() => {
return Promise.resolve('token');
});

try {
await store.getOrCreateCustomer(pageId, '123', 'facebook-messenger');
} catch (e) {
expect(await Customers.find({}).countDocuments()).toBe(1);
}

mock.restore();
await mock.restore();
await getPageAccessToken.restore();
await getFacebookUserProfilePic.restore();
});

test('Create or get post', async () => {
Expand All @@ -162,7 +212,7 @@ describe('Facebook test', () => {

expect(await Posts.countDocuments()).toEqual(1);

mock.restore();
await mock.restore();
});

test('Create or get post with rabbitmq error', async () => {
Expand All @@ -176,7 +226,7 @@ describe('Facebook test', () => {
expect(await Posts.find({}).countDocuments()).toBe(0);
}

mock.restore();
await mock.restore();
});

test('Create or get comment', async () => {
Expand All @@ -194,7 +244,7 @@ describe('Facebook test', () => {

expect(await Comments.countDocuments()).toEqual(1);

mock.restore();
await mock.restore();
});

// receiveMessage function test
Expand All @@ -213,7 +263,7 @@ describe('Facebook test', () => {
expect(conversation.content).toEqual(activity.channelData.text);
}

mock.restore();
await mock.restore();
});

test('Receive message conversation rabbitmq error', async () => {
Expand All @@ -227,7 +277,7 @@ describe('Facebook test', () => {
expect(await Conversations.find({}).countDocuments()).toBe(0);
}

mock.restore();
await mock.restore();
});

test('Receive message conversationMessage with mongo error', async () => {
Expand All @@ -246,7 +296,7 @@ describe('Facebook test', () => {
expect(conversationMessage.content).toEqual(activity.channelData.text);
}

mock.restore();
await mock.restore();
});

test('Receive message conversationMessage rabbitmq error', async () => {
Expand All @@ -270,7 +320,7 @@ describe('Facebook test', () => {

expect(conversationMessage.mid).toEqual('mid');

mock.restore();
await mock.restore();
});

// recivePost
Expand Down
4 changes: 2 additions & 2 deletions src/facebook/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export const getOrCreateCustomer = async (pageId: string, userId: string, kind:
}

let fbUser = {} as any;
let fbUserProfilePic = {} as any;
let fbUserProfilePic;

try {
// create customer
Expand All @@ -166,7 +166,7 @@ export const getOrCreateCustomer = async (pageId: string, userId: string, kind:

facebookPageTokensMap[pageId] = newPageAccessToken;

await integration.updateOne({ facebookPageTokensMap });
await Integrations.updateOne({ _id: integration._id }, { $set: { facebookPageTokensMap } });
}

// save on integrations db
Expand Down
6 changes: 3 additions & 3 deletions src/facebook/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const getFacebookUser = async (pageId: string, pageTokens: { [key: string
pageAccessToken = getPageAccessTokenFromMap(pageId, pageTokens);
} catch (e) {
debugFacebook(`Error occurred while getting page access token: ${e.message}`);
throw new Error();
return null;
}

const pageToken = pageAccessToken;
Expand All @@ -84,7 +84,7 @@ export const getFacebookUser = async (pageId: string, pageTokens: { [key: string
return response;
} catch (e) {
debugFacebook(`Error occurred while getting facebook user: ${e.message}`);
throw new Error();
return null;
}
};

Expand Down Expand Up @@ -159,7 +159,7 @@ export const sendReply = async (url: string, data: any, recipientId: string, int

facebookPageTokensMap[recipientId] = newPageAccessToken;

await integration.updateOne({ facebookPageTokensMap });
await Integrations.updateOne({ _id: integration._id }, { $set: { facebookPageTokensMap } });
}

if (e.message.includes('does not exist')) {
Expand Down

0 comments on commit c313e27

Please sign in to comment.