1 | var request = require("supertest");
|
2 | var assert = require('chai').assert;
|
3 | var jsdom = require('mocha-jsdom');
|
4 | var fs = require('fs');
|
5 |
|
6 |
|
7 | var conn_name = "TestConnection";
|
8 | var db_name = "NewTestDB";
|
9 | var coll_name = "NewTestCollection";
|
10 | var user_name = "TestNewUser";
|
11 |
|
12 | app = require('../app');
|
13 | var agent = request.agent(app);
|
14 |
|
15 | before(function (done) {
|
16 | app.on("adminMongoStarted", function(){
|
17 | done();
|
18 | });
|
19 | });
|
20 |
|
21 | describe("Add connection, database and collection",function(){
|
22 |
|
23 | it("Add a new connection",function(done){
|
24 | agent
|
25 | .post("/add_config")
|
26 | .send({0: conn_name,1: "mongodb://localhost:27017", 2: "{}"})
|
27 | .expect(200)
|
28 | .expect({"msg": "Config successfully added"}, done);
|
29 | });
|
30 |
|
31 | it("Add a database",function(done){
|
32 | agent
|
33 | .post("/" + conn_name + "/db_create")
|
34 | .send({"db_name" : db_name})
|
35 | .expect(200)
|
36 | .expect({"msg": "Database successfully created"}, done);
|
37 | });
|
38 |
|
39 | it("Add a collection",function(done){
|
40 | agent
|
41 | .post("/" + conn_name + "/" + db_name + "/coll_create")
|
42 | .send({"collection_name" : coll_name})
|
43 | .expect(200)
|
44 | .expect({"msg": "Collection successfully created"}, done);
|
45 | });
|
46 | });
|
47 |
|
48 | describe("User tests",function(){
|
49 | it("Create a user",function(done){
|
50 | var json = {
|
51 | "username": user_name,
|
52 | "user_password": "test",
|
53 | "roles_text": "read"
|
54 | }
|
55 | agent
|
56 | .post("/" + conn_name + "/" + db_name + "/" + coll_name + "/user_create")
|
57 | .send(json)
|
58 | .expect(200)
|
59 | .expect({"msg": "User successfully created"}, done);
|
60 | });
|
61 |
|
62 | it('Delete a user', function(done){
|
63 | agent
|
64 | .post("/" + conn_name + "/" + db_name + "/" + coll_name + "/user_delete")
|
65 | .send({"username": user_name})
|
66 | .expect(200)
|
67 | .expect({"msg": "User successfully deleted"}, done);
|
68 | });
|
69 | });
|
70 |
|
71 | describe("Document tests",function(){
|
72 | var oid_doc_id = "";
|
73 | var $;
|
74 |
|
75 | jsdom({
|
76 | src: fs.readFileSync('./public/js/toEJSON.js', 'utf-8')
|
77 | });
|
78 |
|
79 | before(function () {
|
80 | $ = require('jquery');
|
81 | });
|
82 |
|
83 | it("Add a document",function(done){
|
84 | var dte = new Date();
|
85 | var json = '{"NewTestDocument":"Test Data","NewTestDateToday": ISODate("' + dte.toISOString() + '"),"NewTestDate5Days": ISODate("' + new Date(dte.setDate(dte.getDate() - 5)).toISOString() + '")}';
|
86 |
|
87 | var strJSON = toEJSON.serializeString(json);
|
88 |
|
89 | agent
|
90 | .post("/" + conn_name + "/" + db_name + "/" + coll_name + "/insert_doc")
|
91 | .send({"objectData": strJSON})
|
92 | .expect(200)
|
93 | .end(function(err, result) {
|
94 | assert.equal(result.body.msg, "Document successfully added");
|
95 | oid_doc_id = result.body.doc_id;
|
96 | done();
|
97 | });
|
98 | });
|
99 |
|
100 | it("Find document using ObjectID",function(done){
|
101 | var qryJson = "{'_id': ObjectId('" + oid_doc_id + "')}";
|
102 | var strJSON = toEJSON.serializeString(qryJson);
|
103 | agent
|
104 | .post("/api/" + conn_name + "/" + db_name + "/" + coll_name + "/1")
|
105 | .send({"query": strJSON})
|
106 | .expect(200)
|
107 | .end(function(err, result) {
|
108 | assert.equal(result.body.data[0]._id, oid_doc_id);
|
109 | done();
|
110 | });
|
111 | });
|
112 |
|
113 | it("Find document using non existant ObjectID",function(done){
|
114 | var qryJson = '{"_id": ObjectId("56a97ed3f718fe9a4f59948c")}';
|
115 | var strJSON = toEJSON.serializeString(qryJson);
|
116 | agent
|
117 | .post("/api/" + conn_name + "/" + db_name + "/" + coll_name + "/1")
|
118 | .send({"query": strJSON})
|
119 | .expect(200)
|
120 | .end(function(err, result) {
|
121 | assert.equal(result.body.data.length, 0);
|
122 | done();
|
123 | });
|
124 | });
|
125 |
|
126 | it("Send in an incorrect syntax query",function(done){
|
127 | var qryJson = '{"_id": ObjectId("56a97ed3f718fe9a4f59948cds")}';
|
128 | var strJSON = toEJSON.serializeString(qryJson);
|
129 | agent
|
130 | .post("/api/" + conn_name + "/" + db_name + "/" + coll_name + "/1")
|
131 | .send({"query": strJSON})
|
132 | .expect(200)
|
133 | .end(function(err, result) {
|
134 | assert.equal(result.body.validQuery, false);
|
135 | done();
|
136 | });
|
137 | });
|
138 |
|
139 | it("Find document using valid date",function(done){
|
140 | var qryJson = '{"NewTestDateToday" : {"$gte": ISODate("2013-10-01T00:00:00.000Z")}}';
|
141 | var strJSON = toEJSON.serializeString(qryJson);
|
142 | agent
|
143 | .post("/api/" + conn_name + "/" + db_name + "/" + coll_name + "/1")
|
144 | .send({"query": strJSON})
|
145 | .expect(200)
|
146 | .end(function(err, result) {
|
147 | assert.equal(result.body.data[0]._id, oid_doc_id);
|
148 | done();
|
149 | });
|
150 | });
|
151 |
|
152 | it("Find document using invalid date",function(done){
|
153 | var qryJson = '{"NewTestDateToday" : {"$lte": ISODate("2013-10-01T00:00:00.000Z")}}';
|
154 | var strJSON = toEJSON.serializeString(qryJson);
|
155 | agent
|
156 | .post("/api/" + conn_name + "/" + db_name + "/" + coll_name + "/1")
|
157 | .send({"query": strJSON})
|
158 | .expect(200)
|
159 | .end(function(err, result) {
|
160 | assert.equal(result.body.data.length, 0);
|
161 | done();
|
162 | });
|
163 | });
|
164 |
|
165 | it("Find document using string values",function(done){
|
166 | var json = {"NewTestDocument":"Test Data"};
|
167 | var strJSON = JSON.stringify(json);
|
168 | agent
|
169 | .post("/api/" + conn_name + "/" + db_name + "/" + coll_name + "/1")
|
170 | .send({"query": strJSON})
|
171 | .expect(200)
|
172 | .end(function(err, res) {
|
173 | assert.equal(res.body.data[0].NewTestDocument, 'Test Data');
|
174 | doc_id = res.body.data[0]._id;
|
175 | done();
|
176 | });
|
177 | });
|
178 |
|
179 | it('Delete our new document', function(done){
|
180 | agent
|
181 | .post("/" + conn_name + "/" + db_name + "/" + coll_name + "/doc_delete")
|
182 | .send({"doc_id": doc_id})
|
183 | .expect(200)
|
184 | .expect({"msg": "Document successfully deleted"}, done);
|
185 | });
|
186 | });
|
187 |
|
188 | describe("Remove and remove collection and connection",function(){
|
189 | it("Rename the collection",function(done){
|
190 | agent
|
191 | .post("/" + conn_name + "/" + db_name + "/" + coll_name + "/coll_name_edit")
|
192 | .send({"new_collection_name" : coll_name + "Changed"})
|
193 | .expect(200)
|
194 | .expect({"msg": "Collection successfully renamed"}, done);
|
195 | });
|
196 |
|
197 | it("Remove the collection",function(done){
|
198 | agent
|
199 | .post("/" + conn_name + "/" + db_name + "/coll_delete")
|
200 | .send({"collection_name" : coll_name + "Changed"})
|
201 | .expect(200)
|
202 | .expect({"coll_name": coll_name + "Changed", "msg": "Collection successfully deleted"}, done);
|
203 | });
|
204 |
|
205 | it("Remove the database",function(done){
|
206 | agent
|
207 | .post("/" + conn_name + "/db_delete")
|
208 | .send({"db_name" : db_name})
|
209 | .expect(200)
|
210 | .expect({"db_name": db_name, "msg": "Database successfully deleted"}, done);
|
211 | });
|
212 |
|
213 | it("Remove the connection",function(done){
|
214 | agent
|
215 | .post("/drop_config")
|
216 | .send({"curr_config": conn_name})
|
217 | .expect(200)
|
218 | .expect({"msg": "Config successfully deleted"}, done);
|
219 | });
|
220 | }); |
\ | No newline at end of file |