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