@@ -1727,4 +1727,103 @@ describe('UsersManager', () => {
17271727 expect ( request . isDone ( ) ) . toBe ( true ) ;
17281728 } ) ;
17291729 } ) ;
1730+
1731+ describe ( '#getConnectedAccounts' , ( ) => {
1732+ const data = {
1733+ id : 'user_id' ,
1734+ } ;
1735+
1736+ let scope : nock . Scope ;
1737+
1738+ beforeEach ( ( ) => {
1739+ scope = nock ( API_URL ) . get ( `/users/${ data . id } /connected-accounts` ) . reply ( 200 , [ ] ) ;
1740+ } ) ;
1741+
1742+ it ( 'should return a promise when no callback is given' , ( done ) => {
1743+ expect ( usersManager . getConnectedAccounts ( data ) . then ( ( ) => done ( ) ) ) . toBeInstanceOf ( Promise ) ;
1744+ } ) ;
1745+
1746+ it ( 'should perform a GET request to /api/v2/users/user_id/connected-accounts' , async ( ) => {
1747+ await usersManager . getConnectedAccounts ( data ) ;
1748+ expect ( scope . isDone ( ) ) . toBe ( true ) ;
1749+ } ) ;
1750+
1751+ it ( 'should pass any errors to the promise catch handler' , async ( ) => {
1752+ nock . cleanAll ( ) ;
1753+
1754+ nock ( API_URL ) . get ( `/users/${ data . id } /connected-accounts` ) . reply ( 500 , { } ) ;
1755+
1756+ try {
1757+ await usersManager . getConnectedAccounts ( data ) ;
1758+ } catch ( err ) {
1759+ expect ( err ) . toBeDefined ( ) ;
1760+ }
1761+ } ) ;
1762+
1763+ it ( 'should include the token in the authorization header' , async ( ) => {
1764+ nock . cleanAll ( ) ;
1765+
1766+ const request = nock ( API_URL )
1767+ . get ( `/users/${ data . id } /connected-accounts` )
1768+ . matchHeader ( 'authorization' , `Bearer ${ token } ` )
1769+ . reply ( 200 , [ ] ) ;
1770+
1771+ await usersManager . getConnectedAccounts ( data ) ;
1772+ expect ( request . isDone ( ) ) . toBe ( true ) ;
1773+ } ) ;
1774+
1775+ it ( 'should pass the body of the response to the "then" handler' , async ( ) => {
1776+ nock . cleanAll ( ) ;
1777+
1778+ const connectedAccountsData = [
1779+ {
1780+ id : 'conn_123' ,
1781+ connection : 'google-oauth2' ,
1782+ connection_id : 'con_abc123' ,
1783+ strategy : 'google-oauth2' ,
1784+ access_type : 'offline' ,
1785+ scopes : [ 'profile' , 'email' ] ,
1786+ created_at : '2023-01-01T00:00:00.000Z' ,
1787+ expires_at : '2024-01-01T00:00:00.000Z' ,
1788+ } ,
1789+ ] ;
1790+
1791+ const response = {
1792+ connected_accounts : connectedAccountsData ,
1793+ next : null ,
1794+ } ;
1795+
1796+ nock ( API_URL ) . get ( `/users/${ data . id } /connected-accounts` ) . reply ( 200 , response ) ;
1797+
1798+ const connectedAccounts = await usersManager . getConnectedAccounts ( data ) ;
1799+ expect ( connectedAccounts . data . connected_accounts ) . toBeInstanceOf ( Array ) ;
1800+ expect ( connectedAccounts . data . connected_accounts . length ) . toBe ( connectedAccountsData . length ) ;
1801+ expect ( connectedAccounts . data . connected_accounts [ 0 ] . id ) . toBe ( connectedAccountsData [ 0 ] . id ) ;
1802+ expect ( connectedAccounts . data . connected_accounts [ 0 ] . connection ) . toBe ( connectedAccountsData [ 0 ] . connection ) ;
1803+ expect ( connectedAccounts . data . connected_accounts [ 0 ] . strategy ) . toBe ( connectedAccountsData [ 0 ] . strategy ) ;
1804+ } ) ;
1805+
1806+ it ( 'should pass the parameters in the query-string' , async ( ) => {
1807+ nock . cleanAll ( ) ;
1808+
1809+ const params = {
1810+ from : 'conn_123' ,
1811+ take : 10 ,
1812+ } ;
1813+ const request = nock ( API_URL )
1814+ . get ( `/users/${ data . id } /connected-accounts` )
1815+ . query ( params )
1816+ . reply ( 200 , [ ] ) ;
1817+
1818+ await usersManager . getConnectedAccounts ( {
1819+ id : data . id ,
1820+ ...params ,
1821+ } ) ;
1822+ expect ( request . isDone ( ) ) . toBe ( true ) ;
1823+ } ) ;
1824+
1825+ it ( 'should validate empty id' , async ( ) => {
1826+ await expect ( usersManager . getConnectedAccounts ( { } as any ) ) . rejects . toThrowError ( ) ;
1827+ } ) ;
1828+ } ) ;
17301829} ) ;
0 commit comments