git.fiddlerwoaroof.com
Browse code

validator looks up user

Greg Wiley authored on 12/04/2017 18:55:53
Showing 5 changed files
... ...
@@ -48,8 +48,7 @@ validator_test: validator_test.o t_validator.o
48 48
 
49 49
 
50 50
 .PHONY: test
51
-test: user_test validator_test
52
-	@./user_test
51
+test: validator_test
53 52
 	@./validator_test
54 53
 
55 54
 
... ...
@@ -6,17 +6,19 @@
6 6
 #include <pwd.h>
7 7
 
8 8
 class user {
9
-    private:
10
-        struct passwd *info;
11
-
12 9
     public:
13
-        user(struct passwd *sys_info);
14
-        std::string home_directory();
10
+        virtual ~user() {}
11
+//        virtual std::string home_directory() = 0;
15 12
 };
16 13
 
17 14
 
18
-const std::shared_ptr<user> create_user(const std::string &user_name);
19 15
 typedef std::shared_ptr<user> user_p;
16
+class directory {
17
+    public:
18
+        virtual ~directory() {}
19
+        virtual const user_p find_user(const std::string &user_name) = 0;
20
+};
20 21
 
22
+typedef std::shared_ptr<directory> directory_p;
21 23
 #endif
22 24
 
... ...
@@ -3,6 +3,6 @@
3 3
 #include "validator.h"
4 4
 
5 5
 bool validator::validate(const std::string &user_name, const std::string &token) {
6
-    return user_name == "user" && token == "token";
6
+    return directory_->find_user(user_name) && token == "token";
7 7
 }
8 8
 
... ...
@@ -3,8 +3,13 @@
3 3
 
4 4
 #include <string>
5 5
 
6
+#include "user.h"
7
+
6 8
 class validator {
9
+    private:
10
+        directory_p directory_;
7 11
     public:
12
+        validator(const directory_p &directory): directory_(directory) {}
8 13
         bool validate(const std::string &user, const std::string &token);
9 14
 };
10 15
 
... ...
@@ -1,29 +1,51 @@
1 1
 #include "validator.h"
2
+#include "user.h"
2 3
 #include "test_util.h"
3 4
 
5
+class fake_directory : public directory {
6
+    private:
7
+        std::string user_name_;
8
+    public:
9
+        fake_directory(const std::string &user_name) : user_name_(user_name) {}
10
+        fake_directory() : user_name_("_NOT_A_USER") {}
11
+
12
+        virtual const user_p find_user(const std::string &user_name) {
13
+            user_p result;
14
+            if (user_name == user_name_) {
15
+                result.reset(new user);
16
+            }
17
+
18
+            return result;
19
+        }
20
+};
21
+
22
+
4 23
 bool validator_validates() {
5 24
 
6 25
    // given
7
-   validator v;
26
+    std::string user_name = "msmith";
27
+    directory_p directory(new fake_directory(user_name));
28
+    validator validator(directory);
8 29
 
9 30
 
10
-   // when
11
-   bool actual = v.validate("user", "token");
31
+    // when
32
+    bool actual = validator.validate(user_name, "token");
12 33
 
13 34
 
14
-   // then
15
-   check(actual, "should be valid");
16
-   succeed();
35
+    // then
36
+    check(actual, "should be valid");
37
+    succeed();
17 38
 }
18 39
 
19 40
 bool validator_fails_unknown_user() {
20 41
 
21 42
    // given
22
-   validator v;
43
+   directory_p directory(new fake_directory);
44
+   validator validator(directory);
23 45
 
24 46
 
25 47
    // when
26
-   bool actual = v.validate("notuser", "token");
48
+   bool actual = validator.validate("notuser", "token");
27 49
 
28 50
 
29 51
    // then
... ...
@@ -34,11 +56,13 @@ bool validator_fails_unknown_user() {
34 56
 bool validator_fails_incorrect_token() {
35 57
 
36 58
    // given
37
-   validator v;
59
+    std::string user_name = "user";
60
+    directory_p directory(new fake_directory(user_name));
61
+    validator validator(directory);
38 62
 
39 63
 
40 64
    // when
41
-   bool actual = v.validate("user", "nottoken");
65
+   bool actual = validator.validate(user_name, "nottoken");
42 66
 
43 67
 
44 68
    // then