Skip to content

Commit

Permalink
Run specs with docker compose - add tests related to user email/IP
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvain-morin authored and sylvain-morin committed Oct 4, 2023
1 parent b77ced6 commit ec5ccbb
Show file tree
Hide file tree
Showing 18 changed files with 887 additions and 62 deletions.
3 changes: 1 addition & 2 deletions config.js.ci
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ module.exports = {
user: "postgres",
host: "127.0.0.1",
port: 5432,
geometry_field: "geom",
dbname: "inaturalist_test"
geometry_field: "geom"
},
websiteURL: "http://localhost:3000/",
staticImagePrefix: "http://localhost:3000/attachments/",
Expand Down
70 changes: 70 additions & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
version: "2"
services:

redis:
container_name: redis
image: redis:6.0.3
ports:
- 6379:6379
volumes:
- redis_data_test:/data

memcached:
container_name: memcached
image: memcached:1.6.6
ports:
- 11211:11211

pg:
container_name: pg
image: postgis/postgis:12-3.0
environment:
POSTGRES_USER: 'inaturalist'
POSTGRES_PASSWORD: 'inaturalist'
POSTGRES_DB: inaturalist_test
ports:
- 5432:5432
volumes:
- pg_data_test:/var/lib/postgresql/data
- ./schema/database.sql:/docker-entrypoint-initdb.d/database.sql

es:
container_name: es
image: docker.elastic.co/elasticsearch/elasticsearch:8.9.1
environment:
- discovery.type=single-node
- xpack.security.enabled=false
ports:
- 9200:9200
volumes:
- es_data_test:/usr/share/elasticsearch/data
command: >
/bin/sh -c "bin/elasticsearch-plugin list | grep -q analysis-kuromoji
|| bin/elasticsearch-plugin install analysis-kuromoji;
/usr/local/bin/docker-entrypoint.sh"
api-test:
container_name: api-test
build: .
environment:
NODE_ENV: test
INAT_DB_HOST: pg
INAT_DB_USER : 'inaturalist'
INAT_DB_PASS: 'inaturalist'
INAT_ES_HOST: es
INAT_REDIS_HOST: redis
INAT_WEB_HOST: host.docker.internal
INAT_DB_NAME: inaturalist_test
INAT_ES_INDEX_PREFIX: test
DB_ALREADY_INITIALIZED: true
ports:
- 4000:4000
command:
/bin/sh -c "npm install ; npm run coverage"
extra_hosts:
- "host.docker.internal:host-gateway"

volumes:
redis_data_test:
pg_data_test:
es_data_test:
9 changes: 9 additions & 0 deletions lib/map_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ const DEFAULT_POINTS_STYLE = `

const MapGenerator = { merc: null };

MapGenerator.buildDbName = ( ) => {
// Throw exception is NODE_ENV is not set
if ( _.isEmpty( process.env.NODE_ENV ) ) {
throw new Error( "env.NODE_ENV is not set" );
}
return process.env.NODE_ENV === "test" ? "inaturalist_test" : ( process.env.INAT_DB_NAME || `inaturalist_${process.env.NODE_ENV}` );
};

MapGenerator.blankImage = fs.readFileSync( path.join( __dirname, "assets/blank.png" ) );

MapGenerator.createMercator = ( ) => {
Expand Down Expand Up @@ -63,6 +71,7 @@ MapGenerator.postgisDatasource = req => {
const datasourceConfig = {
...config.database,
...( config.database.replica || { } ),
dbname: MapGenerator.buildDbName( ),
type: "postgis",
table: req.postgis.query,
simplify_geometries: true,
Expand Down
40 changes: 40 additions & 0 deletions lib/test_helper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require( "fs" );
const _ = require( "lodash" );
const timersPromises = require( "timers/promises" );
const Promise = require( "bluebird" );
const { expect } = require( "chai" ); // eslint-disable-line import/no-extraneous-dependencies
const sinon = require( "sinon" ); // eslint-disable-line import/no-extraneous-dependencies
Expand All @@ -8,6 +9,43 @@ const esClient = require( "./es_client" );

const testHelper = { };

testHelper.waitForPG = async numberOfAttempts => {
console.log( "Awaiting PG connection..." );
await timersPromises.setTimeout( 1000 );
try {
if ( await pgClient.connect( ) ) {
return true;
}
} catch ( e ) {
// Do nothing
}
numberOfAttempts -= 1;
if ( numberOfAttempts === 0 ) {
process.exit( );
}
return testHelper.waitForPG( numberOfAttempts );
};

testHelper.waitForES = async numberOfAttempts => {
console.log( "Awaiting ES connection..." );
await timersPromises.setTimeout( 1000 );
try {
await esClient.connect();
if ( esClient.connection ) {
if ( await esClient.connection.ping() ) {
return true;
}
}
} catch ( e ) {
// Do nothing
}
numberOfAttempts -= 1;
if ( numberOfAttempts === 0 ) {
process.exit( );
}
return testHelper.waitForES( numberOfAttempts );
};

testHelper.forEachIndex = async action => {
if ( process.env.NODE_ENV !== "test" ) { return; }
const settings = JSON.parse( fs.readFileSync( "schema/settings.js" ) );
Expand Down Expand Up @@ -108,6 +146,8 @@ testHelper.testInatJSNoPreload = async ( controller, endpoint, method, done ) =>
};

module.exports = {
waitForPG: testHelper.waitForPG,
waitForES: testHelper.waitForES,
createIndices: testHelper.createIndices,
deleteIndices: testHelper.deleteIndices,
loadElasticsearchFixtures: testHelper.loadElasticsearchFixtures,
Expand Down
Loading

0 comments on commit ec5ccbb

Please sign in to comment.