Quellcode durchsuchen

Add unit tests for the /user/auth/ route

Matt Coles vor 9 Jahren
Ursprung
Commit
20b411d4dd
1 geänderte Dateien mit 75 neuen und 13 gelöschten Zeilen
  1. 75 13
      spec/user-ops-spec.js

+ 75 - 13
spec/user-ops-spec.js

@@ -3,20 +3,33 @@ var base_url = "http://localhost:3000";
3 3
 
4 4
 describe("User Operations", function () {
5 5
 
6
-  var register_user = {
7
-    url: base_url + "/user/register",
8
-    method: "POST",
9
-    json: {
10
-      user: "foo123",
11
-      password: "foofoo"
12
-    }
6
+  var register_user = function (username) {
7
+    return {
8
+      url: base_url + "/user/register/",
9
+      method: "POST",
10
+      json: {
11
+        user: username,
12
+        password: "foofoo"
13
+      }
14
+    };
15
+  };
16
+
17
+  var login_user = function (username, pass) {
18
+    return {
19
+      url: base_url + "/user/auth/",
20
+      method: "POST",
21
+      json: {
22
+        user: username,
23
+        password: pass
24
+      }
25
+    };
13 26
   };
14 27
 
15 28
   var user_ops_auth_key = "";
16 29
 
17 30
   describe("POST /user/register/", function () {
18 31
     it("can register user", function (done) {
19
-      request(register_user, function (error, response, body) {
32
+      request(register_user("foo123"), function (error, response, body) {
20 33
         expect(response.statusCode).toBe(200);
21 34
         expect(body.registered).toBe(1);
22 35
         if (body["auth-key"]) {
@@ -27,8 +40,8 @@ describe("User Operations", function () {
27 40
       });
28 41
     });
29 42
 
30
-    it ("cannot register the same user twice", function (done) {
31
-      request(register_user, function (error, response, body) {
43
+    it("cannot register the same user twice", function (done) {
44
+      request(register_user("foo123"), function (error, response, body) {
32 45
         expect(response.statusCode).toBe(200);
33 46
         expect(body.registered).toBe(0);
34 47
         expect(body["auth-key"]).toBe(undefined);
@@ -37,7 +50,7 @@ describe("User Operations", function () {
37 50
       });
38 51
     });
39 52
 
40
-    it ("cannot accept malformed requests", function (done) {
53
+    it("cannot accept malformed requests", function (done) {
41 54
       request({
42 55
         url: base_url + "/user/register/",
43 56
         method: "POST",
@@ -52,7 +65,56 @@ describe("User Operations", function () {
52 65
         expect(body.error).toBe(2);
53 66
         done();
54 67
       });
55
-    })
56
-  });
68
+    });
69
+  }); //end POST /user/register/
70
+
71
+  describe("POST /user/auth/", function () {
72
+    it("can login as an existing user", function (done) {
73
+      request(login_user("foo123", "foofoo"), function (error, response, body) {
74
+        expect(response.statusCode).toBe(200);
75
+        expect(body.logged_in).toBe(1);
76
+        expect(body["auth-key"].length).toBe(60);
77
+        expect(body.error).toBe(0);
78
+        done();
79
+      });
80
+    });
81
+
82
+    it("cannot login with an incorrect password", function (done) {
83
+      request(login_user("foo123", "foofo"), function (error, response, body) {
84
+        expect(response.statusCode).toBe(200);
85
+        expect(body.logged_in).toBe(0);
86
+        expect(body["auth-key"]).toBe(undefined);
87
+        expect(body.error).toBe(1);
88
+        done();
89
+      });
90
+    });
91
+
92
+    it("cannot login with an incorrect username", function (done) {
93
+      request(login_user("foo1233", "foofoo"), function (error, response, body) {
94
+        expect(response.statusCode).toBe(200);
95
+        expect(body.logged_in).toBe(0);
96
+        expect(body["auth-key"]).toBe(undefined);
97
+        expect(body.error).toBe(1);
98
+        done();
99
+      });
100
+    });
101
+
102
+    it("cannot accept malformed requests", function (done) {
103
+      request({
104
+        url: base_url + "/user/auth/",
105
+        method: "POST",
106
+        json: {
107
+          usr: "foo123",
108
+          password: "foofoo"
109
+        }
110
+      }, function (error, response, body) {
111
+        expect(response.statusCode).toBe(200);
112
+        expect(body.logged_in).toBe(0);
113
+        expect(body["auth-key"]).toBe(undefined);
114
+        expect(body.error).toBe(2);
115
+        done();
116
+      });
117
+    });
118
+  }); //end POST /user/auth/
57 119
 
58 120
 });