2022-06-06 12:38:07 +03:00
|
|
|
//
|
2024-09-06 16:34:30 +03:00
|
|
|
// Copyright 2022-2024 New Vector Ltd.
|
2022-06-06 12:38:07 +03:00
|
|
|
//
|
2025-01-06 11:27:37 +01:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
// Please see LICENSE files in the repository root for full details.
|
2022-06-06 12:38:07 +03:00
|
|
|
//
|
|
|
|
|
|
|
|
@testable import ElementX
|
2023-06-22 13:24:37 +02:00
|
|
|
|
|
|
|
import Combine
|
2022-07-06 14:49:05 +01:00
|
|
|
import Foundation
|
2022-06-06 12:38:07 +03:00
|
|
|
import XCTest
|
|
|
|
|
|
|
|
class BugReportServiceTests: XCTestCase {
|
2023-12-13 09:51:57 +01:00
|
|
|
var bugReportService: BugReportServiceProtocol!
|
2023-03-08 17:04:31 +01:00
|
|
|
|
|
|
|
override func setUpWithError() throws {
|
2023-12-13 09:51:57 +01:00
|
|
|
let bugReportServiceMock = BugReportServiceMock()
|
|
|
|
bugReportServiceMock.underlyingCrashedLastRun = false
|
|
|
|
bugReportServiceMock.submitBugReportProgressListenerReturnValue = .success(SubmitBugReportResponse(reportUrl: "https://www.example.com/123"))
|
|
|
|
bugReportService = bugReportServiceMock
|
2023-03-08 17:04:31 +01:00
|
|
|
}
|
2022-06-06 12:38:07 +03:00
|
|
|
|
|
|
|
func testInitialStateWithMockService() {
|
|
|
|
XCTAssertFalse(bugReportService.crashedLastRun)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSubmitBugReportWithMockService() async throws {
|
2023-02-24 14:51:36 +02:00
|
|
|
let bugReport = BugReport(userID: "@mock:client.com",
|
|
|
|
deviceID: nil,
|
2024-05-21 10:54:16 +02:00
|
|
|
ed25519: nil,
|
|
|
|
curve25519: nil,
|
2023-02-24 14:51:36 +02:00
|
|
|
text: "i cannot send message",
|
2023-02-08 21:52:31 +01:00
|
|
|
includeLogs: true,
|
2023-07-12 10:20:51 +02:00
|
|
|
canContact: false,
|
2023-02-08 21:52:31 +01:00
|
|
|
githubLabels: [],
|
|
|
|
files: [])
|
2023-06-22 13:24:37 +02:00
|
|
|
let progressSubject = CurrentValueSubject<Double, Never>(0.0)
|
|
|
|
let response = try await bugReportService.submitBugReport(bugReport, progressListener: progressSubject).get()
|
2023-06-06 09:24:01 +01:00
|
|
|
XCTAssertFalse(response.reportUrl.isEmpty)
|
2022-06-06 12:38:07 +03:00
|
|
|
}
|
2022-09-12 21:34:53 +03:00
|
|
|
|
2022-06-06 12:38:07 +03:00
|
|
|
func testInitialStateWithRealService() throws {
|
2023-06-29 15:47:04 +02:00
|
|
|
let service = BugReportService(withBaseURL: "https://www.example.com",
|
2022-09-12 21:34:53 +03:00
|
|
|
applicationId: "mock_app_id",
|
2023-08-01 17:28:56 +03:00
|
|
|
sdkGitSHA: "1234",
|
2023-06-23 09:56:22 +03:00
|
|
|
maxUploadSize: ServiceLocator.shared.settings.bugReportMaxUploadSize,
|
2024-07-01 13:53:24 +01:00
|
|
|
session: .mock,
|
|
|
|
appHooks: AppHooks())
|
2022-06-06 12:38:07 +03:00
|
|
|
XCTAssertFalse(service.crashedLastRun)
|
|
|
|
}
|
2022-09-12 21:34:53 +03:00
|
|
|
|
2022-06-06 12:38:07 +03:00
|
|
|
@MainActor func testSubmitBugReportWithRealService() async throws {
|
2023-06-29 15:47:04 +02:00
|
|
|
let service = BugReportService(withBaseURL: "https://www.example.com",
|
2022-09-12 21:34:53 +03:00
|
|
|
applicationId: "mock_app_id",
|
2023-08-01 17:28:56 +03:00
|
|
|
sdkGitSHA: "1234",
|
2023-06-23 09:56:22 +03:00
|
|
|
maxUploadSize: ServiceLocator.shared.settings.bugReportMaxUploadSize,
|
2024-07-01 13:53:24 +01:00
|
|
|
session: .mock,
|
|
|
|
appHooks: AppHooks())
|
2023-02-08 21:52:31 +01:00
|
|
|
|
2023-02-24 14:51:36 +02:00
|
|
|
let bugReport = BugReport(userID: "@mock:client.com",
|
|
|
|
deviceID: nil,
|
2024-05-21 10:54:16 +02:00
|
|
|
ed25519: nil,
|
|
|
|
curve25519: nil,
|
2023-02-24 14:51:36 +02:00
|
|
|
text: "i cannot send message",
|
2023-02-08 21:52:31 +01:00
|
|
|
includeLogs: true,
|
2023-07-12 10:20:51 +02:00
|
|
|
canContact: false,
|
2023-02-08 21:52:31 +01:00
|
|
|
githubLabels: [],
|
|
|
|
files: [])
|
2023-06-22 13:24:37 +02:00
|
|
|
let progressSubject = CurrentValueSubject<Double, Never>(0.0)
|
|
|
|
let response = try await service.submitBugReport(bugReport, progressListener: progressSubject).get()
|
2022-06-06 12:38:07 +03:00
|
|
|
|
2023-06-06 09:24:01 +01:00
|
|
|
XCTAssertEqual(response.reportUrl, "https://example.com/123")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testLogsMaxSize() {
|
|
|
|
// Given a new set of logs
|
|
|
|
var logs = BugReportService.Logs(maxFileSize: 1000)
|
|
|
|
XCTAssertEqual(logs.zippedSize, 0)
|
|
|
|
XCTAssertEqual(logs.originalSize, 0)
|
|
|
|
XCTAssertTrue(logs.files.isEmpty)
|
|
|
|
|
|
|
|
// When adding new files within the size limit
|
|
|
|
logs.appendFile(at: .homeDirectory, zippedSize: 250, originalSize: 1000)
|
|
|
|
logs.appendFile(at: .picturesDirectory, zippedSize: 500, originalSize: 2000)
|
|
|
|
|
|
|
|
// Then the logs should be included
|
|
|
|
XCTAssertEqual(logs.zippedSize, 750)
|
|
|
|
XCTAssertEqual(logs.originalSize, 3000)
|
|
|
|
XCTAssertEqual(logs.files, [.homeDirectory, .picturesDirectory])
|
|
|
|
|
|
|
|
// When adding a new file larger that will exceed the size limit
|
|
|
|
logs.appendFile(at: .homeDirectory, zippedSize: 500, originalSize: 2000)
|
|
|
|
|
|
|
|
// Then the files shouldn't be included.
|
|
|
|
XCTAssertEqual(logs.zippedSize, 750)
|
|
|
|
XCTAssertEqual(logs.originalSize, 3000)
|
|
|
|
XCTAssertEqual(logs.files, [.homeDirectory, .picturesDirectory])
|
2022-06-06 12:38:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class MockURLProtocol: URLProtocol {
|
|
|
|
override func startLoading() {
|
|
|
|
let response = "{\"report_url\":\"https://example.com/123\"}"
|
2023-06-06 09:24:01 +01:00
|
|
|
if let data = response.data(using: .utf8),
|
|
|
|
let url = request.url,
|
|
|
|
let urlResponse = HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil) {
|
2022-06-06 12:38:07 +03:00
|
|
|
client?.urlProtocol(self, didReceive: urlResponse, cacheStoragePolicy: .allowedInMemoryOnly)
|
|
|
|
client?.urlProtocol(self, didLoad: data)
|
|
|
|
client?.urlProtocolDidFinishLoading(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func stopLoading() {
|
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
|
|
|
|
override class func canonicalRequest(for request: URLRequest) -> URLRequest {
|
2022-07-06 14:49:05 +01:00
|
|
|
request
|
2022-06-06 12:38:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
override class func canInit(with request: URLRequest) -> Bool {
|
2022-07-06 14:49:05 +01:00
|
|
|
true
|
2022-06-06 12:38:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension URLSession {
|
|
|
|
static var mock: URLSession {
|
|
|
|
let configuration = URLSessionConfiguration.default
|
|
|
|
configuration.protocolClasses = [MockURLProtocol.self] + (configuration.protocolClasses ?? [])
|
|
|
|
let result = URLSession(configuration: configuration)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|