Skip to content

Commit

Permalink
Add daos tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benefacto committed Jan 16, 2024
1 parent 9f37585 commit a0a6d6d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 31 deletions.
18 changes: 10 additions & 8 deletions db/conn.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
const { MongoClient } = require("mongodb");

const dbURI = process.env.NODE_ENV === 'test' ? process.env.TEST_MONGO_URI : process.env.ATLAS_URI;

const client = new MongoClient(dbURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});

let _db;

async function connectToServer() {
const dbUri = process.env.NODE_ENV === 'test' ? mongoServer.getUri() : process.env.ATLAS_URI;
const db = await MongoClient.connect(dbUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});

const db = await client.connect();
// Verify we got a good "db" object
if (db) {
_db = db.db("Lite");
console.log("Successfully connected to MongoDB.");
}
}


function getDb() {
return _db;
}
Expand All @@ -28,4 +30,4 @@ module.exports = {
connectToServer,
getDb,
getClient,
};
};
9 changes: 3 additions & 6 deletions globalTestSetup.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
const { MongoMemoryServer } = require('mongodb-memory-server');
const fs = require('fs');
const path = require('path');

module.exports = async () => {
const mongoServer = await MongoMemoryServer.create();
process.env.MONGO_URI = mongoServer.getUri();
process.env.TEST_MONGO_URI = mongoServer.getUri();

// Store server information in a temporary file
const tempFilePath = path.join(__dirname, 'mongoServerInfo.json');
fs.writeFileSync(tempFilePath, JSON.stringify({ uri: mongoServer.getUri(), instanceId: mongoServer.instanceInfo?.instanceId }));
// Store the MongoMemoryServer instance in a global variable
global.__MONGOSERVER__ = mongoServer;
};
19 changes: 3 additions & 16 deletions globalTestTeardown.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
const fs = require('fs');
const path = require('path');
const { MongoMemoryServer } = require('mongodb-memory-server');

module.exports = async () => {
// Read the server information from the temporary file
const tempFilePath = path.join(__dirname, 'mongoServerInfo.json');
if (fs.existsSync(tempFilePath)) {
const serverInfo = JSON.parse(fs.readFileSync(tempFilePath, 'utf8'));
if (serverInfo && serverInfo.instanceId) {
// Stop the server
await MongoMemoryServer.stop({ instanceId: serverInfo.instanceId });
}

// Remove the temporary file
fs.unlinkSync(tempFilePath);
if (global.__MONGOSERVER__) {
await global.__MONGOSERVER__.stop();
}
};
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"dev": "nodemon ./server.js"
},
"jest": {
"testEnvironment": "node"
"testEnvironment": "node",
"globalSetup": "<rootDir>/globalTestSetup.js",
"globalTeardown": "<rootDir>/globalTestTeardown.js"
},
"keywords": [],
"author": "",
Expand Down
14 changes: 14 additions & 0 deletions routes/daos.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const daoRoutes = express.Router();
* responses:
* 200:
* description: Successfully joined the DAO
* 500:
* description: Invalid signature payload
*/
daoRoutes.route("/daos/join").all(requireSignature).post(joinDAO);
/**
Expand All @@ -56,6 +58,8 @@ daoRoutes.route("/daos/join").all(requireSignature).post(joinDAO);
* responses:
* 200:
* description: DAO created successfully
* 500:
* description: Invalid signature payload
*/
daoRoutes.route("/dao/add").all(requireSignature).post(createDAO);
/**
Expand All @@ -73,6 +77,8 @@ daoRoutes.route("/dao/add").all(requireSignature).post(createDAO);
* type: array
* items:
* type: object
* 500:
* description: Invalid signature payload
*/
daoRoutes.route("/daos").post(getAllLiteOnlyDAOs);
/**
Expand All @@ -91,6 +97,8 @@ daoRoutes.route("/daos").post(getAllLiteOnlyDAOs);
* responses:
* 200:
* description: Details of the DAO
* 400:
* description: Invalid signature payload
*/
daoRoutes.route("/daos/contracts/:daoContract").post(getDAOFromContractAddress);
/**
Expand All @@ -109,6 +117,8 @@ daoRoutes.route("/daos/contracts/:daoContract").post(getDAOFromContractAddress);
* responses:
* 200:
* description: Details of the DAO
* 400:
* description: Invalid signature payload
*/
daoRoutes.route("/daos/:id").get(getDAOById);
/**
Expand All @@ -120,6 +130,8 @@ daoRoutes.route("/daos/:id").get(getDAOById);
* responses:
* 200:
* description: New field added to DAO Collection
* 400:
* description: Invalid signature payload
*/
daoRoutes.route("/daos/create/voting").get(updateTotalHolders);
/**
Expand Down Expand Up @@ -148,6 +160,8 @@ daoRoutes.route("/daos/create/voting").get(updateTotalHolders);
* responses:
* 200:
* description: Total voting addresses count updated
* 400:
* description: Invalid signature payload
*/
daoRoutes.route("/daos/count/:id").post(updateTotalCount);

Expand Down
57 changes: 57 additions & 0 deletions routes/daos.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const request = require("supertest");
const express = require("express");
const daosRoutes = require("../routes/daos");

const app = express();
app.use(express.json());
app.use("/", daosRoutes);
const id = 123;

describe("Daos Routes", () => {
it("should not join a dao with an invalid signature payload", async () => {
await request(app)
.post(`/daos/join`)
.send({daoId: `${id}`})
.set('Accept', 'application/json')
.expect(500)
});
it("should not add a dao with an invalid signature payload", async () => {
await request(app)
.post(`/dao/add`)
.send({daoDetails: {}})
.set('Accept', 'application/json')
.expect(500)
});
it("should not return a list of daos with an invalid signature payload", async () => {
await request(app)
.post(`/daos`)
.send('')
.expect(400)
.expect("Content-Type", /json/)
});
it("should not return a dao contract with an invalid signature payload", async () => {
await request(app)
.post(`/daos/contracts/${id}`)
.send('')
.expect(400)
.expect("Content-Type", /json/)
});
it("should not find a dao with an invalid ID", async () => {
await request(app)
.get(`/daos/${id}`)
.expect(400)
.expect("Content-Type", /json/)
});
it("should not add a new field to the DAO collection with an invalid signature payload", async () => {
await request(app)
.get(`/daos/create/voting`)
.expect(400)
.expect("Content-Type", /json/)
});
it("should not update total voting addresses count for a dao with an invalid ID", async () => {
await request(app)
.get(`/daos/${id}`)
.expect(400)
.expect("Content-Type", /json/)
});
});

0 comments on commit a0a6d6d

Please sign in to comment.