|
9 | 9 | import java.util.ArrayList; |
10 | 10 | import java.util.List; |
11 | 11 |
|
12 | | -import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.OrderDataBuilder.getNewOrder; |
13 | | -import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.OrderDataBuilder.getOrderDataWithMissingProductId; |
| 12 | +import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.OrderDataBuilder.*; |
| 13 | +import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.TokenBuilder.getCredentials; |
14 | 14 | import static org.testng.Assert.assertEquals; |
15 | 15 |
|
16 | 16 | public class SadPathTests extends BaseTest { |
@@ -103,4 +103,74 @@ public void testShouldNotFetchOrder_WhenNoOrderExistsForProductId() { |
103 | 103 | assertEquals(responseObject.get("message"), "No Order found with the given parameters!"); |
104 | 104 | } |
105 | 105 |
|
| 106 | + @Test |
| 107 | + public void testShouldNotUpdateOrder_WhenTokenIsMissing() { |
| 108 | + |
| 109 | + int orderId = 1; |
| 110 | + |
| 111 | + final OrderData updatedOrder = getUpdatedOrder(); |
| 112 | + |
| 113 | + final APIResponse response = this.request.put("/updateOrder/" + orderId, RequestOptions.create() |
| 114 | + .setData(updatedOrder)); |
| 115 | + |
| 116 | + final JSONObject responseObject = new JSONObject(response.text()); |
| 117 | + |
| 118 | + assertEquals(response.status(), 403); |
| 119 | + assertEquals(responseObject.get("message"), "Forbidden! Token is missing!"); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testShouldNotUpdateOrder_WhenOrderIdIsNotFound() { |
| 124 | + final APIResponse authResponse = this.request.post("/auth", RequestOptions.create().setData(getCredentials())); |
| 125 | + |
| 126 | + final JSONObject authResponseObject = new JSONObject(authResponse.text()); |
| 127 | + final String token = authResponseObject.get("token").toString(); |
| 128 | + |
| 129 | + final OrderData updatedOrder = getUpdatedOrder(); |
| 130 | + |
| 131 | + final int orderId = 90; |
| 132 | + |
| 133 | + final APIResponse response = this.request.put("/updateOrder/" + orderId, RequestOptions.create() |
| 134 | + .setHeader("Authorization", token) |
| 135 | + .setData(updatedOrder)); |
| 136 | + |
| 137 | + |
| 138 | + final JSONObject responseObject = new JSONObject(response.text()); |
| 139 | + |
| 140 | + assertEquals(response.status(), 404); |
| 141 | + assertEquals(responseObject.get("message"), "No Order found with the given Order Id!"); |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void testShouldNotUpdateOrder_WhenOrderDetailsAreNotProvided() { |
| 147 | + final APIResponse authResponse = this.request.post("/auth", RequestOptions.create().setData(getCredentials())); |
| 148 | + |
| 149 | + final JSONObject authResponseObject = new JSONObject(authResponse.text()); |
| 150 | + final String token = authResponseObject.get("token").toString(); |
| 151 | + |
| 152 | + final int orderId = 2; |
| 153 | + |
| 154 | + final APIResponse response = this.request.put("/updateOrder/" + orderId, RequestOptions.create() |
| 155 | + .setHeader("Authorization", token)); |
| 156 | + |
| 157 | + final JSONObject responseObject = new JSONObject(response.text()); |
| 158 | + |
| 159 | + assertEquals(response.status(), 400); |
| 160 | + assertEquals(responseObject.get("message"), "Each Order must have user_id, product_id, product_name, product_amount, qty, tax_amt, and total_amt!"); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void testShouldNotUpdateOrderWithInvalidToken() { |
| 165 | + final int orderId = 2; |
| 166 | + |
| 167 | + final APIResponse response = this.request.put("/updateOrder/" + orderId, RequestOptions.create() |
| 168 | + .setHeader("Authorization", "token273678")); |
| 169 | + |
| 170 | + final JSONObject responseObject = new JSONObject(response.text()); |
| 171 | + |
| 172 | + assertEquals(response.status(), 400); |
| 173 | + assertEquals(responseObject.get("message"), "Failed to authenticate token!"); |
| 174 | + } |
| 175 | + |
106 | 176 | } |
0 commit comments