Skip to content

Commit 735869e

Browse files
authored
Fix: Missing & Incorrect API URLs in .env.example (PalisadoesFoundation#3512)
* .env.example updated * Script fixed * url correction * code coverage for url prompt
1 parent a61d983 commit 735869e

File tree

11 files changed

+127
-29
lines changed

11 files changed

+127
-29
lines changed

.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ PORT=4321
88

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

11-
REACT_APP_TALAWA_URL=
11+
REACT_APP_TALAWA_URL=http://localhost:4000/graphql
1212

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

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

2929
# If you want to logs Compiletime and Runtime error , warning and info write YES or if u want to
3030
# keep the console clean leave it blank

docs/docs/auto-docs/setup/checkEnvFile/checkEnvFile/functions/checkEnvFile.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
# Function: checkEnvFile()
66

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

1111
## Returns
1212

13-
`void`
13+
`boolean`
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[Admin Docs](/)
2+
3+
***
4+
5+
# Function: modifyEnvFile()
6+
7+
> **modifyEnvFile**(): `void`
8+
9+
Defined in: [src/setup/checkEnvFile/checkEnvFile.ts:18](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/setup/checkEnvFile/checkEnvFile.ts#L18)
10+
11+
## Returns
12+
13+
`void`

docs/docs/docs/getting-started/installation.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,21 +202,21 @@ Add a custom port number for Talawa-Admin development purposes to the variable n
202202
Add the endpoint for accessing talawa-api graphql service to the variable named `REACT_APP_TALAWA_URL` in the `.env` file.
203203

204204
```
205-
REACT_APP_TALAWA_URL="http://API-IP-ADRESS:4000/graphql/"
205+
REACT_APP_TALAWA_URL="http://API-IP-ADRESS:4000/graphql"
206206
207207
```
208208

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

211211
```
212-
REACT_APP_TALAWA_URL="http://localhost:4000/graphql/"
212+
REACT_APP_TALAWA_URL="http://localhost:4000/graphql"
213213
214214
```
215215

216216
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
217217

218218
```
219-
REACT_APP_TALAWA_URL="http://YOUR-REMOTE-ADDRESS:4000/graphql/"
219+
REACT_APP_TALAWA_URL="http://YOUR-REMOTE-ADDRESS:4000/graphql"
220220
221221
```
222222

@@ -225,21 +225,21 @@ REACT_APP_TALAWA_URL="http://YOUR-REMOTE-ADDRESS:4000/graphql/"
225225
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.
226226

227227
```
228-
REACT_APP_BACKEND_WEBSOCKET_URL="ws://API-IP-ADRESS:4000/graphql/"
228+
REACT_APP_BACKEND_WEBSOCKET_URL="ws://API-IP-ADRESS:4000/graphql"
229229
230230
```
231231

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

234234
```
235-
REACT_APP_BACKEND_WEBSOCKET_URL="ws://localhost:4000/graphql/"
235+
REACT_APP_BACKEND_WEBSOCKET_URL="ws://localhost:4000/graphql"
236236
237237
```
238238

239239
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
240240

241241
```
242-
REACT_APP_BACKEND_WEBSOCKET_URL="ws://YOUR-REMOTE-ADDRESS:4000/graphql/"
242+
REACT_APP_BACKEND_WEBSOCKET_URL="ws://YOUR-REMOTE-ADDRESS:4000/graphql"
243243
244244
```
245245

setup.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import dotenv from 'dotenv';
22
import fs from 'fs';
33
import inquirer from 'inquirer';
4-
import { checkEnvFile } from './src/setup/checkEnvFile/checkEnvFile';
4+
import {
5+
checkEnvFile,
6+
modifyEnvFile,
7+
} from './src/setup/checkEnvFile/checkEnvFile';
58
import { validateRecaptcha } from './src/setup/validateRecaptcha/validateRecaptcha';
69
import askAndSetDockerOption from './src/setup/askAndSetDockerOption/askAndSetDockerOption';
710
import updateEnvFile from './src/setup/updateEnvFile/updateEnvFile';
@@ -59,9 +62,13 @@ const askAndSetLogErrors = async (): Promise<void> => {
5962
// Main function to run the setup process
6063
export async function main(): Promise<void> {
6164
try {
65+
if (!checkEnvFile()) {
66+
return;
67+
}
68+
6269
console.log('Welcome to the Talawa Admin setup! 🚀');
6370

64-
checkEnvFile();
71+
modifyEnvFile();
6572
await askAndSetDockerOption();
6673
const envConfig = dotenv.parse(fs.readFileSync('.env', 'utf8'));
6774
const useDocker = envConfig.USE_DOCKER === 'YES';

src/setup/askForTalawaApiUrl/askForTalawaApiUrl.spec.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@ describe('askForTalawaApiUrl', () => {
1616
});
1717

1818
it('should return the provided endpoint when user enters it', async () => {
19+
const mockPrompt = vi.spyOn(inquirer, 'prompt').mockResolvedValueOnce({
20+
endpoint: 'http://example.com/graphql',
21+
});
22+
23+
const result = await askForTalawaApiUrl();
24+
25+
expect(mockPrompt).toHaveBeenCalledWith([
26+
{
27+
type: 'input',
28+
name: 'endpoint',
29+
message: 'Enter your talawa-api endpoint:',
30+
default: 'http://localhost:4000/graphql',
31+
},
32+
]);
33+
34+
expect(result).toBe('http://example.com/graphql');
35+
});
36+
37+
it('should return the corrected endpoint when user enters with trailing slash', async () => {
1938
const mockPrompt = vi.spyOn(inquirer, 'prompt').mockResolvedValueOnce({
2039
endpoint: 'http://example.com/graphql/',
2140
});
@@ -27,16 +46,16 @@ describe('askForTalawaApiUrl', () => {
2746
type: 'input',
2847
name: 'endpoint',
2948
message: 'Enter your talawa-api endpoint:',
30-
default: 'http://localhost:4000/graphql/',
49+
default: 'http://localhost:4000/graphql',
3150
},
3251
]);
3352

34-
expect(result).toBe('http://example.com/graphql/');
53+
expect(result).toBe('http://example.com/graphql');
3554
});
3655

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

4261
const result = await askForTalawaApiUrl();
@@ -46,10 +65,10 @@ describe('askForTalawaApiUrl', () => {
4665
type: 'input',
4766
name: 'endpoint',
4867
message: 'Enter your talawa-api endpoint:',
49-
default: 'http://localhost:4000/graphql/',
68+
default: 'http://localhost:4000/graphql',
5069
},
5170
]);
5271

53-
expect(result).toBe('http://localhost:4000/graphql/');
72+
expect(result).toBe('http://localhost:4000/graphql');
5473
});
5574
});

