IntegrationRoomsManagerTest.swift 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import XCTest
  6. import Foundation
  7. @testable import NextcloudTalk
  8. final class IntegrationRoomsManagerTest: TestBase {
  9. func testJoinNonExistantRoom() throws {
  10. let roomToken = "nonexistantToken"
  11. expectation(forNotification: .NCRoomsManagerDidJoinRoom, object: nil) { notification -> Bool in
  12. XCTAssertEqual(NCRoomsManager.sharedInstance().joiningAttempts, 3)
  13. XCTAssertNotNil(notification.userInfo?["error"])
  14. XCTAssertNotNil(notification.userInfo?["statusCode"])
  15. XCTAssertNotNil(notification.userInfo?["errorReason"])
  16. // swiftlint:disable:next force_cast
  17. XCTAssertEqual(notification.userInfo?["token"] as! String, roomToken)
  18. // There's no NCRoomController when joining fails
  19. XCTAssertNil(notification.userInfo?["roomController"])
  20. return true
  21. }
  22. NCRoomsManager.sharedInstance().joinRoom(roomToken, forCall: false)
  23. waitForExpectations(timeout: TestConstants.timeoutShort)
  24. }
  25. func testJoinLeaveExistantRoom() throws {
  26. let activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
  27. var roomToken = ""
  28. let exp = expectation(description: "\(#function)\(#line)")
  29. // Create a room
  30. NCAPIController.sharedInstance().createRoom(forAccount: activeAccount, withInvite: nil, ofType: .public, andName: "Test Join Room") { room, error in
  31. XCTAssertNil(error)
  32. roomToken = room?.token ?? ""
  33. exp.fulfill()
  34. }
  35. waitForExpectations(timeout: TestConstants.timeoutShort, handler: nil)
  36. // Setup expectations for the DidJoinRoom notification
  37. expectation(forNotification: .NCRoomsManagerDidJoinRoom, object: nil) { notification -> Bool in
  38. XCTAssertEqual(NCRoomsManager.sharedInstance().joiningAttempts, 0)
  39. XCTAssertNil(notification.userInfo?["error"])
  40. XCTAssertNil(notification.userInfo?["statusCode"])
  41. XCTAssertNil(notification.userInfo?["errorReason"])
  42. // swiftlint:disable:next force_cast
  43. XCTAssertEqual(notification.userInfo?["token"] as! String, roomToken)
  44. // Check if the NCRoomController was correctly added to the activeRooms dictionary
  45. XCTAssertNotNil(NCRoomsManager.sharedInstance().activeRooms[roomToken])
  46. // When successfully joined, the NCRoomController should be included in the notification
  47. XCTAssertNotNil(notification.userInfo?["roomController"])
  48. return true
  49. }
  50. // Try to join the room
  51. NCRoomsManager.sharedInstance().joinRoom(roomToken, forCall: false)
  52. waitForExpectations(timeout: TestConstants.timeoutShort)
  53. // Setup expectations for the DidLeaveRoom notification
  54. expectation(forNotification: .NCRoomsManagerDidLeaveRoom, object: nil) { notification -> Bool in
  55. XCTAssertNil(notification.userInfo?["error"])
  56. // swiftlint:disable:next force_cast
  57. XCTAssertEqual(notification.userInfo?["token"] as! String, roomToken)
  58. // Check if the NCRoomController was correctly removed from the activeRooms dictionary
  59. XCTAssertNil(NCRoomsManager.sharedInstance().activeRooms[roomToken])
  60. return true
  61. }
  62. // Try to leave the room
  63. NCRoomsManager.sharedInstance().leaveChat(inRoom: roomToken)
  64. waitForExpectations(timeout: TestConstants.timeoutShort)
  65. }
  66. }