Skip to content

Commit

Permalink
v1.156.0
Browse files Browse the repository at this point in the history
  • Loading branch information
varovaro committed Mar 4, 2024
2 parents e46fccf + 3770e30 commit ec6eca7
Show file tree
Hide file tree
Showing 119 changed files with 3,634 additions and 2,867 deletions.
23 changes: 15 additions & 8 deletions app/api/auth/routes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Joi from 'joi';
import cookieParser from 'cookie-parser';
import MongoStore from 'connect-mongo';
import passport from 'passport';
Expand Down Expand Up @@ -41,13 +40,21 @@ export default app => {
app.post(
'/api/login',

validation.validateRequest(
Joi.object({
username: Joi.string().required(),
password: Joi.string().required(),
token: Joi.string(),
}).required()
),
validation.validateRequest({
type: 'object',
properties: {
body: {
type: 'object',
properties: {
username: { type: 'string' },
password: { type: 'string' },
token: { type: 'string' },
},
required: ['username', 'password'],
},
},
required: ['body'],
}),

(req, res, next) => {
passport.authenticate('local', (err, user) => {
Expand Down
44 changes: 19 additions & 25 deletions app/api/auth/specs/__snapshots__/routes.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,29 @@

exports[`Auth Routes /login should have a validation schema 1`] = `
Object {
"children": Object {
"password": Object {
"flags": Object {
"presence": "required",
"properties": Object {
"body": Object {
"properties": Object {
"password": Object {
"type": "string",
},
"token": Object {
"type": "string",
},
"username": Object {
"type": "string",
},
},
"invalids": Array [
"",
"required": Array [
"username",
"password",
],
"type": "string",
"type": "object",
},
"token": Object {
"invalids": Array [
"",
],
"type": "string",
},
"username": Object {
"flags": Object {
"presence": "required",
},
"invalids": Array [
"",
],
"type": "string",
},
},
"flags": Object {
"presence": "required",
},
"required": Array [
"body",
],
"type": "object",
}
`;
40 changes: 30 additions & 10 deletions app/api/auth2fa/routes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import Joi from 'joi';
import { Application } from 'express';
import needsAuthorization from 'api/auth/authMiddleware';
import * as usersUtils from 'api/auth2fa/usersUtils';
import { validation } from 'api/utils';
import { ObjectIdAsString } from 'api/utils/ajvSchemas';

export default (app: Application) => {
app.post(
'/api/auth2fa-secret',
needsAuthorization(['admin', 'editor', 'collaborator']),
validation.validateRequest(Joi.object().keys({})),
validation.validateRequest({
type: 'object',
}),
async (req, res, next) => {
try {
const { otpauth, secret } = await usersUtils.setSecret(req.user);
Expand All @@ -22,7 +24,19 @@ export default (app: Application) => {
app.post(
'/api/auth2fa-enable',
needsAuthorization(['admin', 'editor', 'collaborator']),
validation.validateRequest(Joi.object().keys({ token: Joi.string().required() }).required()),
validation.validateRequest({
type: 'object',
properties: {
body: {
type: 'object',
properties: {
token: { type: 'string' },
},
required: ['token'],
},
},
required: ['body'],
}),
async (req, res, next) => {
try {
await usersUtils.enable2fa(req.user, req.body.token);
Expand All @@ -36,13 +50,19 @@ export default (app: Application) => {
app.post(
'/api/auth2fa-reset',
needsAuthorization(['admin']),
validation.validateRequest(
Joi.object()
.keys({
_id: Joi.string().length(24).alphanum().required(),
})
.required()
),
validation.validateRequest({
type: 'object',
properties: {
body: {
type: 'object',
properties: {
_id: ObjectIdAsString,
},
required: ['_id'],
},
},
required: ['body'],
}),
async (req, res, next) => {
try {
await usersUtils.reset2fa({ _id: req.body._id });
Expand Down
55 changes: 25 additions & 30 deletions app/api/auth2fa/specs/__snapshots__/routes.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

exports[`Auth2fa Routes auth2fa-enable should conform the route with auth, validation, respond correctly and call next on error 1`] = `
Object {
"children": Object {
"token": Object {
"flags": Object {
"presence": "required",
"properties": Object {
"body": Object {
"properties": Object {
"token": Object {
"type": "string",
},
},
"invalids": Array [
"",
"required": Array [
"token",
],
"type": "string",
"type": "object",
},
},
"flags": Object {
"presence": "required",
},
"required": Array [
"body",
],
"type": "object",
}
`;
Expand All @@ -28,29 +30,23 @@ Object {

exports[`Auth2fa Routes auth2fa-reset should conform the route with auth, validation, respond correctly and call next on error 1`] = `
Object {
"children": Object {
"_id": Object {
"flags": Object {
"presence": "required",
},
"invalids": Array [
"",
],
"rules": Array [
Object {
"arg": 24,
"name": "length",
},
Object {
"name": "alphanum",
"properties": Object {
"body": Object {
"properties": Object {
"_id": Object {
"pattern": "^[0-9a-fA-F]{24}$",
"type": "string",
},
},
"required": Array [
"_id",
],
"type": "string",
"type": "object",
},
},
"flags": Object {
"presence": "required",
},
"required": Array [
"body",
],
"type": "object",
}
`;
Expand All @@ -63,7 +59,6 @@ Object {

exports[`Auth2fa Routes auth2fa-secret should conform the route with auth, validation, respond correctly and call next on error 1`] = `
Object {
"children": Object {},
"type": "object",
}
`;
Expand Down
19 changes: 16 additions & 3 deletions app/api/common.v2/database/MongoDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SessionScopedCollection } from './SessionScopedCollection';
import { SyncedCollection } from './SyncedCollection';

export abstract class MongoDataSource<CollectionSchema extends Document = any> {
protected db: Db;
private db: Db;

protected abstract collectionName: string;

Expand All @@ -16,17 +16,30 @@ export abstract class MongoDataSource<CollectionSchema extends Document = any> {
this.transactionManager = transactionManager;
}

protected getCollection() {
protected getCollection(collectionName = this.collectionName) {
return new SyncedCollection<CollectionSchema>(
new SessionScopedCollection<CollectionSchema>(
this.db.collection(this.collectionName),
this.db.collection(collectionName),
this.transactionManager
),
this.transactionManager,
this.db
);
}

protected async collectionExists(): Promise<boolean> {
const collections = await this.db.listCollections({ name: this.collectionName }).toArray();
return collections.length > 0;
}

protected async dropCollection() {
await this.db.dropCollection(this.collectionName, { session: this.getSession() });
}

protected async createCollection() {
await this.db.createCollection(this.collectionName, { session: this.getSession() });
}

protected getSession() {
return this.transactionManager.getSession();
}
Expand Down
48 changes: 48 additions & 0 deletions app/api/common.v2/database/specs/MongoDataSource.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,25 @@ afterAll(async () => {
class DataSource extends MongoDataSource<{ data: string }> {
protected collectionName = 'collection';

setCollectionName(name: string) {
this.collectionName = name;
}

collection() {
return this.getCollection();
}

async exists() {
return this.collectionExists();
}

async drop() {
return this.dropCollection();
}

async create() {
return this.createCollection();
}
}

describe('session scoped collection', () => {
Expand Down Expand Up @@ -261,4 +277,36 @@ describe('session scoped collection', () => {
}
);
});

describe('collectionExists', () => {
it('should return true if the collection exists', async () => {
const dataSource = new DataSource(getConnection(), DefaultTransactionManager());
expect(await dataSource.exists()).toBe(true);
});

it('should return false if the collection does not exist', async () => {
const dataSource = new DataSource(getConnection(), DefaultTransactionManager());
dataSource.setCollectionName('some_other_collection');
expect(await dataSource.exists()).toBe(false);
});
});

describe('dropCollection', () => {
it('should remove the collection from the DB', async () => {
const dataSource = new DataSource(getConnection(), DefaultTransactionManager());
expect(await dataSource.exists()).toBe(true);
await dataSource.drop();
expect(await dataSource.exists()).toBe(false);
});
});

describe('createCollection', () => {
it('should create the collection in the DB', async () => {
const dataSource = new DataSource(getConnection(), DefaultTransactionManager());
dataSource.setCollectionName('some_other_collection');
expect(await dataSource.exists()).toBe(false);
await dataSource.create();
expect(await dataSource.exists()).toBe(true);
});
});
});
Loading

0 comments on commit ec6eca7

Please sign in to comment.