Skip to content

Commit 7f0f707

Browse files
committed
added sad path tests for auth API
1 parent 8077fba commit 7f0f707

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

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

+30
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

+9
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

+2
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)