Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix db setup in tests #458

Merged
merged 11 commits into from
Dec 18, 2023
4 changes: 0 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Init MariaDB
run: |
mysql -u directus -ppassword --database=directus --protocol=tcp < config/init.sql

- uses: actions/setup-node@v3
with:
node-version: 18.x
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ services:
ports:
- "3306:3306"
volumes:
- ./config/init.sql:/docker-entrypoint-initdb.d/init.sql
- ./migrations/init.sql:/docker-entrypoint-initdb.d/init.sql
3 changes: 3 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export default _.merge({}, ...[ 'development', 'production', 'staging', 'test' ]
propagateCreateError: false,
},
acquireConnectionTimeout: 10000,
seeds: {
directory: `./seeds/${environment}`,
},
},
};
}));
13 changes: 13 additions & 0 deletions migrations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Bluebird from 'bluebird';
import fs from 'node:fs';
import path from 'path';
import { fileURLToPath } from 'url';

export const up = async (db) => {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const sql = fs.readFileSync(path.join(__dirname, 'init.sql'), 'utf8');
const queries = sql.split('\n\n');
await Bluebird.map(queries, query => db.schema.raw(query));
MartinKolarik marked this conversation as resolved.
Show resolved Hide resolved
};

export const down = () => {};
55 changes: 5 additions & 50 deletions config/init.sql → migrations/init.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
CREATE DATABASE IF NOT EXISTS directus;
USE directus;

CREATE TABLE IF NOT EXISTS adopted_probes (
id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_created CHAR(36),
Expand All @@ -11,16 +8,16 @@ CREATE TABLE IF NOT EXISTS adopted_probes (
ip VARCHAR(255) NOT NULL,
uuid VARCHAR(255),
lastSyncDate DATE NOT NULL,
isCustomCity TINYINT DEFAULT 0,
tags LONGTEXT,
isCustomCity TINYINT DEFAULT 0,
tags LONGTEXT,
status VARCHAR(255) NOT NULL,
version VARCHAR(255) NOT NULL,
country VARCHAR(255) NOT NULL,
city VARCHAR(255),
state VARCHAR(255),
latitude FLOAT(10, 5),
longitude FLOAT(10, 5),
asn INTEGER NOT NULL,
latitude FLOAT(10, 5),
longitude FLOAT(10, 5),
asn INTEGER NOT NULL,
network VARCHAR(255) NOT NULL,
countryOfCustomCity VARCHAR(255)
);
Expand All @@ -37,45 +34,3 @@ CREATE TABLE IF NOT EXISTS directus_notifications (
subject VARCHAR(255),
message TEXT
);

INSERT IGNORE INTO adopted_probes (
userId,
lastSyncDate,
ip,
uuid,
isCustomCity,
tags,
status,
version,
country,
city,
latitude,
longitude,
network,
asn,
countryOfCustomCity
) VALUES (
'89da69bd-a236-4ab7-9c5d-b5f52ce09959',
CURRENT_DATE,
'51.158.22.211',
'c77f021d-23ff-440a-aa96-35e82c73e731',
1,
'["mytag1"]',
'ready',
'0.26.0',
'FR',
'Marseille',
'43.29695',
'5.38107',
'SCALEWAY S.A.S.',
12876,
'FR'
);

INSERT IGNORE INTO directus_users (
id,
github
) VALUES (
'89da69bd-a236-4ab7-9c5d-b5f52ce09959',
'jimaek'
);
6 changes: 6 additions & 0 deletions seeds/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const seed = async (db) => {
await db('directus_users').insert({
id: '89da69bd-a236-4ab7-9c5d-b5f52ce09959',
github: 'jimaek',
});
};
4 changes: 2 additions & 2 deletions src/lib/adopted-probes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ export class AdoptedProbes {

private async sendNotification (adoptedProbe: AdoptedProbe, connectedProbe: Probe) {
await this.sql.raw(`
INSERT INTO directus_notifications (recipient, subject, message) SELECT :recipient, :subject, :message
WHERE NOT EXISTS (SELECT 1 FROM directus_notifications WHERE recipient = :recipient AND message = :message AND DATE(timestamp) = CURRENT_DATE)
INSERT INTO ${NOTIFICATIONS_TABLE} (recipient, subject, message) SELECT :recipient, :subject, :message
WHERE NOT EXISTS (SELECT 1 FROM ${NOTIFICATIONS_TABLE} WHERE recipient = :recipient AND message = :message AND DATE(timestamp) = CURRENT_DATE)
`, {
recipient: adoptedProbe.userId,
subject: 'Adopted probe country change',
Expand Down
25 changes: 21 additions & 4 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Bluebird from 'bluebird';
import chai from 'chai';
import nock from 'nock';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import type { Knex } from 'knex';

import { populateMemList } from '../src/lib/geoip/whitelist.js';
import {
Expand All @@ -10,24 +12,39 @@ import {
populateIpRangeList,
populateNockCitiesList,
} from './utils/populate-static-files.js';

import chaiOas from './plugins/oas/index.js';
import { getRedisClient, initRedis } from '../src/lib/redis/client.js';
import { client } from '../src/lib/sql/client.js';
import { USERS_TABLE } from '../src/lib/adopted-probes.js';
import { client as sql } from '../src/lib/sql/client.js';

if (process.env['NODE_ENV'] !== 'test') {
throw new Error(`NODE_ENV is not 'test' but '${process.env['NODE_ENV']}'.`);
}
MartinKolarik marked this conversation as resolved.
Show resolved Hide resolved

before(async () => {
chai.use(await chaiOas({ specPath: path.join(fileURLToPath(new URL('.', import.meta.url)), '../public/v1/spec.yaml') }));

await initRedis();
const redis = getRedisClient();
await redis.flushDb();
await client(USERS_TABLE).insert({ id: '89da69bd-a236-4ab7-9c5d-b5f52ce09959', github: 'jimaek' }).onConflict().ignore();

await dropAllTables(sql);
await sql.migrate.latest();
await sql.seed.run();

nock.disableNetConnect();
nock.enableNetConnect('127.0.0.1');

await populateIpList();
await populateDomainList();
await populateIpRangeList();
await populateMemList();
await populateNockCitiesList();
});

const dropAllTables = async (sql: Knex) => {
const allTables = (await sql('information_schema.tables')
.whereRaw(`table_schema = database()`)
.select(`table_name as table`)
).map(({ table }: { table: string }) => table);
await Bluebird.map(allTables, table => sql.schema.raw(`drop table \`${table}\``));
};
15 changes: 9 additions & 6 deletions test/tests/integration/adoption-code.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import type { Server } from 'node:http';
import nock from 'nock';
import { expect } from 'chai';
import * as sinon from 'sinon';
import type { Socket } from 'socket.io-client';
import request from 'supertest';
import request, { type SuperTest, type Test } from 'supertest';
import { getTestServer, addFakeProbe, deleteFakeProbe } from '../../utils/server.js';
import nockGeoIpProviders from '../../utils/nock-geo-ip.js';

let probe: Socket;
const app = await getTestServer();
const requestAgent = request(app);
const adoptionCodeStub = sinon.stub();

describe('Adoption code', () => {
let app: Server;
let requestAgent: SuperTest<Test>;
let probe: Socket;
const adoptionCodeStub = sinon.stub();

before(async () => {
app = await getTestServer();
requestAgent = request(app);
nockGeoIpProviders();

probe = await addFakeProbe({
Expand Down