Skip to content

Commit

Permalink
fix(env): fix get uri from environment
Browse files Browse the repository at this point in the history
  • Loading branch information
lgaticaq committed May 1, 2018
1 parent 8069247 commit 7516a50
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 60 deletions.
25 changes: 4 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,19 @@

const getMongodbUri = dbname => {
dbname = dbname || 'test';
let uri = `mongodb://localhost/${dbname}`;
if (process.env.MONGO_PORT_27017_TCP_ADDR && process.env.MONGO_PORT_27017_TCP_PORT) {
uri = `mongodb://${process.env.MONGO_PORT_27017_TCP_ADDR}:${process.env.MONGO_PORT_27017_TCP_PORT}/${dbname}`;
} else if (process.env.MONGODB_URI) {
uri = process.env.MONGODB_URI;
}
return uri;
return process.env.MONGODB_URI || `mongodb://localhost/${dbname}`;
};

const getRedisUri = () => {
let uri = 'redis://localhost';
if (process.env.REDIS_PORT_6379_TCP_ADDR && process.env.REDIS_PORT_6379_TCP_PORT) {
uri = `redis://${process.env.REDIS_PORT_6379_TCP_ADDR}:${process.env.REDIS_PORT_6379_TCP_PORT}`;
} else if (process.env.REDIS_URI) {
uri = process.env.REDIS_URI;
}
return uri;
return process.env.REDIS_URI || 'redis://localhost';
};

const getPostgresqlUri = password => {
const username = process.env.POSTGRES_USER || 'postgres';
const dbname = process.env.POSTGRES_DB || process.env.POSTGRES_USER || 'postgres';
password = password || process.env.POSTGRES_PASSWORD || 'mysecretpassword';
let uri = `postgres://${username}:${password}@localhost/${dbname}`;
if (process.env.POSTGRES_PORT_5432_TCP_ADDR && process.env.POSTGRES_PORT_5432_TCP_PORT && process.env.POSTGRES_PASSWORD) {
uri = `postgres://${username}:${password}@${process.env.POSTGRES_PORT_5432_TCP_ADDR}:${process.env.POSTGRES_PORT_5432_TCP_PORT}/${dbname}`;
} else if (process.env.POSTGRESQL_URI) {
uri = process.env.POSTGRESQL_URI;
}
return uri;
const uri = `postgres://${username}:${password}@localhost/${dbname}`;
return process.env.POSTGRESQL_URI || uri;
};

module.exports = {
Expand Down
39 changes: 0 additions & 39 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,77 +6,38 @@ const expect = require('chai').expect;
describe('config-dburi', () => {
describe('mongo', () => {
it('should return mongo uri default', () => {
delete process.env.MONGO_PORT_27017_TCP_ADDR;
delete process.env.MONGO_PORT_27017_TCP_PORT;
delete process.env.MONGODB_URI;
expect(config.mongo()).to.eql('mongodb://localhost/test');
});
it('should return mongo uri docker default', () => {
process.env.MONGO_PORT_27017_TCP_ADDR = '172.168.1.2';
process.env.MONGO_PORT_27017_TCP_PORT = 27017;
delete process.env.MONGODB_URI;
expect(config.mongo()).to.eql('mongodb://172.168.1.2:27017/test');
});
it('should return mongo uri docker with dbname', () => {
process.env.MONGO_PORT_27017_TCP_ADDR = '172.168.1.2';
process.env.MONGO_PORT_27017_TCP_PORT = 27017;
delete process.env.MONGODB_URI;
expect(config.mongo('example')).to.eql('mongodb://172.168.1.2:27017/example');
});
it('should return mongo uri from env default', () => {
delete process.env.MONGO_PORT_27017_TCP_ADDR;
delete process.env.MONGO_PORT_27017_TCP_PORT;
process.env.MONGODB_URI = 'mongodb://user:[email protected]:1234';
expect(config.mongo()).to.eql('mongodb://user:[email protected]:1234');
});
});

describe('redis', () => {
it('should return redis uri default', () => {
delete process.env.REDIS_PORT_6379_TCP_ADDR;
delete process.env.REDIS_PORT_6379_TCP_PORT;
delete process.env.REDIS_URI;
expect(config.redis()).to.eql('redis://localhost');
});
it('should return redis uri docker default', () => {
process.env.REDIS_PORT_6379_TCP_ADDR = '172.168.1.2';
process.env.REDIS_PORT_6379_TCP_PORT = 6379;
delete process.env.REDIS_URI;
expect(config.redis()).to.eql('redis://172.168.1.2:6379');
});
it('should return redis uri from env default', () => {
delete process.env.REDIS_PORT_6379_TCP_ADDR;
delete process.env.REDIS_PORT_6379_TCP_PORT;
process.env.REDIS_URI = 'redis://redis:[email protected]:1234';
expect(config.redis()).to.eql('redis://redis:[email protected]:1234');
});
});

describe('postgres', () => {
it('should return postgres uri default', () => {
delete process.env.POSTGRES_PORT_5432_TCP_ADDR;
delete process.env.POSTGRES_PORT_5432_TCP_PORT;
delete process.env.POSTGRES_PASSWORD;
delete process.env.POSTGRESQL_URI;
expect(config.postgres()).to.eql('postgres://postgres:mysecretpassword@localhost/postgres');
});
it('should return postgres uri default with password', () => {
delete process.env.POSTGRES_PORT_5432_TCP_ADDR;
delete process.env.POSTGRES_PORT_5432_TCP_PORT;
delete process.env.POSTGRES_PASSWORD;
delete process.env.POSTGRESQL_URI;
expect(config.postgres('mypass')).to.eql('postgres://postgres:mypass@localhost/postgres');
});
it('should return postgres uri docker default', () => {
process.env.POSTGRES_PORT_5432_TCP_ADDR = '172.168.1.2';
process.env.POSTGRES_PORT_5432_TCP_PORT = 5432;
process.env.POSTGRES_PASSWORD = 'mypass';
delete process.env.POSTGRESQL_URI;
expect(config.postgres()).to.eql('postgres://postgres:[email protected]:5432/postgres');
});
it('should return postgres uri from env default', () => {
delete process.env.POSTGRES_PORT_5432_TCP_ADDR;
delete process.env.POSTGRES_PORT_5432_TCP_PORT;
delete process.env.POSTGRES_PASSWORD;
process.env.POSTGRESQL_URI = 'postgres://user:[email protected]:1234';
expect(config.postgres()).to.eql('postgres://user:[email protected]:1234');
Expand Down

0 comments on commit 7516a50

Please sign in to comment.