git.fiddlerwoaroof.com
Browse code

add validator class

Greg Wiley authored on 11/04/2017 23:57:56
Showing 7 changed files
... ...
@@ -42,10 +42,15 @@ t_%.o: %.cc
42 42
 user_test: user_test.o t_user.o
43 43
 	$(CXX) $(CXXFLAGS) $(CPPFLAGS)  -o $@ $^
44 44
 
45
+validator_test: validator_test.o t_validator.o
46
+	$(CXX) $(CXXFLAGS) $(CPPFLAGS)  -o $@ $^
47
+
48
+
45 49
 
46 50
 .PHONY: test
47
-test: user_test
51
+test: user_test validator_test
48 52
 	@./user_test
53
+	@./validator_test
49 54
 
50 55
 
51 56
 #	@./dual_control_test
... ...
@@ -62,13 +62,12 @@ user::user(const std::string &user_name) :
62 62
 
63 63
 int validate_token(const char *c_user_name, const char *c_token) {
64 64
 
65
-    user user(c_user_name);
66
-    if (!user.valid()) {
67
-        return 0;
68
-    }
69
-    std::string token(c_token);
65
+   user_p user(create_user(c_user_name));
66
+
67
+   validator_p validator(create_validator(user));
68
+
69
+   return validator->valid(c_token);
70 70
 
71
-    return user.token() == token;
72 71
 
73 72
 }
74 73
 
... ...
@@ -1,5 +1,18 @@
1 1
 #ifndef _TOKEN_H
2 2
 #define _TOKEN_H
3
+#include <string>
4
+#include <memory>
5
+
6
+#include "user.h"
7
+
8
+class token {
9
+       std::string value_
10
+    public:
11
+       token(const std::string &value) : value_(value) {}
12
+       std::string value() { return value_; }
13
+}
14
+
15
+shared_ptr<token> create_token(std::shared_ptr<user> user);
3 16
 
4 17
 int validate_token(const char *user, const char *token);
5 18
 
... ...
@@ -16,6 +16,7 @@ class user {
16 16
 
17 17
 
18 18
 const std::shared_ptr<user> create_user(const std::string &user_name);
19
+typedef std::shared_ptr<user> user_p;
19 20
 
20 21
 #endif
21 22
 
22 23
new file mode 100644
... ...
@@ -0,0 +1,8 @@
1
+#include <string>
2
+
3
+#include "validator.h"
4
+
5
+bool validator::validate(const std::string &user_name, const std::string &token) {
6
+    return true;
7
+}
8
+
0 9
new file mode 100644
... ...
@@ -0,0 +1,11 @@
1
+#ifndef _VALIDATOR_
2
+#define _VALIDATOR_
3
+
4
+#include <string>
5
+
6
+class validator {
7
+    public:
8
+        bool validate(const std::string &user, const std::string &token);
9
+};
10
+
11
+#endif
0 12
new file mode 100644
... ...
@@ -0,0 +1,31 @@
1
+#include "validator.h"
2
+#include "test_util.h"
3
+
4
+bool validator_validates() {
5
+
6
+   // given
7
+   validator v;
8
+
9
+
10
+   // when
11
+   bool actual = v.validate("user", "code");
12
+
13
+
14
+   // then
15
+   check(actual, "user and code should be valid");
16
+   succeed();
17
+}
18
+
19
+RESET_VARS_START
20
+RESET_VARS_END
21
+
22
+bool run_tests() {
23
+    test(validator_validates);
24
+    succeed();
25
+
26
+}
27
+
28
+int main(int argc, char *argv[]) {
29
+    return !run_tests();
30
+}
31
+