Skip to content

Added sad path tests for auth API and readme updated #38

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 2 commits into from
Oct 8, 2024
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This project is the outcome of my self-learning about the API Testing Automation

## :hammer_and_wrench: Talking more about the Scenarios Covered in this project:
You will get the answers to the following questions and its respective working code example with [Playwright Java](https://playwright.dev/java/docs/api-testing#writing-api-test) framework in this repository:
- How to write tests for `Get` requests?
- How to write tests for `GET` requests?
- How to write tests for `POST` requests?
- How to write tests for `PUT` requests?
- How to write tests for `PATCH` requests?
Expand All @@ -35,12 +35,16 @@ You will get the answers to the following questions and its respective working c
- How to write end-to-end api tests?
- How to write Happy Path scenarios for the APIs?
- How to write Sad Path scenarios for the APIs?
- How to log the Response ?

## :writing_hand: Blog Links
- [What is API Testing?](https://mfaisalkhatri.github.io/2020/08/08/apitesting/)
- [How to perform End to End API Testing using Playwright with Java and TestNG](https://medium.com/@iamfaisalkhatri/how-to-perform-end-to-end-api-testing-using-playwright-with-java-and-testng-26b318927115)
- [Playwright Java API Testing | How to test POST requests?](https://medium.com/@iamfaisalkhatri/playwright-java-api-testing-how-to-test-post-requests-4c9102d3ab03)
- [Playwright Java API Testing | How to test GET requests?](https://medium.com/@iamfaisalkhatri/playwright-java-api-testing-how-to-test-get-requests-c036b984cc6d)
- [Playwright Java API Testing | How to test PUT requests?](https://medium.com/@iamfaisalkhatri/playwright-java-api-testing-how-to-test-put-requests-d6b1d054d64b)
- [Playwright Java API Testing | How to test PATCH requests?](https://medium.com/@iamfaisalkhatri/playwright-java-api-testing-how-to-test-patch-requests-f6b0867d91e7)
- [Playwright Java API Testing | How to test DELETE requests?](https://medium.com/@iamfaisalkhatri/playwright-java-api-testing-how-to-test-delete-requests-2ff77feb0383)

## :question: Need Assistance?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.OrderDataBuilder.getPartialUpdatedOrder;
import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.OrderDataBuilder.getUpdatedOrder;
import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.TokenBuilder.getCredentials;
import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.TokenBuilder.getInvalidCredentials;
import static org.testng.Assert.assertEquals;

import java.util.ArrayList;
Expand Down Expand Up @@ -294,6 +295,7 @@ public void testShouldNotDeleteOrder_WhenTokenIsMissing () {

@Test
public void testShouldNotDeleteOrder_WhenOrderIdIsNotFound () {

final APIResponse authResponse = this.request.post ("/auth", RequestOptions.create ()
.setData (getCredentials ()));

Expand Down Expand Up @@ -330,4 +332,32 @@ public void testShouldNotDeleteOrderWithInvalidToken () {
assertEquals (response.status (), 400);
assertEquals (responseObject.get ("message"), "Failed to authenticate token!");
}

@Test
public void testShouldNotGenerateToken_ForInvalidCredentials () {

final APIResponse response = this.request.post ("/auth", RequestOptions.create ()
.setData (getInvalidCredentials ()));

logResponse (response);

final JSONObject responseObject = new JSONObject (response.text ());

assertEquals (response.status (), 401);
assertEquals (responseObject.get ("message"), "Authentication Failed! Invalid username or password!");
}

@Test
public void testShouldNotGenerateToken_WhenCredentialsAreMissing () {

final APIResponse response = this.request.post ("/auth", RequestOptions.create ());

logResponse (response);

final JSONObject responseObject = new JSONObject (response.text ());

assertEquals (response.status (), 400);
assertEquals (responseObject.get ("message"), "Username and Password is required for authentication!");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,13 @@ public static TokenData getCredentials() {
.password ("secretPass123")
.build ();
}

public static TokenData getInvalidCredentials() {
return TokenData.builder ()
.username ("Manager")
.password ("PAssword@123")
.build ();


}
}
2 changes: 2 additions & 0 deletions test-suite/testng-restfulecommerce.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<include name="testShouldNotDeleteOrder_WhenTokenIsMissing"/>
<include name="testShouldNotDeleteOrder_WhenOrderIdIsNotFound"/>
<include name="testShouldNotDeleteOrderWithInvalidToken"/>
<include name="testShouldNotGenerateToken_ForInvalidCredentials"/>
<include name="testShouldNotGenerateToken_WhenCredentialsAreMissing"/>
</methods>
</class>
</classes>
Expand Down
Loading