Skip to content

Commit

Permalink
IOS-8524: Resolve issue where json structure from user attribute fail…
Browse files Browse the repository at this point in the history
…s to fetch for new accounts

(cherry picked from commit 8479039)
  • Loading branch information
dg-mega committed May 5, 2024
1 parent 12685b8 commit 8869b23
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public enum UserAttributeErrorEntity: Error {
case attributeNotFound
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ public protocol UserAttributeRepositoryProtocol: Sendable {
/// - object: Encodable object to be stored as a JSON string.
func mergeUserAttribute(_ attribute: UserAttributeEntity, key: String, object: Encodable) async throws
func updateUserAttribute(_ attribute: UserAttributeEntity, key: String, value: String) async throws

/// Fetches dictionary structure under the given attribute. If the attribute does not exist or was never set before, it will throw an error. If a optional value has been set previously it will return the value stored under the attribute.
/// - Parameter attribute: UserAttributeEntity location of where the value will be fetched from .
/// - Returns: Optional [String: String] dictionary stored at the attribute, if attribute was never set previously it will throw.
///
/// - Throws: UserAttributeErrorEntity.attributeNotFound if the attribute has never set before.
func userAttribute(for attribute: UserAttributeEntity) async throws -> [String: String]?

/// Retrieve the decodable object from the associated user attribute for the given key. If the object for the attribute and key does not exist it will throw an error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public struct UserAttributeRepository: UserAttributeRepositoryProtocol {

public func mergeUserAttribute(_ attribute: UserAttributeEntity, key: String, object: Encodable) async throws {
let supportedModelDictionary = try object.convertToDictionary()
let currentAppsPreference = try await userAttribute(for: attribute)
let currentAppsPreference = try? await userAttribute(for: attribute)

let contentToSave: [String: Any] = if let existingEncodedString = currentAppsPreference?[key],
existingEncodedString.isNotEmpty,
Expand Down Expand Up @@ -74,8 +74,14 @@ public struct UserAttributeRepository: UserAttributeRepositoryProtocol {
switch result {
case .success(let request):
completion(.success(request.megaStringDictionary))
case .failure:
completion(.failure(GenericErrorEntity()))
case .failure(let error):
let mappedError: any Error = switch error.type {
case .apiERange:
UserAttributeErrorEntity.attributeNotFound
default:
GenericErrorEntity()
}
completion(.failure(mappedError))
}
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public final class MockSdk: MEGASdk {
private var _lastReadNotificationId: Int32
private var _isNodeInheritingSensitivity: Bool
private var _hasVersionsForNode: Bool

private let setUserAttributeTypeMegaSetError: (MEGAUserAttribute) -> MEGAErrorType

public private(set) var sendEvent_Calls = [(
eventType: Int,
message: String,
Expand Down Expand Up @@ -128,7 +129,8 @@ public final class MockSdk: MEGASdk {
enabledNotificationIdList: MEGAIntegerList? = nil,
lastReadNotificationId: Int32 = 0,
isNodeInheritingSensitivity: Bool = false,
hasVersionsForNode: Bool = false
hasVersionsForNode: Bool = false,
setUserAttributeTypeMegaSetError: @escaping (MEGAUserAttribute) -> MEGAErrorType = { _ in .apiOk }
) {
self.nodes = nodes
self.rubbishNodes = rubbishNodes
Expand Down Expand Up @@ -175,6 +177,7 @@ public final class MockSdk: MEGASdk {
_lastReadNotificationId = lastReadNotificationId
_isNodeInheritingSensitivity = isNodeInheritingSensitivity
_hasVersionsForNode = hasVersionsForNode
self.setUserAttributeTypeMegaSetError = setUserAttributeTypeMegaSetError
super.init()
}

Expand Down Expand Up @@ -574,7 +577,7 @@ public final class MockSdk: MEGASdk {
return
}

delegate.onRequestFinish?(self, request: MockRequest(handle: 1), error: MockError(errorType: megaSetError))
delegate.onRequestFinish?(self, request: MockRequest(handle: 1), error: MockError(errorType: setUserAttributeTypeMegaSetError(type)))
}

public override func deviceId() -> String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,35 @@ final class UserAttributeRepositoryTests: XCTestCase {
XCTAssertEqual(sdk.contentConsumptionPreferences[key]?.sorted(), try XCTUnwrap(targetJson).sorted())
}

private func sut(contentConsumptionPreferences: [String: String] = [:]) -> (UserAttributeRepository, MockSdk) {
let sdk = MockSdk(contentConsumptionPreferences: contentConsumptionPreferences)
func testMergeUserAttribute_withErrorWhenRetrievingAttribute_shouldSave() async throws {
let targetJson = """
{"ios":{"timeline":{"mediaType":"videos","location":"cloudDrive","usePreference":false},"sensitives":{"showHiddenNodes":false}},"sensitives":{"onboarded":false}}
""".trim
let key = ContentConsumptionKeysEntity.key
let (sut, sdk) = sut(megaSetError: .apiERange)
let objectToSave = ContentConsumptionEntity(
ios: .init(
timeline: .init(mediaType: .videos, location: .cloudDrive, usePreference: false),
sensitives: .init(showHiddenNodes: false)),
sensitives: .init(onboarded: false))

try await sut.mergeUserAttribute(
.contentConsumptionPreferences,
key: ContentConsumptionKeysEntity.key,
object: objectToSave)

XCTAssertEqual(sdk.contentConsumptionPreferences[key]?.sorted(), try XCTUnwrap(targetJson).sorted())
}

private func sut(
contentConsumptionPreferences: [String: String] = [:],
megaSetError: MEGAErrorType = .apiOk,
setUserAttributeTypeMegaSetError: @escaping (MEGAUserAttribute) -> MEGAErrorType = { _ in .apiOk }
) -> (UserAttributeRepository, MockSdk) {
let sdk = MockSdk(
megaSetError: megaSetError,
contentConsumptionPreferences: contentConsumptionPreferences,
setUserAttributeTypeMegaSetError: setUserAttributeTypeMegaSetError)
return (UserAttributeRepository(sdk: sdk), sdk)
}
}

0 comments on commit 8869b23

Please sign in to comment.