Skip to content

Commit b00d70a

Browse files
Added sad path tests for auth API and readme updated (#38)
* added sad path tests for auth API * readme updated
1 parent 8077fba commit b00d70a

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This project is the outcome of my self-learning about the API Testing Automation
1919

2020
## :hammer_and_wrench: Talking more about the Scenarios Covered in this project:
2121
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:
22-
- How to write tests for `Get` requests?
22+
- How to write tests for `GET` requests?
2323
- How to write tests for `POST` requests?
2424
- How to write tests for `PUT` requests?
2525
- How to write tests for `PATCH` requests?
@@ -35,12 +35,16 @@ You will get the answers to the following questions and its respective working c
3535
- How to write end-to-end api tests?
3636
- How to write Happy Path scenarios for the APIs?
3737
- How to write Sad Path scenarios for the APIs?
38+
- How to log the Response ?
3839

3940
## :writing_hand: Blog Links
4041
- [What is API Testing?](https://mfaisalkhatri.github.io/2020/08/08/apitesting/)
4142
- [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)
4243
- [Playwright Java API Testing | How to test POST requests?](https://medium.com/@iamfaisalkhatri/playwright-java-api-testing-how-to-test-post-requests-4c9102d3ab03)
4344
- [Playwright Java API Testing | How to test GET requests?](https://medium.com/@iamfaisalkhatri/playwright-java-api-testing-how-to-test-get-requests-c036b984cc6d)
45+
- [Playwright Java API Testing | How to test PUT requests?](https://medium.com/@iamfaisalkhatri/playwright-java-api-testing-how-to-test-put-requests-d6b1d054d64b)
46+
- [Playwright Java API Testing | How to test PATCH requests?](https://medium.com/@iamfaisalkhatri/playwright-java-api-testing-how-to-test-patch-requests-f6b0867d91e7)
47+
- [Playwright Java API Testing | How to test DELETE requests?](https://medium.com/@iamfaisalkhatri/playwright-java-api-testing-how-to-test-delete-requests-2ff77feb0383)
4448

4549
## :question: Need Assistance?
4650

src/test/java/io/github/mfaisalkhatri/api/restfulecommerce/SadPathTests.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.OrderDataBuilder.getPartialUpdatedOrder;
66
import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.OrderDataBuilder.getUpdatedOrder;
77
import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.TokenBuilder.getCredentials;
8+
import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.TokenBuilder.getInvalidCredentials;
89
import static org.testng.Assert.assertEquals;
910

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

295296
@Test
296297
public void testShouldNotDeleteOrder_WhenOrderIdIsNotFound () {
298+
297299
final APIResponse authResponse = this.request.post ("/auth", RequestOptions.create ()
298300
.setData (getCredentials ()));
299301

@@ -330,4 +332,32 @@ public void testShouldNotDeleteOrderWithInvalidToken () {
330332
assertEquals (response.status (), 400);
331333
assertEquals (responseObject.get ("message"), "Failed to authenticate token!");
332334
}
335+
336+
@Test
337+
public void testShouldNotGenerateToken_ForInvalidCredentials () {
338+
339+
final APIResponse response = this.request.post ("/auth", RequestOptions.create ()
340+
.setData (getInvalidCredentials ()));
341+
342+
logResponse (response);
343+
344+
final JSONObject responseObject = new JSONObject (response.text ());
345+
346+
assertEquals (response.status (), 401);
347+
assertEquals (responseObject.get ("message"), "Authentication Failed! Invalid username or password!");
348+
}
349+
350+
@Test
351+
public void testShouldNotGenerateToken_WhenCredentialsAreMissing () {
352+
353+
final APIResponse response = this.request.post ("/auth", RequestOptions.create ());
354+
355+
logResponse (response);
356+
357+
final JSONObject responseObject = new JSONObject (response.text ());
358+
359+
assertEquals (response.status (), 400);
360+
assertEquals (responseObject.get ("message"), "Username and Password is required for authentication!");
361+
}
362+
333363
}

src/test/java/io/github/mfaisalkhatri/api/restfulecommerce/testdata/TokenBuilder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,13 @@ public static TokenData getCredentials() {
88
.password ("secretPass123")
99
.build ();
1010
}
11+
12+
public static TokenData getInvalidCredentials() {
13+
return TokenData.builder ()
14+
.username ("Manager")
15+
.password ("PAssword@123")
16+
.build ();
17+
18+
19+
}
1120
}

test-suite/testng-restfulecommerce.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
<include name="testShouldNotDeleteOrder_WhenTokenIsMissing"/>
2222
<include name="testShouldNotDeleteOrder_WhenOrderIdIsNotFound"/>
2323
<include name="testShouldNotDeleteOrderWithInvalidToken"/>
24+
<include name="testShouldNotGenerateToken_ForInvalidCredentials"/>
25+
<include name="testShouldNotGenerateToken_WhenCredentialsAreMissing"/>
2426
</methods>
2527
</class>
2628
</classes>

0 commit comments

Comments
 (0)