Skip to content

Removed the country code mapping table; Adapted tests. #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 1 addition & 31 deletions src/lua/api-gateway/validation/oauth2/userProfileValidator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
-- Properties that can be set by this validator:
-- 1. user_email
-- 2. user_country_code
-- 3. user_region
-- 4. user_name
-- 3. user_name
--
-- User: ddascal
-- Date: 17/12/13
Expand All @@ -54,16 +53,6 @@ local RESPONSES = {
NOT_ALLOWED = { error_code = "403024", message = "Not allowed to read the profile"},
P_UNKNOWN_ERROR = { error_code = "503020", message = "Could not read the profile" }
}
---
-- @field US - countries mapping to US region
-- @field EU - countries mapping to EU region
-- @field AP - countries mapping to AP region
--
local DEFAULT_COUNTRY_MAP = {
US = { "AG", "AI", "AN", "AR", "AS", "AW", "BB", "BL", "BM", "BO", "BQ", "BR", "BS", "BZ", "CA", "CL", "CO", "CR", "CU", "CW", "DM", "DO", "EC", "FK", "GD", "GF", "GP", "GS", "GT", "GY", "HN", "HT", "JM", "KN", "KY", "LC", "MP", "MQ", "MS", "MX", "NI", "PA", "PE", "PM", "PR", "PY", "SR", "SV", "SX", "TC", "TT", "UM", "US", "UY", "VC", "VE", "VG", "VI" },
EU = { "AD", "AL", "AM", "AO", "AT", "AX", "AZ", "BA", "BE", "BF", "BG", "BI", "BJ", "BV", "BW", "BY", "CD", "CF", "CG", "CH", "CI", "CM", "CS", "CV", "CY", "CZ", "DE", "DJ", "DK", "DZ", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FO", "FR", "GA", "GB", "GE", "GG", "GH", "GI", "GL", "GM", "GN", "GQ", "GR", "GW", "HR", "HU", "IE", "IM", "IO", "IR", "IS", "IT", "JE", "KE", "KM", "KP", "KV", "LI", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MK", "ML", "MR", "MT", "MU", "MW", "MZ", "NA", "NE", "NG", "NL", "NO", "PL", "PS", "PT", "RE", "RO", "RS", "RW", "SC", "SD", "SE", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SS", "ST", "SY", "SZ", "TD", "TF", "TG", "TN", "TZ", "UA", "UG", "VA", "YT", "ZA", "ZM", "ZW" },
AP = { "AE", "AF", "AQ", "AU", "BD", "BH", "BN", "BT", "CC", "CK", "CN", "CX", "FJ", "FM", "GU", "HK", "HM", "ID", "IL", "IN", "IQ", "IR", "JO", "JP", "KG", "KH", "KI", "KR", "KW", "KZ", "LA", "LB", "LK", "MH", "MM", "MN", "MO", "MV", "MY", "NC", "NF", "NP", "NR", "NU", "NZ", "OM", "PF", "PG", "PH", "PK", "PN", "PW", "QA", "RU", "SA", "SB", "SG", "TH", "TJ", "TK", "TL", "TM", "TO", "TR", "TV", "TW", "UZ", "VN", "VU", "WF", "WS", "YE" }
}

---
-- Maximum time in seconds specifying how long to cache a valid token in GW's memory
Expand Down Expand Up @@ -151,24 +140,6 @@ function _M:isProfileValid(cachedProfile)
return true
end

---
-- Returns an object mapping countries to regions
function _M:getDefaultCountryMap()
return DEFAULT_COUNTRY_MAP
end

function _M:getUserRegion( user_country_code, country_map )
local cmap = country_map or self:getDefaultCountryMap()
for region,countries in pairs(cmap) do
for i , countryCode in pairs(countries) do
if user_country_code == countryCode then
return region
end
end
end
return "US"
end

---
-- Returns an object with a set of variables to be saved in the request's context and later in the request's vars
-- IMPORTANT: This method is only called when fetching a new profile, otherwise the information from the cache
Expand All @@ -180,7 +151,6 @@ function _M:extractContextVars(profile)
cachingObj.user_email = profile.email
cachingObj.user_country_code = profile.countryCode
cachingObj.user_name = profile.displayName
cachingObj.user_region = self:getUserRegion(profile.countryCode)
cachingObj.user_first_name = profile.first_name
cachingObj.user_last_name = profile.last_name
return cachingObj
Expand Down
38 changes: 16 additions & 22 deletions test/perl/api-gateway/validation/oauth2/userProfileValidator.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use Cwd qw(cwd);

repeat_each(2);

plan tests => repeat_each() * (blocks() * 8 ) - 6;
plan tests => repeat_each() * (blocks() * 8 ) - 12;

my $pwd = cwd();

Expand Down Expand Up @@ -80,7 +80,7 @@ __DATA__
set $validate_user_profile on;

access_by_lua "ngx.apiGateway.validation.validateRequest()";
content_by_lua 'ngx.say("user_email=" .. ngx.var.user_email .. ",user_country_code=" .. ngx.var.user_country_code .. ",user_region=" .. ngx.var.user_region .. ",user_name=" .. ngx.var.user_name)';
content_by_lua 'ngx.say("user_email=" .. ngx.var.user_email .. ",user_country_code=" .. ngx.var.user_country_code .. ",user_name=" .. ngx.var.user_name)';
}
location /local-cache {
set $authtoken $http_authorization;
Expand Down Expand Up @@ -123,13 +123,13 @@ Authorization: Bearer SOME_OAUTH_PROFILE_TEST_1
"GET /redis-cache"
]
--- response_body_like eval
['^user_email=johndoe_ĂÂă\@domain.com,user_country_code=AT,user_region=EU,user_name=display_name%E2%80%94%E5%A4%A7%EF%BC%8D%E5%A5%B3.*',
'^Local: {"user_region":"EU","user_country_code":"AT","user_email":"johndoe_ĂÂă@domain.com","user_name":"display_name—大-女"}.*',
'^Redis: {"user_region":"EU","user_country_code":"AT","user_email":"johndoe_ĂÂă@domain.com","user_name":"display_name—大-女"}.*']
['^user_email=johndoe_ĂÂă\@domain.com,user_country_code=AT,user_name=display_name%E2%80%94%E5%A4%A7%EF%BC%8D%E5%A5%B3.*',
'Local: {"user_name":"display_name—大-女","user_email":"johndoe_ĂÂă@domain.com","user_country_code":"AT"}',
'Redis: {"user_name":"display_name—大-女","user_email":"johndoe_ĂÂă@domain.com","user_country_code":"AT"}']
--- no_error_log
[error]

