-
-
Notifications
You must be signed in to change notification settings - Fork 888
chore: moved setup.ts to src/setup and written tests for it #3568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
palisadoes
merged 9 commits into
PalisadoesFoundation:develop-postgres
from
abbi4code:setup_tests
Feb 15, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
10ba47f
chore: moved setup.ts to src/setup and written tests for it
abbi4code 7dd0f3b
chore: Update package.json so that setup will work
abbi4code 04d5e97
chore: removed repeated tests
abbi4code b7cb2b0
chore: added more tests to achieve 100% test coverage
abbi4code 602d822
Merge branch 'develop-postgres' into setup_tests
abbi4code 1d72fac
Merge branch 'develop-postgres' into setup_tests
abbi4code cfd984d
Merge branch 'develop-postgres' into setup_tests
abbi4code fec17ad
Merge branch 'develop-postgres' into setup_tests
palisadoes e62de43
Merge branch 'develop-postgres' into setup_tests
abbi4code File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
docs/docs/auto-docs/setup/setup/functions/askAndSetRecaptcha.md
This file contains hidden or 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,13 @@ | ||
[Admin Docs](/) | ||
|
||
*** | ||
|
||
# Function: askAndSetRecaptcha() | ||
|
||
> **askAndSetRecaptcha**(): `Promise`\<`void`\> | ||
|
||
Defined in: [src/setup/setup.ts:12](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/setup/setup.ts#L12) | ||
|
||
## Returns | ||
|
||
`Promise`\<`void`\> |
This file contains hidden or 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,13 @@ | ||
[Admin Docs](/) | ||
|
||
*** | ||
|
||
# Function: main() | ||
|
||
> **main**(): `Promise`\<`void`\> | ||
|
||
Defined in: [src/setup/setup.ts:62](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/setup/setup.ts#L62) | ||
|
||
## Returns | ||
|
||
`Promise`\<`void`\> |
This file contains hidden or 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 hidden or 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,180 @@ | ||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
import type { MockInstance } from 'vitest'; | ||
import dotenv from 'dotenv'; | ||
import fs from 'fs'; | ||
import { main, askAndSetRecaptcha } from './setup'; | ||
import { checkEnvFile, modifyEnvFile } from './checkEnvFile/checkEnvFile'; | ||
import { validateRecaptcha } from './validateRecaptcha/validateRecaptcha'; | ||
import askAndSetDockerOption from './askAndSetDockerOption/askAndSetDockerOption'; | ||
import updateEnvFile from './updateEnvFile/updateEnvFile'; | ||
import askAndUpdatePort from './askAndUpdatePort/askAndUpdatePort'; | ||
import { askAndUpdateTalawaApiUrl } from './askForDocker/askForDocker'; | ||
import inquirer from 'inquirer'; | ||
|
||
vi.mock('inquirer'); | ||
vi.mock('dotenv'); | ||
vi.mock('fs'); | ||
vi.mock('./checkEnvFile/checkEnvFile'); | ||
vi.mock('./validateRecaptcha/validateRecaptcha'); | ||
vi.mock('./askAndSetDockerOption/askAndSetDockerOption'); | ||
vi.mock('./updateEnvFile/updateEnvFile'); | ||
vi.mock('./askAndUpdatePort/askAndUpdatePort'); | ||
vi.mock('./askForDocker/askForDocker'); | ||
|
||
describe('Talawa Admin Setup', () => { | ||
let processExitSpy: MockInstance; | ||
let consoleErrorSpy: MockInstance; | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
|
||
vi.mocked(checkEnvFile).mockReturnValue(true); | ||
|
||
vi.mocked(fs.readFileSync).mockReturnValue('USE_DOCKER=NO'); | ||
|
||
vi.mocked(dotenv.parse).mockReturnValue({ USE_DOCKER: 'NO' }); | ||
|
||
processExitSpy = vi.spyOn(process, 'exit').mockImplementation((code) => { | ||
throw new Error(`process.exit called with code ${code}`); | ||
}); | ||
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.resetAllMocks(); | ||
processExitSpy.mockRestore(); | ||
consoleErrorSpy.mockRestore(); | ||
}); | ||
|
||
it('should successfully complete setup with default options', async () => { | ||
vi.mocked(inquirer.prompt) | ||
.mockResolvedValueOnce({ shouldUseRecaptcha: false }) | ||
.mockResolvedValueOnce({ shouldLogErrors: false }); | ||
|
||
await main(); | ||
|
||
expect(checkEnvFile).toHaveBeenCalled(); | ||
expect(modifyEnvFile).toHaveBeenCalled(); | ||
expect(askAndSetDockerOption).toHaveBeenCalled(); | ||
expect(askAndUpdatePort).toHaveBeenCalled(); | ||
expect(askAndUpdateTalawaApiUrl).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should skip port and API URL setup when Docker is used', async () => { | ||
vi.mocked(fs.readFileSync).mockReturnValue('USE_DOCKER=YES'); | ||
vi.mocked(dotenv.parse).mockReturnValue({ USE_DOCKER: 'YES' }); | ||
|
||
vi.mocked(inquirer.prompt) | ||
.mockResolvedValueOnce({ shouldUseRecaptcha: false }) | ||
.mockResolvedValueOnce({ shouldLogErrors: false }); | ||
|
||
await main(); | ||
|
||
expect(askAndUpdatePort).not.toHaveBeenCalled(); | ||
expect(askAndUpdateTalawaApiUrl).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should handle error logging setup when user opts in', async () => { | ||
vi.mocked(inquirer.prompt) | ||
.mockResolvedValueOnce({ shouldUseRecaptcha: false }) | ||
.mockResolvedValueOnce({ shouldLogErrors: true }); | ||
|
||
await main(); | ||
|
||
expect(updateEnvFile).toHaveBeenCalledWith('ALLOW_LOGS', 'YES'); | ||
}); | ||
|
||
it('should exit if env file check fails', async () => { | ||
vi.mocked(checkEnvFile).mockReturnValue(false); | ||
const consoleSpy = vi.spyOn(console, 'error'); | ||
|
||
await main(); | ||
|
||
expect(modifyEnvFile).not.toHaveBeenCalled(); | ||
expect(askAndSetDockerOption).not.toHaveBeenCalled(); | ||
expect(consoleSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should handle errors during setup process', async () => { | ||
const mockError = new Error('Setup failed'); | ||
|
||
vi.mocked(askAndSetDockerOption).mockRejectedValue(mockError); | ||
|
||
const consoleSpy = vi.spyOn(console, 'error'); | ||
|
||
const processExitSpy = vi | ||
.spyOn(process, 'exit') | ||
.mockImplementation(() => undefined as never); | ||
|
||
await main(); | ||
|
||
expect(consoleSpy).toHaveBeenCalledWith('\n❌ Setup failed:', mockError); | ||
expect(processExitSpy).toHaveBeenCalledWith(1); | ||
}); | ||
|
||
it('should handle file system operations correctly', async () => { | ||
vi.mocked(inquirer.prompt) | ||
.mockResolvedValueOnce({ shouldUseRecaptcha: false }) | ||
.mockResolvedValueOnce({ shouldLogErrors: false }); | ||
|
||
const mockEnvContent = 'MOCK_ENV_CONTENT'; | ||
|
||
vi.mocked(fs.readFileSync).mockReturnValue(mockEnvContent); | ||
|
||
await main(); | ||
|
||
expect(fs.readFileSync).toHaveBeenCalledWith('.env', 'utf8'); | ||
expect(dotenv.parse).toHaveBeenCalledWith(mockEnvContent); | ||
}); | ||
it('should handle user opting out of reCAPTCHA setup', async () => { | ||
vi.mocked(inquirer.prompt).mockResolvedValueOnce({ | ||
shouldUseRecaptcha: false, | ||
}); | ||
|
||
await askAndSetRecaptcha(); | ||
|
||
expect(inquirer.prompt).toHaveBeenCalledTimes(1); | ||
expect(updateEnvFile).not.toHaveBeenCalled(); | ||
expect(validateRecaptcha).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should validate reCAPTCHA key input', async () => { | ||
const mockInvalidKey = 'invalid-key'; | ||
|
||
vi.mocked(inquirer.prompt) | ||
.mockResolvedValueOnce({ shouldUseRecaptcha: true }) | ||
.mockImplementationOnce((questions) => { | ||
const question = Array.isArray(questions) ? questions[0] : questions; | ||
|
||
const validationResult = question.validate(mockInvalidKey); | ||
|
||
expect(validationResult).toBe( | ||
'Invalid reCAPTCHA site key. Please try again.', | ||
); | ||
return Object.assign( | ||
Promise.resolve({ recaptchaSiteKeyInput: mockInvalidKey }), | ||
); | ||
}); | ||
|
||
vi.mocked(validateRecaptcha).mockReturnValue(false); | ||
|
||
await askAndSetRecaptcha(); | ||
|
||
expect(validateRecaptcha).toHaveBeenCalledWith(mockInvalidKey); | ||
}); | ||
|
||
it('should handle errors during reCAPTCHA setup', async () => { | ||
const mockError = new Error('ReCAPTCHA setup failed'); | ||
|
||
vi.mocked(inquirer.prompt).mockRejectedValue(mockError); | ||
|
||
await expect(askAndSetRecaptcha()).rejects.toThrow( | ||
'Failed to set up reCAPTCHA: ReCAPTCHA setup failed', | ||
); | ||
|
||
expect(consoleErrorSpy).toHaveBeenCalledWith( | ||
'Error setting up reCAPTCHA:', | ||
mockError, | ||
); | ||
expect(updateEnvFile).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.