-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #643 from SavageLearning/feature/seleniun_work_4_t…
…est_env Feature/seleniun work 4 test env
- Loading branch information
Showing
15 changed files
with
362 additions
and
82 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
Machete.Selenium/Integration/HttpClientUtil/HttpClientUtil.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using AutoMapper; | ||
using Machete.Domain; | ||
using Machete.Web.Maps.Api; | ||
using Newtonsoft.Json; | ||
using LookupViewModel = Machete.Web.ViewModel.Api.LookupVM; | ||
using WorkAssignmentViewModel = Machete.Web.ViewModel.Api.WorkAssignmentVM; | ||
|
||
namespace Machete.Test.Integration.HttpClientUtil | ||
{ | ||
/// <summary> | ||
/// The httpClient utility to grab Machete Lookups from the desired URL. | ||
/// Replaces static lookups and allows UI testing on deployed test environments | ||
/// </summary> | ||
public static class HttpClientUtil | ||
{ | ||
private static readonly HttpClient HttpClient; | ||
private static readonly IMapper _mapper; | ||
|
||
public static List<Lookup> TenantLookupsCache => _tenantLookupsCache; | ||
private static List<Lookup> _tenantLookupsCache; | ||
|
||
/// <summary> | ||
/// Initializes httpClient instance with a cookie container | ||
/// Initializes automapper with the necessary profiles | ||
/// </summary> | ||
static HttpClientUtil() | ||
{ | ||
HttpClientHandler clientHandler = | ||
new HttpClientHandler(); | ||
clientHandler.UseCookies = true; | ||
clientHandler.CookieContainer = new CookieContainer(); | ||
HttpClient = new HttpClient(clientHandler); | ||
var configuration = new MapperConfiguration(cfg => | ||
{ | ||
cfg.AddProfile<LookupsMap>(); | ||
cfg.AddProfile<WorkAssignmentsMap>(); | ||
} | ||
); | ||
_mapper = new Mapper(configuration); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="url">The base url for the Machete tenant e.g. https://tenant.example.com/</param> | ||
/// <returns></returns> | ||
/// <exception cref="Exception"></exception> | ||
public static async Task SetTenantLookUpCache(string url) | ||
{ | ||
if (TenantLookupsCache is null) | ||
{ | ||
var httpResponse = await HttpClient.GetAsync($"{url}api/lookups"); | ||
if (!httpResponse.IsSuccessStatusCode) | ||
{ | ||
throw new Exception("Cannot retrieve records"); | ||
} | ||
var content = await httpResponse.Content.ReadAsStringAsync(); | ||
var deserializedContent = JsonConvert.DeserializeObject<List<LookupViewModel>>(content); | ||
var resultList = _mapper.Map<List<Lookup>>(deserializedContent); | ||
_tenantLookupsCache = new List<Lookup>(resultList); | ||
var temp = _tenantLookupsCache.First(x => x.category == "race" && x.text_EN == "Latino").ID; | ||
Console.WriteLine("asdfasdf"); | ||
} | ||
} | ||
|
||
public static int GetLookup(string category, string text_EN) | ||
{ | ||
var lu = _tenantLookupsCache.FirstOrDefault(x => x.category == category && x.text_EN == text_EN); | ||
if (lu is null) | ||
{ | ||
throw new Exception("No record found"); | ||
} | ||
return lu.ID; | ||
} | ||
|
||
public static int GetFirstLookupInCategory(string category) | ||
{ | ||
var lu = _tenantLookupsCache.FirstOrDefault(x => x.category == category); | ||
if (lu is null) | ||
{ | ||
throw new Exception("No record found"); | ||
} | ||
return lu.ID; | ||
} | ||
|
||
public static string GetFirstLookupTextEn(int id) | ||
{ | ||
var lu = _tenantLookupsCache.FirstOrDefault(x => x.ID == id); | ||
if (lu is null) | ||
{ | ||
throw new Exception("No record found"); | ||
} | ||
return lu.text_EN; | ||
} | ||
|
||
public static string TextEN(this int id) => GetFirstLookupTextEn(id); | ||
|
||
public static async Task<int> GetWorkAssignment(int id) | ||
{ | ||
var waPseudoId = ""; | ||
var creds = JsonConvert.SerializeObject(new | ||
{ | ||
username = SharedConfig.SeleniumUser, | ||
passWord = SharedConfig.SeleniumUserPassword | ||
}); | ||
var body = new StringContent(creds, Encoding.UTF8, "application/json"); | ||
var httpResponse = await HttpClient.PostAsync($"{SharedConfig.BaseSeleniumTestUrl}id/login", body); | ||
httpResponse.EnsureSuccessStatusCode(); | ||
var waResponse = await | ||
HttpClient.GetAsync($"{SharedConfig.BaseSeleniumTestUrl}api/workassignments/{id}"); | ||
var httpResponseString = await waResponse.Content.ReadAsStringAsync(); | ||
var deserializedResponse = JsonConvert.DeserializeObject<WorkAssignmentViewModel>(httpResponseString); | ||
var domainWA = _mapper.Map<WorkAssignment>(deserializedResponse); | ||
return domainWA.pseudoID ?? 0; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Machete.Selenium/Integration/HttpClientUtil/ListResponseModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Collections.Generic; | ||
using Machete.Domain; | ||
|
||
namespace Machete.Test.Integration.HttpClientUtil | ||
{ | ||
public class ListResponseModel<T> where T : class | ||
|
||
{ | ||
public List<T> data { get; set; } | ||
} | ||
|
||
public class ItemResponseModel<T> where T : class | ||
{ | ||
public T data { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.