Bläddra i källkod

Add unit tests for /user/register/ path

Matt Coles 9 år sedan
förälder
incheckning
b78916b61f
4 ändrade filer med 88 tillägg och 6 borttagningar
  1. 22 0
      README.md
  2. 5 1
      package.json
  3. 58 0
      spec/user-ops-spec.js
  4. 3 5
      utils/flushdb.js

+ 22 - 0
README.md

@@ -9,6 +9,7 @@ Server for Integrated Project, powered by Express.js and Redis, listens for HTTP
9 9
 * [uni-society-manager](#uni-society-manager)
10 10
     * [Installation](#installation)
11 11
     * [Running](#running)
12
+    * [Testing](#testing)
12 13
 * [API](#api)
13 14
     * __Misc__
14 15
         * [/hello/:name/](#helloname)
@@ -73,6 +74,27 @@ node index.js -p
73 74
 Do note that this requires both cert.pem and key.pem to be in the root directory
74 75
 of the project for SSL or it will not start.
75 76
 
77
+### Testing
78
+To test, start a __new__ Redis server somewhere other than the main database
79
+with:
80
+```
81
+redis-server
82
+```
83
+
84
+Then start up the Express framework using:
85
+```
86
+node index.js
87
+```
88
+You do not need to use the production environment for this.
89
+
90
+Finally run the tests with:
91
+```
92
+npm test
93
+```
94
+__DO NOT__ run `npm test` whilst the main database is running the testing command
95
+flushes the database at the end of the test and this will occur regardless of
96
+test passes or failures.
97
+
76 98
 # API
77 99
 
78 100
 ### /hello/:name/

+ 5 - 1
package.json

@@ -4,7 +4,7 @@
4 4
   "description": "Server for the IP project",
5 5
   "main": "index.js",
6 6
   "scripts": {
7
-    "test": "node index.js"
7
+    "test": "jasmine-node --verbose --captureExceptions spec; node utils/flushdb.js"
8 8
   },
9 9
   "repository": {
10 10
     "type": "git",
@@ -22,6 +22,10 @@
22 22
     "magic-window": "^1.0.4",
23 23
     "morgan": "^1.7.0",
24 24
     "node-schedule": "^1.1.0",
25
+    "request": "^2.71.0",
25 26
     "serve-index": "^1.7.3"
27
+  },
28
+  "devDependencies": {
29
+    "jasmine-node": "^1.14.5"
26 30
   }
27 31
 }

+ 58 - 0
spec/user-ops-spec.js

@@ -0,0 +1,58 @@
1
+var request = require("request");
2
+var base_url = "http://localhost:3000";
3
+
4
+describe("User Operations", function () {
5
+
6
+  var register_user = {
7
+    url: base_url + "/user/register",
8
+    method: "POST",
9
+    json: {
10
+      user: "foo123",
11
+      password: "foofoo"
12
+    }
13
+  };
14
+
15
+  var user_ops_auth_key = "";
16
+
17
+  describe("POST /user/register/", function () {
18
+    it("can register user", function (done) {
19
+      request(register_user, function (error, response, body) {
20
+        expect(response.statusCode).toBe(200);
21
+        expect(body.registered).toBe(1);
22
+        if (body["auth-key"]) {
23
+          expect(body["auth-key"].length).toBe(60);
24
+        }
25
+        expect(body.error).toBe(0);
26
+        done();
27
+      });
28
+    });
29
+
30
+    it ("cannot register the same user twice", function (done) {
31
+      request(register_user, function (error, response, body) {
32
+        expect(response.statusCode).toBe(200);
33
+        expect(body.registered).toBe(0);
34
+        expect(body["auth-key"]).toBe(undefined);
35
+        expect(body.error).toBe(1);
36
+        done();
37
+      });
38
+    });
39
+
40
+    it ("cannot accept malformed requests", function (done) {
41
+      request({
42
+        url: base_url + "/user/register/",
43
+        method: "POST",
44
+        json: {
45
+          usr: "foo123",
46
+          password: "foofoo"
47
+        }
48
+      }, function (error, response, body) {
49
+        expect(response.statusCode).toBe(200);
50
+        expect(body.registered).toBe(0);
51
+        expect(body["auth-key"]).toBe(undefined);
52
+        expect(body.error).toBe(2);
53
+        done();
54
+      });
55
+    })
56
+  });
57
+
58
+});

+ 3 - 5
utils/flushdb.js

@@ -1,8 +1,6 @@
1 1
 var Redis = require("ioredis");
2 2
 var redis = new Redis();
3 3
 
4
-module.exports = {
5
-  flush: function () {
6
-    redis.flushdb();
7
-  }
8
-}
4
+redis.flushdb();
5
+console.log("Database flushed!");
6
+setTimeout(function() {process.exit(0)}, 1000);