src/setup/askForTalawaApiUrl/askForTalawaApiUrl.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ export async function askForTalawaApiUrl(): Promise<string> {
66
type: 'input',
77
name: 'endpoint',
88
message: 'Enter your talawa-api endpoint:',
9-
default: 'http://localhost:4000/graphql/',
9+
default: 'http://localhost:4000/graphql',
1010
},
1111
]);
12-
return endpoint;
12+
13+
const correctEndpoint = endpoint.replace(/\/graphql\/$/, '/graphql');
14+
15+
return correctEndpoint;
1316
}

src/setup/askForTalawaApiUrl/setupTalawaWebSocketUrl.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('WebSocket URL Configuration', () => {
3333

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

src/setup/checkConnection/checkConnection.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { vi, describe, beforeEach, it, expect } from 'vitest';
33
vi.mock('node-fetch');
44

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

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

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

4747
expect(result).toBe(false);

src/setup/checkEnvFile/checkEnvFile.spec.ts

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import fs from 'fs';
2-
import { checkEnvFile } from './checkEnvFile';
2+
import { checkEnvFile, modifyEnvFile } from './checkEnvFile';
33
import { vi } from 'vitest';
44

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

1515
vi.mock('fs');
1616

17-
describe('checkEnvFile', () => {
17+
describe('modifyEnvFile', () => {
1818
beforeEach(() => {
1919
vi.resetAllMocks();
2020
});
@@ -31,7 +31,7 @@ describe('checkEnvFile', () => {
3131

3232
vi.spyOn(fs, 'appendFileSync');
3333

34-
checkEnvFile();
34+
modifyEnvFile();
3535

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

5050
vi.spyOn(fs, 'appendFileSync');
5151

52-
checkEnvFile();
52+
modifyEnvFile();
5353

5454
expect(fs.appendFileSync).not.toHaveBeenCalled();
5555
});
5656
});
57+
58+
describe('checkEnvFile', () => {
59+
beforeEach(() => {
60+
vi.resetAllMocks();
61+
});
62+
63+
it('should return true if .env file already exists', () => {
64+
vi.spyOn(fs, 'existsSync').mockImplementation((file) => file === '.env');
65+
66+
const result = checkEnvFile();
67+
68+
expect(result).toBe(true);
69+
});
70+
71+
it('should create .env if it does not exist but .env.example exists', () => {
72+
vi.spyOn(fs, 'existsSync').mockImplementation(
73+
(file) => file === '.env.example',
74+
);
75+
76+
const writeFileSyncMock = vi
77+
.spyOn(fs, 'writeFileSync')
78+
.mockImplementation(() => {});
79+
80+
const result = checkEnvFile();
81+
82+
expect(writeFileSyncMock).toHaveBeenCalledWith('.env', '', 'utf8');
83+
expect(result).toBe(true);
84+
});
85+
86+
it('should return false and log an error if .env and .env.example do not exist', () => {
87+
vi.spyOn(fs, 'existsSync').mockImplementation(() => false);
88+
const consoleErrorMock = vi
89+
.spyOn(console, 'error')
90+
.mockImplementation(() => {});
91+
92+
const result = checkEnvFile();
93+
94+
expect(consoleErrorMock).toHaveBeenCalledWith(
95+
'Setup requires .env.example to proceed.\n',
96+
);
97+
expect(result).toBe(false);
98+
expect(fs.writeFileSync).not.toHaveBeenCalled();
99+
});
100+
});

0 commit comments

Comments
 (0)