10Duke Scale C++ Client
Loading...
Searching...
No Matches
OIDCSessionImpl_test.h
1#ifndef TENDUKE_TEST_OIDC_OIDCSESSIONIMPL_TEST_H
2#define TENDUKE_TEST_OIDC_OIDCSESSIONIMPL_TEST_H
3
4#include "oidc/session/OIDCSessionImpl.h"
5#include "oidc/session/DefaultOIDCSessionEventListener.h"
6#include "oauth/OAuthInvalidGrant.h"
7
8#include "mocks/ClockMock.h"
9#include "mocks/OIDCClientMock.h"
10#include "mocks/OIDCLoginMock.h"
11#include "mocks/OIDCLoginRequestMock.h"
12#include "mocks/OIDCRefreshRequestMock.h"
13
14#include "gmock/gmock.h"
15#include "gtest/gtest.h"
16
17#include <thread>
18
30
31using ::testing::_;
32using ::testing::Return;
33using ::testing::Throw;
34
35namespace tenduke { namespace test { namespace oidc {
36
38{
39public:
40 void loginStarting() override
41 {
42 DefaultOIDCSessionEventListener::loginStarting();
43 loginStartingCalled++;
44 }
45
46 void loginComplete(const OIDCState &state) override
47 {
48 DefaultOIDCSessionEventListener::loginComplete(state);
49 loginCompleteAccessTokens.emplace_back(state.getAccessToken());
50 }
51
52 void refreshComplete(const OIDCState &state) override
53 {
54 DefaultOIDCSessionEventListener::refreshComplete(state);
55 refreshCompleteAccessTokens.emplace_back(state.getAccessToken());
56 }
57
58 int loginStartingCalled = 0;
59 std::vector<std::string> loginCompleteAccessTokens;
60 std::vector<std::string> refreshCompleteAccessTokens;
61};
62
64{
65public:
66 explicit FakeOIDCLoginRequest(const std::chrono::milliseconds &sleepFor)
67 : sleepFor(sleepFor), abortCalled(0)
68 {}
69
70 std::unique_ptr<::OIDCState> execute() override
71 {
72 std::this_thread::sleep_for(sleepFor);
73 return std::unique_ptr<::OIDCState>(new ::OIDCState("at", "rt", 200, false, {}, ::IdToken({}, {})));
74 }
75 void abort() override
76 {
77 abortCalled += 1;
78 }
79
80 std::chrono::milliseconds sleepFor;
81 volatile std::uint64_t abortCalled;
82};
83
84class OIDCSessionImplTest : public ::testing::Test
85{
86protected:
87 void SetUp() override
88 {
89 clock = ::ClockMock::createShared();
90 oidcLogin = ::OIDCLoginMock::createShared();
91 oidcClient = ::OIDCClientMock::createShared();
92 sessionEventListener = std::make_shared<SessionEventListener>();
93
94 EXPECT_CALL(*clock, epochSeconds()).WillRepeatedly(::Return(100));
95 }
96
97 std::shared_ptr<::ClockMock> clock;
98 std::shared_ptr<::OIDCLoginMock> oidcLogin;
99 std::shared_ptr<::OIDCClientMock> oidcClient;
100 std::shared_ptr<SessionEventListener> sessionEventListener;
101
102 // Helpers:
103 std::shared_ptr<::OIDCSessionImpl> session(
104 std::unique_ptr<::OIDCState> state,
105 std::uint64_t validitySafetyMarginS = 10,
106 std::chrono::milliseconds loginTimeout = std::chrono::milliseconds(0)
107 )
108 {
109 return std::make_shared<::OIDCSessionImpl>(
110 std::move(state),
111 oidcLogin,
112 oidcClient,
113 clock,
114 sessionEventListener,
115 validitySafetyMarginS,
116 loginTimeout
117 );
118 }
119 std::shared_ptr<::OIDCSessionImpl> session(
120 const ::OIDCState &state,
121 std::uint64_t validitySafetyMarginS = 10,
122 std::chrono::milliseconds loginTimeout = std::chrono::milliseconds(0)
123 )
124 {
125 return session(std::unique_ptr<::OIDCState>(new ::OIDCState(state)), validitySafetyMarginS, loginTimeout);
126 }
127 std::shared_ptr<::OIDCSessionImpl> session_without_state()
128 {
129 return session(nullptr);
130 }
131 std::shared_ptr<::OIDCSessionImpl> session_without_access_token()
132 {
133 return session(::OIDCState("", "", -1, false, {}, ::IdToken({}, {})));
134 }
135 std::shared_ptr<::OIDCSessionImpl> infinite_session(std::chrono::milliseconds loginTimeout = std::chrono::milliseconds (0))
136 {
137 return session(
138 ::OIDCState("at", "", ::TOKEN_DOES_NOT_EXPIRE, true, {}, ::IdToken({}, {})),
139 10,
140 loginTimeout
141 );
142 }
143 std::shared_ptr<::OIDCSessionImpl> session_that_expires_at(const std::int64_t expiresAt, const bool refreshable = true)
144 {
145 return session(::OIDCState("at", (refreshable ? "rt" : ""), expiresAt, false, {}, ::IdToken({}, {})));
146 }
147 ::OIDCState * createRefreshedState()
148 {
149 return new ::OIDCState(
150 "at-new",
151 "rt-new",
152 200,
153 false,
154 {},
155 ::IdToken({}, {})
156 );
157 }
158 void stubSuccessfulRefresh()
159 {
160 auto refreshRequest = new ::OIDCRefreshRequestMock();
161 auto refreshedState = createRefreshedState();
162
163 EXPECT_CALL(*oidcClient, refresh(::_)).WillOnce(::Return(std::unique_ptr<::OIDCRefreshRequestMock>(refreshRequest)));
164 EXPECT_CALL(*refreshRequest, execute()).WillOnce(::Return(std::unique_ptr<::OIDCState>(refreshedState)));
165 }
166 void stubOAuthInvalidGrantRefresh()
167 {
168 auto refreshRequest = new ::OIDCRefreshRequestMock();
169
170 EXPECT_CALL(*oidcClient, refresh(::_)).WillOnce(::Return(std::unique_ptr<::OIDCRefreshRequestMock>(refreshRequest)));
171 EXPECT_CALL(*refreshRequest, execute()).WillOnce(::Throw(::OAuthInvalidGrant(oauth::OAuthException::REFRESH_REQUEST, "", "")));
172 }
173 ::OIDCLoginRequestMock * stubLogin()
174 {
175 auto loginRequest = new ::OIDCLoginRequestMock();
176
177 EXPECT_CALL(*oidcLogin, login()).WillOnce(::Return(std::unique_ptr<::OIDCLoginRequestMock>(loginRequest)));
178 return loginRequest;
179 }
180
181 ::OIDCLoginRequestMock * stubSuccessfulLogin()
182 {
183 auto loginRequest = stubLogin();
184 auto state = createRefreshedState();
185
186 EXPECT_CALL(*loginRequest, execute()).WillOnce(::Return(std::unique_ptr<::OIDCState>(state)));
187
188 return loginRequest;
189 }
190};
191
192}}}
193
194#endif //TENDUKE_TEST_OIDC_OIDCSESSIONIMPL_TEST_H
Exception thrown when server reported "invalid_grant".
Definition OAuthInvalidGrant.h:15
const std::string & getAccessToken() const override
Returns the access token.
Definition OAuthStateImpl.h:50
Default implementation of tenduke::oidc::OIDCSessionEventListener.
Definition DefaultOIDCSessionEventListener.h:14
OIDC ID-token.
Definition IdToken.h:15
Login-request initiated by OIDCLogin-service.
Definition OIDCLoginRequest.h:15
Default implementation of tenduke::oidc::OIDCSession.
Definition OIDCSessionImpl.h:23
Container of OIDC state, describing the user session.
Definition OIDCState.h:17
Definition ClockMock.h:13
Definition OIDCClientMock.h:11
Definition OIDCLoginMock.h:11
Definition OIDCLoginRequestMock.h:11
Definition OIDCRefreshRequestMock.h:11
Definition OIDCSessionImpl_test.h:64
std::unique_ptr<::OIDCState > execute() override
Performs the login.
Definition OIDCSessionImpl_test.h:70
void abort() override
Aborts the login.
Definition OIDCSessionImpl_test.h:75
Definition OIDCSessionImpl_test.h:85
Definition OIDCSessionImpl_test.h:38
void loginStarting() override
Called when OIDCSession starts login.
Definition OIDCSessionImpl_test.h:40
const std::int64_t TOKEN_DOES_NOT_EXPIRE
Magic value to denote that the access token does not expire.
Definition OAuthState.h:12
Root for classes, functions and globals of 10Duke C++ Client.
Definition BackendConfiguration.h:7