-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathRegistrationTests.cs
More file actions
196 lines (176 loc) · 7.57 KB
/
Copy pathRegistrationTests.cs
File metadata and controls
196 lines (176 loc) · 7.57 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using PostHog;
using PostHog.Config;
using PostHog.Library;
namespace RegistrationTests;
public class TheAddPostHogMethod
{
[Fact]
public void RegistersServicesRequiredByPostHogClient()
{
var services = new ServiceCollection();
var configuration = new ConfigurationBuilder().Build();
services.AddSingleton<IConfiguration>(configuration);
services.AddPostHog();
Assert.Contains(services, s => s.ServiceType == typeof(IPostHogClient));
Assert.Contains(services, s => s.ServiceType == typeof(IFeatureFlagCache));
Assert.Contains(services, s => s.ServiceType == typeof(ITaskScheduler));
Assert.Contains(services, s => s.ServiceType == typeof(IHttpClientFactory));
Assert.Contains(services, s => s.ServiceType == typeof(TimeProvider));
Assert.Contains(services, s => s.ServiceType == typeof(ILoggerFactory));
var provider = services.BuildServiceProvider(new ServiceProviderOptions
{
ValidateOnBuild = true,
ValidateScopes = true
});
// Make sure I can create an instance of PostHogClient.
var client = provider.GetRequiredService<IPostHogClient>();
Assert.NotNull(client);
}
[Fact]
public void CanReadConfiguration()
{
var services = new ServiceCollection();
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
["PostHogTest:PersonalApiKey"] = "fake-secret-personal-api-key",
["PostHogTest:ProjectToken"] = "fake-public-project-token",
["PostHogTest:HostUrl"] = "https://test-host/",
["PostHogTest:FeatureFlagPollInterval"] = "00:00:10",
["PostHogTest:FlushAt"] = "10",
["PostHogTest:MaxBatchSize"] = "99",
["PostHogTest:EvaluationContexts:0"] = "main-app",
["PostHogTest:EvaluationContexts:1"] = "api",
["PostHogTest:SuperProperties:Castle"] = "Winterfell",
["PostHogTest:SuperProperties:Family"] = "Starks"
})
.Build();
services.AddSingleton<IConfiguration>(configuration);
services.AddPostHog(
options =>
{
options.UseConfigurationSection(configuration.GetSection("PostHogTest"));
});
var provider = services.BuildServiceProvider(new ServiceProviderOptions
{
ValidateOnBuild = true,
ValidateScopes = true
});
var options = provider.GetRequiredService<IOptions<PostHogOptions>>().Value;
Assert.Equal("fake-public-project-token", options.ProjectToken);
Assert.Equal("fake-secret-personal-api-key", options.PersonalApiKey);
Assert.Equal(new Uri("https://test-host/"), options.HostUrl);
Assert.Equal(TimeSpan.FromSeconds(10), options.FeatureFlagPollInterval);
Assert.Equal(10, options.FlushAt);
Assert.Equal(99, options.MaxBatchSize);
Assert.Equal(new[] { "main-app", "api" }, options.EvaluationContexts);
Assert.Equal(new()
{
["Castle"] = "Winterfell",
["Family"] = "Starks"
}, options.SuperProperties);
// Check the defaults
Assert.Equal(1000, options.MaxQueueSize);
Assert.Equal(TimeSpan.FromSeconds(30), options.FlushInterval);
Assert.Equal(TimeSpan.FromMinutes(10), options.FeatureFlagSentCacheSlidingExpiration);
Assert.Equal(50_000, options.FeatureFlagSentCacheSizeLimit);
}
[Fact]
public void CanReadLegacyProjectApiKeyConfiguration()
{
var services = new ServiceCollection();
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
["PostHogTest:ProjectApiKey"] = "fake-public-project-api-key",
})
.Build();
services.AddSingleton<IConfiguration>(configuration);
services.AddPostHog(options =>
{
options.UseConfigurationSection(configuration.GetSection("PostHogTest"));
});
var provider = services.BuildServiceProvider(new ServiceProviderOptions
{
ValidateOnBuild = true,
ValidateScopes = true
});
var options = provider.GetRequiredService<IOptions<PostHogOptions>>().Value;
Assert.Equal("fake-public-project-api-key", options.ProjectToken);
}
[Fact]
public void ProjectApiKeyAliasMirrorsProjectToken()
{
var options = new PostHogOptions
{
ProjectToken = "fake-public-project-token"
};
#pragma warning disable CS0618
Assert.Equal("fake-public-project-token", options.ProjectApiKey);
options = new PostHogOptions
{
ProjectApiKey = "fake-public-project-api-key"
};
#pragma warning restore CS0618
Assert.Equal("fake-public-project-api-key", options.ProjectToken);
}
[Fact]
public void AllowsOverridingConfiguration()
{
var services = new ServiceCollection();
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
["PostHogTest:PersonalApiKey"] = "fake-secret-personal-api-key",
["PostHogTest:ProjectToken"] = "fake-public-project-token",
["PostHogTest:HostUrl"] = "https://test-host/",
["PostHogTest:FeatureFlagPollInterval"] = "00:00:10",
["PostHogTest:FlushAt"] = "10",
["PostHogTest:MaxBatchSize"] = "99",
["PostHogTest:SuperProperties:Castle"] = "Winterfell",
["PostHogTest:SuperProperties:Family"] = "Starks"
})
.Build();
services.AddSingleton<IConfiguration>(configuration);
services.AddPostHog(
options =>
{
options.UseConfigurationSection(configuration.GetSection("PostHogTest"));
options.PostConfigure(o =>
{
o.FlushAt = 42;
o.MaxBatchSize = 123;
o.MaxQueueSize = 4200;
o.SuperProperties["Castle"] = "Black";
o.SuperProperties.Add("House", "Tully");
});
});
var provider = services.BuildServiceProvider(new ServiceProviderOptions
{
ValidateOnBuild = true,
ValidateScopes = true
});
var options = provider.GetRequiredService<IOptions<PostHogOptions>>().Value;
Assert.Equal("fake-public-project-token", options.ProjectToken);
Assert.Equal("fake-secret-personal-api-key", options.PersonalApiKey);
Assert.Equal(new Uri("https://test-host/"), options.HostUrl);
Assert.Equal(TimeSpan.FromSeconds(10), options.FeatureFlagPollInterval);
Assert.Equal(42, options.FlushAt);
Assert.Equal(123, options.MaxBatchSize);
Assert.Equal(4200, options.MaxQueueSize);
Assert.Equal(new()
{
["Castle"] = "Black",
["Family"] = "Starks",
["House"] = "Tully"
}, options.SuperProperties);
// Check the defaults
Assert.Equal(TimeSpan.FromSeconds(30), options.FlushInterval);
Assert.Equal(TimeSpan.FromMinutes(10), options.FeatureFlagSentCacheSlidingExpiration);
Assert.Equal(50_000, options.FeatureFlagSentCacheSizeLimit);
}
}