Cucumber/BDD security tests example for Java (Spring Boot API).
Cucumber/BDD security tests example for Java (Spring Boot API).
This type of work has also been called “security-as-code” as written security requirements drive executable test logic.
There is a fairly comprehensive toy/sample application here to allow for realistic security testing.
Grab the code.
git clone https://github.com/gingeleski/cucumber-spring-security-tests.git
cd cucumber-spring-security-tests
To launch the API with a root address http://localhost:8080/
…
# Windows
.\gradlew.bat bootRun
# Linux
./gradlew bootRun
To compile and then run the test suite…
# Windows
.\gradlew.bat build
# Linux
./gradlew build
If you encounter issues, see the next section for Vagrant info. That virtual machine will have all dependencies.
This project has been developed for Java 13 and very current Spring Boot dependencies (April 2020).
An Ubuntu 19.x virtual machine has been configured with Vagrant (2.2.7) for this project.
With Vagrant installed, you should be able to simply run vagrant up
from the root of this project.
Notable dependencies and components in this image are as follows…
Building on the Quickstart and Vagrant subsections, a suggested workflow for developers will be described here.
Commands for steps 3 through 6 are as follows.
git clone https://github.com/gingeleski/cucumber-spring-security-tests.git
cd cucumber-spring-security-tests
vagrant up
# Windows
.\gradlew.bat idea
# Linux
./gradlew idea
# Windows
.\gradlew.bat openIdea
# Linux
./gradlew openIdea
Going into your virtual machine to do step 7 can be performed as follows.
# From your local machine, confirm your VM is running
vagrant status
# If it's NOT running then do...
vagrant up
# Go into the VM to run commands now
vagrant ssh
# Make working directory the mapped folder where Vagrantfile resides - so, this project folder
cd /vagrant
# Compile and test the code
gradle build
# View all other possible Gradle tasks
gradle tasks
The toy/sample application “RoomBook” is a room reservation system, like corporate drones will be familiar with.
The API is documented in an OpenAPI version 3.0 spec that resides at /openapi.json
.
Below is an overall plan for authorization.
ROLE_ADMIN
)ROLE_HR_MGR
)ROLE_ASSISTANT
)ROLE_EMPLOYEE
)Note that the API in its current state may not support all of these actions.
“Canned data” comes from src/main/java/resources/import.sql
, directed to an H2 (in-memory) relational database.
The following test users will be set up…
Username | Password | Authorization | Activated |
---|---|---|---|
administrator |
admin |
ROLE_ADMIN |
True |
employee |
password |
ROLE_EMPLOYEE |
True |
disabled |
password |
ROLE_EMPLOYEE |
False |
assistant |
password |
ROLE_ASSISTANT |
True |
hrmanager |
password |
ROLE_HR_MGR |
True |
These detailed calls represent the current state of the API.
Pay particular attention to any noted TODOs, and note there may be some lacking functionality versus the access control list.
POST /api/authenticate
Authorization: Bearer ...
headerContent-Type: application/json
username
is a string, requiredpassword
is a string, requiredid_token
GET /api/rooms
EMPLOYEE
GET /api/rooms/{roomName}
EMPLOYEE
GET /api/rooms/{roomName}/availability
EMPLOYEE
start
is a Unix time which available time blocks must be equal or later thanend
is a Unix time which available time blocks must be earlier or equal toPOST /api/admin/clearAllRooms
(TODO)ADMIN
POST /api/admin/clearAllReservations
(TODO)ADMIN