Browse code
token installer handles user not found
Greg Wiley authored on 03/05/2017 22:16:30
Showing 2 changed files
Showing 2 changed files
... | ... |
@@ -19,7 +19,13 @@ class impl : public installer_ifc { |
19 | 19 |
const directory &directory, const installer_ifc::generator &generator) : |
20 | 20 |
tokens_(tokens), unistd_(unistd), directory_(directory), generator_(generator) {} |
21 | 21 |
std::string install_token() const override { |
22 |
- std::string user_name = unistd_.getlogin(); |
|
22 |
+ const char *c_user_name = unistd_.getlogin(); |
|
23 |
+ if (c_user_name == nullptr) { |
|
24 |
+ return ""; |
|
25 |
+ } |
|
26 |
+ |
|
27 |
+ std::string user_name = c_user_name; |
|
28 |
+ |
|
23 | 29 |
auto found_user = directory_.find_user(user_name); |
24 | 30 |
user user(found_user[0]); |
25 | 31 |
std::string token(generator_()); |
... | ... |
@@ -26,6 +26,13 @@ class fake_unistd : public unistd_ifc { |
26 | 26 |
} |
27 | 27 |
}; |
28 | 28 |
|
29 |
+class fail_unistd : public unistd_ifc { |
|
30 |
+ public: |
|
31 |
+ const char *getlogin() const override { |
|
32 |
+ return nullptr; |
|
33 |
+ } |
|
34 |
+}; |
|
35 |
+ |
|
29 | 36 |
class fake_directory : public directory_ifc { |
30 | 37 |
private: |
31 | 38 |
std::string expected_user_name_; |
... | ... |
@@ -63,10 +70,30 @@ int installs_token() { |
63 | 70 |
succeed(); |
64 | 71 |
} |
65 | 72 |
|
73 |
+int unistd_does_not_find_user_name() { |
|
74 |
+ //given |
|
75 |
+ std::string user_name("user"); |
|
76 |
+ std::string token("token"); |
|
77 |
+ auto test_tokens = std::make_shared<mock_tokens>(); |
|
78 |
+ tokens tokens{test_tokens}; |
|
79 |
+ unistd unistd(std::make_shared<fail_unistd>()); |
|
80 |
+ directory directory(std::make_shared<fake_directory>(user_name)); |
|
81 |
+ installer_ifc::generator generator = [&] { return token; }; |
|
82 |
+ |
|
83 |
+ installer installer = installer::create (tokens, unistd, directory, generator); |
|
84 |
+ |
|
85 |
+ |
|
86 |
+ //when |
|
87 |
+ installer.install_token(); |
|
66 | 88 |
|
89 |
+ //then |
|
90 |
+ check(test_tokens->captured_token == "", "should not have installed a token"); |
|
91 |
+ succeed(); |
|
92 |
+} |
|
67 | 93 |
|
68 | 94 |
int run_tests() { |
69 | 95 |
test(installs_token); |
96 |
+ test(unistd_does_not_find_user_name); |
|
70 | 97 |
succeed(); |
71 | 98 |
} |
72 | 99 |
|