|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "google/cloud/internal/external_account_source_format.h" |
| 16 | +#include "google/cloud/testing_util/status_matchers.h" |
| 17 | +#include <gmock/gmock.h> |
| 18 | + |
| 19 | +namespace google { |
| 20 | +namespace cloud { |
| 21 | +namespace oauth2_internal { |
| 22 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 23 | + |
| 24 | +using ::google::cloud::testing_util::StatusIs; |
| 25 | +using ::testing::HasSubstr; |
| 26 | +using ::testing::IsSupersetOf; |
| 27 | +using ::testing::Pair; |
| 28 | + |
| 29 | +internal::ErrorContext MakeTestErrorContext() { |
| 30 | + return internal::ErrorContext{ |
| 31 | + {{"filename", "my-credentials.json"}, {"key", "value"}}}; |
| 32 | +} |
| 33 | + |
| 34 | +TEST(ParseExternalAccountSourceFormat, ValidText) { |
| 35 | + auto const creds = nlohmann::json{ |
| 36 | + {"file", "/var/run/token-file.txt"}, |
| 37 | + {"format", nlohmann::json{{"type", "text"}}}, |
| 38 | + }; |
| 39 | + auto const format = |
| 40 | + ParseExternalAccountSourceFormat(creds, MakeTestErrorContext()); |
| 41 | + ASSERT_STATUS_OK(format); |
| 42 | + EXPECT_EQ(format->type, "text"); |
| 43 | +} |
| 44 | + |
| 45 | +TEST(ParseExternalAccountSourceFormat, ValidJson) { |
| 46 | + auto const creds = nlohmann::json{ |
| 47 | + {"file", "/var/run/token-file.txt"}, |
| 48 | + {"format", nlohmann::json{{"type", "json"}, |
| 49 | + {"subject_token_field_name", "fieldName"}}}, |
| 50 | + }; |
| 51 | + auto const format = |
| 52 | + ParseExternalAccountSourceFormat(creds, MakeTestErrorContext()); |
| 53 | + ASSERT_STATUS_OK(format); |
| 54 | + EXPECT_EQ(format->type, "json"); |
| 55 | + EXPECT_EQ(format->subject_token_field_name, "fieldName"); |
| 56 | +} |
| 57 | + |
| 58 | +TEST(ParseExternalAccountSourceFormat, MissingIsText) { |
| 59 | + auto const creds = nlohmann::json{ |
| 60 | + {"file", "/var/run/token-file.txt"}, |
| 61 | + }; |
| 62 | + auto const format = |
| 63 | + ParseExternalAccountSourceFormat(creds, MakeTestErrorContext()); |
| 64 | + ASSERT_STATUS_OK(format); |
| 65 | + EXPECT_EQ(format->type, "text"); |
| 66 | +} |
| 67 | + |
| 68 | +TEST(ParseExternalAccountSourceFormat, MissingTypeIsText) { |
| 69 | + auto const creds = nlohmann::json{ |
| 70 | + {"file", "/var/run/token-file.txt"}, |
| 71 | + {"format", nlohmann::json{{"unused", "value"}}}, |
| 72 | + }; |
| 73 | + auto const format = |
| 74 | + ParseExternalAccountSourceFormat(creds, MakeTestErrorContext()); |
| 75 | + ASSERT_STATUS_OK(format); |
| 76 | + EXPECT_EQ(format->type, "text"); |
| 77 | +} |
| 78 | + |
| 79 | +TEST(ParseExternalAccountSourceFormat, InvalidFormatType) { |
| 80 | + auto const creds = nlohmann::json{ |
| 81 | + {"file", "/var/run/token-file.txt"}, |
| 82 | + {"format", {{"type", true}}}, |
| 83 | + }; |
| 84 | + auto const format = |
| 85 | + ParseExternalAccountSourceFormat(creds, MakeTestErrorContext()); |
| 86 | + EXPECT_THAT(format, StatusIs(StatusCode::kInvalidArgument, |
| 87 | + HasSubstr("invalid type for `type` field"))); |
| 88 | + EXPECT_THAT(format.status().error_info().metadata(), |
| 89 | + IsSupersetOf({Pair("filename", "my-credentials.json"), |
| 90 | + Pair("key", "value")})); |
| 91 | +} |
| 92 | + |
| 93 | +TEST(ParseExternalAccountSourceFormat, UnknownFormatType) { |
| 94 | + auto const creds = nlohmann::json{ |
| 95 | + {"file", "/var/run/token-file.txt"}, |
| 96 | + {"format", {{"type", "neither-json-nor-text"}}}, |
| 97 | + }; |
| 98 | + auto const format = |
| 99 | + ParseExternalAccountSourceFormat(creds, MakeTestErrorContext()); |
| 100 | + EXPECT_THAT(format, |
| 101 | + StatusIs(StatusCode::kInvalidArgument, |
| 102 | + HasSubstr("invalid file type <neither-json-nor-text>"))); |
| 103 | + EXPECT_THAT(format.status().error_info().metadata(), |
| 104 | + IsSupersetOf({Pair("filename", "my-credentials.json"), |
| 105 | + Pair("key", "value")})); |
| 106 | +} |
| 107 | + |
| 108 | +TEST(ParseExternalAccountSourceFormat, MissingFormatSubject) { |
| 109 | + auto const creds = nlohmann::json{ |
| 110 | + {"file", "/var/run/token-file.json"}, |
| 111 | + {"format", {{"type", "json"}}}, |
| 112 | + }; |
| 113 | + auto const format = |
| 114 | + ParseExternalAccountSourceFormat(creds, MakeTestErrorContext()); |
| 115 | + EXPECT_THAT(format, StatusIs(StatusCode::kInvalidArgument, |
| 116 | + HasSubstr("`subject_token_field_name`"))); |
| 117 | + EXPECT_THAT(format.status().error_info().metadata(), |
| 118 | + IsSupersetOf({Pair("filename", "my-credentials.json"), |
| 119 | + Pair("key", "value")})); |
| 120 | +} |
| 121 | + |
| 122 | +TEST(ParseExternalAccountSourceFormat, InvalidFormatSubject) { |
| 123 | + auto const creds = nlohmann::json{ |
| 124 | + {"file", "/var/run/token-file.json"}, |
| 125 | + {"format", {{"type", "json"}, {"subject_token_field_name", true}}}, |
| 126 | + }; |
| 127 | + auto const format = |
| 128 | + ParseExternalAccountSourceFormat(creds, MakeTestErrorContext()); |
| 129 | + EXPECT_THAT(format, StatusIs(StatusCode::kInvalidArgument, |
| 130 | + HasSubstr("`subject_token_field_name`"))); |
| 131 | + EXPECT_THAT(format.status().error_info().metadata(), |
| 132 | + IsSupersetOf({Pair("filename", "my-credentials.json"), |
| 133 | + Pair("key", "value")})); |
| 134 | +} |
| 135 | + |
| 136 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 137 | +} // namespace oauth2_internal |
| 138 | +} // namespace cloud |
| 139 | +} // namespace google |
0 commit comments