10Duke Scale C++ Client
Loading...
Searching...
No Matches
OAuthPKCEFlow_test.h
1#ifndef TENDUKE_TEST_OAUTHPKCEFLOW_TEST_H
2#define TENDUKE_TEST_OAUTHPKCEFLOW_TEST_H
3
4#include "oauth/pkce/OAuthPKCEFlow.h"
5#include "utl/DefaultBase64Encoder.h"
6#include "utl/random/RandomURLSafeString.h"
7
8#include "URLMockingTest.h"
9#include "mocks/MessageDigestMock.h"
10#include "mocks/HTTPCallMock.h"
11#include "mocks/HTTPClientMock.h"
12#include "mocks/JSONObjectMock.h"
13#include "mocks/JSONParserMock.h"
14#include "mocks/JSONNumberMock.h"
15#include "mocks/JSONStringMock.h"
16#include "mocks/ClockMock.h"
17#include "mocks/RandomBytesMock.h"
18#include "mocks/URLMock.h"
19#include "mocks/URLEncoderMock.h"
20#include "utils/BinaryDataUtils.h"
21
22#include "gmock/gmock.h"
23#include "gtest/gtest.h"
24
25#include <cstring>
26
27namespace xdcrypto = tenduke::crypto;
28namespace xdhttp = tenduke::http;
29namespace xdnet = tenduke::net;
30namespace xdoauth = tenduke::oauth;
32namespace xdutl = tenduke::utl;
33
34namespace xdmock = tenduke::test::mocks;
35
45using tenduke::test::utils::makeFixedSizeBinaryData;
46
47using ::testing::_;
48using ::testing::StrEq;
49
50using ::testing::Invoke;
51using ::testing::Return;
52using ::testing::ReturnRef;
53
54
55namespace tenduke { namespace test { namespace oauth {
56
57void generateRandomBytes(unsigned char * buffer, std::size_t len)
58{
59 switch (len) {
60 case 16:
61 std::strcpy((char *) buffer, "simulated-state"); break;
62 case 64:
63 std::strcpy((char *) buffer, "simulated-code-generator-value-on-which-sha256-will-be-applied."); break;
64 }
65}
66
67static void fakeSha256(unsigned char * digest)
68{
69 xdmock::generateFakeSha256Hash(digest, "0123456789ABCDEF0123456789ABCDEF");
70}
71
73{
74protected:
75 void SetUp() override
76 {
77 ::URLMockingTest::SetUp();
78
79 httpClient = std::shared_ptr<xdmock::HTTPClientMock>(new xdmock::HTTPClientMock(urlService));
80 jsonParser = std::shared_ptr<xdmock::JSONParserMock>(new xdmock::JSONParserMock());
81 base64Encoder = std::shared_ptr<xdutl::Base64Encoder>(new xdutl::DefaultBase64Encoder());
82 messageDigestFactory = std::shared_ptr<xdmock::MessageDigestFactoryMock>(new xdmock::MessageDigestFactoryMock());
83 clock = std::shared_ptr<xdmock::ClockMock>(new xdmock::ClockMock());
84 randomBytes = std::shared_ptr<xdmock::RandomBytesMock>(new xdmock::RandomBytesMock());
85
86 oauth = std::unique_ptr<xdpkce::OAuthPKCEFlow>(new xdpkce::OAuthPKCEFlow(
87 std::shared_ptr<xdoauth::OAuthConfiguration>(new xdoauth::OAuthConfiguration("/authz", "/token", "client-id", "", "/redir", tenduke::oauth::OAuthClientConfiguration::PKCE)),
88 httpClient,
89 urlService,
90 jsonParser,
91 base64Encoder,
92 messageDigestFactory,
93 clock,
94 std::shared_ptr<tenduke::utl::random::RandomUrlSafeString>(new tenduke::utl::random::RandomUrlSafeString(randomBytes))
95 ));
96
97 EXPECT_CALL(*randomBytes, generate(_, _))
98 .WillRepeatedly(Invoke(generateRandomBytes));
99 EXPECT_CALL(*httpClient, request())
100 .WillRepeatedly(Return(xdhttp::HTTPRequestBuilder(*urlService, httpClient.get())));
101 }
102
103 void stubForBuildAuthorizationUrl()
104 {
105 mockDefaultURLParsing();
106 mockIdentityURLEncoding();
107 mockParse("/authz", ::URL("", "", "", "", 0, "/authz", {}, ""));
108
109 digest = new xdmock::MessageDigestMock();
110 EXPECT_CALL(*messageDigestFactory, create(xdcrypto::MessageDigest::SHA256))
111 .Times(1)
112 .WillOnce(Return(std::unique_ptr<xdcrypto::MessageDigest>(digest)));
113 EXPECT_CALL(*digest, update(_, _))
114 .Times(1)
115 .WillOnce(Return(digest));
116 EXPECT_CALL(*digest, digestTo(_))
117 .Times(1)
118 .WillOnce(Invoke(fakeSha256));
119 }
120
121 void stubForTokenResponse()
122 {
123 auto responseBody = ::makeFixedSizeBinaryData("simulated-response-body");
124 auto tokenResponse = new ::HTTPResponse(200, {}, std::move(responseBody));
125
126 auto tokenRequestCall = new ::HTTPCallMock();
127 EXPECT_CALL(*httpClient, request()).WillRepeatedly(::Return(xdhttp::HTTPRequestBuilder(*urlService, httpClient.get())));
128 EXPECT_CALL(*httpClient, call(::_)).WillOnce(::Return(std::unique_ptr<::HTTPCallMock>(tokenRequestCall)));
129 EXPECT_CALL(*tokenRequestCall, execute()).WillOnce(::Return(std::unique_ptr<::HTTPResponse>(tokenResponse)));
130
131 auto json = ::JSONObjectMock::create();
132 auto accessToken = ::JSONStringMock::createShared("simulated-access-token");
133 auto refreshToken = ::JSONStringMock::createShared("simulated-refresh-token");
134 auto expiresIn = ::JSONNumberMock::createShared(42);
135 auto additionalProperty1 = ::JSONStringMock::createShared("value-1");
136 auto additionalProperty2 = ::JSONStringMock::createShared("value-2");
137 additionalTokenResponseProperties.emplace("name-1", additionalProperty1);
138 additionalTokenResponseProperties.emplace("name-2", additionalProperty2);
139
140 EXPECT_CALL(*jsonParser, from("simulated-response-body")).WillOnce(::Return(std::unique_ptr<::JSONObjectMock>(json)));
141 EXPECT_CALL(*json, removeProperty("access_token")).WillOnce(::Return(accessToken));
142 EXPECT_CALL(*json, removeProperty("refresh_token")).WillOnce(::Return(refreshToken));
143 EXPECT_CALL(*json, removeProperty("expires_in")).WillOnce(::Return(expiresIn));
144 EXPECT_CALL(*json, getProperties()).WillOnce(::ReturnRef(additionalTokenResponseProperties));
145
146 EXPECT_CALL(*clock, epochSeconds()).WillRepeatedly(::Return(100));
147 }
148
150
151 std::map<std::string, std::shared_ptr<::JSONElement>> additionalTokenResponseProperties;
152 std::shared_ptr<xdmock::HTTPClientMock> httpClient;
153 std::shared_ptr<xdmock::JSONParserMock> jsonParser;
154 std::shared_ptr<xdutl::Base64Encoder> base64Encoder;
155 std::shared_ptr<xdmock::MessageDigestFactoryMock> messageDigestFactory;
156 std::shared_ptr<xdmock::ClockMock> clock;
157 std::shared_ptr<xdmock::RandomBytesMock> randomBytes;
158
159 std::unique_ptr<xdpkce::OAuthPKCEFlow> oauth;
160}; // OAuthPKCEFlowTest
161}}} // namespace
162
163
164#endif //TENDUKE_TEST_OAUTHPKCEFLOW_TEST_H
Builds HTTPRequest.
Definition HTTPRequestBuilder.h:23
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
OAuth Authorization Code Grant with PKCE implementation of tenduke::oauth::OAuthClient.
Definition OAuthPKCEFlow.h:35
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 MessageDigestMock.h:23
Definition MessageDigestMock.h:13
Definition RandomBytesMock.h:11
Definition URLEncoderMock.h:13
Definition OAuthPKCEFlow_test.h:73
Default tenduke::utl::Base64Encoder implementation.
Definition DefaultBase64Encoder.h:13
Generates string of random URL-safe characters.
Definition RandomURLSafeString.h:17
Cryptography services.
Definition CryptoException.h:7
HTTP-related services.
Definition BadRequest.h:6
JSON support.
Definition JSONArray.h:10
Generic networking support.
Definition AbstractURLCodec.h:6
Implementation of OAuth "Authorization Code Flow with PKCE".
Definition OAuthPKCEFlow.h:21
OAuth services.
Definition AccessTokenRequestAuthenticator.h:8
Utilities.
Definition Base64Decoder.h:10
Root for classes, functions and globals of 10Duke C++ Client.
Definition AbstractClientFactory.h:16