Skip to content

Commit da244aa

Browse files
OKTA-857283 - Trim values over 100 char length in Firebase provider (#106)
* Trim values over 100 length in firebase provider * Assert for > 100 length property
1 parent 99adf77 commit da244aa

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

OktaAnalytics.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "OktaAnalytics"
3-
s.version = "2.1"
3+
s.version = "2.2"
44
s.summary = "Implementation of Analytics logger destination"
55
s.description = "Implementation of Analytics logger destination. Requires OktaLogger/Core"
66
s.homepage = "https://github.com/okta/okta-logger-swift"

OktaLogger.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
A5EE1CAAED18D1855CF2AD9D /* libPods-OktaLoggerDemoApp.a in Frameworks */ = {isa = PBXBuildFile; fileRef = E550F471F7327DDB6E1D46AC /* libPods-OktaLoggerDemoApp.a */; };
3737
AF440CECA17F54C6A76918E4 /* libPods-OktaSQLiteStorage.a in Frameworks */ = {isa = PBXBuildFile; fileRef = F629A90EB36B561056639A11 /* libPods-OktaSQLiteStorage.a */; };
3838
B4097F2B3E75E54D45A1009F /* libPods-OktaAnalyticsTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = E765F1F6491E4477B4B829F4 /* libPods-OktaAnalyticsTests.a */; };
39+
B82C62702D4450F300378CB3 /* FirebaseAnalyticsProviderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B82C626E2D444B3B00378CB3 /* FirebaseAnalyticsProviderTests.swift */; };
3940
D54461C6246A02CF00C755F1 /* OktaLogger.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D5C824D42469DBF1005CF747 /* OktaLogger.framework */; };
4041
D5B22D0B246CA47E007ECC2F /* MockLoggerDestination.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5B22D0A246CA47E007ECC2F /* MockLoggerDestination.swift */; };
4142
D5C824E32469DBF1005CF747 /* OktaLoggerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5C824E22469DBF1005CF747 /* OktaLoggerTests.swift */; };
@@ -148,6 +149,7 @@
148149
A2E088AB328FFC328F8E4B01 /* Pods-OktaAnalyticsTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OktaAnalyticsTests.debug.xcconfig"; path = "Target Support Files/Pods-OktaAnalyticsTests/Pods-OktaAnalyticsTests.debug.xcconfig"; sourceTree = "<group>"; };
149150
A98DCB66D1308CC91F19CECF /* Pods-OktaSQLiteStorageTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OktaSQLiteStorageTests.debug.xcconfig"; path = "Target Support Files/Pods-OktaSQLiteStorageTests/Pods-OktaSQLiteStorageTests.debug.xcconfig"; sourceTree = "<group>"; };
150151
AB97EA576638C766A84A9DCD /* libPods-OktaLoggerTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-OktaLoggerTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
152+
B82C626E2D444B3B00378CB3 /* FirebaseAnalyticsProviderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirebaseAnalyticsProviderTests.swift; sourceTree = "<group>"; };
151153
C405B4030120FF72171FD9DA /* Pods-OktaLoggerDemoApp.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OktaLoggerDemoApp.release.xcconfig"; path = "Target Support Files/Pods-OktaLoggerDemoApp/Pods-OktaLoggerDemoApp.release.xcconfig"; sourceTree = "<group>"; };
152154
D55F2C1C246F153200680A96 /* MockLoggerDestination.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MockLoggerDestination.h; sourceTree = "<group>"; };
153155
D55F2C1D246F153200680A96 /* MockLoggerDestination.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MockLoggerDestination.m; sourceTree = "<group>"; };
@@ -486,6 +488,7 @@
486488
isa = PBXGroup;
487489
children = (
488490
F3C0913B2A16EFC90015E9FC /* OktaAnalyticsTests.swift */,
491+
B82C626E2D444B3B00378CB3 /* FirebaseAnalyticsProviderTests.swift */,
489492
F3D5EA112A283D0F00530838 /* OktaAnalytics.xctestplan */,
490493
);
491494
path = OktaAnalyticsTests;
@@ -1204,6 +1207,7 @@
12041207
isa = PBXSourcesBuildPhase;
12051208
buildActionMask = 2147483647;
12061209
files = (
1210+
B82C62702D4450F300378CB3 /* FirebaseAnalyticsProviderTests.swift in Sources */,
12071211
F3D5EA202A283E9700530838 /* OktaAnalyticsTests.swift in Sources */,
12081212
);
12091213
runOnlyForDeploymentPostprocessing = 0;

Sources/OktaAnalytics/FirebaseAnalyticsProvider.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class FirebaseAnalyticsProvider: NSObject, AnalyticsProviderProtocol {
2323
public let defaultProperties: [String: String]?
2424
public let name: String
2525
public let logger: OktaLoggerProtocol?
26+
static let maxPropertyLength = 100
2627

2728
private var firebase: Analytics.Type = Analytics.self
2829
// MARK: - Initializer
@@ -50,7 +51,12 @@ public class FirebaseAnalyticsProvider: NSObject, AnalyticsProviderProtocol {
5051
public func trackEvent(_ eventName: String, withProperties: [String: String]?) {
5152
var properties = withProperties ?? [:]
5253
Dictionary.mergeRecursive(left: &properties, right: defaultProperties)
53-
firebase.logEvent(eventName, parameters: properties)
54+
let trimmedProperties = FirebaseAnalyticsProvider.trimProperties(properties: properties)
55+
firebase.logEvent(eventName, parameters: trimmedProperties)
5456
logger?.log(level: .debug, eventName: eventName, message: nil, properties: properties, file: #file, line: #line, funcName: #function)
5557
}
58+
59+
public static func trimProperties(properties: [String: String]) -> [String: String] {
60+
return properties.mapValues({ String($0.prefix(maxPropertyLength)) })
61+
}
5662
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2020-Present, Okta, Inc. and/or its affiliates. All rights reserved.
3+
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
4+
*
5+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
6+
* Unless required by applicable law or agreed to in writing, software
7+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
8+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
*
10+
* See the License for the specific language governing permissions and limitations under the License.
11+
*/
12+
13+
import XCTest
14+
@testable import OktaAnalytics
15+
import OktaLogger
16+
17+
final class FirebaseAnalyticsProviderTests: XCTestCase {
18+
19+
func testTrackEvent_verifyPropertiesWithMaxCharLimit() {
20+
21+
let properties = ["prop1": "value",
22+
"prop2": "valuevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevalue"
23+
]
24+
XCTAssertTrue(properties["prop2"]!.count > 100)
25+
let trimmedProperties = FirebaseAnalyticsProvider.trimProperties(properties: properties)
26+
trimmedProperties.forEach { key, value in
27+
// All value lengths should be under the limit
28+
XCTAssertTrue(value.count <= FirebaseAnalyticsProvider.maxPropertyLength)
29+
if let originalVal = properties[key] {
30+
// values under the length limit should be the same
31+
if originalVal.count <= FirebaseAnalyticsProvider.maxPropertyLength {
32+
XCTAssertEqual(originalVal, value)
33+
}
34+
}
35+
}
36+
}
37+
38+
}

0 commit comments

Comments
 (0)