新增单元测试项目 封装基础调用api
parent
3b7f8f078e
commit
2a14e5586c
|
@ -0,0 +1,55 @@
|
|||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.Json.Nodes;
|
||||
using Azure.Core;
|
||||
using EI_TestProject;
|
||||
using IRaCIS.Application.Contracts;
|
||||
using IRaCIS.Core.Infrastructure;
|
||||
using IRaCIS.Core.Infrastructure.Extention;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Org.BouncyCastle.Asn1.Crmf;
|
||||
using Org.BouncyCastle.Asn1.Ocsp;
|
||||
using RestSharp;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
public class UserApiTests : IDisposable
|
||||
{
|
||||
private RestClient _client;
|
||||
|
||||
|
||||
public UserApiTests()
|
||||
{
|
||||
// 创建一个 RestClient 对象,并设置基本 URL
|
||||
//client = new RestClient("http://localhost:6100");
|
||||
|
||||
_client = RestHelper.InitRestHelper("http://123.56.94.154:8090/api", "Admin", MD5Helper.Md5("WHxckj@2019"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 删除系统用户
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async void Test_DeleteUserById()
|
||||
{
|
||||
var userId = Guid.Parse("98430000-3e2c-0016-225f-08db7612c7ae");
|
||||
var url = $"/user/deleteUser/{userId}";
|
||||
//测试删除Api
|
||||
var result = await RestHelper.DeleteRequestAsync<ResponseOutput<string>>(url);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// 在测试结束后释放资源(例如关闭网络连接等)
|
||||
_client?.Dispose();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentAssertions" Version="6.11.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.19" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
|
||||
<PackageReference Include="RestSharp" Version="110.2.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector" Version="3.2.0">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\IRaCIS.Core.API\IRaCIS.Core.API.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,111 @@
|
|||
using Azure.Core;
|
||||
using IRaCIS.Core.Infrastructure.Extention;
|
||||
using IRaCIS.Core.Infrastructure;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using RestSharp;
|
||||
using System.Net;
|
||||
using IRaCIS.Application.Contracts;
|
||||
|
||||
namespace EI_TestProject
|
||||
{
|
||||
public static class RestHelper
|
||||
{
|
||||
private static RestClient _client { get; set; }
|
||||
|
||||
private static string _token { get; set; }
|
||||
|
||||
|
||||
|
||||
public static RestClient InitRestHelper(string baseUrl, string userName, string md5Pwd)
|
||||
{
|
||||
_client = new RestClient(baseUrl);
|
||||
|
||||
_client.AddDefaultHeader("Content-Type", "application/json");
|
||||
|
||||
var result = (LoginAndGetAccessTokenAsync<ResponseOutput<LoginReturnDTO>>(userName, md5Pwd)).Result;
|
||||
|
||||
_token = result.Data.JWTStr;
|
||||
|
||||
return _client;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 되쩌삿혤Token
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="userName"></param>
|
||||
/// <param name="md5Pwd"></param>
|
||||
/// <returns></returns>
|
||||
private static async Task<T> LoginAndGetAccessTokenAsync<T>(string userName, string md5Pwd)
|
||||
{
|
||||
var jsonBody = new JObject {
|
||||
{ "UserName", userName.Trim() },
|
||||
{ "Password", md5Pwd }
|
||||
};
|
||||
|
||||
var result = await Post_JsonBodyRequestAsync<T>("/user/login", jsonBody, false);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// json Post 헝헹 룐陋
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="jsonBody"></param>
|
||||
/// <param name="isAddToken"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public static async Task<T> Post_JsonBodyRequestAsync<T>(string url, JObject jsonBody, bool isAddToken = true)
|
||||
{
|
||||
var request = new RestRequest(url, Method.Post);
|
||||
|
||||
if (isAddToken)
|
||||
{
|
||||
var accessToken = _token;
|
||||
request.AddHeader("Authorization", "Bearer " + accessToken);
|
||||
}
|
||||
|
||||
request.AddParameter("application/json", jsonBody.ToString(), ParameterType.RequestBody);
|
||||
|
||||
var response = await _client.ExecuteAsync(request);
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(response.Content);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Error {response.StatusCode}: {response.StatusDescription}. {response.Content}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// URL Delete 헝헹룐陋
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="url"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public static async Task<T> DeleteRequestAsync<T>(string url)
|
||||
{
|
||||
var request = new RestRequest(url, Method.Delete);
|
||||
var accessToken = _token;
|
||||
request.AddHeader("Authorization", "Bearer " + accessToken);
|
||||
|
||||
var response = await _client.ExecuteAsync(request);
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(response.Content);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Error {response.StatusCode}: {response.StatusDescription}. {response.Content}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
namespace EI_TestProject
|
||||
{
|
||||
public class UnitTest1
|
||||
{
|
||||
[Fact]
|
||||
public void Test1()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
global using Xunit;
|
|
@ -27,6 +27,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EI_Image_Viewer_Activation"
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PublishSite", "PublishSite\PublishSite.csproj", "{1CC3F0F8-8701-4F49-837B-DBFDAC2C19ED}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EI_TestProject", "EI_TestProject\EI_TestProject.csproj", "{47F99CA7-E55B-4A0E-A511-7EDF34C57A20}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -175,6 +177,18 @@ Global
|
|||
{1CC3F0F8-8701-4F49-837B-DBFDAC2C19ED}.Release|x64.Build.0 = Release|x64
|
||||
{1CC3F0F8-8701-4F49-837B-DBFDAC2C19ED}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{1CC3F0F8-8701-4F49-837B-DBFDAC2C19ED}.Release|x86.Build.0 = Release|Any CPU
|
||||
{47F99CA7-E55B-4A0E-A511-7EDF34C57A20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{47F99CA7-E55B-4A0E-A511-7EDF34C57A20}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{47F99CA7-E55B-4A0E-A511-7EDF34C57A20}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{47F99CA7-E55B-4A0E-A511-7EDF34C57A20}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{47F99CA7-E55B-4A0E-A511-7EDF34C57A20}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{47F99CA7-E55B-4A0E-A511-7EDF34C57A20}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{47F99CA7-E55B-4A0E-A511-7EDF34C57A20}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{47F99CA7-E55B-4A0E-A511-7EDF34C57A20}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{47F99CA7-E55B-4A0E-A511-7EDF34C57A20}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{47F99CA7-E55B-4A0E-A511-7EDF34C57A20}.Release|x64.Build.0 = Release|Any CPU
|
||||
{47F99CA7-E55B-4A0E-A511-7EDF34C57A20}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{47F99CA7-E55B-4A0E-A511-7EDF34C57A20}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
|
@ -55,10 +55,7 @@ namespace IRaCIS.Application.Services
|
|||
{
|
||||
return ResponseOutput.NotOk("该医院下已经注册有医生,不可以删除。");
|
||||
}
|
||||
//if (_userRepository.Find().Any(t => t.OrganizationId == hospitalId))
|
||||
//{
|
||||
// return ResponseOutput.NotOk("该医院下存在用户,暂时无法删除。");
|
||||
//}
|
||||
|
||||
|
||||
var success = await _hospitalRepository.BatchDeleteNoTrackingAsync(x => x.Id == hospitalId);
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ namespace IRaCIS.Core.Infrastructure.Extention
|
|||
/// <summary>
|
||||
/// 是否成功标记
|
||||
/// </summary>
|
||||
public bool IsSuccess { get; private set; }
|
||||
public bool IsSuccess { get; set; }
|
||||
|
||||
|
||||
public ApiResponseCodeEnum Code { get; set; } = ApiResponseCodeEnum.OK;
|
||||
|
@ -19,13 +19,13 @@ namespace IRaCIS.Core.Infrastructure.Extention
|
|||
/// <summary>
|
||||
/// 消息
|
||||
/// </summary>
|
||||
public string ErrorMessage { get; private set; }
|
||||
public string ErrorMessage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 数据 兼顾以前 Json序列化的时候返回属性名为“Result”
|
||||
/// </summary>
|
||||
[JsonProperty("Result")]
|
||||
public T Data { get; private set; }
|
||||
public T Data { get; set; }
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue