added backend test and updated api reports

Signed-off-by: Lykke Axlin <lykkeaxlin@hotmail.com>

Co-authored-by: klaraab <klarabroman@live.se>
This commit is contained in:
Lykke Axlin
2021-10-03 13:01:13 +02:00
parent 11d373f4e0
commit bdb1fc1e11
10 changed files with 177 additions and 70 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ export interface RouterOptions {
// (undocumented)
config: Config;
// (undocumented)
database?: PluginDatabaseManager;
database: PluginDatabaseManager;
// (undocumented)
logger: Logger_2;
}
+1
View File
@@ -27,6 +27,7 @@
"express-promise-router": "^4.1.0",
"knex": "^0.95.1",
"supertest": "^6.1.6",
"uuid": "^8.3.2",
"winston": "^3.2.1",
"yn": "^4.0.0"
},
@@ -0,0 +1,38 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { DatabaseHandler } from './DatabaseHandler';
const members: Array<any> = [
{
entity_ref: 'project1',
user_id: 'member1',
},
];
describe('DatabaseHandler', () => {
let database: DatabaseHandler;
beforeAll(async () => {
database = await DatabaseHandler.createTestDatabase();
await database.addMember(members[0].user_id, members[0].entity_ref);
});
it("can get members that's in the database", async () => {
const cov: any[] = await database.getMembers('project1');
expect(cov[0].entity_ref).toEqual(members[0].entity_ref);
expect(cov[0].user_id).toEqual(members[0].user_id);
});
});
@@ -0,0 +1,103 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { resolvePackagePath } from '@backstage/backend-common';
import knexFactory, { Knex } from 'knex';
import { v4 as uuidv4 } from 'uuid';
const migrationsDir = resolvePackagePath(
'@backstage/plugin-bazaar-backend',
'migrations',
);
type Options = {
database: Knex;
};
export class DatabaseHandler {
static async create(options: Options): Promise<DatabaseHandler> {
const { database } = options;
await database.migrate.latest({
directory: migrationsDir,
});
return new DatabaseHandler(options);
}
private readonly database: Knex;
private constructor(options: Options) {
this.database = options.database;
}
public static async createTestDatabase(): Promise<DatabaseHandler> {
const knex = await this.createTestDatabaseConnection();
return await this.create({ database: knex });
}
public static async createTestDatabaseConnection(): Promise<Knex> {
const config: Knex.Config<any> = {
client: 'pg',
connection: {
host: 'localhost',
port: 5432,
user: 'postgres',
password: 'postgres',
},
/*
client: 'sqlite3',
connection: ':memory:',
useNullAsDefault: true,
*/
};
let knex = knexFactory(config);
if (typeof config.connection !== 'string') {
const tempDbName = `d${uuidv4().replace(/-/g, '')}`;
await knex.raw(`CREATE DATABASE ${tempDbName};`);
knex = knexFactory({
...config,
connection: {
...config.connection,
database: tempDbName,
},
});
}
knex.client.pool.on('createSuccess', (_eventId: any, resource: any) => {
resource.run('PRAGMA foreign_keys = ON', () => {});
});
return knex;
}
async getMembers(entityRef: any) {
return await this.database
.select('*')
.from('public.members')
.where({ entity_ref: entityRef });
}
async addMember(userId: any, entityRef: any) {
await this.database
.insert({
entity_ref: entityRef,
user_id: userId,
})
.into('public.members');
}
}
+9 -26
View File
@@ -14,15 +14,12 @@
* limitations under the License.
*/
import {
errorHandler,
PluginDatabaseManager,
resolvePackagePath,
} from '@backstage/backend-common';
import { errorHandler, PluginDatabaseManager } from '@backstage/backend-common';
import express from 'express';
import Router from 'express-promise-router';
import { Logger } from 'winston';
import { Config } from '@backstage/config';
import { DatabaseHandler } from './DatabaseHandler';
export interface RouterOptions {
logger: Logger;
@@ -33,30 +30,20 @@ export interface RouterOptions {
export async function createRouter(
options: RouterOptions,
): Promise<express.Router> {
const { logger } = options;
const db = await options.database.getClient();
const { logger, database } = options;
const db = await database.getClient();
const dbHandler = await DatabaseHandler.create({ database: db });
logger.info('Initializing Bazaar backend');
const migrationsDir = resolvePackagePath(
'@backstage/plugin-bazaar-backend',
'migrations',
);
await db.migrate.latest({
directory: migrationsDir,
});
const router = Router();
router.use(express.json());
router.get('/members', async (request, response) => {
const entityRef = request.headers.entity_ref;
const data = await db
.select('*')
.from('public.members')
.where({ entity_ref: entityRef });
const data = await dbHandler.getMembers(entityRef);
if (data?.length) {
response.json({ status: 'ok', data: data });
@@ -69,12 +56,8 @@ export async function createRouter(
const userId = request.headers.user_id;
const entityRef = request.headers.entity_ref;
await db
.insert({
entity_ref: entityRef,
user_id: userId,
})
.into('public.members');
await dbHandler.addMember(userId, entityRef);
response.json({ status: 'ok' });
});