Skip to content

Commit

Permalink
[CONFIG] Use v2 of config system
Browse files Browse the repository at this point in the history
  • Loading branch information
LoicPoullain committed Sep 3, 2020
1 parent b4ce78d commit 580c119
Show file tree
Hide file tree
Showing 47 changed files with 498 additions and 427 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
redis:
image: redis:4.0.14
ports:
- 6379:6379
- 6380:6379

steps:
- uses: actions/checkout@v1
Expand Down
7 changes: 1 addition & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,4 @@ services:
redis:
image: "redis:4.0.14"
ports:
- "6379:6379"

adminer:
image: adminer
ports:
- 8080:8080
- "6380:6379"
42 changes: 16 additions & 26 deletions docs/authentication-and-access-control/session-tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,40 +478,30 @@ This store uses the default TypeORM connection which is usually specified in `or
npm install @foal/redis
```
In order to use this store, you must provide the redis URI in either:
- a configuration file
*Example with config/default.yml*
```yaml
redis:
uri: 'redis://localhost:6379'
```
- or in a `.env` file or in an environment variable:
```
REDIS_URI=redis://localhost:6379
```
In order to use this store, you must provide the redis URI in either a configuration file
*Example with config/default.yml*
```yaml
settings:
redis:
uri: 'redis://localhost:6379'
```


### MongoDBStore

```
npm install @foal/mongodb
```

This store saves your session states in a MongoDB database (using the collection `sessions`). In order to use it, you must provide the MongoDB URI in either:
- a configuration file
*Example with config/default.yml*
```yaml
mongodb:
uri: 'mongodb://localhost:27017'
```
- or in a `.env` file or in an environment variable:
```
MONGODB_URI=mongodb://localhost:27017
```
This store saves your session states in a MongoDB database (using the collection `sessions`). In order to use it, you must provide the MongoDB URI a configuration file:

