-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathGlobalRootScopeIntegrationTests.cs
More file actions
145 lines (116 loc) · 4.42 KB
/
GlobalRootScopeIntegrationTests.cs
File metadata and controls
145 lines (116 loc) · 4.42 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
using Sentry.Integrations;
namespace Sentry.Tests.Integrations;
public class GlobalRootScopeIntegrationTests
{
[Fact]
public void Register_GlobalModeEnabled_SetsInstallationIdOnRootScope()
{
// Arrange
var options = new SentryOptions
{
Dsn = ValidDsn,
IsGlobalModeEnabled = true,
AutoSessionTracking = false
};
var hub = Substitute.For<IHub>();
var integration = new GlobalRootScopeIntegration();
// Act
integration.Register(hub, options);
// Assert
hub.Received(1).ConfigureScope(Arg.Any<Action<Scope>>());
}
[Fact]
public void Register_GlobalModeDisabled_DoesNotConfigureScope()
{
// Arrange
var options = new SentryOptions
{
Dsn = ValidDsn,
IsGlobalModeEnabled = false,
AutoSessionTracking = false
};
var hub = Substitute.For<IHub>();
var integration = new GlobalRootScopeIntegration();
// Act
integration.Register(hub, options);
// Assert
hub.DidNotReceive().ConfigureScope(Arg.Any<Action<Scope>>());
}
[Fact]
public void Register_GlobalModeEnabled_SetsUserIdFromInstallationId()
{
// Arrange
var options = new SentryOptions
{
Dsn = ValidDsn,
IsGlobalModeEnabled = true,
AutoSessionTracking = false
};
// Capture the action passed to ConfigureScope
Action<Scope> capturedAction = null;
var hub = Substitute.For<IHub>();
hub.When(h => h.ConfigureScope(Arg.Any<Action<Scope>>()))
.Do(call => capturedAction = call.Arg<Action<Scope>>());
var integration = new GlobalRootScopeIntegration();
integration.Register(hub, options);
// Apply the captured action to a real scope
var scope = new Scope(options);
capturedAction.Should().NotBeNull();
capturedAction(scope);
// The scope's User.Id should be set to the InstallationId
scope.User.Id.Should().Be(options.InstallationId);
}
[Fact]
public void Register_GlobalModeEnabled_DoesNotOverwriteExistingUserId()
{
// Arrange
var options = new SentryOptions
{
Dsn = ValidDsn,
IsGlobalModeEnabled = true,
AutoSessionTracking = false
};
// Capture the action passed to ConfigureScope
Action<Scope> capturedAction = null;
var hub = Substitute.For<IHub>();
hub.When(h => h.ConfigureScope(Arg.Any<Action<Scope>>()))
.Do(call => capturedAction = call.Arg<Action<Scope>>());
var integration = new GlobalRootScopeIntegration();
integration.Register(hub, options);
// Apply the captured action to a scope that already has a User.Id
var scope = new Scope(options);
const string existingUserId = "my-custom-user-id";
scope.User.Id = existingUserId;
capturedAction.Should().NotBeNull();
capturedAction(scope);
// The existing User.Id should not be overwritten
scope.User.Id.Should().Be(existingUserId);
}
[Fact]
public void Enricher_GlobalModeEnabled_DoesNotSetInstallationId()
{
// Verify the enricher no longer sets User.Id when global mode is enabled,
// ensuring users can clear the User.Id set by GlobalRootScopeIntegration.
var options = new SentryOptions { IsGlobalModeEnabled = true };
var enricher = new Sentry.Internal.Enricher(options);
var eventLike = Substitute.For<IEventLike>();
eventLike.Sdk.Returns(new SdkVersion());
eventLike.User = new SentryUser();
eventLike.Contexts = new SentryContexts();
enricher.Apply(eventLike);
eventLike.User.Id.Should().BeNull();
}
[Fact]
public void Enricher_GlobalModeDisabled_SetsInstallationIdAsFallback()
{
// Verify the enricher still sets User.Id when global mode is disabled (e.g. ASP.NET Core).
var options = new SentryOptions { IsGlobalModeEnabled = false };
var enricher = new Sentry.Internal.Enricher(options);
var eventLike = Substitute.For<IEventLike>();
eventLike.Sdk.Returns(new SdkVersion());
eventLike.User = new SentryUser();
eventLike.Contexts = new SentryContexts();
enricher.Apply(eventLike);
eventLike.User.Id.Should().Be(options.InstallationId);
}
}