using System; using System.Net.Http; using System.Threading.Tasks; using Fab2ApprovalSystem.DMO; using Fab2ApprovalSystem.Models; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace Fab2ApprovalTests.DMO; [TestClass] public class AccountDMOTests { #pragma warning disable CS8618 private static ILogger? _Logger; private static TestContext _TestContext; private static WebApplicationFactory _WebApplicationFactory; #pragma warning restore [ClassInitialize] public static void ClassInitAsync(TestContext testContext) { _TestContext = testContext; _WebApplicationFactory = new WebApplicationFactory(); IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; _Logger = serviceProvider.GetRequiredService>(); } private static void NonThrowTryCatch() { try { throw new Exception(); } catch (Exception) { } } private static void AccountDMO(AppSettings appSettings, HttpClient httpClient) { #pragma warning disable IDE0059, CS8625 UserAccountDMO userAccountDMO = new(); LoginModel loginModel = userAccountDMO.GetUserByID(appSettings.UserId); Task loginResultTask = Fab2ApprovalSystem.DMO.AccountDMO.LoginAsync(httpClient, loginModel: loginModel); loginResultTask.Wait(); Task loginResultTaskB = Fab2ApprovalSystem.DMO.AccountDMO.ExternalAuthSetupAsync(httpClient, authAttempt: null); loginResultTaskB.Wait(); if (loginModel is null) { } #pragma warning restore IDE0059, CS8625 } #if Release [Ignore] #endif [TestMethod] public void AccountDMOIsAttachedOnly() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; AppSettings? appSettings = serviceProvider?.GetRequiredService(); Assert.IsTrue(appSettings is not null); HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); Assert.IsTrue(httpClient is not null); httpClient.BaseAddress = new Uri(appSettings.ApiBaseUrl); if (System.Diagnostics.Debugger.IsAttached) AccountDMO(appSettings, httpClient); _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } }