Skip to content

Commit 2919eef

Browse files
authored
Merge pull request #240 from anthonykhoa/frontend
closes #238 - Display tutorial for Arango
2 parents 624355a + 290aafa commit 2919eef

File tree

15 files changed

+930
-542
lines changed

15 files changed

+930
-542
lines changed

database/arango/arango.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ const logger = require("./../../lib/log")(__filename);
66
const arangoModule = {};
77
let db;
88

9-
arangoModule.checkIfDatabaseExists = async (name) => {
10-
// returns array of objects containing a '_name' key.
11-
// Ex [{ _name: 'lol' }, { _name: 'cakes' }]
12-
const names = await db.databases();
13-
return names.map((db) => db._name).includes(name);
9+
arangoModule.checkIfDatabaseExists = async (username) => {
10+
const db = new Database({
11+
url: process.env.ARANGO_URL,
12+
databaseName: username,
13+
auth: {
14+
username: process.env.ARANGO_USER,
15+
password: process.env.ARANGO_PW,
16+
},
17+
});
18+
const res = await db.exists();
19+
db.close();
20+
return res;
1421
};
1522

16-
arangoModule.startArangoDB = async () => {
23+
arangoModule.startArangoDB = () => {
1724
try {
1825
db = new Database({
1926
url: process.env.ARANGO_URL,

database/arango/arango.test.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jest.mock("dotenv");
1313
Arango.Database = jest.fn().mockReturnValue({
1414
close: jest.fn(),
1515
createDatabase: jest.fn(),
16+
exists: jest.fn(),
1617
});
1718

1819
const {
@@ -33,16 +34,16 @@ describe("ArangoDB functions", () => {
3334
expect(Arango.Database).toHaveBeenCalledTimes(1);
3435
});
3536

36-
test("should retun false", async () => {
37+
test("should return false", async () => {
3738
const db = new Arango.Database();
38-
db.databases = jest.fn().mockReturnValue([{ _name: "lol" }]);
39+
db.exists = jest.fn().mockReturnValue(false);
3940
const res = await checkIfDatabaseExists("hi");
4041
expect(res).toEqual(false);
4142
});
4243

43-
test("should retun true", async () => {
44+
test("should return true", async () => {
4445
const db = new Arango.Database();
45-
db.databases = jest.fn().mockReturnValue([{ _name: "lol" }]);
46+
db.exists = jest.fn().mockReturnValue(true);
4647
const res = await checkIfDatabaseExists("lol");
4748
expect(res).toEqual(true);
4849
});
@@ -69,22 +70,22 @@ describe("ArangoDB functions", () => {
6970
error, and do nothing if argument is invalid", async () => {
7071
const db = new Arango.Database();
7172
await createAccount();
73+
db.close = jest.fn().mockImplementation(() => 1);
7274
expect(db.createDatabase).toHaveBeenCalledTimes(0);
7375
expect(logger.error).toHaveBeenCalledTimes(0);
7476

75-
db.databases = jest.fn().mockReturnValue([{ _name: "lol" }]);
77+
db.exists = jest.fn().mockReturnValue(true);
7678
await createAccount({ username: "lol", dbPassword: "hi" });
7779
expect(db.createDatabase).toHaveBeenCalledTimes(0);
7880

79-
db.databases = jest.fn().mockReturnValue([{ _name: "lol" }]);
8081
await createAccount({ username: "", dbPassword: "" });
8182
expect(db.createDatabase).toHaveBeenCalledTimes(0);
8283

8384
try {
84-
db.databases = jest.fn().mockReturnValue([{ _name: "lol" }]);
85+
db.exists = jest.fn().mockReturnValue(false);
8586
await createAccount({ username: "hi", dbPassword: "hi" });
8687
expect(db.createDatabase).toHaveBeenCalledTimes(1);
87-
88+
db.exists = jest.fn().mockReturnValue(false);
8889
db.createDatabase = jest.fn().mockImplementation(() => {
8990
throw new Error();
9091
});
@@ -96,17 +97,21 @@ describe("ArangoDB functions", () => {
9697
test("should send two fetch requests if successful, logger.error when theres \
9798
an error, and do nothing if argument is invalid", async () => {
9899
const db = new Arango.Database();
100+
99101
await deleteAccount();
100102
expect(sendFetch).toHaveBeenCalledTimes(0);
101103
expect(logger.error).toHaveBeenCalledTimes(0);
102104

103-
db.databases = jest.fn().mockReturnValue([{ _name: "lol" }]);
104-
sendFetch.mockReturnValue({ jwt: "lol" });
105+
db.close = jest.fn().mockImplementation(() => 1);
106+
db.exists = jest.fn().mockReturnValue(false);
105107
await deleteAccount("hi");
106108
expect(sendFetch).toHaveBeenCalledTimes(0);
107-
109+
sendFetch.mockReturnValue({ jwt: "lol" });
110+
db.exists = jest.fn().mockReturnValue(true);
108111
db.dropDatabase = jest.fn().mockReturnValue("lol");
109-
await deleteAccount("lol");
112+
try {
113+
await deleteAccount("lol");
114+
} catch (err) {}
110115
expect(sendFetch).toHaveBeenCalledTimes(2);
111116
expect(db.dropDatabase).toHaveBeenCalledTimes(1);
112117

@@ -120,10 +125,13 @@ describe("ArangoDB functions", () => {
120125
});
121126

122127
test("should call Database function and FAIL", () => {
123-
Arango.Database.mockImplementation(() => {
124-
throw new Error();
125-
});
126-
startArangoDB();
128+
try {
129+
Arango.Database.mockImplementation(() => {
130+
throw new Error();
131+
});
132+
startArangoDB();
133+
} catch (err) {}
134+
127135
expect(logger.error).toHaveBeenCalledTimes(1);
128136
});
129137
});

database/elasticsearch/elastic.js

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,36 @@ const authorization =
99
Buffer.from(`elastic:${process.env.ES_PASSWORD}`).toString("base64");
1010

1111
es.createAccount = async (account) => {
12-
if (!account.username || !account.dbPassword) {
12+
const { username, email, dbPassword } = account;
13+
if (!username || !dbPassword) {
1314
logger.error("Account data is invalid");
1415
throw new Error("Account data is invalid");
1516
}
1617
const r1 = await sendFetch(
17-
`${ES_HOST}/_security/role/${account.username}`,
18+
`${ES_HOST}/_security/role/${username}`,
1819
"POST",
1920
{
2021
indices: [
2122
{
22-
names: [`${account.username}-*`],
23+
names: [`${username}-*`],
2324
privileges: ["all"],
2425
},
2526
],
2627
},
2728
authorization
2829
);
2930
const r2 = await sendFetch(
30-
`${ES_HOST}/_security/user/${account.username}`,
31+
`${ES_HOST}/_security/user/${username}`,
3132
"POST",
3233
{
33-
email: account.email,
34-
password: account.dbPassword,
35-
roles: [account.username],
34+
email: email,
35+
password: dbPassword,
36+
roles: [username],
3637
},
3738
authorization
3839
);
3940
const r3 = await sendFetch(
40-
`${ES_HOST}/${account.username}-example/_doc`,
41+
`${ES_HOST}/${username}-example/_doc`,
4142
"POST",
4243
{
4344
message:
@@ -49,61 +50,65 @@ es.createAccount = async (account) => {
4950
if (err) {
5051
logger.error(err);
5152
throw new Error(
52-
`Failed to create Elasticsearch account for user: ${account.email}`,
53+
`Failed to create Elasticsearch account for user: ${email}`,
5354
err
5455
);
5556
}
5657
if (!r1.role.created || !r2.created) {
57-
logger.error("User already exists", account.email);
58-
throw new Error(
59-
`Elasticsearch account already exists for user: ${account.email}`
60-
);
58+
logger.error("User already exists", email);
59+
throw new Error(`Elasticsearch account already exists for user: ${email}`);
6160
}
6261
logger.info("Successfully created Elastisearch user", account.email);
6362
};
6463

6564
es.deleteAccount = async (account) => {
66-
if (!account.username) {
65+
const { username, id } = account;
66+
if (!username) {
6767
logger.error("Account data is invalid");
6868
throw new Error("Account data is invalid");
6969
}
7070
const r1 = await sendFetch(
71-
`${ES_HOST}/_security/user/${account.username}`,
71+
`${ES_HOST}/_security/user/${username}`,
7272
"DELETE",
7373
null,
7474
authorization
7575
);
7676
const r2 = await sendFetch(
77-
`${ES_HOST}/_security/role/${account.username}`,
77+
`${ES_HOST}/_security/role/${username}`,
7878
"DELETE",
7979
null,
8080
authorization
8181
);
82-
const r3 = await sendFetch(`/${account.username}-*`, "DELETE", null, authorization);
82+
const r3 = await sendFetch(`/${username}-*`, "DELETE", null, authorization);
8383
const err = r1.error || r2.error;
8484
if (err || !r1.found || !r2.found) {
8585
logger.error("Deleting Elasticsearch user failed");
86-
throw new Error(
87-
`Failed to delete Elasticsearch account for user: ${account.id}`
88-
);
86+
throw new Error(`Failed to delete Elasticsearch account for user: ${id}`);
8987
}
90-
logger.info("Successfully deleted Elasticsearch user", account.id);
88+
logger.info("Successfully deleted Elasticsearch user", id);
9189
};
9290

93-
es.checkAccount = async (account) => {
94-
const username = account.username;
95-
if (!username) {
96-
logger.error("Account data is invalid");
97-
throw new Error("Account data is invalid");
91+
es.checkAccount = async (username) => {
92+
if (!username) return;
93+
try {
94+
const r1 = await sendFetch(
95+
`${ES_HOST}/_security/user/${username}`,
96+
"GET",
97+
null,
98+
authorization
99+
);
100+
logger.info(
101+
`Checking Elasticsearch account for ${username} result:`,
102+
!!r1[username]
103+
);
104+
if (r1[username]) return true;
105+
return false;
106+
} catch (err) {
107+
logger.error("Error checking for elasticsearch Account", err);
108+
throw new Error(
109+
"hell0 sum ting went wong with checkin for the elasticsearch account"
110+
);
98111
}
99-
100-
const r1 = await sendFetch(`${ES_HOST}/_security/user/${username}`, "GET", null, authorization);
101-
logger.info(
102-
`Checking Elasticsearch account for ${username} result:`,
103-
!!r1[username]
104-
);
105-
if (r1[username]) return true;
106-
return false;
107112
};
108113

109114
module.exports = es;

database/elasticsearch/elastic.test.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ describe("testing es delete account function", () => {
105105
} catch (err) {}
106106
return expect(logger.error).toHaveBeenCalled();
107107
});
108-
it("should log info if deleting user seccess", async () => {
108+
it("should log info if deleting user success", async () => {
109109
const account = {
110110
username: "testuser",
111111
email: "em@i.l",
@@ -127,43 +127,39 @@ describe("testing es check account function", () => {
127127
beforeEach(() => {
128128
jest.clearAllMocks();
129129
});
130-
it("should log error if account data is invalid", async () => {
131-
const account = {};
132-
try {
133-
await es.checkAccount(account);
134-
} catch (err) {}
135-
return expect(logger.error).toHaveBeenCalled();
136-
});
137130
it("should return false if index does not exist", async () => {
138-
const account = {
139-
username: "testuser",
140-
email: "em@i.l",
141-
password: "1234qwer",
142-
};
143131
fetch.mockReturnValue(
144132
Promise.resolve({
145133
json: () => {
146134
return { error: "error" };
147135
},
148136
})
149137
);
150-
const result = await es.checkAccount(account);
138+
const result = await es.checkAccount("testuser");
151139
return expect(result).toEqual(false);
152140
});
153141
it("should return true if index exists", async () => {
154-
const account = {
155-
username: "testuser",
156-
email: "em@i.l",
157-
password: "1234qwer",
158-
};
159142
fetch.mockReturnValue(
160143
Promise.resolve({
161144
json: () => {
162145
return { testuser: {} };
163146
},
164147
})
165148
);
166-
const result = await es.checkAccount(account);
149+
const result = await es.checkAccount("testuser");
167150
return expect(result).toEqual(true);
168151
});
152+
it("should log error if somethin goes wrong", async () => {
153+
fetch.mockImplementation(() => {
154+
throw new Error();
155+
});
156+
try {
157+
await es.checkAccount("jeez");
158+
} catch (err) {}
159+
return expect(logger.error).toHaveBeenCalled();
160+
});
161+
it("should do nothing if username is a falsy value", async () => {
162+
await es.checkAccount("");
163+
return expect(fetch).toHaveBeenCalledTimes(0);
164+
});
169165
});

lib/util.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { Op } = require("sequelize");
22
const db = require("../sequelize/db");
33
const pg = require("../database/postgres/pg");
4+
const arango = require("../database/arango/arango");
45
const es = require("../database/elasticsearch/elastic");
56
const logger = require("./log")(__filename);
67

@@ -25,12 +26,18 @@ util.cleanAnonymous = async () => {
2526
});
2627
logger.info(`${userAccount.length} expired accounts are found`);
2728
return Promise.all(
28-
userAccount.map(async (e) => {
29-
const pgDbExists = await pg.userHasPgAccount(e.username);
30-
if (pgDbExists) await pg.deletePgAccount(e);
31-
const esDbExists = await es.checkAccount(e);
32-
if (esDbExists) await es.deleteAccount(e);
33-
return await e.destroy();
29+
userAccount.map(async (user) => {
30+
const { username } = user;
31+
const pgDbExists = await pg.userHasPgAccount(username);
32+
if (pgDbExists) await pg.deletePgAccount(user);
33+
34+
const esDbExists = await es.checkAccount(username);
35+
if (esDbExists) await es.deleteAccount(user);
36+
37+
const arangoDbExists = await arango.checkIfDatabaseExists(username);
38+
if (arangoDbExists) await arango.deleteAccount(username);
39+
40+
return await user.destroy();
3441
})
3542
).then(() => {
3643
logger.info("Cleaning expired accounts has completed");

0 commit comments

Comments
 (0)