-
Notifications
You must be signed in to change notification settings - Fork 11
Add unit tests for credentials files. #125
Changes from all commits
e233de6
ff39c98
0ce5f42
49a4138
da531f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| #include "../src/environment.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| #include <fstream> | ||
| #include <sstream> | ||
| #include <boost/filesystem.hpp> | ||
|
|
||
| namespace google { | ||
|
|
||
| class EnvironmentTest : public ::testing::Test { | ||
| protected: | ||
| static void ReadApplicationDefaultCredentials(const Environment& environment) { | ||
| environment.ReadApplicationDefaultCredentials(); | ||
| } | ||
| }; | ||
|
|
||
| namespace { | ||
| // A file with a given name in a temporary (unique) directory. | ||
| boost::filesystem::path TempPath(const std::string& filename) { | ||
| boost::filesystem::path path = boost::filesystem::temp_directory_path(); | ||
| path.append(boost::filesystem::unique_path().native()); | ||
| path.append(filename); | ||
| return path; | ||
| } | ||
|
|
||
| // Creates a file for the lifetime of the object and removes it after. | ||
| class TemporaryFile { | ||
| public: | ||
| TemporaryFile(const std::string& filename, const std::string& contents) | ||
| : path_(TempPath(filename)) { | ||
| boost::filesystem::create_directories(path_.parent_path()); | ||
| SetContents(contents); | ||
| } | ||
| ~TemporaryFile() { | ||
| boost::filesystem::remove_all(path_.parent_path()); | ||
| } | ||
| void SetContents(const std::string& contents) const { | ||
| std::ofstream file(path_.native()); | ||
| file << contents << std::flush; | ||
| } | ||
| const boost::filesystem::path& FullPath() const { return path_; } | ||
| private: | ||
| boost::filesystem::path path_; | ||
| }; | ||
| } // namespace | ||
|
|
||
| TEST(TemporaryFile, Basic) { | ||
| boost::filesystem::path path; | ||
| { | ||
| TemporaryFile f("foo", "bar"); | ||
| path = f.FullPath(); | ||
| EXPECT_TRUE(boost::filesystem::exists(path)); | ||
| std::string contents; | ||
| { | ||
| std::ifstream in(path.native()); | ||
| in >> contents; | ||
| } | ||
| EXPECT_EQ("bar", contents); | ||
| f.SetContents("xyz"); | ||
| { | ||
| std::ifstream in(path.native()); | ||
| in >> contents; | ||
| } | ||
| EXPECT_EQ("xyz", contents); | ||
| } | ||
| EXPECT_FALSE(boost::filesystem::exists(path)); | ||
| } | ||
|
|
||
| TEST_F(EnvironmentTest, ReadApplicationDefaultCredentialsSucceeds) { | ||
| TemporaryFile credentials_file( | ||
| std::string(test_info_->name()) + "_creds.json", | ||
| "{\"client_email\":\"user@example.com\",\"private_key\":\"some_key\"}"); | ||
| std::string cfg; | ||
| Configuration config(std::istringstream( | ||
| "CredentialsFile: '" + credentials_file.FullPath().native() + "'\n" | ||
| )); | ||
| Environment environment(config); | ||
| EXPECT_NO_THROW(ReadApplicationDefaultCredentials(environment)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my opinion this line is not required. It will be called on any call to CredentialsClientEmail or CredentialsPrivateKey. If it throws, the test case will exit and fail.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know, but I'd like to control where the credentials file is actually read. Especially in the next test. |
||
| EXPECT_EQ("user@example.com", environment.CredentialsClientEmail()); | ||
| EXPECT_EQ("some_key", environment.CredentialsPrivateKey()); | ||
| } | ||
|
|
||
| TEST_F(EnvironmentTest, ReadApplicationDefaultCredentialsCaches) { | ||
| TemporaryFile credentials_file( | ||
| std::string(test_info_->name()) + "_creds.json", | ||
| "{\"client_email\":\"user@example.com\",\"private_key\":\"some_key\"}"); | ||
| Configuration config(std::istringstream( | ||
| "CredentialsFile: '" + credentials_file.FullPath().native() + "'\n" | ||
| )); | ||
| Environment environment(config); | ||
| EXPECT_NO_THROW(ReadApplicationDefaultCredentials(environment)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would personally change this line to something like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. This is checking that the first call to |
||
| credentials_file.SetContents( | ||
| "{\"client_email\":\"changed@example.com\",\"private_key\":\"12345\"}" | ||
| ); | ||
| EXPECT_EQ("user@example.com", environment.CredentialsClientEmail()); | ||
| credentials_file.SetContents( | ||
| "{\"client_email\":\"extra@example.com\",\"private_key\":\"09876\"}" | ||
| ); | ||
| EXPECT_EQ("some_key", environment.CredentialsPrivateKey()); | ||
| } | ||
| } // namespace google | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where does
test_info_come from?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test_info_is provided by gtest.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Umm, yeah, except the preferred way to access it seems to be by calling
current_test_info()onUnitTest. I'll send a separate PR with a fix.