*Example with config/default.yml*
```yaml
settings:
mongodb:
uri: 'mongodb://localhost:27017'
```
### Custom Store
Expand Down
10 changes: 6 additions & 4 deletions packages/acceptance-tests/src/authentication/jwt.cookie.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('[Authentication|JWT|cookie|no redirection] Users', () => {
async logout() {
return new HttpResponseNoContent()
.setCookie(
Config.get('settings.jwt.cookieName', 'string', 'auth'),
Config.get('settings.jwt.cookie.name', 'string', 'auth'),
'',
{ ...cookieOptions, maxAge: 0 }
);
Expand All @@ -123,7 +123,7 @@ describe('[Authentication|JWT|cookie|no redirection] Users', () => {
});
return new HttpResponseNoContent()
.setCookie(
Config.get('settings.jwt.cookieName', 'string', 'auth'),
Config.get('settings.jwt.cookie.name', 'string', 'auth'),
token,
{ ...cookieOptions, maxAge: 3600 }
);
Expand All @@ -138,7 +138,8 @@ describe('[Authentication|JWT|cookie|no redirection] Users', () => {
}

before(async () => {
process.env.SETTINGS_JWT_SECRET = 'session-secret';
Config.set('settings.jwt.secret', 'session-secret');

await createConnection({
database: 'e2e_db.sqlite',
dropSchema: true,
Expand All @@ -154,7 +155,8 @@ describe('[Authentication|JWT|cookie|no redirection] Users', () => {

after(async () => {
await getConnection().close();
delete process.env.SETTINGS_JWT_SECRET;

Config.remove('settings.jwt.secret');
});

it('cannot access protected routes if they are not logged in.', () => {
Expand Down
12 changes: 7 additions & 5 deletions packages/acceptance-tests/src/authentication/jwt.token.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import * as request from 'supertest';

// FoalTS
import {
Context, controller, createApp,
Get, hashPassword, HttpResponseOK,
HttpResponseUnauthorized, Post, ValidateBody, verifyPassword
Config, Context, controller,
createApp, Get, hashPassword,
HttpResponseOK, HttpResponseUnauthorized, Post, ValidateBody, verifyPassword
} from '@foal/core';
import { getSecretOrPrivateKey, JWTRequired } from '@foal/jwt';
import { fetchUser } from '@foal/typeorm';
Expand Down Expand Up @@ -120,7 +120,8 @@ describe('[Authentication|JWT|no cookie|no redirection] Users', () => {
}

before(async () => {
process.env.SETTINGS_JWT_SECRET = 'session-secret';
Config.set('settings.jwt.secret', 'session-secret');

await createConnection({
database: 'e2e_db.sqlite',
dropSchema: true,
Expand All @@ -136,7 +137,8 @@ describe('[Authentication|JWT|no cookie|no redirection] Users', () => {

after(async () => {
await getConnection().close();
delete process.env.SETTINGS_JWT_SECRET;

Config.remove('settings.jwt.secret');
});

it('cannot access protected routes if they are not logged in.', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as request from 'supertest';

// FoalTS
import {
Config,
Context,
controller,
createApp,
Expand Down Expand Up @@ -123,8 +124,8 @@ describe('Feature: Stateful CSRF protection in a Regular Web App', () => {
let csrfToken: string;

before(async () => {
process.env.SETTINGS_SESSION_CSRF_ENABLED = 'true';
process.env.SETTINGS_SESSION_CSRF_COOKIE_NAME = csrfCookieName;
Config.set('settings.session.csrf.enabled', true);
Config.set('settings.session.csrf.cookie.name', csrfCookieName);

connection = await createTestConnection([ User, DatabaseSession ]);

Expand All @@ -135,8 +136,8 @@ describe('Feature: Stateful CSRF protection in a Regular Web App', () => {
});

after(async () => {
delete process.env.SETTINGS_SESSION_CSRF_ENABLED;
delete process.env.SETTINGS_SESSION_CSRF_COOKIE_NAME;
Config.remove('settings.session.csrf.enabled');
Config.remove('settings.session.csrf.cookie.name');

await connection.close();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as request from 'supertest';

// FoalTS
import {
Config,
Context,
controller,
createApp,
Expand Down Expand Up @@ -91,8 +92,8 @@ describe('Feature: Stateful CSRF protection in a Single-Page Application', () =>
let csrfToken: string;

before(async () => {
process.env.SETTINGS_SESSION_CSRF_ENABLED = 'true';
process.env.SETTINGS_SESSION_CSRF_COOKIE_NAME = csrfCookieName;
Config.set('settings.session.csrf.enabled', true);
Config.set('settings.session.csrf.cookie.name', csrfCookieName);

connection = await createTestConnection([ User, DatabaseSession ]);

Expand All @@ -103,8 +104,8 @@ describe('Feature: Stateful CSRF protection in a Single-Page Application', () =>
});

after(async () => {
delete process.env.SETTINGS_SESSION_CSRF_ENABLED;
delete process.env.SETTINGS_SESSION_CSRF_COOKIE_NAME;
Config.remove('settings.session.csrf.enabled');
Config.remove('settings.session.csrf.cookie.name');

await connection.close();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as request from 'supertest';

// FoalTS
import {
Config,
Context,
controller,
createApp,
Expand Down Expand Up @@ -91,9 +92,9 @@ describe('Feature: Stateless CSRF protection in a Single-Page Application', () =
let csrfToken: string;

before(async () => {
process.env.SETTINGS_JWT_SECRET = 'jwtSecret';
process.env.SETTINGS_JWT_CSRF_ENABLED = 'true';
process.env.SETTINGS_JWT_CSRF_COOKIE_NAME = csrfCookieName;
Config.set('settings.jwt.secret', 'jwtSecret');
Config.set('settings.jwt.csrf.enabled', true);
Config.set('settings.jwt.csrf.cookie.name', csrfCookieName);

connection = await createTestConnection([ User ]);

Expand All @@ -104,9 +105,9 @@ describe('Feature: Stateless CSRF protection in a Single-Page Application', () =
});

after(async () => {
delete process.env.SETTINGS_JWT_SECRET;
delete process.env.SETTINGS_JWT_CSRF_ENABLED;
delete process.env.SETTINGS_JWT_CSRF_COOKIE_NAME;
Config.remove('settings.jwt.secret');
Config.remove('settings.jwt.csrf.enabled');
Config.remove('settings.jwt.csrf.cookie.name');

await connection.close();
});
Expand Down
9 changes: 5 additions & 4 deletions packages/acceptance-tests/src/session.config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Config,
Context,
createApp,
createSession,
Expand All @@ -21,14 +22,14 @@ describe('The session store', () => {

beforeEach(() => {
// Use ".." to remove the "build/" directory.
process.env.SETTINGS_SESSION_STORE = './../node_modules/@foal/redis';
process.env.SETTINGS_SESSION_SECRET = 'secret';
Config.set('settings.session.store', './../node_modules/@foal/redis');
Config.set('settings.session.secret', 'secret');
serviceManager = new ServiceManager();
});

afterEach(() => {
delete process.env.SETTINGS_SESSION_STORE;
delete process.env.SETTINGS_SESSION_SECRET;
Config.remove('settings.session.store');
Config.remove('settings.session.secret');
// Hack to close the redis connection in this test.
(serviceManager as any).map.forEach((value: any) => {
if (value.service.close) {
Expand Down
7 changes: 5 additions & 2 deletions packages/acceptance-tests/src/typeorm.mongodb-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { strictEqual } from 'assert';

// 3p
import {
Config,
Context,
createAndInitApp,
createSession,
Expand Down Expand Up @@ -136,7 +137,8 @@ describe('[Sample] TypeORM & MongoDB Store', async () => {
let connection: Connection;

before(async () => {
process.env.MONGODB_URI = 'mongodb://localhost:27017/e2e_db';
Config.set('settings.mongodb.uri', 'mongodb://localhost:27017/e2e_db');

connection = await createConnection({
database: 'e2e_db',
dropSchema: true,
Expand All @@ -160,7 +162,8 @@ describe('[Sample] TypeORM & MongoDB Store', async () => {
});

after(async () => {
delete process.env.MONGODB_URI;
Config.remove('settings.mongodb.uri');

return Promise.all([
connection.close(),
app.foal.services.get(MongoDBStore).close(),
Expand Down
7 changes: 5 additions & 2 deletions packages/acceptance-tests/src/typeorm.redis-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { strictEqual } from 'assert';

// 3p
import {
Config,
Context,
createAndInitApp,
createSession,
Expand Down Expand Up @@ -134,7 +135,8 @@ describe('[Sample] MongoDB & Redis Store', async () => {
let connection: Connection;

before(async () => {
process.env.MONGODB_URI = 'mongodb://localhost:27017/e2e_db';
Config.set('settings.mongodb.uri', 'mongodb://localhost:27017/e2e_db');

connection = await createConnection({
database: 'e2e_db',
dropSchema: true,
Expand Down Expand Up @@ -165,7 +167,8 @@ describe('[Sample] MongoDB & Redis Store', async () => {
});

after(() => {
delete process.env.MONGODB_URI;
Config.remove('settings.mongodb.uri');

return Promise.all([
connection.close(),
app.foal.services.get(RedisStore).close(),
Expand Down
21 changes: 13 additions & 8 deletions packages/aws-s3/src/s3-disk.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,21 @@ describe('S3Disk', () => {
}));

beforeEach(() => {
process.env.SETTINGS_DISK_S3_BUCKET = bucketName;
Config.set('settings.disk.s3.bucket', bucketName);

disk = createService(S3Disk);
});

afterEach(async () => {
delete process.env.SETTINGS_DISK_S3_BUCKET;
delete process.env.SETTINGS_AWS_ENDPOINT;
Config.remove('settings.disk.s3.bucket');
Config.remove('settings.aws.endpoint');
await rmObjectsIfExist(s3);
});

it('should accept a S3 custom endpoint in the config.', async () => {
// This test assumes that the "delete" method tries at least to connect to AWS.
process.env.SETTINGS_AWS_ENDPOINT = 'foobar';
Config.set('settings.aws.endpoint', 'foobar');

try {
await disk.delete('foo/test.txt');
throw new Error('An error should have been thrown.');
Expand All @@ -95,7 +96,8 @@ describe('S3Disk', () => {
describe('has a "write" method that', () => {

it('should throw an Error if no bucket is specified in the config.', async () => {
delete process.env.SETTINGS_DISK_S3_BUCKET;
Config.remove('settings.disk.s3.bucket');

try {
await disk.write('foo', Buffer.from('hello', 'utf8'));
throw new Error('An error should have been thrown.');
Expand Down Expand Up @@ -160,7 +162,8 @@ describe('S3Disk', () => {
describe('has a "read" method that', () => {

it('should throw an Error if no bucket is specified in the config.', async () => {
delete process.env.SETTINGS_DISK_S3_BUCKET;
Config.remove('settings.disk.s3.bucket');

try {
await disk.read('foo', 'buffer');
throw new Error('An error should have been thrown.');
Expand Down Expand Up @@ -247,7 +250,8 @@ describe('S3Disk', () => {
describe('has a "readSize" method that', () => {

it('should throw an Error if no bucket is specified in the config.', async () => {
delete process.env.SETTINGS_DISK_S3_BUCKET;
Config.remove('settings.disk.s3.bucket');

try {
await disk.readSize('foo');
throw new Error('An error should have been thrown.');
Expand Down Expand Up @@ -288,7 +292,8 @@ describe('S3Disk', () => {
describe('has a "delete" method that', () => {

it('should throw an Error if no bucket is specified in the config.', async () => {
delete process.env.SETTINGS_DISK_S3_BUCKET;
Config.remove('settings.disk.s3.bucket');

try {
await disk.delete('foo');
throw new Error('An error should have been thrown.');
Expand Down
Loading

0 comments on commit 580c119

Please sign in to comment.