@@ -11,6 +11,7 @@ import datadog.trace.api.config.AppSecConfig
11
11
import datadog.trace.core.DDSpan
12
12
import okhttp3.FormBody
13
13
import okhttp3.HttpUrl
14
+ import okhttp3.OkHttpClient
14
15
import okhttp3.Request
15
16
import okhttp3.RequestBody
16
17
import org.springframework.boot.SpringApplication
@@ -290,17 +291,7 @@ class SpringBootBasedTest extends AppSecHttpServerTest<ConfigurableApplicationCo
290
291
void ' test user event' () {
291
292
setup :
292
293
def client = clientBuilder(). cookieJar(cookieJar()). followRedirects(false ). build()
293
- def formBody = new FormBody.Builder ()
294
- .add(" username" , " admin" )
295
- .add(" password" , " admin" )
296
- .build()
297
-
298
- def loginRequest = request(LOGIN , " POST" , formBody). build()
299
- def loginResponse = client. newCall(loginRequest). execute()
300
- assert loginResponse. code() == LOGIN . status
301
- assert loginResponse. body(). string() == LOGIN . body
302
- TEST_WRITER . waitForTraces(1 )
303
- TEST_WRITER . start() // clear all traces
294
+ doLogin(client, ' admin' , ' admin' )
304
295
305
296
when :
306
297
def request = request(SUCCESS , " GET" , null ). build()
@@ -322,13 +313,7 @@ class SpringBootBasedTest extends AppSecHttpServerTest<ConfigurableApplicationCo
322
313
setup :
323
314
def logMessagePrefix = ' Attempt to replace'
324
315
def client = clientBuilder(). cookieJar(cookieJar()). followRedirects(false ). build()
325
- def formBody = new FormBody.Builder ()
326
- .add(' username' , ' admin' )
327
- .add(' password' , ' admin' )
328
- .build()
329
- def loginRequest = request(LOGIN , ' POST' , formBody). build()
330
- def loginResponse = client. newCall(loginRequest). execute()
331
- assert loginResponse. code() == LOGIN . status
316
+ doLogin(client, ' admin' , ' admin' )
332
317
333
318
when : ' sdk with different user'
334
319
def sdkBody = new FormBody.Builder (). add(" sdkUser" , " sdkUser" ). build()
@@ -350,4 +335,37 @@ class SpringBootBasedTest extends AppSecHttpServerTest<ConfigurableApplicationCo
350
335
event. message. startsWith(logMessagePrefix)
351
336
})
352
337
}
338
+
339
+ void ' test automated user tracking and setUser SDK used simultaneously' () {
340
+ setup :
341
+ def client = clientBuilder(). cookieJar(cookieJar()). followRedirects(false ). build()
342
+ doLogin(client, ' admin' , ' admin' )
343
+
344
+ when :
345
+ def sdkBody = new FormBody.Builder (). add(" sdkEvent" , " setUser" ). add(" sdkUser" , " sdkUser" ). build()
346
+ def sdkRequest = request(SDK , ' POST' , sdkBody). build()
347
+ final response = client. newCall(sdkRequest). execute()
348
+ TEST_WRITER . waitForTraces(1 )
349
+ def span = TEST_WRITER . flatten(). first() as DDSpan
350
+
351
+ then :
352
+ response. code() == SDK . status
353
+ response. body(). string() == SDK . body
354
+ span. getTag(' _dd.appsec.usr.id' ) == ' admin' //
355
+ // SDK should take priority over automated login events
356
+ span. getTag(' usr.id' ) == ' sdkUser'
357
+ span. getTag(' _dd.appsec.user.collection_mode' ) == ' sdk'
358
+ }
359
+
360
+ private void doLogin (final OkHttpClient client , final String username , final String password ) {
361
+ def formBody = new FormBody.Builder ()
362
+ .add(' username' , username)
363
+ .add(' password' , password)
364
+ .build()
365
+ def loginRequest = request(LOGIN , ' POST' , formBody). build()
366
+ def loginResponse = client. newCall(loginRequest). execute()
367
+ assert loginResponse. code() == LOGIN . status
368
+ TEST_WRITER . waitForTraces(1 )
369
+ TEST_WRITER . start() // clear all traces
370
+ }
353
371
}
0 commit comments