-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathCertificateConfigurationTests.cs
More file actions
154 lines (131 loc) · 6.31 KB
/
CertificateConfigurationTests.cs
File metadata and controls
154 lines (131 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System.ComponentModel.DataAnnotations;
using Microsoft.Extensions.Logging;
using Moq;
namespace Cosmos.DataTransfer.CosmosExtension.UnitTests
{
[TestClass]
public class CertificateConfigurationTests
{
[TestMethod]
public void CosmosSettingsBase_WithDisableSslValidation_ShouldValidateSuccessfully()
{
var settings = new TestableCosmosSettings
{
ConnectionString = "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDj...",
Database = "testDb",
Container = "testContainer",
DisableSslValidation = true
};
var validationResults = settings.Validate(new ValidationContext(settings)).ToList();
Assert.AreEqual(0, validationResults.Count, "Should have no validation errors");
}
[TestMethod]
public void CosmosSettingsBase_WithDisableSslValidationFalse_ShouldUseDefaultValidation()
{
var settings = new TestableCosmosSettings
{
ConnectionString = "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDj...",
Database = "testDb",
Container = "testContainer",
DisableSslValidation = false
};
var validationResults = settings.Validate(new ValidationContext(settings)).ToList();
Assert.AreEqual(0, validationResults.Count, "Settings should validate with DisableSslValidation=false");
}
[TestMethod]
public void CreateClient_WithDisableSslValidation_LogsWarningAndSetsCallback()
{
var loggerMock = new Mock<ILogger>();
var settings = new TestableCosmosSettings
{
ConnectionString = "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
Database = "testDb",
Container = "testContainer",
DisableSslValidation = true
};
var client = CosmosExtensionServices.CreateClient(settings, "TestDisplay", loggerMock.Object);
// Verify warning was logged when DisableSslValidation is true
loggerMock.Verify(
x => x.Log(
LogLevel.Warning,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((v, t) => v.ToString()!.Contains("SSL certificate validation is DISABLED")),
It.IsAny<Exception?>(),
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
Times.Once);
// Verify client was created successfully
Assert.IsNotNull(client, "CosmosClient should be created");
}
[TestMethod]
public void CreateClient_WithoutDisableSslValidation_DoesNotLogWarning()
{
var loggerMock = new Mock<ILogger>();
var settings = new TestableCosmosSettings
{
ConnectionString = "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
Database = "testDb",
Container = "testContainer",
DisableSslValidation = false
};
var client = CosmosExtensionServices.CreateClient(settings, "TestDisplay", loggerMock.Object);
// Verify warning was NOT logged when DisableSslValidation is false
loggerMock.Verify(
x => x.Log(
LogLevel.Warning,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((v, t) => v.ToString()!.Contains("SSL certificate validation is DISABLED")),
It.IsAny<Exception?>(),
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
Times.Never);
// Verify client was created successfully
Assert.IsNotNull(client, "CosmosClient should be created");
}
[TestMethod]
public void CreateClient_WithEnableNetHttpLogging_LogsWarning()
{
var loggerMock = new Mock<ILogger>();
var settings = new TestableCosmosSettings
{
ConnectionString = "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
Database = "testDb",
Container = "testContainer",
EnableNetHttpLogging = true
};
var client = CosmosExtensionServices.CreateClient(settings, "TestDisplay", loggerMock.Object);
loggerMock.Verify(
x => x.Log(
LogLevel.Warning,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((v, t) => v.ToString()!.Contains(".NET HTTP diagnostic logging is ENABLED")),
It.IsAny<Exception?>(),
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
Times.Once);
Assert.IsNotNull(client, "CosmosClient should be created");
}
[TestMethod]
public void CreateClient_WithoutEnableNetHttpLogging_DoesNotLogHttpWarning()
{
var loggerMock = new Mock<ILogger>();
var settings = new TestableCosmosSettings
{
ConnectionString = "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
Database = "testDb",
Container = "testContainer",
EnableNetHttpLogging = false
};
var client = CosmosExtensionServices.CreateClient(settings, "TestDisplay", loggerMock.Object);
loggerMock.Verify(
x => x.Log(
LogLevel.Warning,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((v, t) => v.ToString()!.Contains(".NET HTTP diagnostic logging is ENABLED")),
It.IsAny<Exception?>(),
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
Times.Never);
Assert.IsNotNull(client, "CosmosClient should be created");
}
private class TestableCosmosSettings : CosmosSettingsBase
{
}
}
}