Skip to content

Commit 9b876b3

Browse files
added testlib
1 parent 965895a commit 9b876b3

File tree

6 files changed

+84
-3
lines changed

6 files changed

+84
-3
lines changed

ydb/mvp/core/mvp_ut.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <library/cpp/json/json_reader.h>
77
#include <library/cpp/testing/unittest/registar.h>
88
#include <util/generic/map.h>
9-
#include <kikimr/yndx/testlib/service_mocks/session_service_mock.h>
9+
#include <ydb/mvp/core/testlib/session_service_mock.h>
1010
#include <ydb/mvp/core/protos/mvp.pb.h>
1111
#include "mvp_test_runtime.h"
1212

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#pragma once
2+
3+
#include <ydb/public/api/client/yc_private/oauth/session_service.grpc.pb.h>
4+
#include <ydb/public/api/client/yc_private/iam/iam_token_service.grpc.pb.h>
5+
#include <util/generic/hash_set.h>
6+
#include <util/generic/hash.h>
7+
8+
class TSessionServiceMock : public yandex::cloud::priv::oauth::v1::SessionService::Service {
9+
yandex::cloud::priv::oauth::v1::AuthorizationRequired AuthorizationRequiredMessage;
10+
11+
THashMap<TString, TString> ParseCookie(TStringBuf cookie) {
12+
THashMap<TString, TString> parsedCookies;
13+
for (TStringBuf param = cookie.NextTok(';'); !param.empty(); param = cookie.NextTok(';')) {
14+
param.SkipPrefix(" ");
15+
TStringBuf name = param.NextTok('=');
16+
parsedCookies[name] = param;
17+
}
18+
return parsedCookies;
19+
}
20+
21+
public:
22+
std::pair<const TString, TString> AllowedCookies {"yc_session", "allowed_session_cookie"};
23+
bool IsTokenAllowed {true};
24+
bool IsOpenIdScopeMissed {false};
25+
THashSet<TString> AllowedAccessTokens;
26+
27+
TSessionServiceMock() {
28+
AuthorizationRequiredMessage.Setauthorize_url("https://auth.cloud.yandex.ru/oauth/authorize");
29+
}
30+
31+
grpc::Status Check(grpc::ServerContext*,
32+
const yandex::cloud::priv::oauth::v1::CheckSessionRequest* request,
33+
yandex::cloud::priv::oauth::v1::CheckSessionResponse* response) override {
34+
if (!IsTokenAllowed) {
35+
return grpc::Status(grpc::StatusCode::UNAUTHENTICATED, "Authorization IAM token are invalid or may have expired");
36+
}
37+
const THashMap<TString, TString> cookies = ParseCookie(request->Getcookie_header());
38+
auto it = cookies.find(AllowedCookies.first);
39+
if (it != cookies.cend()) {
40+
if (it->second == AllowedCookies.second) {
41+
auto iam_token = response->Mutableiam_token();
42+
iam_token->Setiam_token("protected_page_iam_token");
43+
return grpc::Status(grpc::StatusCode::OK, "Cookie is corrected");
44+
}
45+
}
46+
const TString errorDetailsPrefix = "Error details perfix\n";
47+
const TString errorDetailsSuffix = "\nError details suffix";
48+
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT,
49+
"The provided cookies are invalid or may have expired",
50+
errorDetailsPrefix + AuthorizationRequiredMessage.SerializeAsString() + errorDetailsSuffix);
51+
}
52+
53+
grpc::Status Create(grpc::ServerContext*,
54+
const yandex::cloud::priv::oauth::v1::CreateSessionRequest* request,
55+
yandex::cloud::priv::oauth::v1::CreateSessionResponse* response) override {
56+
if (!IsTokenAllowed) {
57+
return grpc::Status(grpc::StatusCode::UNAUTHENTICATED, "Authorization IAM token are invalid or may have expired");
58+
}
59+
if (IsOpenIdScopeMissed) {
60+
return grpc::Status(grpc::StatusCode::FAILED_PRECONDITION, "Openid scope is missed for specified access_token");
61+
}
62+
if (AllowedAccessTokens.count(request->Getaccess_token()) > 0) {
63+
response->Addset_cookie_header(AllowedCookies.first + "=" + AllowedCookies.second + "; SameSite=Lax");
64+
return grpc::Status(grpc::StatusCode::OK, "Cookie was created");
65+
}
66+
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "The provided access_token is invalid or may have expired", AuthorizationRequiredMessage.SerializeAsString());
67+
}
68+
};

ydb/mvp/core/testlib/ya.make

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
LIBRARY()
2+
3+
SRCS(
4+
session_service_mock.h
5+
)
6+
7+
PEERDIR(
8+
ydb/public/api/client/yc_private/ydb
9+
ydb/public/api/client/yc_private/oauth
10+
)
11+
12+
END()

ydb/mvp/core/ut/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SRCS(
1010

1111
PEERDIR(
1212
ydb/core/testlib/actors
13+
ydb/mvp/core/testlib
1314
contrib/libs/jwt-cpp
1415
)
1516

ydb/mvp/oidc_proxy/oidc_proxy_ut.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <library/cpp/json/json_reader.h>
44
#include <library/cpp/testing/unittest/registar.h>
55
#include <util/generic/map.h>
6-
#include <kikimr/yndx/testlib/service_mocks/session_service_mock.h>
6+
#include <ydb/mvp/core/testlib/session_service_mock.h>
77
#include <ydb/mvp/core/protos/mvp.pb.h>
88
#include <ydb/mvp/core/mvp_test_runtime.h>
99
#include "oidc_protected_page.h"

ydb/mvp/oidc_proxy/ut/ya.make

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ SRCS(
1414

1515
PEERDIR(
1616
ydb/mvp/core
17+
ydb/mvp/core/testlib
1718
ydb/core/testlib/actors
18-
kikimr/yndx/testlib/service_mocks
1919
)
2020

2121
END()

0 commit comments

Comments
 (0)