Skip to content

Commit

Permalink
Merge pull request #123 from Arquisoft/quality
Browse files Browse the repository at this point in the history
Backend refactoring
  • Loading branch information
IyanRobles authored Apr 10, 2024
2 parents 378579b + ae18a1a commit 29738cc
Show file tree
Hide file tree
Showing 16 changed files with 30 additions and 32 deletions.
2 changes: 1 addition & 1 deletion gatewayservice/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ ENV WEBAPP_ENDPOINT=$WEBAPP_URI
RUN npm run tsc

# Define the command to run your app
CMD ["npm", "start"]
CMD ["npm", "start"]
8 changes: 4 additions & 4 deletions gatewayservice/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const AUTH_SERVICE_URL =
process.env.AUTH_SERVICE_URL || 'http://localhost:8002';
process.env.AUTH_SERVICE_URL ?? 'http://localhost:8002';
const USER_SERVICE_URL =
process.env.USER_SERVICE_URL || 'http://localhost:8001';
process.env.USER_SERVICE_URL ?? 'http://localhost:8001';
const HISTORY_SERVICE_URL =
process.env.HISTORY_SERVICE_URL || 'http://localhost:8001';
process.env.HISTORY_SERVICE_URL ?? 'http://localhost:8001';
const QUESTION_SERVICE_URL =
process.env.QUESTION_SERVICE_URL || 'http://localhost:8004';
process.env.QUESTION_SERVICE_URL ?? 'http://localhost:8004';

export {
AUTH_SERVICE_URL,
Expand Down
12 changes: 5 additions & 7 deletions gatewayservice/test/gateway-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ const getMocks = (url: string) => {
} else if (url.endsWith('/profile')) {
return Promise.resolve({data: { bio: 'Test' }});
}

return;
return Promise.resolve({});
};

const postMocks = (url: string) => {
Expand All @@ -33,8 +32,7 @@ const postMocks = (url: string) => {
} else if (url.endsWith('/profile')) {
return Promise.resolve({data: { bio: 'Test' }});
}

return;
return Promise.resolve({});
};

