@@ -309,6 +309,89 @@ final class ChatClient_Tests: XCTestCase {
309
309
XCTAssertTrue ( testEnv. databaseContainer!. removeAllData_called)
310
310
}
311
311
312
+ func test_logout_whenCurrentDevice_removesDevice( ) throws {
313
+ // GIVEN
314
+ let client = ChatClient (
315
+ config: inMemoryStorageConfig,
316
+ environment: testEnv. environment
317
+ )
318
+ let connectionRepository = try XCTUnwrap ( client. connectionRepository as? ConnectionRepository_Mock )
319
+ connectionRepository. disconnectResult = . success( ( ) )
320
+
321
+ // WHEN
322
+ let userId = UserId . unique
323
+ testEnv. authenticationRepository? . mockedCurrentUserId = userId
324
+ try testEnv. databaseContainer? . writeSynchronously {
325
+ try $0. saveCurrentUser ( payload: . dummy( userId: userId, role: . admin) )
326
+ try $0. saveCurrentDevice ( . unique)
327
+ }
328
+ let expectation = self . expectation ( description: " logout completes " )
329
+ client. logout {
330
+ expectation. fulfill ( )
331
+ }
332
+ waitForExpectations ( timeout: defaultTimeout)
333
+
334
+ // THEN
335
+ XCTAssertCall ( ConnectionRepository_Mock . Signature. disconnect, on: testEnv. connectionRepository!)
336
+ XCTAssertEqual ( testEnv. apiClient? . request_endpoint? . path, . devices)
337
+ XCTAssertEqual ( testEnv. apiClient? . request_endpoint? . method, . delete)
338
+ }
339
+
340
+ func test_logout_whenNoCurrentDevice_doesNotRemoveDevice( ) throws {
341
+ // GIVEN
342
+ let client = ChatClient (
343
+ config: inMemoryStorageConfig,
344
+ environment: testEnv. environment
345
+ )
346
+ let connectionRepository = try XCTUnwrap ( client. connectionRepository as? ConnectionRepository_Mock )
347
+ connectionRepository. disconnectResult = . success( ( ) )
348
+
349
+ // WHEN
350
+ let userId = UserId . unique
351
+ testEnv. authenticationRepository? . mockedCurrentUserId = userId
352
+ try testEnv. databaseContainer? . writeSynchronously {
353
+ try $0. saveCurrentUser ( payload: . dummy( userId: userId, role: . admin) )
354
+ }
355
+ let expectation = self . expectation ( description: " logout completes " )
356
+ client. logout {
357
+ expectation. fulfill ( )
358
+ }
359
+ waitForExpectations ( timeout: defaultTimeout)
360
+
361
+ // THEN
362
+ XCTAssertCall ( ConnectionRepository_Mock . Signature. disconnect, on: testEnv. connectionRepository!)
363
+ XCTAssertNil ( testEnv. apiClient? . request_endpoint? . path)
364
+ XCTAssertNil ( testEnv. apiClient? . request_endpoint? . method)
365
+ }
366
+
367
+ func test_logout_whenRemoveDeviceIsFalse_doesNotRemoveDevice( ) throws {
368
+ // GIVEN
369
+ let client = ChatClient (
370
+ config: inMemoryStorageConfig,
371
+ environment: testEnv. environment
372
+ )
373
+ let connectionRepository = try XCTUnwrap ( client. connectionRepository as? ConnectionRepository_Mock )
374
+ connectionRepository. disconnectResult = . success( ( ) )
375
+
376
+ // WHEN
377
+ let userId = UserId . unique
378
+ testEnv. authenticationRepository? . mockedCurrentUserId = userId
379
+ try testEnv. databaseContainer? . writeSynchronously {
380
+ try $0. saveCurrentUser ( payload: . dummy( userId: userId, role: . admin) )
381
+ try $0. saveCurrentDevice ( . unique)
382
+ }
383
+ let expectation = self . expectation ( description: " logout completes " )
384
+ client. logout ( removeDevice: false ) {
385
+ expectation. fulfill ( )
386
+ }
387
+ waitForExpectations ( timeout: defaultTimeout)
388
+
389
+ // THEN
390
+ XCTAssertCall ( ConnectionRepository_Mock . Signature. disconnect, on: testEnv. connectionRepository!)
391
+ XCTAssertNil ( testEnv. apiClient? . request_endpoint? . path)
392
+ XCTAssertNil ( testEnv. apiClient? . request_endpoint? . method)
393
+ }
394
+
312
395
func test_logout_clearsActiveControllers( ) throws {
313
396
// GIVEN
314
397
let client = ChatClient (
@@ -709,7 +792,7 @@ final class ChatClient_Tests: XCTestCase {
709
792
connectionRepository. disconnectResult = . success( ( ) )
710
793
711
794
let expectation = self . expectation ( description: " logout completes " )
712
- client. logout {
795
+ client. logout ( removeDevice : false ) {
713
796
expectation. fulfill ( )
714
797
}
715
798
waitForExpectations ( timeout: defaultTimeout)
0 commit comments