-
Notifications
You must be signed in to change notification settings - Fork 747
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Justin Wilaby
committed
Feb 11, 2019
1 parent
445f75c
commit 77d9813
Showing
5 changed files
with
170 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
packages/emulator/core/src/utils/oauthLinkEncoder.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { AttachmentContentTypes } from '@bfemulator/sdk-shared'; | ||
import OAuthLinkEncoder from './oauthLinkEncoder'; | ||
jest.mock('./uniqueId', () => () => 'fgfdsggf5432534'); | ||
const mockArgsSentToFetch = []; | ||
let ok = true; | ||
let statusText = ''; | ||
let shouldThrow = false; | ||
describe('The OauthLinkEncoder', () => { | ||
let encoder: OAuthLinkEncoder; | ||
beforeAll(() => { | ||
(global as any).fetch = async (...args) => { | ||
mockArgsSentToFetch.push(args); | ||
if (shouldThrow) { | ||
throw new Error("I'm in your throw!"); | ||
} | ||
return { | ||
text: async () => 'im in your text!', | ||
ok, | ||
statusText, | ||
}; | ||
}; | ||
}); | ||
|
||
beforeEach(() => { | ||
ok = true; | ||
statusText = ''; | ||
shouldThrow = false; | ||
mockArgsSentToFetch.length = 0; | ||
const emulator = { | ||
getServiceUrl: async () => 'http://localhost', | ||
facilities: { | ||
conversations: { | ||
conversationById: () => ({ | ||
codeVerifier: '5432654365475677647655676542524352563457', | ||
botEndpoint: { | ||
botUrl: 'http://botbot.bot', | ||
}, | ||
conversationId: 'testConversation', | ||
}), | ||
}, | ||
}, | ||
}; | ||
encoder = new OAuthLinkEncoder( | ||
emulator as any, | ||
'Bearer 54k52n', | ||
{ | ||
attachments: [{ contentType: AttachmentContentTypes.oAuthCard }], | ||
text: 'a message', | ||
}, | ||
'testConversation' | ||
); | ||
}); | ||
|
||
it('should resolveOAuthCards as expected with the happy path', async () => { | ||
const mockActivity = { | ||
attachments: [ | ||
{ | ||
contentType: AttachmentContentTypes.oAuthCard, | ||
content: { | ||
buttons: [{ type: 'signin' }], | ||
}, | ||
}, | ||
], | ||
text: 'a message', | ||
}; | ||
await encoder.resolveOAuthCards(mockActivity); | ||
expect(mockActivity.attachments[0].content.buttons[0]).toEqual({ | ||
type: 'openUrl', | ||
value: 'oauthlink://im in your text!&&&testConversation', | ||
}); | ||
}); | ||
|
||
it('should throw when an error occurs retrieving the link while calling resolveOAuthCards', async () => { | ||
ok = false; | ||
statusText = 'oh noes!'; | ||
const mockActivity = { | ||
attachments: [ | ||
{ | ||
contentType: AttachmentContentTypes.oAuthCard, | ||
content: { | ||
buttons: [{ type: 'signin' }], | ||
}, | ||
}, | ||
], | ||
text: 'a message', | ||
}; | ||
try { | ||
await encoder.resolveOAuthCards(mockActivity); | ||
expect(false); | ||
} catch (e) { | ||
expect(e.message).toEqual(statusText); | ||
} | ||
}); | ||
|
||
it('should throw if fetch throws', async () => { | ||
shouldThrow = true; | ||
const mockActivity = { | ||
attachments: [ | ||
{ | ||
contentType: AttachmentContentTypes.oAuthCard, | ||
content: { | ||
buttons: [{ type: 'signin' }], | ||
}, | ||
}, | ||
], | ||
text: 'a message', | ||
}; | ||
try { | ||
await encoder.resolveOAuthCards(mockActivity); | ||
expect(false); | ||
} catch (e) { | ||
expect(e.message).toEqual("I'm in your throw!"); | ||
} | ||
}); | ||
|
||
it('should generateCodeVerifier as expected', async () => { | ||
const v = encoder.generateCodeVerifier('testConversation'); | ||
expect(v).toBe('84731c2a08da84c59261d2a79a2b1a0bc6ca70b1d1fc2ce9eb74f4ad979d7dad'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters