10Duke Scale C++ Client
Loading...
Searching...
No Matches
DefaultOAuthDeviceFlowAuthorizationRequest_test.h
1#ifndef TENDUKE_TEST_OAUTH_DEVICE_DEFAULTOAUTHDEVICEFLOWAUTHORIZATIONREQUESTTEST_H
2#define TENDUKE_TEST_OAUTH_DEVICE_DEFAULTOAUTHDEVICEFLOWAUTHORIZATIONREQUESTTEST_H
3
4#include "oauth/device/DefaultOAuthDeviceFlowAuthorizationRequest.h"
5
6#include "URLMockingTest.h"
7#include "mocks/ClockMock.h"
8#include "mocks/HTTPCallMock.h"
9#include "mocks/HTTPClientMock.h"
10#include "mocks/JSONNumberMock.h"
11#include "mocks/JSONObjectMock.h"
12#include "mocks/JSONParserMock.h"
13#include "mocks/JSONStringMock.h"
14#include "mocks/URLEncoderMock.h"
15#include "utils/BinaryDataUtils.h"
16
17#include "gtest/gtest.h"
18#include "gmock/gmock.h"
19
20
25using ::tenduke::net::URL;
37using tenduke::test::utils::makeFixedSizeBinaryData;
38
39using ::testing::_;
40using ::testing::Return;
41using ::testing::ReturnRef;
42
43namespace tenduke { namespace test {namespace oauth { namespace device {
44
46{
47public:
48 FakeRequest(
49 const std::string &scope,
50 const std::map<std::string, std::string> &additionalParameters,
51 uint64_t timeoutS,
52 const std::shared_ptr<const tenduke::oauth::OAuthConfiguration> &configuration,
53 const std::shared_ptr<tenduke::time::Clock> &clock,
54 const std::shared_ptr<const tenduke::http::HTTPClient> &httpClient,
55 const std::shared_ptr<const tenduke::http::HTTPResponseToException> &throwException,
56 const std::shared_ptr<const tenduke::json::JSONParser> &jsonParser
58 scope,
59 additionalParameters,
60 timeoutS,
61 configuration,
62 clock,
63 httpClient,
64 throwException,
65 jsonParser
66 )
67 {}
68
69 std::vector<std::chrono::milliseconds> sleepTimes;
70
71 void letMeSleep()
72 {
74 }
75
76protected:
77 void sleepFor(std::chrono::milliseconds slumbertime) override
78 {
79 sleepTimes.emplace_back(slumbertime);
80 }
81};
82
84{
85protected:
86 FakeRequest * mkRequest(const std::uint64_t localTimeoutS = 100)
87 {
88 std::map<std::string, std::string> params;
89 params.emplace("key-1", "value-1");
90 params.emplace("key-2", "value-2");
91
92 return new FakeRequest(
93 "profile",
94 params,
95 localTimeoutS,
96 std::make_shared<::OAuthConfiguration>(
97 "/authz",
98 "/token",
99 "cl-id",
100 "",
101 "",
102 ::OAuthConfiguration::DEVICE,
103 "/device"
104 ),
105 clock,
106 httpClient,
107 std::make_shared<::HTTPResponseToException>(),
108 jsonParser
109 );
110 }
111
112 void SetUp() override
113 {
114 ::URLMockingTest::SetUp();
115
116 clock = ::ClockMock::createShared();
117 httpClient = ::HTTPClientMock::createShared(urlService);
118 jsonParser = ::JSONParserMock::createShared();
119 urlEncoder = ::URLEncoderMock::createShared();
120
121 request.reset(mkRequest());
122
123 mockIdentityURLEncoding();
124 mockDefaultURLParsing();
125 }
126
127 std::shared_ptr<FakeRequest> request;
128 std::shared_ptr<::ClockMock> clock;
129 std::shared_ptr<::HTTPClientMock> httpClient;
130 std::shared_ptr<::JSONParserMock> jsonParser;
131 std::shared_ptr<::URLEncoderMock> urlEncoder;
132
133 std::map<std::string, std::shared_ptr<::JSONElement>> tokenResponseProps;
134
135 void stubAuthorizationRequest(
136 const std::int64_t expiresIn = 42,
137 const std::int64_t interval = 69
138 )
139 {
140 mockParse("/device", ::URL("", "", "", "", 0, "/device", {}, ""));
141
142 auto document = ::JSONObjectMock::create();
143 EXPECT_CALL(*jsonParser, from("successful-authorization-request")).WillOnce(::Return(std::unique_ptr<::JSONObjectMock>(document)));
144 EXPECT_CALL(*document, getProperty("device_code")).WillRepeatedly(::Return(::JSONStringMock::createShared("-device-code-")));
145 EXPECT_CALL(*document, getProperty("user_code")).WillRepeatedly(::Return(::JSONStringMock::createShared("-user-code-")));
146 EXPECT_CALL(*document, getProperty("verification_uri")).WillRepeatedly(::Return(::JSONStringMock::createShared("/verify")));
147 EXPECT_CALL(*document, getProperty("verification_uri_complete")).WillRepeatedly(::Return(::JSONStringMock::createShared("/verify-complete")));
148 EXPECT_CALL(*document, getProperty("expires_in")).WillRepeatedly(::Return(::JSONNumberMock::createShared(expiresIn)));
149 if (interval > 0) {
150 EXPECT_CALL(*document, getProperty("interval")).WillRepeatedly(::Return(::JSONNumberMock::createShared(interval)));
151 }
152 else {
153 EXPECT_CALL(*document, getProperty("interval")).WillRepeatedly(::Return(nullptr));
154 }
155 }
156 void stubSuccessfulTokenRequest()
157 {
158 auto document = ::JSONObjectMock::create();
159 EXPECT_CALL(*jsonParser, from("successful-token-request")).WillOnce(::Return(std::unique_ptr<::JSONObjectMock>(document)));
160 EXPECT_CALL(*document, removeProperty("access_token")).WillOnce(::Return(::JSONStringMock::createShared("at")));
161 EXPECT_CALL(*document, removeProperty("refresh_token")).WillOnce(::Return(::JSONStringMock::createShared("rt")));
162 EXPECT_CALL(*document, removeProperty("expires_in")).WillOnce(::Return(::JSONNumberMock::createShared(77)));
163
164 tokenResponseProps.emplace("key-1", ::JSONStringMock::createShared("value-1"));
165 tokenResponseProps.emplace("key-2", ::JSONStringMock::createShared("value-2"));
166
167 EXPECT_CALL(*document, getProperties()).WillOnce(::ReturnRef(tokenResponseProps));
168 }
169 void stubErrorResponses(
170 const unsigned int numTimes,
171 const std::string jsonPayload,
172 const std::string error
173 ) {
174 auto &documentStub = EXPECT_CALL(*jsonParser, from(jsonPayload));
175 for (unsigned int i = 0; i < numTimes; i++) {
176 auto document = ::JSONObjectMock::create();
177 documentStub.WillOnce(::Return(std::unique_ptr<::JSONObjectMock>(document)));
178 EXPECT_CALL(*document, getProperty("error")).WillRepeatedly(::Return(::JSONStringMock::createShared(error)));
179 EXPECT_CALL(*document, getProperty("error_description")).WillRepeatedly(::Return(::JSONStringMock::createShared("simulated_error")));
180 }
181 }
182 void stubAuthorizationPending(const unsigned int numTimes) {
183 stubErrorResponses(numTimes, "authorization-pending", "authorization_pending");
184 }
185 void stubSlowDown(const unsigned int numTimes)
186 {
187 stubErrorResponses(numTimes, "slow-down", "slow_down");
188 }
189};
190}}}}
191
192
193#endif //TENDUKE_TEST_OAUTH_DEVICE_DEFAULTOAUTHDEVICEFLOWAUTHORIZATIONREQUESTTEST_H
virtual void sleepFor(std::chrono::milliseconds slumbertime)
Sleeps for given time.
Definition DefaultOAuthDeviceFlowAuthorizationRequest.cpp:166
static std::shared_ptr< URLEncoderMock > createShared()
Makes identity version.
Definition URLEncoderMock.h:18
Builds HTTPRequest.
Definition HTTPRequestBuilder.h:23
Utility service to throw an exception based on tenduke::http::HTTPResponse.
Definition HTTPResponseToException.h:18
A HTTP Response.
Definition HTTPResponse.h:15
Superclass of JSON elements.
Definition JSONElement.h:12
Represents URL.
Definition URL.h:21
Container for OAuth-configuration.
Definition OAuthConfiguration.h:17
Default implementation of tenduke::oauth::device::OAuthDeviceFlowAuthorizationRequest.
Definition DefaultOAuthDeviceFlowAuthorizationRequest.h:27
DefaultOAuthDeviceFlowAuthorizationRequest(std::string scope, const std::map< std::string, std::string > &additionalParameters, std::uint64_t timeoutS, const std::shared_ptr< const tenduke::oauth::OAuthConfiguration > &configuration, const std::shared_ptr< tenduke::time::Clock > &clock, const std::shared_ptr< const tenduke::http::HTTPClient > &httpClient, const std::shared_ptr< const tenduke::http::HTTPResponseToException > &throwException, const std::shared_ptr< const tenduke::json::JSONParser > &jsonParser)
Constructs new instance.
Definition DefaultOAuthDeviceFlowAuthorizationRequest.cpp:171
Definition URLMockingTest.h:14
Definition ClockMock.h:13
Definition HTTPCallMock.h:10
Definition HTTPClientMock.h:14
Definition JSONNumberMock.h:10
Definition JSONObjectMock.h:10
Definition JSONParserMock.h:11
Definition JSONStringMock.h:10
Definition URLEncoderMock.h:13
Definition DefaultOAuthDeviceFlowAuthorizationRequest_test.h:84
void sleepFor(std::chrono::milliseconds slumbertime) override
Sleeps for given time.
Definition DefaultOAuthDeviceFlowAuthorizationRequest_test.h:77
Root for classes, functions and globals of 10Duke C++ Client.
Definition AbstractClientFactory.h:16