Fix Github Actions tests

This commit is contained in:
Maksim Karasev 2021-03-24 15:09:19 +03:00
parent 9d601e3948
commit 2b1d890b0e
4 changed files with 68 additions and 72 deletions

View File

@ -16,7 +16,7 @@ jobs:
name: Build and push Docker images
uses: docker/build-push-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
repository: karasevm/personal-gallery-node
always_pull: true

View File

@ -15,38 +15,42 @@ const nanoid = customAlphabet(alphabet, 7);
* Reads all files in the directory and tries to add them to the database.
*/
export const registerFileInFolder = async () => {
const files = fs.readdirSync(IMAGE_DIR);
const filesWithStats = await Promise.all(
files.map((filename) => fs.promises
.stat(path.join(IMAGE_DIR, filename))
.then((stat) => ({ filename, stat }))),
);
const filteredFilesWithStats:{
filename: string;
stat: fs.Stats;
}[] = [];
const filePromises = filesWithStats.map(async (file) => {
const result = await fileType.fromFile(
path.join(IMAGE_DIR, file.filename),
try {
const files = fs.readdirSync(IMAGE_DIR);
const filesWithStats = await Promise.all(
files.map((filename) => fs.promises
.stat(path.join(IMAGE_DIR, filename))
.then((stat) => ({ filename, stat }))),
);
if (result !== undefined && ACCEPTED_MIME.includes(result.mime)) {
filteredFilesWithStats.push(file);
}
});
await Promise.all(filePromises);
let newCount = 0;
filteredFilesWithStats.forEach((file) => {
try {
insertImageIntoDB(file.filename, file.stat.mtime.getTime());
newCount += 1;
} catch (e) { logger.error(`${e.name}: ${e.message}`); }
});
logger.info(
`Found ${filesWithStats.length} files, ${filteredFilesWithStats.length} valid images, ${newCount} new`,
);
const filteredFilesWithStats:{
filename: string;
stat: fs.Stats;
}[] = [];
const filePromises = filesWithStats.map(async (file) => {
const result = await fileType.fromFile(
path.join(IMAGE_DIR, file.filename),
);
if (result !== undefined && ACCEPTED_MIME.includes(result.mime)) {
filteredFilesWithStats.push(file);
}
});
await Promise.all(filePromises);
let newCount = 0;
filteredFilesWithStats.forEach((file) => {
try {
insertImageIntoDB(file.filename, file.stat.mtime.getTime());
newCount += 1;
} catch (e) { logger.error(`${e.name}: ${e.message}`); }
});
logger.info(
`Found ${filesWithStats.length} files, ${filteredFilesWithStats.length} valid images, ${newCount} new`,
);
} catch (e) {
logger.error(`Error while registering files ${e.message}`);
}
};
/**

View File

@ -11,15 +11,11 @@ const API_URL = '/api';
const clearTempDir = () => {
const directory = path.join(process.cwd(), 'test', 'i');
fs.readdir(directory, (err, files) => {
if (err) throw err;
files.forEach((file) => {
if (!file.startsWith('.')) {
fs.unlink(path.join(directory, file), (e) => {
if (e) throw e;
});
}
});
const files = fs.readdirSync(directory);
files.forEach((file) => {
if (!file.startsWith('.')) {
fs.unlinkSync(path.join(directory, file));
}
});
};
@ -29,13 +25,16 @@ const getFileFromTempDir = (fullPath = false) => {
return fullPath ? path.join(directory, files[1]) : files[1];
};
beforeAll(async (done) => {
clearTempDir();
database.db.prepare('DELETE FROM sessions').run();
database.db.prepare('DELETE FROM meta').run();
database.db.prepare('DELETE FROM images').run();
done();
}, 30000);
describe('Logged in', () => {
const api = supertest.agent(app);
beforeAll(async () => {
clearTempDir();
database.db.exec('DELETE FROM sessions');
database.db.exec('DELETE FROM meta');
database.db.exec('DELETE FROM images');
await register('testing', 'testing');
await api
.post(`${API_URL}/login`)
@ -184,10 +183,3 @@ describe('Not logged in', () => {
});
});
});
afterAll(() => {
clearTempDir();
database.db.exec('DELETE FROM sessions');
database.db.exec('DELETE FROM meta');
database.db.exec('DELETE FROM images');
});

View File

@ -5,29 +5,29 @@ import * as database from '../src/utils/db';
const api = supertest(app);
const API_URL = '/api';
beforeAll(() => {
database.db.exec('DELETE FROM sessions');
database.db.exec('DELETE FROM meta');
database.db.exec('DELETE FROM images');
});
describe('Login API', () => {
test('meta status correct', (done) => {
api
.get(`${API_URL}/meta`)
.expect(200)
.end((err, res) => {
if (typeof err !== 'undefined') {
done(err);
}
try {
expect(res.body.setupFinished).toBe(false);
done();
} catch (error) {
done(error);
}
});
});
describe('Registration', () => {
beforeAll(() => {
database.db.exec('DELETE FROM sessions');
database.db.exec('DELETE FROM meta');
database.db.exec('DELETE FROM images');
});
test('meta status correct', (done) => {
api
.get(`${API_URL}/meta`)
.expect(200)
.end((err, res) => {
if (typeof err !== 'undefined') {
done(err);
}
try {
expect(res.body.setupFinished).toBe(false);
done();
} catch (error) {
done(error);
}
});
});
test('400 on registering with empty username', () => {
return api
.post(`${API_URL}/login/register`)