fix: fix dandan unit test. #69

This commit is contained in:
cxfksword
2025-02-03 15:20:29 +08:00
parent 8d1dd6c379
commit adb41e8251
6 changed files with 48 additions and 24 deletions

4
.gitignore vendored
View File

@@ -1,7 +1,9 @@
bin/ bin/
obj/ obj/
.vs/ .vs/
.vscode/
.idea/ .idea/
artifacts artifacts
**/.DS_Store **/.DS_Store
*.json *.json
**/.env

View File

@@ -18,5 +18,18 @@ namespace Jellyfin.Plugin.Danmu.Test
options.SingleLine = true; options.SingleLine = true;
options.TimestampFormat = "hh:mm:ss "; options.TimestampFormat = "hh:mm:ss ";
})); }));
[TestInitialize]
public void SetUp()
{
DotNetEnv.Env.TraversePath().Load();
}
[TestCleanup]
public void TearDown()
{
// 清理代码
// 例如,释放资源或重置状态
}
} }
} }

View File

@@ -11,16 +11,8 @@ namespace Jellyfin.Plugin.Danmu.Test
{ {
[TestClass] [TestClass]
public class DandanApiTest public class DandanApiTest : BaseTest
{ {
ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
builder.AddSimpleConsole(options =>
{
options.IncludeScopes = true;
options.SingleLine = true;
options.TimestampFormat = "hh:mm:ss ";
}));
[TestMethod] [TestMethod]
public void TestSearch() public void TestSearch()
{ {

View File

@@ -17,18 +17,8 @@ namespace Jellyfin.Plugin.Danmu.Test
{ {
[TestClass] [TestClass]
public class DandanTest public class DandanTest : BaseTest
{ {
ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
builder.AddSimpleConsole(options =>
{
options.IncludeScopes = true;
options.SingleLine = true;
options.TimestampFormat = "hh:mm:ss ";
}));
[TestMethod] [TestMethod]
public void TestAddMovie() public void TestAddMovie()
{ {

View File

@@ -6,6 +6,7 @@
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="DotNetEnv" Version="3.1.1" />
<PackageReference Include="Google.Protobuf" Version="3.22.0" /> <PackageReference Include="Google.Protobuf" Version="3.22.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.18.2" /> <PackageReference Include="Moq" Version="4.18.2" />

View File

@@ -31,6 +31,32 @@ public class DandanApi : AbstractApi
} }
} }
protected string ApiID {
get
{
var apiId = Environment.GetEnvironmentVariable("DANDAN_API_ID");
if (!string.IsNullOrEmpty(apiId))
{
return apiId;
}
return API_ID;
}
}
protected string ApiSecret {
get
{
var apiSecret = Environment.GetEnvironmentVariable("DANDAN_API_SECRET");
if (!string.IsNullOrEmpty(apiSecret))
{
return apiSecret;
}
return API_SECRET;
}
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="DandanApi"/> class. /// Initializes a new instance of the <see cref="DandanApi"/> class.
/// </summary> /// </summary>
@@ -148,7 +174,7 @@ public class DandanApi : AbstractApi
var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds(); var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
var signature = GenerateSignature(url, timestamp); var signature = GenerateSignature(url, timestamp);
httpClient.DefaultRequestHeaders.Add("X-AppId", API_ID); httpClient.DefaultRequestHeaders.Add("X-AppId", ApiID);
httpClient.DefaultRequestHeaders.Add("X-Signature", signature); httpClient.DefaultRequestHeaders.Add("X-Signature", signature);
httpClient.DefaultRequestHeaders.Add("X-Timestamp", timestamp.ToString()); httpClient.DefaultRequestHeaders.Add("X-Timestamp", timestamp.ToString());
var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false); var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
@@ -158,13 +184,13 @@ public class DandanApi : AbstractApi
protected string GenerateSignature(string url, long timestamp) protected string GenerateSignature(string url, long timestamp)
{ {
if (string.IsNullOrEmpty(API_ID) || string.IsNullOrEmpty(API_SECRET)) if (string.IsNullOrEmpty(ApiID) || string.IsNullOrEmpty(ApiSecret))
{ {
throw new Exception("弹弹接口缺少API_ID和API_SECRET"); throw new Exception("弹弹接口缺少API_ID和API_SECRET");
} }
var uri = new Uri(url); var uri = new Uri(url);
var path = uri.AbsolutePath; var path = uri.AbsolutePath;
var str = $"{API_ID}{timestamp}{path}{API_SECRET}"; var str = $"{ApiID}{timestamp}{path}{ApiSecret}";
using (var sha256 = SHA256.Create()) using (var sha256 = SHA256.Create())
{ {
var hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(str)); var hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(str));