Skip to content

Commit f2d9cc1

Browse files
RahulHereRahulHere
authored andcommitted
Test Keycloak Integration (#130)
- Created comprehensive integration test suite for Keycloak - Added test for valid token validation with real Keycloak tokens - Implemented JWKS fetching and caching verification tests - Added expired token rejection test - Created invalid signature rejection test - Implemented wrong issuer rejection test - Added scope validation test with Keycloak tokens - Created cache invalidation test for unknown key IDs - Implemented concurrent token validation test - Added token refresh scenario test - Created audience validation test - Added helper functions for Keycloak token acquisition - Implemented Keycloak availability check with graceful skip - Created test runner script with environment configuration - Added CMake configuration for test compilation
1 parent ddcabfc commit f2d9cc1

File tree

3 files changed

+508
-0
lines changed

3 files changed

+508
-0
lines changed

tests/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_executable(benchmark_jwt_validation auth/benchmark_jwt_validation.cc)
1010
add_executable(test_memory_cache auth/test_memory_cache.cc)
1111
add_executable(test_http_client auth/test_http_client.cc)
1212
add_executable(test_jwks_client auth/test_jwks_client.cc)
13+
add_executable(test_keycloak_integration auth/test_keycloak_integration.cc)
1314
add_executable(test_variant core/test_variant.cc)
1415
add_executable(test_variant_extensive core/test_variant_extensive.cc)
1516
add_executable(test_variant_advanced core/test_variant_advanced.cc)
@@ -179,6 +180,14 @@ target_link_libraries(test_jwks_client
179180
Threads::Threads
180181
)
181182

183+
target_link_libraries(test_keycloak_integration
184+
gopher_mcp_c
185+
gtest
186+
gtest_main
187+
Threads::Threads
188+
CURL::libcurl
189+
)
190+
182191
target_link_libraries(test_variant
183192
gtest
184193
gtest_main

tests/auth/run_keycloak_tests.sh

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/bin/bash
2+
# Script to run Keycloak integration tests
3+
4+
# Configuration
5+
KEYCLOAK_URL=${KEYCLOAK_URL:-"http://localhost:8080"}
6+
KEYCLOAK_REALM=${KEYCLOAK_REALM:-"master"}
7+
KEYCLOAK_CLIENT_ID=${KEYCLOAK_CLIENT_ID:-"test-client"}
8+
KEYCLOAK_CLIENT_SECRET=${KEYCLOAK_CLIENT_SECRET:-"test-secret"}
9+
KEYCLOAK_USERNAME=${KEYCLOAK_USERNAME:-"test-user"}
10+
KEYCLOAK_PASSWORD=${KEYCLOAK_PASSWORD:-"test-password"}
11+
12+
# Colors for output
13+
RED='\033[0;31m'
14+
GREEN='\033[0;32m'
15+
YELLOW='\033[1;33m'
16+
NC='\033[0m' # No Color
17+
18+
echo "========================================="
19+
echo "Keycloak Integration Test Runner"
20+
echo "========================================="
21+
echo ""
22+
echo "Configuration:"
23+
echo " Keycloak URL: $KEYCLOAK_URL"
24+
echo " Realm: $KEYCLOAK_REALM"
25+
echo " Client ID: $KEYCLOAK_CLIENT_ID"
26+
echo ""
27+
28+
# Check if Keycloak is running
29+
echo -n "Checking Keycloak availability... "
30+
if curl -s -o /dev/null -w "%{http_code}" "$KEYCLOAK_URL/health" | grep -q "200\|404"; then
31+
echo -e "${GREEN}Available${NC}"
32+
else
33+
echo -e "${RED}Not available${NC}"
34+
echo ""
35+
echo -e "${YELLOW}Keycloak appears to be unavailable at $KEYCLOAK_URL${NC}"
36+
echo "To run these tests, you need a running Keycloak instance."
37+
echo ""
38+
echo "You can start a local Keycloak using Docker:"
39+
echo " docker run -d --name keycloak -p 8080:8080 \\"
40+
echo " -e KEYCLOAK_ADMIN=admin \\"
41+
echo " -e KEYCLOAK_ADMIN_PASSWORD=admin \\"
42+
echo " quay.io/keycloak/keycloak:latest start-dev"
43+
echo ""
44+
echo "Then create a test client and user in the Keycloak admin console."
45+
echo ""
46+
echo "Tests will be skipped."
47+
exit 0
48+
fi
49+
50+
# Run the tests
51+
echo ""
52+
echo "Running integration tests..."
53+
echo "========================================="
54+
55+
# Export environment variables
56+
export KEYCLOAK_URL
57+
export KEYCLOAK_REALM
58+
export KEYCLOAK_CLIENT_ID
59+
export KEYCLOAK_CLIENT_SECRET
60+
export KEYCLOAK_USERNAME
61+
export KEYCLOAK_PASSWORD
62+
63+
# Run test executable
64+
TEST_BINARY="../../build/tests/test_keycloak_integration"
65+
66+
if [ ! -f "$TEST_BINARY" ]; then
67+
echo -e "${RED}Test binary not found at $TEST_BINARY${NC}"
68+
echo "Please build the tests first:"
69+
echo " cd ../../build && make test_keycloak_integration"
70+
exit 1
71+
fi
72+
73+
# Run with verbose output
74+
$TEST_BINARY --gtest_color=yes
75+
76+
TEST_RESULT=$?
77+
78+
echo ""
79+
echo "========================================="
80+
if [ $TEST_RESULT -eq 0 ]; then
81+
echo -e "${GREEN}All tests passed!${NC}"
82+
else
83+
echo -e "${RED}Some tests failed!${NC}"
84+
fi
85+
86+
exit $TEST_RESULT

0 commit comments

Comments
 (0)