Skip to content

Commit

Permalink
Fix: Missing & Incorrect API URLs in .env.example (#3512)
Browse files Browse the repository at this point in the history
* .env.example updated

* Script fixed

* url correction

* code coverage for url prompt
  • Loading branch information
JaiPannu-IITI authored Feb 1, 2025
1 parent a61d983 commit 735869e
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ PORT=4321

# Run Talawa-api locally in your system, and put its url into the same.

REACT_APP_TALAWA_URL=
REACT_APP_TALAWA_URL=http://localhost:4000/graphql

# Do you want to setup and use "I'm not a robot" Checkbox (Google Recaptcha)?
# If no, leave blank, else write yes
Expand All @@ -24,7 +24,7 @@ REACT_APP_USE_RECAPTCHA=
REACT_APP_RECAPTCHA_SITE_KEY=

# has to be inserted in the env file to use plugins and other websocket based features.
REACT_APP_BACKEND_WEBSOCKET_URL=ws://localhost:4000/graphql/
REACT_APP_BACKEND_WEBSOCKET_URL=ws://localhost:4000/graphql

# If you want to logs Compiletime and Runtime error , warning and info write YES or if u want to
# keep the console clean leave it blank
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

# Function: checkEnvFile()

> **checkEnvFile**(): `void`
> **checkEnvFile**(): `boolean`
Defined in: [src/setup/checkEnvFile/checkEnvFile.ts:6](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/setup/checkEnvFile/checkEnvFile.ts#L6)

## Returns

`void`
`boolean`
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Admin Docs](/)

***

# Function: modifyEnvFile()

> **modifyEnvFile**(): `void`
Defined in: [src/setup/checkEnvFile/checkEnvFile.ts:18](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/setup/checkEnvFile/checkEnvFile.ts#L18)

## Returns

`void`
12 changes: 6 additions & 6 deletions docs/docs/docs/getting-started/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,21 @@ Add a custom port number for Talawa-Admin development purposes to the variable n
Add the endpoint for accessing talawa-api graphql service to the variable named `REACT_APP_TALAWA_URL` in the `.env` file.

```
REACT_APP_TALAWA_URL="http://API-IP-ADRESS:4000/graphql/"
REACT_APP_TALAWA_URL="http://API-IP-ADRESS:4000/graphql"
```

If you are a software developer working on your local system, then the URL would be:

```
REACT_APP_TALAWA_URL="http://localhost:4000/graphql/"
REACT_APP_TALAWA_URL="http://localhost:4000/graphql"
```

If you are trying to access Talawa Admin from a remote host with the API URL containing "localhost", You will have to change the API URL to

```
REACT_APP_TALAWA_URL="http://YOUR-REMOTE-ADDRESS:4000/graphql/"
REACT_APP_TALAWA_URL="http://YOUR-REMOTE-ADDRESS:4000/graphql"
```

Expand All @@ -225,21 +225,21 @@ REACT_APP_TALAWA_URL="http://YOUR-REMOTE-ADDRESS:4000/graphql/"
The endpoint for accessing talawa-api WebSocket graphql service for handling subscriptions is automatically added to the variable named `REACT_APP_BACKEND_WEBSOCKET_URL` in the `.env` file.

```
REACT_APP_BACKEND_WEBSOCKET_URL="ws://API-IP-ADRESS:4000/graphql/"
REACT_APP_BACKEND_WEBSOCKET_URL="ws://API-IP-ADRESS:4000/graphql"
```

If you are a software developer working on your local system, then the URL would be:

```
REACT_APP_BACKEND_WEBSOCKET_URL="ws://localhost:4000/graphql/"
REACT_APP_BACKEND_WEBSOCKET_URL="ws://localhost:4000/graphql"
```

If you are trying to access Talawa Admin from a remote host with the API URL containing "localhost", You will have to change the API URL to

```
REACT_APP_BACKEND_WEBSOCKET_URL="ws://YOUR-REMOTE-ADDRESS:4000/graphql/"
REACT_APP_BACKEND_WEBSOCKET_URL="ws://YOUR-REMOTE-ADDRESS:4000/graphql"
```

Expand Down
11 changes: 9 additions & 2 deletions setup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import dotenv from 'dotenv';
import fs from 'fs';
import inquirer from 'inquirer';
import { checkEnvFile } from './src/setup/checkEnvFile/checkEnvFile';
import {
checkEnvFile,
modifyEnvFile,
} from './src/setup/checkEnvFile/checkEnvFile';
import { validateRecaptcha } from './src/setup/validateRecaptcha/validateRecaptcha';
import askAndSetDockerOption from './src/setup/askAndSetDockerOption/askAndSetDockerOption';
import updateEnvFile from './src/setup/updateEnvFile/updateEnvFile';
Expand Down Expand Up @@ -59,9 +62,13 @@ const askAndSetLogErrors = async (): Promise<void> => {
// Main function to run the setup process
export async function main(): Promise<void> {
try {
if (!checkEnvFile()) {
return;
}

console.log('Welcome to the Talawa Admin setup! 🚀');

checkEnvFile();
modifyEnvFile();
await askAndSetDockerOption();
const envConfig = dotenv.parse(fs.readFileSync('.env', 'utf8'));
const useDocker = envConfig.USE_DOCKER === 'YES';
Expand Down
29 changes: 24 additions & 5 deletions src/setup/askForTalawaApiUrl/askForTalawaApiUrl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ describe('askForTalawaApiUrl', () => {
});

it('should return the provided endpoint when user enters it', async () => {
const mockPrompt = vi.spyOn(inquirer, 'prompt').mockResolvedValueOnce({
endpoint: 'http://example.com/graphql',
});

const result = await askForTalawaApiUrl();

expect(mockPrompt).toHaveBeenCalledWith([
{
type: 'input',
name: 'endpoint',
message: 'Enter your talawa-api endpoint:',
default: 'http://localhost:4000/graphql',
},
]);

expect(result).toBe('http://example.com/graphql');
});

it('should return the corrected endpoint when user enters with trailing slash', async () => {
const mockPrompt = vi.spyOn(inquirer, 'prompt').mockResolvedValueOnce({
endpoint: 'http://example.com/graphql/',
});
Expand All @@ -27,16 +46,16 @@ describe('askForTalawaApiUrl', () => {
type: 'input',
name: 'endpoint',
message: 'Enter your talawa-api endpoint:',
default: 'http://localhost:4000/graphql/',
default: 'http://localhost:4000/graphql',
},
]);

expect(result).toBe('http://example.com/graphql/');
expect(result).toBe('http://example.com/graphql');
});

it('should return the default endpoint when the user does not enter anything', async () => {
const mockPrompt = vi.spyOn(inquirer, 'prompt').mockResolvedValueOnce({
endpoint: 'http://localhost:4000/graphql/',
endpoint: 'http://localhost:4000/graphql',
});

const result = await askForTalawaApiUrl();
Expand All @@ -46,10 +65,10 @@ describe('askForTalawaApiUrl', () => {
type: 'input',
name: 'endpoint',
message: 'Enter your talawa-api endpoint:',
default: 'http://localhost:4000/graphql/',
default: 'http://localhost:4000/graphql',
},
]);

expect(result).toBe('http://localhost:4000/graphql/');
expect(result).toBe('http://localhost:4000/graphql');
});
});
7 changes: 5 additions & 2 deletions src/setup/askForTalawaApiUrl/askForTalawaApiUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ export async function askForTalawaApiUrl(): Promise<string> {
type: 'input',
name: 'endpoint',
message: 'Enter your talawa-api endpoint:',
default: 'http://localhost:4000/graphql/',
default: 'http://localhost:4000/graphql',
},
]);
return endpoint;

