|
8 | 8 | import java.util.Calendar;
|
9 | 9 | import java.util.Date;
|
10 | 10 | import java.util.HashMap;
|
| 11 | +import java.util.Map; |
11 | 12 | import java.util.Set;
|
12 | 13 | import java.util.TreeSet;
|
13 | 14 |
|
|
40 | 41 | import io.mosip.testrig.apirig.utils.CertsUtil;
|
41 | 42 | import io.mosip.testrig.apirig.utils.EncryptionDecrptionUtil;
|
42 | 43 | import io.mosip.testrig.apirig.utils.GlobalConstants;
|
| 44 | +import io.mosip.testrig.apirig.utils.GlobalMethods; |
43 | 45 | import io.mosip.testrig.apirig.utils.JWKKeyUtil;
|
44 | 46 | import io.mosip.testrig.apirig.utils.KeycloakUserManager;
|
45 | 47 | import io.mosip.testrig.apirig.utils.RestClient;
|
46 | 48 | import io.mosip.testrig.apirig.utils.SkipTestCaseHandler;
|
| 49 | +import io.restassured.RestAssured; |
47 | 50 | import io.restassured.response.Response;
|
48 | 51 |
|
49 | 52 | public class EsignetUtil extends AdminTestUtil {
|
@@ -201,7 +204,9 @@ public static String isTestCaseValidForExecution(TestCaseDTO testCaseDTO) {
|
201 | 204 | }
|
202 | 205 | if ((testCaseName.contains("_KycBioAuth_") || testCaseName.contains("_BioAuth_")
|
203 | 206 | || testCaseName.contains("_SendBindingOtp_uin_Email_Valid_Smoke")
|
204 |
| - || testCaseName.contains("ESignet_AuthenticateUserIDP_NonAuth_uin_Otp_Valid_Smoke"))) { |
| 207 | + || testCaseName.contains("ESignet_AuthenticateUserIDP_NonAuth_uin_Otp_Valid_Smoke") |
| 208 | + || testCaseName.contains("ESignet_UpdateOIDCClient_StatusCode_Diff_Token_Neg") |
| 209 | + || testCaseName.contains("ESignet_CreateOIDCClient_StatusCode_Diff_Token_Neg"))) { |
205 | 210 | throw new SkipException(GlobalConstants.FEATURE_NOT_SUPPORTED_MESSAGE);
|
206 | 211 | }
|
207 | 212 |
|
@@ -686,6 +691,7 @@ public static String replaceKeywordValue(String jsonString, String keyword, Stri
|
686 | 691 |
|
687 | 692 | }
|
688 | 693 | }
|
| 694 | + |
689 | 695 | public static String getAuthTransactionId(String oidcTransactionId) {
|
690 | 696 | final String transactionId = oidcTransactionId.replaceAll("_|-", "");
|
691 | 697 | String lengthOfTransactionId = getValueFromEsignetActuator(
|
@@ -1185,4 +1191,155 @@ private static void settriggerESignetKeyGen13(boolean value) {
|
1185 | 1191 | private static boolean gettriggerESignetKeyGen13() {
|
1186 | 1192 | return triggerESignetKeyGen13;
|
1187 | 1193 | }
|
| 1194 | + |
| 1195 | + private static final String TOKEN_URL = EsignetConfigManager.getproperty("keycloak-external-url") |
| 1196 | + + EsignetConfigManager.getproperty("keycloakAuthTokenEndPoint"); |
| 1197 | + private static final String GRANT_TYPE = "client_credentials"; |
| 1198 | + private static final String CLIENT_ID = "client_id"; |
| 1199 | + private static final String CLIENT_SECRET = "client_secret"; |
| 1200 | + private static final String GRANT_TYPE_KEY = "grant_type"; |
| 1201 | + private static final String ACCESS_TOKEN = "access_token"; |
| 1202 | + |
| 1203 | + private static String partnerCookie = null; |
| 1204 | + private static String mobileAuthCookie = null; |
| 1205 | + |
| 1206 | + private static Response sendPostRequest(String url, Map<String, String> params) { |
| 1207 | + try { |
| 1208 | + return RestAssured.given().contentType("application/x-www-form-urlencoded; charset=utf-8") |
| 1209 | + .formParams(params).log().all().when().log().all().post(url); |
| 1210 | + } catch (Exception e) { |
| 1211 | + logger.error("Error sending POST request to URL: " + url, e); |
| 1212 | + return null; |
| 1213 | + } |
| 1214 | + } |
| 1215 | + |
| 1216 | + public static String getAuthTokenFromKeyCloak(String clientId, String clientSecret) { |
| 1217 | + Map<String, String> params = new HashMap<>(); |
| 1218 | + params.put(CLIENT_ID, clientId); |
| 1219 | + params.put(CLIENT_SECRET, clientSecret); |
| 1220 | + params.put(GRANT_TYPE_KEY, GRANT_TYPE); |
| 1221 | + |
| 1222 | + Response response = sendPostRequest(TOKEN_URL, params); |
| 1223 | + |
| 1224 | + if (response == null) { |
| 1225 | + return ""; |
| 1226 | + } |
| 1227 | + logger.info(response.getBody().asString()); |
| 1228 | + |
| 1229 | + JSONObject responseJson = new JSONObject(response.getBody().asString()); |
| 1230 | + return responseJson.optString(ACCESS_TOKEN, ""); |
| 1231 | + } |
| 1232 | + |
| 1233 | + public static String getAuthTokenByRole(String role) { |
| 1234 | + if (role == null) return ""; |
| 1235 | + |
| 1236 | + String roleLowerCase = role.toLowerCase(); |
| 1237 | + switch (roleLowerCase) { |
| 1238 | + case "partner": |
| 1239 | + if (!AdminTestUtil.isValidToken(partnerCookie)) { |
| 1240 | + partnerCookie = getAuthTokenFromKeyCloak(EsignetConfigManager.getPmsClientId(), EsignetConfigManager.getPmsClientSecret()); |
| 1241 | + } |
| 1242 | + return partnerCookie; |
| 1243 | + case "mobileauth": |
| 1244 | + if (!AdminTestUtil.isValidToken(mobileAuthCookie)) { |
| 1245 | + mobileAuthCookie = getAuthTokenFromKeyCloak(EsignetConfigManager.getMPartnerMobileClientId(), EsignetConfigManager.getMPartnerMobileClientSecret()); |
| 1246 | + } |
| 1247 | + return mobileAuthCookie; |
| 1248 | + default: |
| 1249 | + return ""; |
| 1250 | + } |
| 1251 | + } |
| 1252 | + |
| 1253 | + public static Response postRequestWithCookieAndAuthHeader(String url, String jsonInput, String cookieName, String role, |
| 1254 | + String testCaseName) { |
| 1255 | + Response response = null; |
| 1256 | + token = getAuthTokenByRole(role); |
| 1257 | + String apiKey = null; |
| 1258 | + String partnerId = null; |
| 1259 | + JSONObject req = new JSONObject(jsonInput); |
| 1260 | + apiKey = req.getString(GlobalConstants.APIKEY); |
| 1261 | + req.remove(GlobalConstants.APIKEY); |
| 1262 | + partnerId = req.getString(GlobalConstants.PARTNERID); |
| 1263 | + req.remove(GlobalConstants.PARTNERID); |
| 1264 | + |
| 1265 | + HashMap<String, String> headers = new HashMap<>(); |
| 1266 | + headers.put("PARTNER-API-KEY", apiKey); |
| 1267 | + headers.put("PARTNER-ID", partnerId); |
| 1268 | + headers.put(cookieName, "Bearer " + token); |
| 1269 | + jsonInput = req.toString(); |
| 1270 | + if (BaseTestCase.currentModule.equals(GlobalConstants.ESIGNET)) { |
| 1271 | + jsonInput = smtpOtpHandler(jsonInput, testCaseName); |
| 1272 | + } |
| 1273 | + |
| 1274 | + logger.info(GlobalConstants.POST_REQ_URL + url); |
| 1275 | + GlobalMethods.reportRequest(headers.toString(), jsonInput, url); |
| 1276 | + try { |
| 1277 | + response = RestClient.postRequestWithMultipleHeadersWithoutCookie(url, jsonInput, |
| 1278 | + MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON, headers); |
| 1279 | + GlobalMethods.reportResponse(response.getHeaders().asList().toString(), url, response); |
| 1280 | + return response; |
| 1281 | + } catch (Exception e) { |
| 1282 | + logger.error(GlobalConstants.EXCEPTION_STRING_2 + e); |
| 1283 | + return response; |
| 1284 | + } |
| 1285 | + } |
| 1286 | + |
| 1287 | + public static Response postWithBodyAndBearerToken(String url, String jsonInput, String cookieName, |
| 1288 | + String role, String testCaseName, String idKeyName) { |
| 1289 | + Response response = null; |
| 1290 | + if (testCaseName.contains("Invalid_Token")) { |
| 1291 | + token = "xyz"; |
| 1292 | + } else if (testCaseName.contains("NOAUTH")) { |
| 1293 | + token = ""; |
| 1294 | + } else { |
| 1295 | + token = getAuthTokenByRole(role); |
| 1296 | + } |
| 1297 | + logger.info(GlobalConstants.POST_REQ_URL + url); |
| 1298 | + GlobalMethods.reportRequest(null, jsonInput, url); |
| 1299 | + try { |
| 1300 | + response = RestClient.postRequestWithBearerToken(url, jsonInput, MediaType.APPLICATION_JSON, |
| 1301 | + MediaType.APPLICATION_JSON, cookieName, token); |
| 1302 | + GlobalMethods.reportResponse(response.getHeaders().asList().toString(), url, response); |
| 1303 | + |
| 1304 | + return response; |
| 1305 | + } catch (Exception e) { |
| 1306 | + logger.error(GlobalConstants.EXCEPTION_STRING_2 + e); |
| 1307 | + return response; |
| 1308 | + } |
| 1309 | + } |
| 1310 | + |
| 1311 | + public static Response putWithPathParamsAndBodyAndBearerToken(String url, String jsonInput, String cookieName, String role, |
| 1312 | + String testCaseName, String pathParams) { |
| 1313 | + Response response = null; |
| 1314 | + logger.info("inputJson is::" + jsonInput); |
| 1315 | + JSONObject req = new JSONObject(jsonInput); |
| 1316 | + logger.info(GlobalConstants.REQ_STR + req); |
| 1317 | + HashMap<String, String> pathParamsMap = new HashMap<>(); |
| 1318 | + String[] params = pathParams.split(","); |
| 1319 | + for (String param : params) { |
| 1320 | + logger.info("param is::" + param); |
| 1321 | + if (req.has(param)) { |
| 1322 | + logger.info(GlobalConstants.REQ_STR + req); |
| 1323 | + pathParamsMap.put(param, req.get(param).toString()); |
| 1324 | + req.remove(param); |
| 1325 | + } else |
| 1326 | + logger.error(GlobalConstants.ERROR_STRING_2 + param + GlobalConstants.IN_STRING + jsonInput); |
| 1327 | + } |
| 1328 | + if (testCaseName.contains("Invalid_Token")) { |
| 1329 | + token = "xyz"; |
| 1330 | + } else { |
| 1331 | + token = getAuthTokenByRole(role); |
| 1332 | + } |
| 1333 | + logger.info(GlobalConstants.PUT_REQ_STRING + url); |
| 1334 | + GlobalMethods.reportRequest(null, req.toString(), url); |
| 1335 | + try { |
| 1336 | + response = RestClient.putWithPathParamsBodyAndBearerToken(url, pathParamsMap, req.toString(), |
| 1337 | + MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON, cookieName, token); |
| 1338 | + GlobalMethods.reportResponse(response.getHeaders().asList().toString(), url, response); |
| 1339 | + return response; |
| 1340 | + } catch (Exception e) { |
| 1341 | + logger.error(GlobalConstants.EXCEPTION_STRING_2 + e); |
| 1342 | + return response; |
| 1343 | + } |
| 1344 | + } |
1188 | 1345 | }
|
0 commit comments