Skip to content

Commit

Permalink
added sad path tests for auth API
Browse files Browse the repository at this point in the history
  • Loading branch information
mfaisalkhatri committed Oct 8, 2024
1 parent 8077fba commit 7f0f707
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
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

0 comments on commit 7f0f707

Please sign in to comment.