Skip to content

Commit 2daa6c3

Browse files
committed
# added support for executing unit tests with Docker, using adobeapiplatform/apigateway
1 parent e4f1fe7 commit 2daa6c3

File tree

12 files changed

+118
-40
lines changed

12 files changed

+118
-40
lines changed

Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ redis: all
4444
tar -xf test/resources/redis/redis-$(REDIS_VERSION).tar.gz -C $(BUILD_DIR)/
4545
cd $(BUILD_DIR)/redis-$(REDIS_VERSION) && make
4646

47+
test-docker:
48+
echo "running tests with docker ..."
49+
mkdir -p $(BUILD_DIR)
50+
mkdir -p $(BUILD_DIR)/test-logs
51+
cp -r test/resources/api-gateway $(BUILD_DIR)
52+
sed -i '' 's/127\.0\.0\.1/redis\.docker/g' $(BUILD_DIR)/api-gateway/redis-upstream.conf
53+
rm -f $(BUILD_DIR)/test-logs/*
54+
mkdir -p ~/tmp/apiplatform/api-gateway-request-validation
55+
cp -r ./src ~/tmp/apiplatform/api-gateway-request-validation/
56+
cp -r ./test ~/tmp/apiplatform/api-gateway-request-validation/
57+
cp -r ./target ~/tmp/apiplatform/api-gateway-request-validation/
58+
cd ./test && docker-compose up
59+
cp -r ~/tmp/apiplatform/api-gateway-request-validation/target/ ./target
60+
rm -rf ~/tmp/apiplatform/api-gateway-request-validation
61+
4762
package:
4863
git archive --format=tar --prefix=api-gateway-request-validation-1.1/ -o api-gateway-request-validation-1.1.tar.gz -v HEAD
4964

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,14 @@ git submodule update --init --recursive
393393

394394
## Running the tests
395395

396+
### With docker
397+
398+
```
399+
make test-docker
400+
```
401+
This command spins up 2 containers ( Redis and API Gateway ) and executes the tests in `test/perl`
402+
403+
### With native binary
396404
```
397405
make test
398406
```

src/lua/api-gateway/validation/key/redisApiKeyValidator.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ function ApiKeyValidator:validate_api_key()
9191
local api_key = ngx.var.api_key
9292
local service_id = ngx.var.service_id
9393

94+
ngx.log(ngx.DEBUG, "Validating API-KEY:", api_key, ", service_id:", service_id)
95+
9496
-- Block the requests if there is no apikey --
9597
if api_key == nil or api_key == "" or api_key == true then
9698
ngx.log(ngx.WARN, "Api Key not found as a part of the request")

test/docker-compose.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
gateway:
2+
image: adobeapiplatform/apigateway
3+
links:
4+
- redis:redis.docker
5+
volumes:
6+
- ~/tmp/apiplatform/api-gateway-request-validation/src/lua/api-gateway/validation:/usr/local/api-gateway/lualib/api-gateway/validation
7+
- ~/tmp/apiplatform/api-gateway-request-validation/test/perl:/tmp/perl
8+
- ~/tmp/apiplatform/api-gateway-request-validation/target/:/t
9+
entrypoint: ["prove", "-I", "/usr/local/test-nginx-0.24/lib", "-I", "/usr/local/test-nginx-0.24/inc", "-r", "/tmp/perl/"]
10+
redis:
11+
image: redis:2.8
12+
ports:
13+
- "6379:6379"

test/perl/api-gateway/validation/key/api_key.t

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,7 @@ our $HttpConfig = <<_EOC_;
4848
ngx.apiGateway.validation = require "api-gateway.validation.factory"
4949
';
5050
lua_shared_dict cachedkeys 50m; # caches api-keys
51-
upstream api-gateway-redis {
52-
server 127.0.0.1:6379;
53-
}
54-
upstream api-gateway-redis-replica { # Default config for redis health check test
55-
server 127.0.0.1:6379;
56-
}
51+
include ../../api-gateway/redis-upstream.conf;
5752
_EOC_
5853
5954
#no_diff();

test/perl/api-gateway/validation/oauth2/oauthTokenValidator.t

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,7 @@ our $HttpConfig = <<_EOC_;
5151
lua_shared_dict cachedkeys 50m; # caches api-keys
5252
# dict used by OAuth validator to cache valid tokens
5353
lua_shared_dict cachedOauthTokens 50m;
54-
55-
upstream api-gateway-redis {
56-
server 127.0.0.1:6379;
57-
}
58-
upstream api-gateway-redis-replica { # Default config for redis health check test
59-
server 127.0.0.1:6379;
60-
}
54+
include ../../api-gateway/redis-upstream.conf;
6155
_EOC_
6256
6357
#no_diff();

test/perl/api-gateway/validation/oauth2/userProfileValidator.t

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use Cwd qw(cwd);
3131

3232
repeat_each(2);
3333

34-
plan tests => repeat_each() * (blocks() * 8 ) + 2;
34+
plan tests => repeat_each() * (blocks() * 8 ) - 2;
3535

3636
my $pwd = cwd();
3737

@@ -53,13 +53,7 @@ our $HttpConfig = <<_EOC_;
5353
lua_shared_dict cachedOauthTokens 50m;
5454
# dict used by User Profile validator to cache valid profiles
5555
lua_shared_dict cachedUserProfiles 50m;
56-
57-
upstream api-gateway-redis {
58-
server 127.0.0.1:6379;
59-
}
60-
upstream api-gateway-redis-replica { # Default config for redis health check test
61-
server 127.0.0.1:6379;
62-
}
56+
include ../../api-gateway/redis-upstream.conf;
6357
_EOC_
6458

6559
#no_diff();
@@ -237,3 +231,47 @@ X-User-Name: display_name-%E5%B7%A5%EF%BC%8D%E5%A5%B3%EF%BC%8D%E9%95%BF
237231
--- error_code: 200
238232
--- no_error_log
239233
[error]
234+
235+
=== TEST 4: test ims_profile with a null field
236+
--- http_config eval: $::HttpConfig
237+
--- config
238+
include ../../api-gateway/api-gateway-cache.conf;
239+
include ../../api-gateway/default_validators.conf;
240+
241+
error_log ../test-logs/userProfileValidator_test4_error.log debug;
242+
243+
location /test-validate-user {
244+
set $service_id s-123;
245+
# get OAuth token either from header or from the user_token query string
246+
set $authtoken $http_authorization;
247+
set_if_empty $authtoken $arg_user_token;
248+
set_by_lua $authtoken 'return ngx.re.gsub(ngx.arg[1], "bearer ", "","ijo") ' $authtoken;
249+
250+
set $validate_user_profile on;
251+
252+
access_by_lua "ngx.apiGateway.validation.validateRequest()";
253+
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)';
254+
255+
add_header X-User-Id $user_email;
256+
add_header X-User-Country-Code $user_country_code;
257+
add_header X-User-Region $user_region;
258+
add_header X-User-Name $user_name;
259+
}
260+
261+
location /validate-user {
262+
internal;
263+
return 200 '{"countryCode":null,"emailVerified":"true","email":"noreply-ăâ@domain.com","userId":"1234","name":"full name","displayName":"display_name-工-女-长"}';
264+
}
265+
--- more_headers
266+
Authorization: Bearer SOME_OAUTH_TOKEN_TEST_FOUR
267+
--- request
268+
GET /test-validate-user
269+
--- response_body_like eval
270+
"^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.*"
271+
--- response_headers_like
272+
X-User-Id: noreply-ăâ@domain.com
273+
X-User-Region: US
274+
X-User-Name: display_name-%E5%B7%A5%EF%BC%8D%E5%A5%B3%EF%BC%8D%E9%95%BF
275+
--- error_code: 200
276+
--- no_error_log
277+
[error]

test/perl/api-gateway/validation/signing/hmacGenericSignatureValidator.t

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,7 @@ our $HttpConfig = <<_EOC_;
4848
ngx.apiGateway.validation = require "api-gateway.validation.factory"
4949
';
5050
include "$pwd/conf.d/http.d/*.conf";
51-
upstream api-gateway-redis {
52-
server 127.0.0.1:6379;
53-
}
54-
upstream api-gateway-redis-replica { # Default config for redis health check test
55-
server 127.0.0.1:6379;
56-
}
51+
include ../../api-gateway/redis-upstream.conf;
5752
_EOC_
5853

5954
#no_diff();

test/perl/api-gateway/validation/validator.t

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,7 @@ our $HttpConfig = <<_EOC_;
4646
';
4747
lua_shared_dict test_dict 50m;
4848
# include "$pwd/conf.d/http.d/*.conf";
49-
upstream api-gateway-redis {
50-
server 127.0.0.1:6379;
51-
}
52-
upstream api-gateway-redis-replica { # Default config for redis health check test
53-
server 127.0.0.1:6379;
54-
}
49+
include ../../api-gateway/redis-upstream.conf;
5550
_EOC_
5651
5752
#no_diff();

test/perl/api-gateway/validation/validatorHandler.t

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ our $HttpConfig = <<_EOC_;
4949
ngx.apiGateway.validation = require "api-gateway.validation.factory"
5050
';
5151
# include "$pwd/conf.d/http.d/*.conf";
52-
upstream api-gateway-redis {
53-
server 127.0.0.1:6379;
54-
}
55-
upstream api-gateway-redis-replica { # Default config for redis health check test
56-
server 127.0.0.1:6379;
57-
}
52+
include ../../api-gateway/redis-upstream.conf;
5853
_EOC_
5954
6055
#no_diff();

0 commit comments

Comments
 (0)