Merge branch 'master' of github.com:Re-Ignited-Development/new-phone-who-dis into bug/mysql-connection-strings

This commit is contained in:
erik-sn committed 2020-10-17 18:33:57 -04:00
commit 11f0302fea
4 files changed
+157 -204

No files matched your search

+7 -7
View File
@@ -1,13 +1,13 @@
import mysql from "mysql2/promise";
import { ConnectionStringParser } from "connection-string-parser";
import {
CONNECTION_STRING,
DEFAULT_PORT,
parseSemiColonFormat,
getServerHost,
getUserId,
getPassword,
} from './db_utils';
CONNECTION_STRING,
DEFAULT_PORT,
parseSemiColonFormat,
getServerHost,
getUserId,
getPassword,
} from "./db_utils";
// we require set mysql_connection_string to be set in the config
const mysqlConnectionString = GetConvar(CONNECTION_STRING, null);
+25 -27
View File
@@ -1,22 +1,21 @@
import * as db from "./db_utils";
const t = 'test value';
const t = "test value";
describe("parseSemiColonFormat", () => {
it("Raises a error with invalid string", () => {
expect(() => db.parseSemiColonFormat("no_variables_here")).toThrowError();
});
it("Correctly parses configuration options", () => {
const test = 'server=127.0.0.1;database=es_extended;userid=user;password=pass';
const test =
"server=127.0.0.1;database=es_extended;userid=user;password=pass";
const result = db.parseSemiColonFormat(test);
expect(result.server).toEqual('127.0.0.1');
expect(result.database).toEqual('es_extended');
expect(result.userid).toEqual('user');
expect(result.password).toEqual('pass');
expect(result.server).toEqual("127.0.0.1");
expect(result.database).toEqual("es_extended");
expect(result.userid).toEqual("user");
expect(result.password).toEqual("pass");
});
});
@@ -24,27 +23,27 @@ describe("getServerHost", () => {
it("returns default", () => {
expect(db.getServerHost({})).toEqual(db.DEFAULT_HOST);
});
it("Correctly returns host", () => {
expect(db.getServerHost({ host: t })).toEqual(t);
});
it("Correctly returns server", () => {
expect(db.getServerHost({ server: t })).toEqual(t);
});
it("Correctly returns data source", () => {
expect(db.getServerHost({ 'data source': t })).toEqual(t);
expect(db.getServerHost({ "data source": t })).toEqual(t);
});
it("Correctly returns datasource", () => {
expect(db.getServerHost({ datasource: t })).toEqual(t);
});
it("Correctly returns addr", () => {
expect(db.getServerHost({ addr: t })).toEqual(t);
});
it("Correctly returns address", () => {
expect(db.getServerHost({ address: t })).toEqual(t);
});
@@ -54,43 +53,42 @@ describe("getUserId", () => {
it("returns default", () => {
expect(db.getUserId({})).toEqual(db.DEFAULT_USER);
});
it("Correctly returns user", () => {
expect(db.getUserId({ user: t })).toEqual(t);
});
it("Correctly returns user id", () => {
expect(db.getUserId({ 'user id': t })).toEqual(t);
expect(db.getUserId({ "user id": t })).toEqual(t);
});
it("Correctly returns userid", () => {
expect(db.getUserId({ userid: t })).toEqual(t);
});
it("Correctly returns user name", () => {
expect(db.getUserId({ 'user name': t })).toEqual(t);
expect(db.getUserId({ "user name": t })).toEqual(t);
});
it("Correctly returns username", () => {
expect(db.getUserId({ username: t })).toEqual(t);
});
it("Correctly returns uid", () => {
expect(db.getUserId({ uid: t })).toEqual(t);
});
});
describe("getPassword", () => {
it("returns default", () => {
expect(() => db.getPassword({})).toThrowError();
});
it("Correctly returns password", () => {
expect(db.getPassword({ password: t })).toEqual(t);
});
it("Correctly returns pwd", () => {
expect(db.getPassword({ pwd: t })).toEqual(t);
});
});
});
+56 -58
View File
@@ -3,71 +3,69 @@ export const DEFAULT_PORT = 3306;
export const DEFAULT_HOST = "localhost";
export const DEFAULT_USER = "root";
interface Map {
[key: string]: string;
}
[key: string]: string;
}
/**
* parse the connection string from the format server=127.0.0.1;database=es_extended;userid=user;password=pass
* @param connectionString - mysql_connection_string value
*/
export function parseSemiColonFormat(connectionString: string): Map {
const parts = connectionString.split(";");
if (parts.length === 1) {
throw new Error(
`Connection string ${connectionString} is in the incorrect format.`
);
}
return parts.reduce((connectionInfo: Map, part) => {
const [key, value] = part.split("=");
connectionInfo[key] = value;
return connectionInfo;
}, {});
}
/**
* allowed variable names: host | server | data source | datasource | addr | address
* @param config - database config variables parsed from mysql_connection_string
*/
export function getServerHost(config: Map): string {
return (
config.host ||
config.server ||
config["data source"] ||
config.datasource ||
config.addr ||
config.address ||
DEFAULT_HOST
const parts = connectionString.split(";");
if (parts.length === 1) {
throw new Error(
`Connection string ${connectionString} is in the incorrect format.`
);
}
/**
* allowed variable names: user | user id | userid | user name | username | uid
* @param config - database config variables parsed from mysql_connection_string
*/
export function getUserId(config: Map): string {
return (
config.user ||
config["user id"] ||
config.userid ||
config["user name"] ||
config.username ||
config.uid ||
DEFAULT_USER
);
return parts.reduce((connectionInfo: Map, part) => {
const [key, value] = part.split("=");
connectionInfo[key] = value;
return connectionInfo;
}, {});
}
/**
* allowed variable names: host | server | data source | datasource | addr | address
* @param config - database config variables parsed from mysql_connection_string
*/
export function getServerHost(config: Map): string {
return (
config.host ||
config.server ||
config["data source"] ||
config.datasource ||
config.addr ||
config.address ||
DEFAULT_HOST
);
}
/**
* allowed variable names: user | user id | userid | user name | username | uid
* @param config - database config variables parsed from mysql_connection_string
*/
export function getUserId(config: Map): string {
return (
config.user ||
config["user id"] ||
config.userid ||
config["user name"] ||
config.username ||
config.uid ||
DEFAULT_USER
);
}
/**
* allowed variable names: password | pwd
* @param config - database config variables parsed from mysql_connection_string
*/
export function getPassword(config: Map): string {
const password = config.password || config.pwd;
if (!password) {
throw new Error("Password is a required field");
}
/**
* allowed variable names: password | pwd
* @param config - database config variables parsed from mysql_connection_string
*/
export function getPassword(config: Map): string {
const password = config.password || config.pwd;
if (!password) {
throw new Error("Password is a required field");
}
return password;
}
return password;
}