=== TEST 2: test ims_profile is saved correctly in cache and in request variables with US region
=== TEST 2: test ims_profile is saved correctly in cache and in request variables
--- http_config eval: $::HttpConfig
--- config
include ../../api-gateway/api-gateway-cache.conf;
Expand All @@ -147,7 +147,7 @@ Authorization: Bearer SOME_OAUTH_PROFILE_TEST_1
set $validate_user_profile on;

access_by_lua "ngx.apiGateway.validation.validateRequest()";
content_by_lua 'ngx.say("user_email=" .. ngx.var.user_email .. ",user_country_code=" .. ngx.var.user_country_code .. ",user_region=" .. ngx.var.user_region .. ",user_name=" .. ngx.var.user_name)';
content_by_lua 'ngx.say("user_email=" .. ngx.var.user_email .. ",user_country_code=" .. ngx.var.user_country_code .. ",user_name=" .. ngx.var.user_name)';
}

location /local-cache {
Expand Down Expand Up @@ -181,9 +181,9 @@ Authorization: Bearer SOME_OAUTH_TOKEN_TEST_TWO
"GET /redis-cache"
]
--- response_body_like eval
['^user_email=noreply\@domain.com,user_country_code=CA,user_region=US,user_name=display_name.*',
'^{"user_region":"US","user_country_code":"CA","user_email":"noreply@domain.com","user_name":"display_name"}.*',
'^{"user_region":"US","user_country_code":"CA","user_email":"noreply@domain.com","user_name":"display_name"}.*']
['^user_email=noreply\@domain.com,user_country_code=CA,user_name=display_name.*',
'{"user_name":"display_name","user_email":"noreply@domain.com","user_country_code":"CA"}',
'{"user_name":"display_name","user_email":"noreply@domain.com","user_country_code":"CA"}']
--- no_error_log
[error]

Expand All @@ -205,11 +205,10 @@ Authorization: Bearer SOME_OAUTH_TOKEN_TEST_TWO
set $validate_user_profile on;

access_by_lua "ngx.apiGateway.validation.validateRequest()";
content_by_lua 'ngx.say("user_email=" .. ngx.var.user_email .. ",user_country_code=" .. ngx.var.user_country_code .. ",user_region=" .. ngx.var.user_region .. ",user_name=" .. ngx.var.user_name)';
content_by_lua 'ngx.say("user_email=" .. ngx.var.user_email .. ",user_country_code=" .. ngx.var.user_country_code .. ",user_name=" .. ngx.var.user_name)';

add_header X-User-Id $user_email;
add_header X-User-Country-Code $user_country_code;
add_header X-User-Region $user_region;
add_header X-User-Name $user_name;
}

