Skip to content

Commit c11781f

Browse files
authored
fix(Analytics): Making PinpointEndpointProfile a struct. (#3457)
1 parent 4b9d9c9 commit c11781f

File tree

9 files changed

+64
-54
lines changed

9 files changed

+64
-54
lines changed

AmplifyPlugins/Analytics/Sources/AWSPinpointAnalyticsPlugin/AWSPinpointAnalyticsPlugin+ClientBehavior.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extension AWSPinpointAnalyticsPlugin {
1818
}
1919

2020
Task {
21-
let currentEndpointProfile = await pinpoint.currentEndpointProfile()
21+
var currentEndpointProfile = await pinpoint.currentEndpointProfile()
2222
currentEndpointProfile.addUserId(userId)
2323
if let userProfile = userProfile {
2424
currentEndpointProfile.addUserProfile(userProfile)

AmplifyPlugins/Analytics/Tests/AWSPinpointAnalyticsPluginUnitTests/AWSPinpointAnalyticsPluginClientBehaviorTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class AWSPinpointAnalyticsPluginClientBehaviorTests: AWSPinpointAnalyticsPluginT
5757
plan: testPlan,
5858
location: testLocation,
5959
properties: testProperties)
60-
let expectedEndpointProfile = PinpointEndpointProfile(applicationId: "appId",
60+
var expectedEndpointProfile = PinpointEndpointProfile(applicationId: "appId",
6161
endpointId: "endpointId")
6262
expectedEndpointProfile.addUserId(testIdentityId)
6363
expectedEndpointProfile.addUserProfile(userProfile)
@@ -108,7 +108,7 @@ class AWSPinpointAnalyticsPluginClientBehaviorTests: AWSPinpointAnalyticsPluginT
108108
plan: testPlan,
109109
location: testLocation,
110110
properties: testProperties)
111-
let expectedEndpointProfile = PinpointEndpointProfile(applicationId: "appId",
111+
var expectedEndpointProfile = PinpointEndpointProfile(applicationId: "appId",
112112
endpointId: "endpointId")
113113
expectedEndpointProfile.addUserId(testIdentityId)
114114
expectedEndpointProfile.addUserProfile(userProfile)

AmplifyPlugins/Analytics/Tests/AnalyticsHostApp/AWSPinpointAnalyticsPluginIntegrationTests/AWSPinpointAnalyticsPluginIntegrationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class AWSPinpointAnalyticsPluginIntergrationTests: XCTestCase {
7575

7676
// Remove userId from the current endpoint
7777
let endpointClient = endpointClient()
78-
let currentProfile = await endpointClient.currentEndpointProfile()
78+
var currentProfile = await endpointClient.currentEndpointProfile()
7979
currentProfile.addUserId("")
8080
try await endpointClient.updateEndpointProfile(with: currentProfile)
8181
}

AmplifyPlugins/Internal/Sources/InternalAWSPinpoint/Endpoint/EndpointClient.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ actor EndpointClient: EndpointClientBehaviour {
9494
}
9595

9696
private func configure(endpointProfile: PinpointEndpointProfile) async -> PinpointEndpointProfile {
97+
var endpointProfile = endpointProfile
9798
var deviceToken: PinpointEndpointProfile.DeviceToken?
9899
if let tokenData = Self.getStoredData(from: keychain, forKey: Constants.deviceTokenKey, fallbackTo: userDefaults) {
99100
deviceToken = tokenData.asHexString()
@@ -109,6 +110,7 @@ actor EndpointClient: EndpointClientBehaviour {
109110
}
110111

111112
private func updateEndpoint(with endpointProfile: PinpointEndpointProfile) async throws {
113+
var endpointProfile = endpointProfile
112114
endpointProfile.effectiveDate = Date()
113115
let input = createUpdateInput(from: endpointProfile)
114116
log.verbose("UpdateEndpointInput: \(input)")

AmplifyPlugins/Internal/Sources/InternalAWSPinpoint/Endpoint/PinpointEndpointProfile.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import AWSPinpoint
1010
import Foundation
1111

1212
@_spi(InternalAWSPinpoint)
13-
public class PinpointEndpointProfile: Codable {
13+
public struct PinpointEndpointProfile: Codable, Equatable {
1414
typealias DeviceToken = String
1515

1616
var applicationId: String
@@ -45,11 +45,11 @@ public class PinpointEndpointProfile: Codable {
4545
self.user = user
4646
}
4747

48-
public func addUserId(_ userId: String) {
48+
public mutating func addUserId(_ userId: String) {
4949
user.userId = userId
5050
}
5151

52-
public func addUserProfile(_ userProfile: UserProfile) {
52+
public mutating func addUserProfile(_ userProfile: UserProfile) {
5353
if let email = userProfile.email {
5454
setCustomProperty(email, forKey: Constants.AttributeKeys.email)
5555
}
@@ -75,18 +75,18 @@ public class PinpointEndpointProfile: Codable {
7575
}
7676
}
7777

78-
public func setAPNsToken(_ apnsToken: Data) {
78+
public mutating func setAPNsToken(_ apnsToken: Data) {
7979
deviceToken = apnsToken.asHexString()
8080
}
8181

82-
private func addCustomProperties(_ properties: [String: UserProfilePropertyValue]?) {
82+
private mutating func addCustomProperties(_ properties: [String: UserProfilePropertyValue]?) {
8383
guard let properties = properties else { return }
8484
for (key, value) in properties {
8585
setCustomProperty(value, forKey: key)
8686
}
8787
}
8888

89-
private func addUserAttributes(_ attributes: [String: [String]]?) {
89+
private mutating func addUserAttributes(_ attributes: [String: [String]]?) {
9090
guard let attributes = attributes else { return }
9191
let userAttributes = user.userAttributes ?? [:]
9292
user.userAttributes = userAttributes.merging(
@@ -95,7 +95,7 @@ public class PinpointEndpointProfile: Codable {
9595
)
9696
}
9797

98-
private func setCustomProperty(_ value: UserProfilePropertyValue,
98+
private mutating func setCustomProperty(_ value: UserProfilePropertyValue,
9999
forKey key: String) {
100100
if let value = value as? String {
101101
attributes[key] = [value]

AmplifyPlugins/Internal/Tests/InternalAWSPinpointUnitTests/EndpointClientTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class EndpointClientTests: XCTestCase {
7575
XCTAssertNotNil(keychain.dataValues[EndpointClient.Constants.endpointProfileKey])
7676
XCTAssertNotNil(keychain.dataValues[EndpointClient.Constants.deviceTokenKey])
7777
XCTAssertEqual(archiver.decodeCount, 1)
78-
XCTAssertTrue(endpointProfile === storedEndpointProfile, "Expected stored PinpointEndpointProfile object")
78+
XCTAssertNotEqual(endpointProfile, storedEndpointProfile, "Expected updated PinpointEndpointProfile object")
7979
XCTAssertEqual(endpointProfile.applicationId, currentApplicationId)
8080
XCTAssertEqual(endpointProfile.endpointId, currentEndpointId)
8181
XCTAssertEqual(endpointProfile.deviceToken, newToken.asHexString())
@@ -104,7 +104,7 @@ class EndpointClientTests: XCTestCase {
104104
XCTAssertEqual(keychain.dataForKeyCountMap[EndpointClient.Constants.endpointProfileKey], 1)
105105
XCTAssertEqual(keychain.dataForKeyCountMap[EndpointClient.Constants.deviceTokenKey], 1)
106106
XCTAssertEqual(archiver.decodeCount, 0)
107-
XCTAssertFalse(endpointProfile === storedEndpointProfile, "Expected new PinpointEndpointProfile object")
107+
XCTAssertNotEqual(endpointProfile, storedEndpointProfile, "Expected new PinpointEndpointProfile object")
108108
XCTAssertEqual(endpointProfile.applicationId, currentApplicationId)
109109
XCTAssertEqual(endpointProfile.endpointId, currentEndpointId)
110110
XCTAssertEqual(endpointProfile.deviceToken, newToken?.asHexString())
@@ -128,7 +128,7 @@ class EndpointClientTests: XCTestCase {
128128
func testUpdateEndpointProfile_withAPNsToken_withoutStoredToken_shouldSaveToken() async {
129129
keychain.resetCounters()
130130

131-
let endpoint = PinpointEndpointProfile(applicationId: "applicationId",
131+
var endpoint = PinpointEndpointProfile(applicationId: "applicationId",
132132
endpointId: "endpointId")
133133
endpoint.setAPNsToken(Data(hexString: newTokenHex)!)
134134
try? await endpointClient.updateEndpointProfile(with: endpoint)
@@ -142,7 +142,7 @@ class EndpointClientTests: XCTestCase {
142142
storeToken("oldToken")
143143
keychain.resetCounters()
144144

145-
let endpoint = PinpointEndpointProfile(applicationId: "applicationId",
145+
var endpoint = PinpointEndpointProfile(applicationId: "applicationId",
146146
endpointId: "endpointId")
147147
endpoint.setAPNsToken(Data(hexString: newTokenHex)!)
148148
try? await endpointClient.updateEndpointProfile(with: endpoint)
@@ -156,7 +156,7 @@ class EndpointClientTests: XCTestCase {
156156
storeToken(newTokenHex)
157157
keychain.resetCounters()
158158

159-
let endpoint = PinpointEndpointProfile(applicationId: "applicationId",
159+
var endpoint = PinpointEndpointProfile(applicationId: "applicationId",
160160
endpointId: "endpointId")
161161
endpoint.setAPNsToken(Data(hexString: newTokenHex)!)
162162
try? await endpointClient.updateEndpointProfile(with: endpoint)

AmplifyPlugins/Notifications/Push/Sources/AWSPinpointPushNotificationsPlugin/AWSPinpointPushNotificationsPlugin+ClientBehaviour.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import AppKit
2020

2121
extension AWSPinpointPushNotificationsPlugin {
2222
public func identifyUser(userId: String, userProfile: UserProfile?) async throws {
23-
let currentEndpointProfile = await pinpoint.currentEndpointProfile()
23+
var currentEndpointProfile = await pinpoint.currentEndpointProfile()
2424
currentEndpointProfile.addUserId(userId)
2525
if let userProfile = userProfile {
2626
currentEndpointProfile.addUserProfile(userProfile)
@@ -30,7 +30,7 @@ extension AWSPinpointPushNotificationsPlugin {
3030
}
3131

3232
public func registerDevice(apnsToken: Data) async throws {
33-
let currentEndpointProfile = await pinpoint.currentEndpointProfile()
33+
var currentEndpointProfile = await pinpoint.currentEndpointProfile()
3434
currentEndpointProfile.setAPNsToken(apnsToken)
3535
do {
3636
try await pinpoint.updateEndpoint(with: currentEndpointProfile,

AmplifyPlugins/Notifications/Push/Tests/AWSPinpointPushNotificationsPluginUnitTests/AWSPinpointPushNotificationsPluginClientBehaviourTests.swift

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class AWSPinpointPushNotificationsPluginClientBehaviourTests: AWSPinpointPushNot
2525

2626
XCTAssertEqual(mockPinpoint.currentEndpointProfileCount, 1)
2727
XCTAssertEqual(mockPinpoint.updateEndpointCount, 1)
28-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.user.userId, "newUserId")
28+
let updatedEndpoint = try XCTUnwrap(mockPinpoint.updatedPinpointEndpointProfile)
29+
XCTAssertEqual(updatedEndpoint.user.userId, "newUserId")
2930
}
3031

3132
func testIdentifyUser_withProfile_shouldUpdateUserProfile() async throws {
@@ -38,13 +39,14 @@ class AWSPinpointPushNotificationsPluginClientBehaviourTests: AWSPinpointPushNot
3839

3940
XCTAssertEqual(mockPinpoint.currentEndpointProfileCount, 1)
4041
XCTAssertEqual(mockPinpoint.updateEndpointCount, 1)
41-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.user.userId, "newUserId")
42-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.attributes.count, 3)
43-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.attributes["name"]?.first, "Name")
44-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.attributes["email"]?.first, "Email")
45-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.attributes["plan"]?.first, "Plan")
46-
XCTAssertTrue(mockPinpoint.mockedPinpointEndpointProfile.metrics.isEmpty)
47-
XCTAssertNil(mockPinpoint.mockedPinpointEndpointProfile.user.userAttributes)
42+
let updatedEndpoint = try XCTUnwrap(mockPinpoint.updatedPinpointEndpointProfile)
43+
XCTAssertEqual(updatedEndpoint.user.userId, "newUserId")
44+
XCTAssertEqual(updatedEndpoint.attributes.count, 3)
45+
XCTAssertEqual(updatedEndpoint.attributes["name"]?.first, "Name")
46+
XCTAssertEqual(updatedEndpoint.attributes["email"]?.first, "Email")
47+
XCTAssertEqual(updatedEndpoint.attributes["plan"]?.first, "Plan")
48+
XCTAssertTrue(updatedEndpoint.metrics.isEmpty)
49+
XCTAssertNil(updatedEndpoint.user.userAttributes)
4850
}
4951

5052
func testIdentifyUser_withAnalyticsProfile_shouldUpdateUserProfile() async throws {
@@ -60,14 +62,15 @@ class AWSPinpointPushNotificationsPluginClientBehaviourTests: AWSPinpointPushNot
6062

6163
XCTAssertEqual(mockPinpoint.currentEndpointProfileCount, 1)
6264
XCTAssertEqual(mockPinpoint.updateEndpointCount, 1)
63-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.user.userId, "newUserId")
64-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.attributes.count, 2)
65-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.attributes["attribute"]?.first, "string")
66-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.attributes["boolAttribute"]?.first, "true")
67-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.metrics["metric"], 2.0)
68-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.metrics["intMetric"], 1)
69-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.metrics.count, 2)
70-
XCTAssertNil(mockPinpoint.mockedPinpointEndpointProfile.user.userAttributes)
65+
let updatedEndpoint = try XCTUnwrap(mockPinpoint.updatedPinpointEndpointProfile)
66+
XCTAssertEqual(updatedEndpoint.user.userId, "newUserId")
67+
XCTAssertEqual(updatedEndpoint.attributes.count, 2)
68+
XCTAssertEqual(updatedEndpoint.attributes["attribute"]?.first, "string")
69+
XCTAssertEqual(updatedEndpoint.attributes["boolAttribute"]?.first, "true")
70+
XCTAssertEqual(updatedEndpoint.metrics["metric"], 2.0)
71+
XCTAssertEqual(updatedEndpoint.metrics["intMetric"], 1)
72+
XCTAssertEqual(updatedEndpoint.metrics.count, 2)
73+
XCTAssertNil(updatedEndpoint.user.userAttributes)
7174
}
7275

7376
func testIdentifyUser_withBasicProfile_shouldUpdateUserProfile() async throws {
@@ -83,14 +86,15 @@ class AWSPinpointPushNotificationsPluginClientBehaviourTests: AWSPinpointPushNot
8386

8487
XCTAssertEqual(mockPinpoint.currentEndpointProfileCount, 1)
8588
XCTAssertEqual(mockPinpoint.updateEndpointCount, 1)
86-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.user.userId, "newUserId")
87-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.attributes.count, 2)
88-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.attributes["attribute"]?.first, "string")
89-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.attributes["boolAttribute"]?.first, "true")
90-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.metrics["metric"], 2.0)
91-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.metrics["intMetric"], 1)
92-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.metrics.count, 2)
93-
XCTAssertNil(mockPinpoint.mockedPinpointEndpointProfile.user.userAttributes)
89+
let updatedEndpoint = try XCTUnwrap(mockPinpoint.updatedPinpointEndpointProfile)
90+
XCTAssertEqual(updatedEndpoint.user.userId, "newUserId")
91+
XCTAssertEqual(updatedEndpoint.attributes.count, 2)
92+
XCTAssertEqual(updatedEndpoint.attributes["attribute"]?.first, "string")
93+
XCTAssertEqual(updatedEndpoint.attributes["boolAttribute"]?.first, "true")
94+
XCTAssertEqual(updatedEndpoint.metrics["metric"], 2.0)
95+
XCTAssertEqual(updatedEndpoint.metrics["intMetric"], 1)
96+
XCTAssertEqual(updatedEndpoint.metrics.count, 2)
97+
XCTAssertNil(updatedEndpoint.user.userAttributes)
9498
}
9599

96100
func testIdentifyUser_withPinpointProfile_shouldUpdateUserProfile() async throws {
@@ -110,26 +114,29 @@ class AWSPinpointPushNotificationsPluginClientBehaviourTests: AWSPinpointPushNot
110114

111115
XCTAssertEqual(mockPinpoint.currentEndpointProfileCount, 1)
112116
XCTAssertEqual(mockPinpoint.updateEndpointCount, 1)
113-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.attributes["attribute"]?.first, "string")
114-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.attributes["attributes"]?.count, 2)
115-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.attributes["attributes"]?.first, "string")
116-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.attributes["boolAttribute"]?.first, "true")
117-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.metrics["metric"], 2)
118-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.metrics["intMetric"], 1)
119-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.user.userId, "newUserId")
120-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.user.userAttributes?["roles"]?.count, 2)
121-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.user.userAttributes?["roles"]?.first, "Test")
117+
let updatedEndpoint = try XCTUnwrap(mockPinpoint.updatedPinpointEndpointProfile)
118+
XCTAssertEqual(updatedEndpoint.attributes["attribute"]?.first, "string")
119+
XCTAssertEqual(updatedEndpoint.attributes["attributes"]?.count, 2)
120+
XCTAssertEqual(updatedEndpoint.attributes["attributes"]?.first, "string")
121+
XCTAssertEqual(updatedEndpoint.attributes["boolAttribute"]?.first, "true")
122+
XCTAssertEqual(updatedEndpoint.metrics["metric"], 2)
123+
XCTAssertEqual(updatedEndpoint.metrics["intMetric"], 1)
124+
XCTAssertEqual(updatedEndpoint.user.userId, "newUserId")
125+
XCTAssertEqual(updatedEndpoint.user.userAttributes?["roles"]?.count, 2)
126+
XCTAssertEqual(updatedEndpoint.user.userAttributes?["roles"]?.first, "Test")
122127
}
123128

124129
func testIdentifyUser_withPinpointProfileOptedOutOfMessages_shouldUpdateUserProfileOptOutValue() async throws {
125130
try await plugin.identifyUser(userId: "newUserId", userProfile: nil)
126-
let updatedEndpoint = try XCTUnwrap(mockPinpoint.updatedPinpointEndpointProfile)
131+
var updatedEndpoint = try XCTUnwrap(mockPinpoint.updatedPinpointEndpointProfile)
127132
XCTAssertFalse(updatedEndpoint.isOptOut)
128133

129134
try await plugin.identifyUser(userId: "newUserId", userProfile: PinpointUserProfile(optedOutOfMessages: true))
135+
updatedEndpoint = try XCTUnwrap(mockPinpoint.updatedPinpointEndpointProfile)
130136
XCTAssertTrue(updatedEndpoint.isOptOut)
131137

132138
try await plugin.identifyUser(userId: "newUserId", userProfile: PinpointUserProfile(name: "User"))
139+
updatedEndpoint = try XCTUnwrap(mockPinpoint.updatedPinpointEndpointProfile)
133140
XCTAssertTrue(updatedEndpoint.isOptOut)
134141
}
135142

@@ -140,7 +147,8 @@ class AWSPinpointPushNotificationsPluginClientBehaviourTests: AWSPinpointPushNot
140147

141148
XCTAssertEqual(mockPinpoint.currentEndpointProfileCount, 1)
142149
XCTAssertEqual(mockPinpoint.updateEndpointCount, 1)
143-
XCTAssertEqual(mockPinpoint.mockedPinpointEndpointProfile.deviceToken, apnsToken.asHexString())
150+
let updatedEndpoint = try XCTUnwrap(mockPinpoint.updatedPinpointEndpointProfile)
151+
XCTAssertEqual(updatedEndpoint.deviceToken, apnsToken.asHexString())
144152
}
145153

146154
// MARK: - Record Notification received tests

AmplifyPlugins/Notifications/Push/Tests/AWSPinpointPushNotificationsPluginUnitTests/Mocks/MockAWSPinpoint.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class MockAWSPinpoint: AWSPinpointBehavior {
4343
)
4444
func currentEndpointProfile() async -> PinpointEndpointProfile {
4545
currentEndpointProfileCount += 1
46-
return mockedPinpointEndpointProfile
46+
return updatedPinpointEndpointProfile ?? mockedPinpointEndpointProfile
4747
}
4848

4949
var updateEndpointCount = 0

0 commit comments

Comments
 (0)