Often when people think of security testing, they think of complicated software scans, request intercepts, and IP address spoofing. But some of the most crucial application security testing can be done simply through making API requests. In this week’s post, I’m taking a look at examples of authentication testing, authorization testing, and field validation testing.
As I have in every post in this Easy Free Automation series, I’ve created an example that you can download here. This is a simple json file that can be run with Newman. As you recall from Easy Free Automation Part III: Services Tests, Newman is the command-line runner for Postman, my favorite API testing tool. If you need to install Postman or Newman, take a look at that post.
For my test application, I’m using the awesome Restful-Booker API. It’s worth noting that this API does come with some intentional bugs, two of which I’ll mention below.
The json file I’ve made available on Github is for the test collection. I didn’t include an environment file this week, because I didn’t need to store any variables. Once you have downloaded the json file, open a command window, change directories to get to the directory where the file is stored, and type newman run easyFreeSecurityTests.json. You should see sixteen tests run and pass.
Let’s take a look at the kinds of tests we’re running. The tests will be easier to interpret if you upload them into Postman; take a look at this post if you need help doing that.
The first six tests in the collection are authentication tests. I am verifying that I can’t log in with invalid credentials. But I’m verifying six different invalid username and password combinations:
- Create token with empty username
- Create token with invalid username
- Create token with empty password
- Create token with invalid password
- Create token with empty username and password
- Create token with invalid username and password
This may seem like overkill, but I have actually encountered bugs where a user can log in if the password field is blank, and where a user can log in if both the username and password are incorrect.
The assertion I am using for each of the six authentication tests is the following:
pm.test(“Bad credential message returned”, function () {
pm.expect(pm.response.text()).to.include(“Bad credentials”);
});
Ordinarily I would assert that the response code I was getting was a 401, but since this request is (incorrectly) returning a 200, I’m instead verifying the text of the response: “Bad credentials”. (For more information on HTTP error codes, see this post.)
The next six tests in my collection are authorization tests. There are three actions in the Restful-Booker that require a valid token: Put, Patch, and Delete. So for each of these requests, I’m testing that I cannot run the request with a missing token, and I cannot run the request with an invalid token:
- Update booking with no token
- Update booking with invalid token
- Partial update booking with no token
- Partial update booking with invalid token
- Delete booking with no token
- Delete booking with invalid token
- Create booking invalid checkin month
- Create booking invalid checkin day
- Create booking invalid checkout month
- Create booking invalid checkout day
In each of these tests, I am validating that I receive an invalid date message: