-
Notifications
You must be signed in to change notification settings - Fork 11
closes #53 - add influxDB #244
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const Influx = require("influx"); | ||
const logger = require("./../../lib/log")(__filename); | ||
require("dotenv").config(); | ||
|
||
const influxModule = {}; | ||
let client; | ||
const HOST = process.env.INF_HOST||"localhost"; | ||
const PORT = process.env.INF_PORT||8086; | ||
influxModule.startInflux = () => { | ||
client = new Influx.InfluxDB({ | ||
host: HOST, | ||
port: PORT, | ||
}); | ||
logger.info("Sucessfully started InfluxDB"); | ||
}; | ||
|
||
influxModule.createAccount = async (account) => { | ||
const { username, dbPassword } = account; | ||
if (!username || !dbPassword) return; | ||
try { | ||
await client.createUser(username, dbPassword); | ||
await client.createDatabase(username); | ||
await client.grantPrivilege(username, "WRITE", username); | ||
logger.info( | ||
`Sucessfully created new user and database with ${username} name` | ||
); | ||
} catch (err) { | ||
logger.error(err); | ||
throw new Error(`failed to create new influx user with ${username} name`); | ||
} | ||
}; | ||
influxModule.deleteAccount = async (account) => { | ||
const { username } = account; | ||
if (!username) return; | ||
try { | ||
await client.dropUser(username); | ||
await client.dropDatabase(username); | ||
logger.info(`Sucessfully deleted account with ${username} name`); | ||
} catch (err) { | ||
logger.error(err); | ||
throw new Error(`failed to delete influx ${username} account`); | ||
} | ||
}; | ||
influxModule.checkAccount = async (username) => { | ||
if (!username) return; | ||
try { | ||
const users = await client.getUsers(); | ||
const user = users.filter((u) => u === username)[0]; | ||
logger.info( | ||
user | ||
? `Found ${user} account with ${username}` | ||
: `No account with ${username} were found` | ||
); | ||
return Boolean(user); | ||
} catch (err) { | ||
logger.error(err); | ||
throw new Error(`failed to check for influx ${username} account`); | ||
} | ||
}; | ||
|
||
module.exports = influxModule; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
jest.mock("../../lib/log"); | ||
const logGen = require("../../lib/log"); | ||
const logger = { error: jest.fn(), info: jest.fn() }; | ||
logGen.mockReturnValue(logger); | ||
jest.mock("dotenv"); | ||
require("dotenv").config(); | ||
jest.mock("influx"); | ||
const { InfluxDB } = require("influx"); | ||
const influx = require("influx"); | ||
const { | ||
startInflux, | ||
createAccount, | ||
deleteAccount, | ||
checkAccount, | ||
} = require("./influx"); | ||
describe("test InfluxDB", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
const mockClient = { | ||
InfluxDB: jest.fn(), | ||
createUser: jest.fn(), | ||
createDatabase: jest.fn(), | ||
grantPrivilege: jest.fn(), | ||
dropUser: jest.fn(), | ||
dropDatabase: jest.fn(), | ||
getUsers: jest.fn(), | ||
}; | ||
const user = { username: "username", dbPassword: "password" }; | ||
InfluxDB.mockImplementation(function () { | ||
return mockClient; | ||
}); | ||
it("should start influx client", () => { | ||
startInflux(); | ||
expect(influx.InfluxDB).toHaveBeenCalledTimes(1); | ||
}); | ||
it("should use global variables", () => { | ||
startInflux(); | ||
expect(influx.InfluxDB.mock.calls[0][0].host).toEqual("localhost"); | ||
expect(influx.InfluxDB.mock.calls[0][0].port).toEqual(8086); | ||
}); | ||
describe("createAccount", () => { | ||
it("should sucessfully create new account", async () => { | ||
await createAccount(user); | ||
expect(mockClient.createUser).toHaveBeenCalledTimes(1); | ||
expect(mockClient.createDatabase).toHaveBeenCalledTimes(1); | ||
expect(mockClient.grantPrivilege).toHaveBeenCalledTimes(1); | ||
}); | ||
it("should call logger in case of an error", async () => { | ||
try { | ||
mockClient.createUser.mockReturnValue(Promise.reject()); | ||
expect(await createAccount(user)).rejects.toThrow(); | ||
} catch (err) { | ||
expect(logger.error).toHaveBeenCalledTimes(1); | ||
} | ||
}); | ||
it("should return if no account argument was provided", async () => { | ||
const res = await createAccount({}); | ||
expect(res).toEqual(undefined); | ||
}); | ||
}); | ||
describe("deleteAccount", () => { | ||
it("should sucessfully delete account", async () => { | ||
await deleteAccount(user); | ||
expect(mockClient.dropUser).toHaveBeenCalledTimes(1); | ||
expect(mockClient.dropDatabase).toHaveBeenCalledTimes(1); | ||
}); | ||
it("should throw error to invalid credentials", async () => { | ||
try { | ||
mockClient.dropUser.mockReturnValue(Promise.reject()); | ||
expect(await deleteAccount(user)).rejects.toThrow(); | ||
} catch (err) { | ||
expect(logger.error).toHaveBeenCalledTimes(1); | ||
} | ||
}); | ||
it("should return if no username was provided", async () => { | ||
const res = await deleteAccount({}); | ||
expect(res).toEqual(undefined); | ||
}); | ||
}); | ||
describe("checkAccount", () => { | ||
it("should return true if account exists", async () => { | ||
mockClient.getUsers.mockReturnValue(["name", "username"]); | ||
const res = await checkAccount("username"); | ||
expect(mockClient.getUsers).toHaveBeenCalledTimes(1); | ||
expect(res).toEqual(true); | ||
}); | ||
it("should return false if account doesnt exitsts", async () => { | ||
mockClient.getUsers.mockReturnValue([]); | ||
const res = await checkAccount(user); | ||
expect(mockClient.getUsers).toHaveBeenCalledTimes(1); | ||
expect(res).toEqual(false); | ||
}); | ||
it("should throw an error", async () => { | ||
try { | ||
mockClient.getUsers.mockReturnValue(Promise.reject()); | ||
expect(await checkAccount(user)).rejects.toThrow(); | ||
} catch (err) { | ||
expect(logger.error).toHaveBeenCalledTimes(1); | ||
} | ||
}); | ||
it("should return if no username was provided", async () => { | ||
const res = await checkAccount(); | ||
expect(res).toEqual(undefined); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ const db = require("../sequelize/db"); | |
const pg = require("../database/postgres/pg"); | ||
const arango = require("../database/arango/arango"); | ||
const es = require("../database/elasticsearch/elastic"); | ||
const influx = require("../database/influx/influx"); | ||
const logger = require("./log")(__filename); | ||
|
||
const util = {}; | ||
|
@@ -37,6 +38,9 @@ util.cleanAnonymous = async () => { | |
const arangoDbExists = await arango.checkIfDatabaseExists(username); | ||
if (arangoDbExists) await arango.deleteAccount(username); | ||
|
||
const influxDbExists = await influx.checkAccount(username); | ||
if (influxDbExists) await influx.deleteAccount(user); | ||
|
||
|
||
return await user.destroy(); | ||
}) | ||
).then(() => { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function only uses
username
, so why is the entireaccount
object needed?