Expand All @@ -222,11 +221,10 @@ Authorization: Bearer SOME_OAUTH_TOKEN_TEST_THREE
--- request
GET /test-validate-user
--- response_body_like eval
"^user_email=noreply-ăâ\@domain.com,user_country_code=CA,user_region=US,user_name=display_name-%E5%B7%A5%EF%BC%8D%E5%A5%B3%EF%BC%8D%E9%95%BF.*"
"^user_email=noreply-ăâ\@domain.com,user_country_code=CA,user_name=display_name-%E5%B7%A5%EF%BC%8D%E5%A5%B3%EF%BC%8D%E9%95%BF.*"
--- response_headers_like
X-User-Id: noreply-ăâ@domain.com
X-User-Country-Code: CA
X-User-Region: US
X-User-Name: display_name-%E5%B7%A5%EF%BC%8D%E5%A5%B3%EF%BC%8D%E9%95%BF
--- error_code: 200
--- no_error_log
Expand All @@ -250,11 +248,10 @@ X-User-Name: display_name-%E5%B7%A5%EF%BC%8D%E5%A5%B3%EF%BC%8D%E9%95%BF
set $validate_user_profile on;

access_by_lua "ngx.apiGateway.validation.validateRequest()";
content_by_lua 'ngx.say("user_email=" .. ngx.var.user_email .. ",user_country_code=" .. ngx.var.user_country_code .. ",user_region=" .. ngx.var.user_region .. ",user_name=" .. ngx.var.user_name)';
content_by_lua 'ngx.say("user_email=" .. ngx.var.user_email .. ",user_country_code=" .. ngx.var.user_country_code .. ",user_name=" .. ngx.var.user_name)';

add_header X-User-Id $user_email;
add_header X-User-Country-Code $user_country_code;
add_header X-User-Region $user_region;
add_header X-User-Name $user_name;
}

Expand All @@ -267,10 +264,9 @@ Authorization: Bearer SOME_OAUTH_TOKEN_TEST_FOUR
--- request
GET /test-validate-user
--- response_body_like eval
"^user_email=noreply-ăâ\@domain.com,user_country_code=,user_region=US,user_name=display_name-%E5%B7%A5%EF%BC%8D%E5%A5%B3%EF%BC%8D%E9%95%BF.*"
"^user_email=noreply-ăâ\@domain.com,user_country_code=,user_name=display_name-%E5%B7%A5%EF%BC%8D%E5%A5%B3%EF%BC%8D%E9%95%BF.*"
--- response_headers_like
X-User-Id: noreply-ăâ@domain.com
X-User-Region: US
X-User-Name: display_name-%E5%B7%A5%EF%BC%8D%E5%A5%B3%EF%BC%8D%E9%95%BF
--- error_code: 200
--- no_error_log
Expand All @@ -294,11 +290,10 @@ X-User-Name: display_name-%E5%B7%A5%EF%BC%8D%E5%A5%B3%EF%BC%8D%E9%95%BF
set $validate_user_profile on;

access_by_lua "ngx.apiGateway.validation.validateRequest()";
content_by_lua 'ngx.say("user_email=" .. ngx.var.user_email .. ",user_country_code=" .. ngx.var.user_country_code .. ",user_region=" .. ngx.var.user_region .. ",user_name=" .. ngx.var.user_name)';
content_by_lua 'ngx.say("user_email=" .. ngx.var.user_email .. ",user_country_code=" .. ngx.var.user_country_code .. ",user_name=" .. ngx.var.user_name)';

add_header X-User-Id $user_email;
add_header X-User-Country-Code $user_country_code;
add_header X-User-Region $user_region;
add_header X-User-Name $user_name;
}

Expand All @@ -311,10 +306,9 @@ Authorization: Bearer SOME_OAUTH_TOKEN_TEST_FIVE
--- request
GET /test-validate-user
--- response_body_like eval
"^user_email=noreply-ăâ\@domain.com,user_country_code=,user_region=US,user_name=display_name-%E5%B7%A5%EF%BC%8D%E5%A5%B3%EF%BC%8D%E9%95%BF.*"
"^user_email=noreply-ăâ\@domain.com,user_country_code=,user_name=display_name-%E5%B7%A5%EF%BC%8D%E5%A5%B3%EF%BC%8D%E9%95%BF.*"
--- response_headers_like
X-User-Id: noreply-ăâ@domain.com
X-User-Region: US
X-User-Name: display_name-%E5%B7%A5%EF%BC%8D%E5%A5%B3%EF%BC%8D%E9%95%BF
--- error_code: 200
--- no_error_log
Expand Down