describe('Gateway Service', () => {
Expand Down Expand Up @@ -363,15 +361,15 @@ describe('Gateway Service', () => {

async function testWithoutServices(paramFunc : Function) {
// Clear the mocks
await (axios.get as jest.Mock).mockImplementation((url: string) => { if (url) return; })
(axios.get as jest.Mock).mockImplementation((url: string) => { if (url) return; })
await (axios.post as jest.Mock).mockImplementation((url: string) => { if (url) return; })

// Execute the function
const response: Response = await paramFunc();

// Restore original mocks
await (axios.get as jest.Mock).mockImplementation(getMocks);
await (axios.post as jest.Mock).mockImplementation(postMocks);
(axios.get as jest.Mock).mockImplementation(getMocks);
(axios.post as jest.Mock).mockImplementation(postMocks);

return response;
}
2 changes: 1 addition & 1 deletion questionservice/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ EXPOSE 8004
RUN npm run tsc

# Define the command to run your app
CMD ["npm", "start"]
CMD ["npm", "start"]
3 changes: 2 additions & 1 deletion questionservice/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import app from './app';
const port = 8004;

// mongodb://127.0.0.1:27018/questiondb
const mongoUri = process.env.MONGODB_URI??'mongodb://127.0.0.1:27017';
const mongoUri = process.env.MONGODB_URI ?? 'mongodb://127.0.0.1:27017';

mongoose.connect(mongoUri);

const server = app.listen(port, () => {
Expand Down
2 changes: 0 additions & 2 deletions questionservice/src/services/question-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ async function generateQuestions(
size: number,
lang: any
): Promise<object[] | void> {

let numberQuestionsDB = Math.min(Math.floor(size / 2), await QuestionModel.countDocuments());
let questionsToGenerate = size - numberQuestionsDB;
console.log('------------------');
Expand Down Expand Up @@ -61,7 +60,6 @@ async function generateQuestions(
if (lang && lang.toLowerCase() !== 'en') {
return await translateQuestionsArray(questionsArray, lang);
}

return questionsArray;
}

Expand Down
2 changes: 1 addition & 1 deletion questionservice/test/question-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe("Question Service - Question Generator", () => {
const aggregateMock = await mocktemplateModelAggregate(defaultNumberQuestions);
await mockWikidataSparql(defaultNumberQuestions)
await mockQuestionCount();
await mockResponseTranslationRequest()
mockResponseTranslationRequest()

const response = await generateQuestions(2, "es") as any;
console.log(response)
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ sonar.projectVersion=1.0
sonar.host.url=https://sonarcloud.io
sonar.language=js

sonar.coverage.exclusions=**/*.test.ts,**/*.test.js,**/jest.config.js
sonar.coverage.exclusions=**/*.test.ts,**/*.test.js,**/jest.config.js,**/index.ts
sonar.sources=webapp/src/components,users/authservice,users/userservice,gatewayservice,users/utils,questionservice
sonar.sourceEncoding=UTF-8
sonar.exclusions=node_modules/**
Expand Down
2 changes: 1 addition & 1 deletion users/authservice/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ EXPOSE 8002
RUN npm run tsc

# Define the command to run your app
CMD ["npm", "start"]
CMD ["npm", "start"]
2 changes: 1 addition & 1 deletion users/authservice/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import app from './app';

const port = 8002;

const mongoUri = process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/userdb';
const mongoUri = process.env.MONGODB_URI ?? 'mongodb://127.0.0.1:27017/userdb';
mongoose.connect(mongoUri);

const server = app.listen(port, () => {
Expand Down
2 changes: 1 addition & 1 deletion users/userservice/src/controllers/history-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const getHistory = async (req: Request, res: Response) => {

if (!user) {
throw new Error(
`The provided user \'${username}\' is not registered in the application`
`The provided user '${username}' is not registered in the application`
);
}

Expand Down
2 changes: 1 addition & 1 deletion users/userservice/src/middlewares/protect-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const protect = async (req: Request, res: Response, next: NextFunction) => {
try {
const { authorization } = req.headers;

if (!authorization || !authorization.startsWith('Bearer')) {
if (!authorization?.startsWith('Bearer')) {
throw new Error('You must be logged in to update your data');
}

Expand Down
10 changes: 5 additions & 5 deletions users/userservice/test/user-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ describe('User Service', () => {
} as Request;
const user = await User.find({ username:'testuser' });

expect(() => validateHistoryBody(mockRequest, user[0])).toThrowError();
expect(() => validateHistoryBody(mockRequest, user[0])).toThrow();
});

// Body validation util, non-numeric
Expand All @@ -294,7 +294,7 @@ describe('User Service', () => {
} as Request;
const user = await User.find({ username:'testuser' });

expect(() => validateHistoryBody(mockRequest, user[0])).toThrowError();
expect(() => validateHistoryBody(mockRequest, user[0])).toThrow();
});

// Body validation util, negative
Expand All @@ -308,7 +308,7 @@ describe('User Service', () => {
} as Request;
const user = await User.find({ username:'testuser' });

expect(() => validateHistoryBody(mockRequest, user[0])).toThrowError();
expect(() => validateHistoryBody(mockRequest, user[0])).toThrow();
});

// Token validator
Expand Down Expand Up @@ -506,7 +506,7 @@ describe('User Service', () => {
} as Request;
const user = await User.find({ username:'testuser' });

expect(() => validateProfileBody(mockRequest, user[0])).toThrowError();
expect(() => validateProfileBody(mockRequest, user[0])).toThrow();
});

// Body validation util, not a string
Expand All @@ -520,7 +520,7 @@ describe('User Service', () => {
} as Request;
const user = await User.find({ username:'testuser' });

expect(() => validateProfileBody(mockRequest, user[0])).toThrowError();
expect(() => validateProfileBody(mockRequest, user[0])).toThrow();
});
});

Expand Down
6 changes: 3 additions & 3 deletions users/utils/test/users-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('Users Utils', () => {
} as Request;
mockRequest.body['history'] = '';

expect(() => validateNotEmpty(mockRequest, ['history'])).toThrowError();
expect(() => validateNotEmpty(mockRequest, ['history'])).toThrow();

// Should ignore field if does not exist
try {
Expand All @@ -26,7 +26,7 @@ describe('Users Utils', () => {
} as Request;
mockRequest.body['test'] = '123456789';

expect(() => validateRequiredLength(mockRequest, ['test'], 10)).toThrowError();
expect(() => validateRequiredLength(mockRequest, ['test'], 10)).toThrow();

// Should ignore field if does not exist
try {
Expand Down Expand Up @@ -58,6 +58,6 @@ describe('Users Utils', () => {
mockRequest.body['test'] = 'test';

// Should get an error when the field does not exist
expect(() => validateRequiredFields(mockRequest, ['nonexistent'])).toThrowError();
expect(() => validateRequiredFields(mockRequest, ['nonexistent'])).toThrow();
});
});
4 changes: 3 additions & 1 deletion webapp/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ RUN npm install serve

#Execute npm run prod to run the server
CMD [ "npm", "run", "prod" ]
#CMD ["npm", "start"]
#CMD ["npm", "start"]

USER node
1 change: 0 additions & 1 deletion webapp/src/components/Base.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
padding: 15px;
margin-bottom: 20px;
border: 1px solid transparent;
border-radius: 4px;
background-color: #f2dede;
border-color: #ebccd1;
color: #a94442;
Expand Down

0 comments on commit 29738cc

Please sign in to comment.