@@ -74,7 +74,9 @@ void main() {
7474 'Content-Type' : 'application/json' ,
7575 'X-Sentry-Auth' : 'Sentry sentry_version=6, '
7676 'sentry_client=${SentryClient .sentryClient }, '
77- 'sentry_timestamp=${fakeClock .now ().millisecondsSinceEpoch }, '
77+ 'sentry_timestamp=${fakeClock
78+ .now ()
79+ .millisecondsSinceEpoch }, '
7880 'sentry_key=public, '
7981 'sentry_secret=secret' ,
8082 };
@@ -171,10 +173,82 @@ void main() {
171173
172174 await client.close ();
173175 });
176+
177+ test ('$Event userContext overrides client' , () async {
178+ final MockClient httpMock = new MockClient ();
179+ final Clock fakeClock = new Clock .fixed (new DateTime (2017 , 1 , 2 ));
180+
181+ String loggedUserId; // used to find out what user context was sent
182+ httpMock.answerWith ((Invocation invocation) async {
183+ if (invocation.memberName == #close) {
184+ return null ;
185+ }
186+ if (invocation.memberName == #post) {
187+ // parse the body and detect which user context was sent
188+ var bodyData = invocation.namedArguments[new Symbol ("body" )];
189+ var decoded = new Utf8Codec ().decode (bodyData);
190+ var decodedJson = new JsonDecoder ().convert (decoded);
191+ loggedUserId = decodedJson['user' ]['id' ];
192+ return new Response ('' , 401 , headers: < String , String > {
193+ 'x-sentry-error' : 'Invalid api key' ,
194+ });
195+ }
196+ fail ('Unexpected invocation of ${invocation .memberName } in HttpMock' );
197+ });
198+
199+ final clientUserContext = new User (
200+ id: "client_user" ,
201+ username: "username" ,
202+ 203+ ipAddress: "127.0.0.1" );
204+ final eventUserContext = new User (
205+ id: "event_user" ,
206+ username: "username" ,
207+ 208+ ipAddress: "127.0.0.1" ,
209+ extras: {"foo" : "bar" });
210+
211+ final SentryClient client = new SentryClient (
212+ dsn: _testDsn,
213+ httpClient: httpMock,
214+ clock: fakeClock,
215+ uuidGenerator: () => 'X' * 32 ,
216+ compressPayload: false ,
217+ environmentAttributes: const Event (
218+ serverName: 'test.server.com' ,
219+ release: '1.2.3' ,
220+ environment: 'staging' ,
221+ ),
222+ );
223+ client.userContext = clientUserContext;
224+
225+ try {
226+ throw new ArgumentError ('Test error' );
227+ } catch (error, stackTrace) {
228+ final eventWithoutContext =
229+ new Event (exception: error, stackTrace: stackTrace);
230+ final eventWithContext = new Event (
231+ exception: error,
232+ stackTrace: stackTrace,
233+ userContext: eventUserContext);
234+ await client.capture (event: eventWithoutContext);
235+ expect (loggedUserId, clientUserContext.id);
236+ await client.capture (event: eventWithContext);
237+ expect (loggedUserId, eventUserContext.id);
238+ }
239+
240+ await client.close ();
241+ });
174242 });
175243
176244 group ('$Event ' , () {
177245 test ('serializes to JSON' , () {
246+ final user = new User (
247+ id: "user_id" ,
248+ username: "username" ,
249+ 250+ ipAddress: "127.0.0.1" ,
251+ extras: {"foo" : "bar" });
178252 expect (
179253 new Event (
180254 message: 'test-message' ,
@@ -190,6 +264,7 @@ void main() {
190264 'g' : 2 ,
191265 },
192266 fingerprint: < String > [Event .defaultFingerprint, 'foo' ],
267+ userContext: user,
193268 ).toJson (),
194269 < String , dynamic > {
195270 'platform' : 'dart' ,
@@ -203,6 +278,13 @@ void main() {
203278 'tags' : {'a' : 'b' , 'c' : 'd' },
204279 'extra' : {'e' : 'f' , 'g' : 2 },
205280 'fingerprint' : ['{{ default }}' , 'foo' ],
281+ 'user' : {
282+ 'id' : 'user_id' ,
283+ 'username' : 'username' ,
284+ 285+ 'ip_address' : '127.0.0.1' ,
286+ 'extras' : {'foo' : 'bar' }
287+ },
206288 },
207289 );
208290 });
0 commit comments