Skip to content

Commit

Permalink
[Parvathy, Rahul] | BAH-3703 | Fix. Improper Session Expiry (#103)
Browse files Browse the repository at this point in the history
* [Parvathy, Rahul] | BAH-3703 | Fix. Improper Session Expiry

* Parvathy | BAH-3703 | Add. Test Cases For Odoo Session Expiry
  • Loading branch information
parvathy00 authored Mar 26, 2024
1 parent 2a65f2d commit 71ed679
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.bahmni.openerp.web;

public class OdooSessionExpiredException extends RuntimeException {

public OdooSessionExpiredException() {
super("Odoo Session Expired");
}

public OdooSessionExpiredException(Throwable cause) {
super("Odoo Session Expired", cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ public static void checkResponse(ResponseEntity<String> responseEntity) throws J
else if (jsonResponse.has("error")) {
JsonNode error = jsonResponse.get("error");
String errorMsg = error.get("message").asText();
int status = error.get("status").asInt();
int status = error.has("status") ? error.get("status").asInt() : error.get("code").asInt();
if (status == 100 || status == 403) throw new OdooSessionExpiredException();
throw new OdooRestException(String.format("Error found in response. Response status: %s. Error message: %s", status, errorMsg));
}
else{
else {
throw new OdooRestException(String.format("Response is empty"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.bahmni.openerp.web.OdooSessionExpiredException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.bahmni.openerp.web.OdooRestException;
Expand Down Expand Up @@ -72,6 +73,10 @@ public String post(String URL, String requestBody) {
logger.error("Post call to {} failed", URL, e);
logger.error("Post data: {}", requestBody);
throw new RuntimeException("Post call to " + URL + " failed", e);
} catch (OdooSessionExpiredException e) {
logger.warn("Session expired for user {}. Recreating user session.", username);
login();
return post(URL, requestBody);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,28 @@ public void shouldThrowOdooRestExceptionWhenResponseIsEmpty() {
});
assertEquals("Response is empty", exception.getMessage());
}

@Test
public void shouldThrowOdooSessionExpiredExceptionWhenOdooSessionExpires() {
ResponseEntity<String> mockResponseEntity = mock(ResponseEntity.class);
when(mockResponseEntity.getStatusCode()).thenReturn(HttpStatus.OK);
when(mockResponseEntity.getBody()).thenReturn("{\"error\":{\"message\":\"Error message\",\"code\":100}}");

OdooSessionExpiredException exception = assertThrows(OdooSessionExpiredException.class, () -> {
responseChecker.checkResponse(mockResponseEntity);
});
assertEquals("Odoo Session Expired", exception.getMessage());
}

@Test
public void shouldThrowOdooSessionExpiredExceptionWhenStatusIsForbidden() {
ResponseEntity<String> mockResponseEntity = mock(ResponseEntity.class);
when(mockResponseEntity.getStatusCode()).thenReturn(HttpStatus.OK);
when(mockResponseEntity.getBody()).thenReturn("{\"error\":{\"message\":\"Error message\",\"status\":403}}");

OdooSessionExpiredException exception = assertThrows(OdooSessionExpiredException.class, () -> {
responseChecker.checkResponse(mockResponseEntity);
});
assertEquals("Odoo Session Expired", exception.getMessage());
}
}

0 comments on commit 71ed679

Please sign in to comment.