git.fiddlerwoaroof.com
token_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.
  */
 
bef74c28
 #include <memory>
8ab94c71
 #include <cstring>
69be5e4e
 #include <pwd.h>
8ab94c71
 #include <cstdio>
69be5e4e
 #include <sys/stat.h>
4bf199d2
 #include <fstream>
 #include <istream>
69be5e4e
 
de34378f
 #include "token.h"
1c7f8bf0
 #include "test_util.h"
bef74c28
 #include "user.h"
 
0e218820
 class fake_file_reader : public file_reader_ifc
 {
bd15e7c1
 private:
     std::string file_path_;
 public:
     bool open (std::ifstream &token_file, const std::string &file_path)
     {
         file_path_ = file_path;
         return true;
     }
     std::string getline (std::ifstream &token_file, std::string &fetched_token)
     {
         return file_path_;
     }
bef74c28
 };
 
ca42f792
 class file_reader_with_open_fail : public file_reader_ifc {
8ddb4fe9
     public:
         bool open (std::ifstream &token_file, std::string &fetched_token) {
             return false;
         }
 };
 
0e218820
 class fake_user : public user_ifc
 {
 private:
     std::string home_directory_;
 public:
8ddb4fe9
     fake_user() {}
0e218820
     fake_user (std::string &user_name) :
         home_directory_ ("home/" + user_name)
     {
     }
     std::string home_directory()
     {
         return home_directory_;
     }
bef74c28
 };
 
0e218820
 int reads_from_the_right_file ()
 {
cb403154
     //given
bef74c28
     std::string user_name = "user";
     std::string expected = "home/" + user_name + "/.dual_control";
8ddb4fe9
     file_reader test_file_reader (file_reader::delegate (new fake_file_reader));
0e218820
     user test_user (user::delegate (new fake_user (user_name)));
     user_token_supplier supplier (user_token_supplier::create (
                                       test_file_reader));
cb403154
 
     //when
0e218820
     std::string actual = supplier.token (test_user);
69be5e4e
 
     //then
0e218820
     check (actual == expected, "read wrong file");
69be5e4e
     succeed();
 }
 
ca42f792
 int returns_empty_string_if_file_open_fail() {
8ddb4fe9
     //given
ca42f792
     file_reader test_file_reader (file_reader::delegate (new file_reader_with_open_fail));
8ddb4fe9
     user test_user (user::delegate (new fake_user));
     user_token_supplier supplier (user_token_supplier::create (
                                       test_file_reader));
 
     //when
     std::string actual = supplier.token (test_user);
 
     //then
     check(actual == "", "should have returned empty string");
     succeed();
 }
 
bef74c28
 RESET_VARS_START
 RESET_VARS_END
6f0de833
 
0e218820
 int run_tests()
 {
     test (reads_from_the_right_file);
ca42f792
     test (returns_empty_string_if_file_open_fail);
de34378f
     succeed();
 }
 
0e218820
 int main (int argc, char *argv[])
 {
bef74c28
     return !run_tests();
de34378f
 }