const correctEndpoint = endpoint.replace(/\/graphql\/$/, '/graphql');

return correctEndpoint;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('WebSocket URL Configuration', () => {

test('should retain default WebSocket URL if no new endpoint is provided', async () => {
vi.spyOn(inquirer, 'prompt').mockResolvedValueOnce({
endpoint: 'http://localhost:4000/graphql/',
endpoint: 'http://localhost:4000/graphql',
});
await askForTalawaApiUrl();

Expand Down
6 changes: 3 additions & 3 deletions src/setup/checkConnection/checkConnection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { vi, describe, beforeEach, it, expect } from 'vitest';
vi.mock('node-fetch');

global.fetch = vi.fn((url) => {
if (url === 'http://example.com/graphql/') {
if (url === 'http://example.com/graphql') {
const responseInit: ResponseInit = {
status: 200,
statusText: 'OK',
Expand All @@ -27,7 +27,7 @@ describe('checkConnection', () => {

test('should return true and log success message if the connection is successful', async () => {
vi.spyOn(console, 'log').mockImplementation((string) => string);
const result = await checkConnection('http://example.com/graphql/');
const result = await checkConnection('http://example.com/graphql');

expect(result).toBe(true);
expect(console.log).toHaveBeenCalledWith(
Expand All @@ -41,7 +41,7 @@ describe('checkConnection', () => {
it('should return false and log error message if the connection fails', async () => {
vi.spyOn(console, 'log').mockImplementation((string) => string);
const result = await checkConnection(
'http://example_not_working.com/graphql/',
'http://example_not_working.com/graphql',
);

expect(result).toBe(false);
Expand Down
54 changes: 49 additions & 5 deletions src/setup/checkEnvFile/checkEnvFile.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from 'fs';
import { checkEnvFile } from './checkEnvFile';
import { checkEnvFile, modifyEnvFile } from './checkEnvFile';
import { vi } from 'vitest';

/**
* This file contains unit tests for the `checkEnvFile` function.
* This file contains unit tests for the `modifyEnvFile` function.
*
* The tests cover:
* - Behavior when the `.env` file is missing required keys and appending them appropriately.
Expand All @@ -14,7 +14,7 @@ import { vi } from 'vitest';

vi.mock('fs');

describe('checkEnvFile', () => {
describe('modifyEnvFile', () => {
beforeEach(() => {
vi.resetAllMocks();
});
Expand All @@ -31,7 +31,7 @@ describe('checkEnvFile', () => {

vi.spyOn(fs, 'appendFileSync');

checkEnvFile();
modifyEnvFile();

expect(fs.appendFileSync).toHaveBeenCalledWith(
'.env',
Expand All @@ -49,8 +49,52 @@ describe('checkEnvFile', () => {

vi.spyOn(fs, 'appendFileSync');

checkEnvFile();
modifyEnvFile();

expect(fs.appendFileSync).not.toHaveBeenCalled();
});
});

describe('checkEnvFile', () => {
beforeEach(() => {
vi.resetAllMocks();
});

it('should return true if .env file already exists', () => {
vi.spyOn(fs, 'existsSync').mockImplementation((file) => file === '.env');

const result = checkEnvFile();

expect(result).toBe(true);
});

it('should create .env if it does not exist but .env.example exists', () => {
vi.spyOn(fs, 'existsSync').mockImplementation(
(file) => file === '.env.example',
);

const writeFileSyncMock = vi
.spyOn(fs, 'writeFileSync')
.mockImplementation(() => {});

const result = checkEnvFile();

expect(writeFileSyncMock).toHaveBeenCalledWith('.env', '', 'utf8');
expect(result).toBe(true);
});

it('should return false and log an error if .env and .env.example do not exist', () => {
vi.spyOn(fs, 'existsSync').mockImplementation(() => false);
const consoleErrorMock = vi
.spyOn(console, 'error')
.mockImplementation(() => {});

const result = checkEnvFile();

expect(consoleErrorMock).toHaveBeenCalledWith(
'Setup requires .env.example to proceed.\n',
);
expect(result).toBe(false);
expect(fs.writeFileSync).not.toHaveBeenCalled();
});
});
14 changes: 13 additions & 1 deletion src/setup/checkEnvFile/checkEnvFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@ import fs from 'fs';

dotenv.config();

export function checkEnvFile(): void {
export function checkEnvFile(): boolean {
if (!fs.existsSync('.env')) {
if (fs.existsSync('.env.example')) {
fs.writeFileSync('.env', '', 'utf8');
} else {
console.error('Setup requires .env.example to proceed.\n');
return false;
}
}
return true;
}

export function modifyEnvFile(): void {
const env = dotenv.parse(fs.readFileSync('.env'));
const envSample = dotenv.parse(fs.readFileSync('.env.example'));
const misplaced = Object.keys(envSample).filter((key) => !(key in env));
Expand Down

0 comments on commit 735869e

Please sign in to comment.