git.fiddlerwoaroof.com
validator_test.cc
7684972a
 /* Copyright (C) CJ Affiliate
  *
  * You may use, distribute and modify this code under  the
  * terms of the  GNU General Public License  version 2  or
  * later.
  *
  * You should have received a copy of the license with this
  * file. If not, you will find a copy in the "LICENSE" file
  * at https://github.com/cjdev/dual-control.
  */
 
28855478
 #include <string>
 #include <vector>
 
f757e35b
 #include "validator.h"
3c673060
 #include "user.h"
f757e35b
 #include "test_util.h"
9af49688
 #include "token.h"
f757e35b
 
28855478
 class fake_directory : public directory_ifc
cdf7fd74
 {
 private:
     std::string user_name_;
 public:
28855478
     fake_directory (const std::string &user_name) : user_name_ (user_name)
     {
     }
b017a4d2
     fake_directory() : user_name_ ("_NOT_A_USER") {}
cdf7fd74
 
28855478
     std::vector<user> find_user (const std::string &user_name)
cdf7fd74
     {
28855478
         std::vector<user> result;
b017a4d2
 
cdf7fd74
         if (user_name == user_name_) {
28855478
             result.push_back (user());
3c673060
         }
0e218820
 
cdf7fd74
         return result;
     }
3c673060
 };
 
cdf7fd74
 class fake_user_token_supplier : public user_token_supplier
 {
 private:
     std::string token_;
 public:
b017a4d2
     fake_user_token_supplier (const std::string &token) : token_ (token) {}
     fake_user_token_supplier() : token_ ("_NOT_A_TOKEN") {}
bef74c28
     std::string token (user &user)
cdf7fd74
     {
         return token_;
     }
9af49688
 };
 
28855478
 template<class T>
 std::shared_ptr<T> share (T *t)
 {
     return std::shared_ptr<T> (t);
 }
 
cdf7fd74
 bool validator_validates()
 {
f757e35b
 
9af49688
     // given
     std::string token = "token";
b65518d3
     user_token_supplier user_token_supplier (share (new
             fake_user_token_supplier (token)));
3c673060
     std::string user_name = "msmith";
28855478
     directory directory (share (new fake_directory (user_name)));
ee12703f
     validator validator = create_validator (directory, user_token_supplier);
f757e35b
 
3c673060
     // when
3a7bcdbe
     bool actual = validator.validate (user_name, token);
f757e35b
 
3c673060
     // then
b017a4d2
     check (actual, "should be valid");
3c673060
     succeed();
92b68213
 }
 
cdf7fd74
 bool validator_fails_unknown_user()
 {
92b68213
 
cdf7fd74
     // given
     std::string token = "token";
b65518d3
     user_token_supplier user_token_supplier (share (new
             fake_user_token_supplier));
28855478
     directory directory (share (new fake_directory));
ee12703f
     validator validator = create_validator (directory, user_token_supplier);
92b68213
 
cdf7fd74
     // when
b017a4d2
     bool actual = validator.validate ("notuser", token);
92b68213
 
cdf7fd74
     // then
b017a4d2
     check (!actual, "should not be valid");
cdf7fd74
     succeed();
92b68213
 }
 
cdf7fd74
 bool validator_fails_incorrect_token()
 {
92b68213
 
cdf7fd74
     // given
b65518d3
     user_token_supplier user_token_supplier (share (new
             fake_user_token_supplier));
9af49688
     std::string user_name = "msmith";
28855478
     directory directory (share (new fake_directory (user_name)));
ee12703f
     validator validator = create_validator (directory, user_token_supplier);
92b68213
 
cdf7fd74
     // when
b017a4d2
     bool actual = validator.validate (user_name, "token");
92b68213
 
cdf7fd74
     // then
b017a4d2
     check (!actual, "should not be valid");
cdf7fd74
     succeed();
f757e35b
 }
 
 RESET_VARS_START
 RESET_VARS_END
 
cdf7fd74
 bool run_tests()
 {
b017a4d2
     test (validator_validates);
     test (validator_fails_unknown_user);
     test (validator_fails_incorrect_token);
f757e35b
     succeed();
 }
 
b017a4d2
 int main (int argc, char *argv[])
cdf7fd74
 {
f757e35b
     return !run_tests();
 }