using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using Utilities; using Philips.EDI.Foundation.APIGateway.AutomationTest.Utilities; using Utilities.Wait; namespace Philips.EDI.Foundation.APIGateway.AutomationTest.BusinessLayer { public class HSPIAMBusinessLayer { public string GetRoleId(string idmClientBaseUrl, Dictionary headers, string OrgId, string roleName) { try { string idmClientRoleUrl = $"{idmClientBaseUrl}/Role?organizationId={OrgId}&name={roleName}"; Logger.Info($"Get Role url: {idmClientRoleUrl}"); headers.TryAdd("api-version", "1"); var responseBody = HttpClientUtility.ExecuteAndGetResponse(HttpMethod.Get, idmClientRoleUrl, headers, null); var entry = responseBody["entry"]?.FirstOrDefault(); var roleId = entry?["id"]?.ToString(); Logger.Info($"Role Id: {roleId}"); return roleId; } catch (Exception ex) { Logger.InfoFailedWithException(ex); throw; } } public List GetAllPersmissionsFromRole(string idmClientBaseUrl, Dictionary headers, string roleID) { try { string idmClientPermissionsUrl = $"{idmClientBaseUrl}/Permission?roleId={roleID}"; Logger.Info($"Get Permission url: {idmClientPermissionsUrl}"); var responseBody = HttpClientUtility.ExecuteAndGetResponse(HttpMethod.Get, idmClientPermissionsUrl, headers, null); return responseBody["entry"]?.Select(x => x["name"].ToString()).ToList(); } catch (Exception ex) { Logger.InfoFailedWithException(ex); throw; } } public bool AssignPermissionToRole(string idmClientBaseUrl, Dictionary headers, string roleID, string permissionsJson) { try { return ManipulatePersmissionsInRole(idmClientBaseUrl, "assign-permission", headers, roleID, permissionsJson); } catch (Exception ex) { Logger.InfoFailedWithException(ex); throw; } } public bool RemovePermissionFromRole(string idmClientBaseUrl, Dictionary headers, string roleID, string permissionsJson) { try { return ManipulatePersmissionsInRole(idmClientBaseUrl, "remove-permission", headers, roleID, permissionsJson); } catch (Exception ex) { Logger.InfoFailedWithException(ex); throw; } } private bool ManipulatePersmissionsInRole(string idmClientBaseUrl, string permissionType, Dictionary headers, string roleID, string permissionsJson) { string iamClientRoleUrl = $"{idmClientBaseUrl}/Role/{roleID}/${permissionType}"; Logger.Info($"Role manipulation url: {iamClientRoleUrl}"); var httpContent = HttpClientUtility.CreateHttpContent(permissionsJson); var response = HttpClientUtility.ExecuteAsync(HttpMethod.Post, iamClientRoleUrl, headers, httpContent).Result; Sleep.Seconds(5); return response.StatusCode == HttpStatusCode.OK; } /// /// Getting the user details from IAM /// /// URL to get the user details form IAM /// user mail id to get details /// headers /// List of usersUUID based on mail id public List GetUserDetails(string iamGetUserUrl, string userMailId, Dictionary headers) { string finalUrl = $"{iamGetUserUrl}{userMailId}"; Logger.Info($"Get Users url: {finalUrl}"); headers.TryAdd("api-version","2"); var responseBody = HttpClientUtility.ExecuteAndGetResponse(HttpMethod.Get, finalUrl, headers, null); if (responseBody != null) { List userLoginId = responseBody["entry"]?.Where(x => x["id"] != null).Select(x => x["id"].ToString()).ToList(); return userLoginId; } return null; } /// /// Deleting the user based on userName and respective Organization ID /// /// URL to get the user details form IAM /// IDM client Base URL /// user mail id to be deleted /// headers /// HttpMessage public HttpResponseMessage DeleteUser(string iamGetUserUrl, string idmClientBaseUrl, string userMailId, Dictionary headers) { List userIdtoDelete = GetUserDetails(iamGetUserUrl, userMailId.ToLower(), headers); headers.TryAdd("api-version", "2"); headers.TryAdd("Accept", "application/json"); if (userIdtoDelete.Count >= 1) { string deleteUrl = $"{idmClientBaseUrl}/User/{userIdtoDelete[0]}"; return HttpClientUtility.ExecuteAsync(HttpMethod.Delete, deleteUrl, headers, null).Result; } return null; } } }