|
| 1 | +import { input } from '@inquirer/prompts'; |
| 2 | +import { getAuthenticationUrl } from 'common/lib/oauth'; |
| 3 | +import { getUserInfo } from 'cli/lib/api'; |
| 4 | +import { |
| 5 | + getAuthToken, |
| 6 | + lockAppdata, |
| 7 | + readAppdata, |
| 8 | + saveAppdata, |
| 9 | + unlockAppdata, |
| 10 | +} from 'cli/lib/appdata'; |
| 11 | +import { openBrowser } from 'cli/lib/browser'; |
| 12 | +import { getAppLocale } from 'cli/lib/i18n'; |
| 13 | +import { Logger, LoggerError } from 'cli/logger'; |
| 14 | + |
| 15 | +jest.mock( '@inquirer/prompts' ); |
| 16 | +jest.mock( 'common/lib/oauth' ); |
| 17 | +jest.mock( 'cli/lib/api' ); |
| 18 | +jest.mock( 'cli/lib/appdata' ); |
| 19 | +jest.mock( 'cli/lib/browser' ); |
| 20 | +jest.mock( 'cli/lib/i18n' ); |
| 21 | +jest.mock( 'cli/logger' ); |
| 22 | + |
| 23 | +describe( 'Auth Login Command', () => { |
| 24 | + const mockAccessToken = 'mock-access-token-12345'; |
| 25 | + const mockAuthUrl = 'https://public-api.wordpress.com/oauth2/authorize?client_id=123'; |
| 26 | + const mockUserData = { |
| 27 | + ID: 12345, |
| 28 | + email: 'test@example.com', |
| 29 | + display_name: 'Test User', |
| 30 | + }; |
| 31 | + const mockAppdata = { |
| 32 | + authToken: { |
| 33 | + accessToken: 'existing-token', |
| 34 | + id: 999, |
| 35 | + email: 'existing@example.com', |
| 36 | + displayName: 'Existing User', |
| 37 | + expiresIn: 1209600, |
| 38 | + expirationTime: Date.now() + 1209600000, |
| 39 | + }, |
| 40 | + }; |
| 41 | + |
| 42 | + let mockLogger: { |
| 43 | + reportStart: jest.Mock; |
| 44 | + reportSuccess: jest.Mock; |
| 45 | + reportError: jest.Mock; |
| 46 | + reportKeyValuePair: jest.Mock; |
| 47 | + }; |
| 48 | + |
| 49 | + beforeEach( () => { |
| 50 | + jest.clearAllMocks(); |
| 51 | + |
| 52 | + mockLogger = { |
| 53 | + reportStart: jest.fn(), |
| 54 | + reportSuccess: jest.fn(), |
| 55 | + reportError: jest.fn(), |
| 56 | + reportKeyValuePair: jest.fn(), |
| 57 | + }; |
| 58 | + |
| 59 | + ( Logger as jest.Mock ).mockReturnValue( mockLogger ); |
| 60 | + ( getAuthenticationUrl as jest.Mock ).mockReturnValue( mockAuthUrl ); |
| 61 | + ( getAppLocale as jest.Mock ).mockResolvedValue( 'en' ); |
| 62 | + ( getUserInfo as jest.Mock ).mockResolvedValue( mockUserData ); |
| 63 | + ( openBrowser as jest.Mock ).mockResolvedValue( undefined ); |
| 64 | + ( input as jest.Mock ).mockResolvedValue( mockAccessToken ); |
| 65 | + ( readAppdata as jest.Mock ).mockResolvedValue( mockAppdata ); |
| 66 | + ( getAuthToken as jest.Mock ).mockRejectedValue( new Error( 'Mock error' ) ); |
| 67 | + ( lockAppdata as jest.Mock ).mockResolvedValue( undefined ); |
| 68 | + ( unlockAppdata as jest.Mock ).mockResolvedValue( undefined ); |
| 69 | + ( saveAppdata as jest.Mock ).mockResolvedValue( undefined ); |
| 70 | + } ); |
| 71 | + |
| 72 | + afterEach( () => { |
| 73 | + jest.restoreAllMocks(); |
| 74 | + } ); |
| 75 | + |
| 76 | + it( 'should skip login if already authenticated', async () => { |
| 77 | + ( getAuthToken as jest.Mock ).mockResolvedValue( mockAppdata.authToken ); |
| 78 | + |
| 79 | + const { runCommand } = await import( '../login' ); |
| 80 | + await runCommand(); |
| 81 | + |
| 82 | + expect( openBrowser ).not.toHaveBeenCalled(); |
| 83 | + expect( input ).not.toHaveBeenCalled(); |
| 84 | + } ); |
| 85 | + |
| 86 | + it( 'should complete the login process successfully', async () => { |
| 87 | + const { runCommand } = await import( '../login' ); |
| 88 | + await runCommand(); |
| 89 | + |
| 90 | + expect( getAuthenticationUrl ).toHaveBeenCalledWith( |
| 91 | + 'en', |
| 92 | + 'https://developer.wordpress.com/copy-oauth-token' |
| 93 | + ); |
| 94 | + expect( openBrowser ).toHaveBeenCalledWith( mockAuthUrl ); |
| 95 | + expect( input ).toHaveBeenCalledWith( { |
| 96 | + message: 'Authentication token:', |
| 97 | + } ); |
| 98 | + expect( getUserInfo ).toHaveBeenCalledWith( mockAccessToken ); |
| 99 | + expect( lockAppdata ).toHaveBeenCalled(); |
| 100 | + expect( saveAppdata ).toHaveBeenCalledWith( { |
| 101 | + authToken: { |
| 102 | + accessToken: mockAccessToken, |
| 103 | + id: mockUserData.ID, |
| 104 | + email: mockUserData.email, |
| 105 | + displayName: mockUserData.display_name, |
| 106 | + expiresIn: expect.any( Number ), |
| 107 | + expirationTime: expect.any( Number ), |
| 108 | + }, |
| 109 | + } ); |
| 110 | + expect( unlockAppdata ).toHaveBeenCalled(); |
| 111 | + } ); |
| 112 | + |
| 113 | + it( 'should proceed with login if existing token is invalid', async () => { |
| 114 | + const { runCommand } = await import( '../login' ); |
| 115 | + await runCommand(); |
| 116 | + |
| 117 | + expect( openBrowser ).toHaveBeenCalled(); |
| 118 | + expect( input ).toHaveBeenCalled(); |
| 119 | + } ); |
| 120 | + |
| 121 | + it( 'should handle browser open failure', async () => { |
| 122 | + const browserError = new LoggerError( 'Failed to open browser' ); |
| 123 | + ( openBrowser as jest.Mock ).mockRejectedValue( browserError ); |
| 124 | + |
| 125 | + const { runCommand } = await import( '../login' ); |
| 126 | + await runCommand(); |
| 127 | + |
| 128 | + expect( input ).toHaveBeenCalled(); |
| 129 | + } ); |
| 130 | + |
| 131 | + it( 'should handle API error when fetching user info', async () => { |
| 132 | + const apiError = new LoggerError( 'Failed to fetch user info' ); |
| 133 | + ( getUserInfo as jest.Mock ).mockRejectedValue( apiError ); |
| 134 | + |
| 135 | + const { runCommand } = await import( '../login' ); |
| 136 | + await runCommand(); |
| 137 | + |
| 138 | + expect( mockLogger.reportError ).toHaveBeenCalled(); |
| 139 | + expect( mockLogger.reportError ).toHaveBeenCalledWith( expect.any( LoggerError ) ); |
| 140 | + expect( getUserInfo ).toHaveBeenCalled(); |
| 141 | + } ); |
| 142 | + |
| 143 | + it( 'should unlock appdata even if save fails', async () => { |
| 144 | + const saveError = new Error( 'Failed to save' ); |
| 145 | + ( saveAppdata as jest.Mock ).mockRejectedValue( saveError ); |
| 146 | + |
| 147 | + const { runCommand } = await import( '../login' ); |
| 148 | + await runCommand(); |
| 149 | + |
| 150 | + expect( mockLogger.reportError ).toHaveBeenCalled(); |
| 151 | + expect( mockLogger.reportError ).toHaveBeenCalledWith( expect.any( LoggerError ) ); |
| 152 | + expect( lockAppdata ).toHaveBeenCalled(); |
| 153 | + expect( unlockAppdata ).toHaveBeenCalled(); |
| 154 | + } ); |
| 155 | + |
| 156 | + it( 'should handle lock appdata failure', async () => { |
| 157 | + const lockError = new Error( 'Failed to lock' ); |
| 158 | + ( lockAppdata as jest.Mock ).mockRejectedValue( lockError ); |
| 159 | + |
| 160 | + const { runCommand } = await import( '../login' ); |
| 161 | + await runCommand(); |
| 162 | + |
| 163 | + expect( mockLogger.reportError ).toHaveBeenCalled(); |
| 164 | + expect( mockLogger.reportError ).toHaveBeenCalledWith( expect.any( LoggerError ) ); |
| 165 | + } ); |
| 166 | + |
| 167 | + it( 'should use provided locale', async () => { |
| 168 | + ( getAppLocale as jest.Mock ).mockResolvedValue( 'fr' ); |
| 169 | + |
| 170 | + const { runCommand } = await import( '../login' ); |
| 171 | + await runCommand(); |
| 172 | + |
| 173 | + expect( getAuthenticationUrl ).toHaveBeenCalledWith( |
| 174 | + 'fr', |
| 175 | + 'https://developer.wordpress.com/copy-oauth-token' |
| 176 | + ); |
| 177 | + } ); |
| 178 | +} ); |
0 commit comments