Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pending secure messaging logic to EntryWidget #1124

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion GliaWidgets/Public/Glia/Glia+EntryWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ extension Glia {
engagementLauncher: try getEngagementLauncher(queueIds: queueIds),
theme: theme,
log: loggerPhase.logger,
isAuthenticated: environment.isAuthenticated
isAuthenticated: environment.isAuthenticated,
pendingSecureConversationStatusUpdates: environment.coreSdk.pendingSecureConversationStatusUpdates
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extension EntryWidget {
var theme: Theme
var log: CoreSdkClient.Logger
var isAuthenticated: () -> Bool
var pendingSecureConversationStatusUpdates: CoreSdkClient.PendingSecureConversationStatusUpdates
}
}

Expand All @@ -23,7 +24,8 @@ extension EntryWidget.Environment {
engagementLauncher: engagementLauncher,
theme: .mock(),
log: .mock,
isAuthenticated: { true }
isAuthenticated: { true },
pendingSecureConversationStatusUpdates: { _ in }
)
}
}
Expand Down
16 changes: 14 additions & 2 deletions GliaWidgets/Sources/EntryWidget/EntryWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,23 @@ public final class EntryWidget: NSObject {

// MARK: - Public methods
public extension EntryWidget {
/// Displays the widget as a modal sheet in the given view controller.
/// Displays the widget as a modal sheet in the given view controller. If there is an ongoing secure conversation, the Chat Transcript screen will be opened instead.
///
/// - Parameter viewController: The `UIViewController` in which the widget will be shown as a sheet.
func show(in viewController: UIViewController) {
showSheet(in: viewController)
var pendingSecureConversationExists = false
environment.pendingSecureConversationStatusUpdates { hasPendingSecureConversation in
pendingSecureConversationExists = hasPendingSecureConversation
}
if pendingSecureConversationExists {
do {
try environment.engagementLauncher.startSecureMessaging()
} catch {
viewState = .error
}
} else {
showSheet(in: viewController)
}
}

/// Embeds the widget as a view in the given parent view.
Expand Down
87 changes: 38 additions & 49 deletions GliaWidgetsTests/Sources/EntryWidget/EntryWidgetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import GliaCoreSDK
@testable import GliaWidgets

class EntryWidgetTests: XCTestCase {
private enum Call {
private enum Call: Equatable {
case observeSecureUnreadMessageCount
case start(EngagementKind)
}

func test_secureMessagingIsHiddenWhenUserIsNotAuthenticated() {
Expand All @@ -21,22 +22,13 @@ class EntryWidgetTests: XCTestCase {
completion(.success(mockQueue))
return UUID.mock.uuidString
}
let queuesMonitor = QueuesMonitor(environment: queueMonitorEnvironment)

let engagementLauncher = EngagementLauncher { _, _ in }
let isAuthenticated: () -> Bool = { false }
var environment = EntryWidget.Environment.mock()
environment.isAuthenticated = { false }
environment.queuesMonitor = QueuesMonitor(environment: queueMonitorEnvironment)

let entryWidget = EntryWidget(
queueIds: [mockQueueId],
environment: .init(
observeSecureUnreadMessageCount: { _ in UUID.mock.uuidString },
unsubscribeFromUpdates: { _, _ in },
queuesMonitor: queuesMonitor,
engagementLauncher: engagementLauncher,
theme: .mock(),
log: .mock,
isAuthenticated: isAuthenticated
)
environment: environment
)

entryWidget.show(in: .init())
Expand All @@ -60,21 +52,12 @@ class EntryWidgetTests: XCTestCase {
completion(.success(mockQueue))
return UUID.mock.uuidString
}
let queuesMonitor = QueuesMonitor(environment: queueMonitorEnvironment)
let engagementLauncher = EngagementLauncher { _, _ in }
let isAuthenticated: () -> Bool = { true }
var environment = EntryWidget.Environment.mock()
environment.queuesMonitor = QueuesMonitor(environment: queueMonitorEnvironment)

let entryWidget = EntryWidget(
queueIds: [mockQueueId],
environment: .init(
observeSecureUnreadMessageCount: { _ in UUID.mock.uuidString },
unsubscribeFromUpdates: { _, _ in },
queuesMonitor: queuesMonitor,
engagementLauncher: engagementLauncher,
theme: .mock(),
log: .mock,
isAuthenticated: isAuthenticated
)
environment: environment
)

entryWidget.show(in: .init())
Expand All @@ -97,27 +80,18 @@ class EntryWidgetTests: XCTestCase {
completion(.success(mockQueue))
return UUID.mock.uuidString
}
let queuesMonitor = QueuesMonitor(environment: queueMonitorEnvironment)

let engagementLauncher = EngagementLauncher { _, _ in }
let isAuthenticated: () -> Bool = { true }

let observeMessageCount: (_ completion: @escaping (Result<Int?, Error>) -> Void) -> String? = { completion in
completion(.success(5))
return UUID.mock.uuidString
}

var environment = EntryWidget.Environment.mock()
environment.queuesMonitor = QueuesMonitor(environment: queueMonitorEnvironment)
environment.observeSecureUnreadMessageCount = observeMessageCount

let entryWidget = EntryWidget(
queueIds: [mockQueueId],
environment: .init(
observeSecureUnreadMessageCount: observeMessageCount,
unsubscribeFromUpdates: { _, _ in },
queuesMonitor: queuesMonitor,
engagementLauncher: engagementLauncher,
theme: .mock(),
log: .mock,
isAuthenticated: isAuthenticated
)
environment: environment
)

entryWidget.show(in: .init())
Expand All @@ -138,20 +112,35 @@ class EntryWidgetTests: XCTestCase {
return mockSubscriptionId
}

var environment = EntryWidget.Environment.mock()
environment.observeSecureUnreadMessageCount = observeMessageCount

let entryWidget = EntryWidget(
queueIds: [UUID.mock.uuidString],
environment: .init(
observeSecureUnreadMessageCount: observeMessageCount,
unsubscribeFromUpdates: { _, _ in },
queuesMonitor: .mock(),
engagementLauncher: EngagementLauncher { _, _ in },
theme: .mock(),
log: .mock,
isAuthenticated: { true }
)
environment: environment
)

XCTAssertEqual(envCalls, [.observeSecureUnreadMessageCount])
XCTAssertEqual(entryWidget.unreadSecureMessageSubscriptionId, mockSubscriptionId)
}

func test_secureMessagingIsOpenedIfPendingSecureConversationExists() {
var envCalls: [Call] = []

let engagementLauncher = EngagementLauncher { engagementKind, _ in
envCalls.append(.start(engagementKind))
}
var environment = EntryWidget.Environment.mock()
environment.pendingSecureConversationStatusUpdates = { $0(true) }
environment.engagementLauncher = engagementLauncher

let entryWidget = EntryWidget(
queueIds: [UUID.mock.uuidString],
environment: environment
)

entryWidget.show(in: .init())

XCTAssertEqual(envCalls, [.start(.messaging(.welcome))])
}
}
Loading