Skip to content

Commit 68ddc87

Browse files
author
SukitOwl
committed
refactor test remove bypass request block out
1 parent a1ae72f commit 68ddc87

12 files changed

+376
-513
lines changed

Cpp/odin-views/app.google.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,7 @@ namespace {
8080
auto const access_token =
8181
fostlib::coerce<fostlib::string>(body["access_token"]);
8282

83-
fostlib::json user_detail;
84-
if (config.has_key("google-mock")) {
85-
if (fostlib::coerce<fostlib::string>(config["google-mock"])
86-
== "OK") {
87-
// Use access token as google ID
88-
fostlib::insert(user_detail, "sub", access_token);
89-
fostlib::insert(user_detail, "name", "Test User");
90-
fostlib::insert(
91-
user_detail, "email",
92-
access_token + "@example.com");
93-
}
94-
} else {
95-
user_detail = odin::google::get_user_detail(access_token);
96-
}
83+
fostlib::json user_detail = odin::google::get_user_detail(access_token);
9784
logger("user_detail", user_detail);
9885
if (user_detail.isnull())
9986
throw fostlib::exceptions::not_implemented(

Cpp/odin-views/google.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,7 @@ namespace {
5757
"odin.google.login", "Must pass access_token field");
5858
const auto access_token =
5959
fostlib::coerce<fostlib::string>(body["access_token"]);
60-
fostlib::json user_detail;
61-
if (config.has_key("google-mock")) {
62-
if (fostlib::coerce<fostlib::string>(config["google-mock"])
63-
== "OK") {
64-
// Use access token as google ID
65-
fostlib::insert(user_detail, "sub", access_token);
66-
fostlib::insert(user_detail, "name", "Test User");
67-
fostlib::insert(
68-
user_detail, "email",
69-
access_token + "@example.com");
70-
}
71-
} else {
72-
user_detail = odin::google::get_user_detail(access_token);
73-
}
60+
fostlib::json user_detail = odin::google::get_user_detail(access_token);
7461
if (user_detail.isnull())
7562
throw fostlib::exceptions::not_implemented(
7663
"odin.google.login", "User not authenticated");

Cpp/odin/facebook.cpp

Lines changed: 17 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
Copyright 2018-2019 Red Anchor Trading Co. Ltd.
2+
Copyright 2018-2020 Red Anchor Trading Co. Ltd.
33
44
Distributed under the Boost Software License, Version 1.0.
55
See <http://www.boost.org/LICENSE_1_0.txt>
@@ -9,38 +9,12 @@
99
#include <odin/fg/native.hpp>
1010
#include <odin/odin.hpp>
1111

12-
#include <fost/http>
13-
#include <fost/log>
1412
#include <fostgres/sql.hpp>
15-
13+
#include <fost/http>
1614
#include <fost/insert>
15+
#include <fost/log>
16+
#include <fost/ua/exceptions.hpp>
1717

18-
namespace {
19-
20-
21-
std::unique_ptr<fostlib::http::user_agent::response> get_or_mock(
22-
fostlib::http::user_agent &ua,
23-
fostlib::url url,
24-
fostlib::json config = {}) {
25-
if (config.isnull()) { return ua.get(url); }
26-
/// Create mock response based on config
27-
int status{200};
28-
if (config.has_key("status")) {
29-
status = fostlib::coerce<int>(config["status"]);
30-
}
31-
fostlib::json body{};
32-
if (config.has_key("body")) { body = config["body"]; }
33-
auto const bodydata = fostlib::json::unparse(body, false);
34-
// TODO: Can set headers
35-
fostlib::mime::mime_headers headers;
36-
// fostlib::json headers{};
37-
return std::make_unique<fostlib::http::user_agent::response>(
38-
"GET", url, 200,
39-
std::make_unique<fostlib::binary_body>(
40-
bodydata.memory().begin(), bodydata.memory().end(),
41-
headers));
42-
}
43-
}
4418

4519
fostlib::json odin::facebook::get_user_detail(
4620
fostlib::pg::connection &cnx,
@@ -55,31 +29,15 @@ fostlib::json odin::facebook::get_user_detail(
5529
fostlib::url::query_string ids_for_biz_qs{};
5630
ids_for_biz_qs.append("access_token", user_token);
5731
ids_for_biz_url.query(ids_for_biz_qs);
58-
fostlib::json ids_for_biz_conf{};
59-
if (config.has_key(fostlib::jcursor{"facebook-mock", "ids_for_business"})) {
60-
ids_for_biz_conf =
61-
config[fostlib::jcursor{"facebook-mock", "ids_for_business"}];
62-
}
6332

6433
fostlib::json ids_for_biz;
65-
if (ids_for_biz_conf.isnull()) {
66-
fostlib::mime::mime_headers headers;
67-
ids_for_biz = fostlib::ua::get_json(ids_for_biz_url, headers);
68-
}
69-
else {
70-
auto const ids_for_biz_resp =
71-
get_or_mock(ua, ids_for_biz_url, ids_for_biz_conf);
72-
ids_for_biz =
73-
fostlib::json::parse(fostlib::coerce<fostlib::string>(
74-
fostlib::coerce<fostlib::utf8_string>(
75-
ids_for_biz_resp->body()->data())));
76-
}
77-
78-
if (!ids_for_biz.has_key("data")) {
34+
try {
35+
ids_for_biz = fostlib::ua::get_json(
36+
ids_for_biz_url, fostlib::mime::mime_headers{});
37+
} catch (fostlib::ua::http_error &e) {
7938
fostlib::log::error(c_odin)("Error", "ids_for_business")(
80-
"URL", ids_for_biz_url)("status", 200)(
39+
"URL", ids_for_biz_url)("status", e.data()["status-code"])(
8140
"body", ids_for_biz);
82-
// TODO: Should return 422
8341
throw fostlib::exceptions::not_implemented(
8442
__PRETTY_FUNCTION__,
8543
"Cannot retrieve /me/ids_for_business from Facebook");
@@ -131,31 +89,21 @@ fostlib::json odin::facebook::get_user_detail(
13189
user_detail_qs.append("access_token", user_token);
13290
user_detail_qs.append("fields", "name,email");
13391
user_detail_url.query(user_detail_qs);
134-
fostlib::json me_conf{};
135-
if (config.has_key(fostlib::jcursor{"facebook-mock", "me"})) {
136-
me_conf = config[fostlib::jcursor{"facebook-mock", "me"}];
137-
}
13892

13993
fostlib::json user_detail;
140-
if (me_conf.isnull()) {
141-
fostlib::mime::mime_headers headers;
142-
user_detail = fostlib::ua::get_json(user_detail_url, headers);
143-
} else {
144-
auto const user_detail_resp = get_or_mock(ua, user_detail_url, me_conf);
145-
user_detail = fostlib::json::parse(fostlib::coerce<fostlib::string>(
146-
fostlib::coerce<fostlib::utf8_string>(
147-
user_detail_resp->body()->data())));
148-
}
149-
fostlib::log::error(c_odin)("Response", user_detail);
150-
if (user_detail.has_key("error")) {
151-
fostlib::log::error(c_odin)("Error", "get-user-detail")(
152-
"URL", user_detail_url)("status", 200)(
94+
try {
95+
user_detail = fostlib::ua::get_json(
96+
user_detail_url, fostlib::mime::mime_headers{});
97+
} catch (fostlib::ua::http_error &e) {
98+
fostlib::log::error(c_odin)("Error", "ids_for_business")(
99+
"URL", user_detail_url)("status", e.data()["status-code"])(
153100
"body", user_detail);
154-
// TODO: Should return 422
155101
throw fostlib::exceptions::not_implemented(
156102
__PRETTY_FUNCTION__,
157103
"Cannot retrieve /me?field=name,email from Facebook");
158104
}
105+
106+
fostlib::log::error(c_odin)("Response", user_detail);
159107
if (user_detail.has_key("name")) {
160108
fostlib::insert(fb_user, "name", user_detail["name"]);
161109
}

Cpp/odin/google.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
Copyright 2018-2019 Red Anchor Trading Co. Ltd.
2+
Copyright 2018-2020 Red Anchor Trading Co. Ltd.
33
44
Distributed under the Boost Software License, Version 1.0.
55
See <http://www.boost.org/LICENSE_1_0.txt>
@@ -9,11 +9,11 @@
99
#include <odin/fg/native.hpp>
1010
#include <odin/odin.hpp>
1111

12-
#include <fost/http>
13-
#include <fost/log>
1412
#include <fostgres/sql.hpp>
15-
13+
#include <fost/http>
1614
#include <fost/insert>
15+
#include <fost/log>
16+
#include <fost/ua/exceptions.hpp>
1717

1818

1919
fostlib::json odin::google::get_user_detail(f5::u8view user_token) {
@@ -22,9 +22,17 @@ fostlib::json odin::google::get_user_detail(f5::u8view user_token) {
2222
fostlib::url::filepath_string api{"/oauth2/v3/tokeninfo"};
2323
fostlib::url gg_url(base_url, api);
2424
gg_url.query().append("id_token", user_token);
25-
fostlib::http::user_agent ua(gg_url);
26-
auto response = ua.get(gg_url);
27-
fostlib::json body = fostlib::json::parse(response->body()->data());
25+
fostlib::json body;
26+
try {
27+
body = fostlib::ua::get_json(gg_url, fostlib::mime::mime_headers{});
28+
// Example body :
29+
// https://developers.google.com/identity/sign-in/android/backend-auth
30+
} catch (fostlib::ua::http_error &e) {
31+
fostlib::log::error(c_odin)("Error", "get_user_detail")("URL", gg_url)(
32+
"status", e.data()["status-code"])("body", body);
33+
throw fostlib::exceptions::not_implemented(
34+
__PRETTY_FUNCTION__, "Cannot retrieve user detail from google");
35+
}
2836
auto aud = fostlib::coerce<fostlib::string>(body["aud"]);
2937
auto gg_aud = c_google_aud.value()["Client_ID"];
3038
for (const auto a : gg_aud) {

tests/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ if(TARGET stress OR TARGET pgtest)
316316
${CMAKE_CURRENT_SOURCE_DIR}/../Schema/bootstrap.sql
317317
${CMAKE_CURRENT_SOURCE_DIR}/../Configuration/odin-views.json
318318
${CMAKE_CURRENT_SOURCE_DIR}/login-google-test.json
319-
${CMAKE_CURRENT_SOURCE_DIR}/facebook-test.json
320319
${CMAKE_CURRENT_SOURCE_DIR}/registration-self.fg
321320
MAIN_DEPENDENCY registration-self.fg
322321
DEPENDS
@@ -351,7 +350,6 @@ if(TARGET stress OR TARGET pgtest)
351350
../Schema/opts/google/006-not-allow-merge-registered-to-guest.blue.sql
352351
../Configuration/odin-views.json
353352
login-google-test.json
354-
facebook-test.json
355353
registration-self.fg
356354
)
357355

@@ -505,7 +503,6 @@ if(TARGET stress OR TARGET pgtest)
505503
${CMAKE_CURRENT_SOURCE_DIR}/../Configuration/odin-views.json
506504
${CMAKE_CURRENT_SOURCE_DIR}/../Configuration/odin-webserver.json
507505
${CMAKE_CURRENT_SOURCE_DIR}/login-google-test.json
508-
${CMAKE_CURRENT_SOURCE_DIR}/facebook-test.json
509506
${CMAKE_CURRENT_SOURCE_DIR}/login-google.fg
510507
MAIN_DEPENDENCY login-google.fg
511508
DEPENDS
@@ -541,7 +538,6 @@ if(TARGET stress OR TARGET pgtest)
541538
../Schema/opts/installation-id/004-merge-account-function.blue.sql
542539
../Configuration/odin-views.json
543540
login-google-test.json
544-
facebook-test.json
545541
login-google.fg
546542
)
547543

tests/app-login-facebook.fg

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ setting odin "Facebook" {
4848
"main": "111111111111111",
4949
"allowed": ["111111111111111", "222222222222222"]
5050
}
51+
setting odin "Google" {
52+
"Client_ID": ["111111111111111", "222222222222222"]
53+
}
5154

5255
## ## Register app
5356
sql.insert odin.identity {"id": "open-app"}
@@ -105,7 +108,7 @@ setting webserver views/odin/test/facebook/login {
105108

106109

107110
# case ids-for-biz-failed
108-
expect GET https://graph.facebook.com/me/ids_for_business?access_token=user_1 200 {
111+
expect GET https://graph.facebook.com/me/ids_for_business?access_token=user_1 400 {
109112
"error": {
110113
"message": "Malformed access token ACCESS_TOKEN",
111114
"type": "OAuthException",
@@ -165,7 +168,7 @@ expect GET https://graph.facebook.com/me/ids_for_business?access_token=user_1 20
165168
}
166169
]
167170
}
168-
expect GET https://graph.facebook.com/me?access_token=user_1&fields=name,email 200 {
171+
expect GET https://graph.facebook.com/me?access_token=user_1&fields=name,email 400 {
169172
"error": {
170173
"message": "Malformed access token ACCESS_TOKEN",
171174
"type": "OAuthException",
@@ -486,14 +489,13 @@ expect GET https://graph.facebook.com/me?access_token=duplicate-email-with-googl
486489
"email": "[email protected]"
487490
}
488491

489-
setting webserver views/odin/test/sec/google/login/ok {
492+
setting webserver views/odin/test/sec/google/login {
490493
"view": "odin.app.secure",
491494
"configuration": {
492495
"secure": {
493496
"view": "odin.app.google.login",
494497
"configuration": {
495-
"expires": {"hours": 72},
496-
"google-mock": "OK"
498+
"expires": {"hours": 72}
497499
}
498500
},
499501
"unsecure": "fost.response.403"
@@ -502,7 +504,13 @@ setting webserver views/odin/test/sec/google/login/ok {
502504

503505
set-path testserver.headers ["Authorization"] (cat "Bearer " (odin.jwt.mint {"sub": "app_user_open_app", "iss": "http://odin.felspar.com/app/open-app"} <JWT_SECRET>open-app))
504506
set-path testserver.headers ["Authorization"] (cat "Bearer " (POST test/sec/installation / {"installation_id": "g-duplicate-email-with-google"} 201))
505-
POST odin/test/sec/google/login/ok / {"access_token": "duplicate-email-with-google"} 200
507+
expect GET https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=duplicate-email-with-google 200 {
508+
"aud": "111111111111111",
509+
"sub": "duplicate-email-with-google",
510+
"name": "Test User",
511+
"email": "[email protected]"
512+
}
513+
POST odin/test/sec/google/login / {"access_token": "duplicate-email-with-google"} 200
506514

507515
GET odin/test/facebook/validate_login /count-users 200 {"count": 6}
508516
GET odin/test/facebook/validate_login /count-facebook-users 200 {"count": 4}

0 commit comments

Comments
 (0)