Skip to content

Commit c33bf1b

Browse files
ruiguoamzlawmicha
andauthored
feat(Core): bootstrap Auth configuration before other categories, and fixed analytics integration tests (#475)
* Start to debug integration test * configure Auth before all others * bug not fixed yet, need to verify other stuff * fix Integration test target - add Host application * fix README.md description and step 4 * deleted amplifyconfiguration.json fix README.md step 4, and add to ignore configuration file in gitignore * commit * Fixed a typo, set up array of categories to be initialised in Categories.swift * set up array of categoried to be initialized * move var category under var display * fix a typo Co-authored-by: Michael law <[email protected]>
1 parent c3ae896 commit c33bf1b

File tree

11 files changed

+332
-309
lines changed

11 files changed

+332
-309
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,7 @@ Scripts/ocunit2junit
4545
__pycache__/
4646
*awsconfiguration.json
4747
*amplifyconfiguration.json
48+
awsconfiguration.json
49+
amplifyconfiguration.json
4850
credentials-mc.json
4951

Amplify/Core/Category/CategoryType.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,25 @@ public extension CategoryType {
6161
return "Storage"
6262
}
6363
}
64+
65+
var category: Category {
66+
switch self {
67+
case .analytics:
68+
return Amplify.Analytics
69+
case .api:
70+
return Amplify.API
71+
case .auth:
72+
return Amplify.Auth
73+
case .dataStore:
74+
return Amplify.DataStore
75+
case .hub:
76+
return Amplify.Hub
77+
case .logging:
78+
return Amplify.Logging
79+
case .predictions:
80+
return Amplify.Predictions
81+
case .storage:
82+
return Amplify.Storage
83+
}
84+
}
6485
}

Amplify/Core/Configuration/AmplifyConfiguration.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,27 +97,28 @@ extension Amplify {
9797

9898
let configuration = try Amplify.resolve(configuration: configuration)
9999

100-
// Always configure Logging and Hub first, so they are available to other categoories.
101-
try configure(Logging, using: configuration)
102-
try configure(Hub, using: configuration)
100+
// Always configure Logging, Hub and Auth first, so they are available to other categories.
101+
// Auth is a special case for other plugins which depend on using Auth when being configured themselves.
102+
let manuallyConfiguredCategories = [CategoryType.logging, .hub, .auth]
103+
for categoryType in manuallyConfiguredCategories {
104+
try configure(categoryType.category, using: configuration)
105+
}
103106

104107
// Looping through all categories to ensure we don't accidentally forget a category at some point in the future
105-
let remainingCategories = CategoryType.allCases.filter { $0 != .hub && $0 != .logging }
108+
let remainingCategories = CategoryType.allCases.filter { !manuallyConfiguredCategories.contains($0) }
106109
for categoryType in remainingCategories {
107110
switch categoryType {
108111
case .analytics:
109112
try configure(Analytics, using: configuration)
110113
case .api:
111114
try configure(API, using: configuration)
112-
case .auth:
113-
try configure(Auth, using: configuration)
114115
case .dataStore:
115116
try configure(DataStore, using: configuration)
116117
case .predictions:
117118
try configure(Predictions, using: configuration)
118119
case .storage:
119120
try configure(Storage, using: configuration)
120-
case .hub, .logging:
121+
case .hub, .logging, .auth:
121122
// Already configured
122123
break
123124
}
Lines changed: 17 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -5,135 +5,33 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8-
@testable import Amplify
9-
import AWSMobileClient
8+
import XCTest
9+
import AmplifyPlugins
1010
import AWSPinpoint
11+
12+
@testable import Amplify
1113
@testable import AWSPinpointAnalyticsPlugin
12-
import XCTest
14+
@testable import AmplifyTestCommon
1315

1416
// swiftlint:disable:next type_name
1517
class AWSPinpointAnalyticsPluginIntergrationTests: XCTestCase {
16-
/*
17-
Set up
18-
`amplify init`
19-
`amplify add analytics`
20-
* Apps need authorization to send analytics events. Do you want to allow guests and unauthenticated users to send
21-
analytics events? (we recommend you allow this when getting started) `Yes`
22-
`amplify push`
23-
24-
Pinpoint URL to track events
25-
https://us-west-2.console.aws.amazon.com/pinpoint/home/?region=us-west-2#/apps/xxx/analytics/overview
2618

27-
awsconfiguration.json
28-
{
29-
"UserAgent": "aws-amplify/cli",
30-
"Version": "0.1.0",
31-
"IdentityManager": {
32-
"Default": {}
33-
},
34-
"CredentialsProvider": {
35-
"CognitoIdentity": {
36-
"Default": {
37-
"PoolId": "us-west-2:xxx",
38-
"Region": "us-west-2"
39-
}
40-
}
41-
},
42-
"PinpointAnalytics": {
43-
"Default": {
44-
"AppId": "xxx",
45-
"Region": "us-west-2"
46-
}
47-
},
48-
"PinpointTargeting": {
49-
"Default": {
50-
"Region": "us-west-2"
51-
}
52-
}
53-
}
54-
55-
amplifyconfiguration.json
56-
{
57-
"UserAgent": "aws-amplify-cli/2.0",
58-
"Version": "1.0",
59-
"analytics": {
60-
"plugins": {
61-
"awsPinpointAnalyticsPlugin": {
62-
"pinpointAnalytics": {
63-
"appId": "xxxx",
64-
"region": "us-west-2"
65-
},
66-
"pinpointTargeting": {
67-
"region": "us-west-2"
68-
}
69-
}
70-
}
71-
}
72-
}
73-
*/
74-
let analyticsPluginKey = "awsPinpointAnalyticsPlugin"
19+
static let amplifyConfiguration = "AWSPinpointAnalyticsPluginIntegrationTests-amplifyconfiguration"
20+
static let analyticsPluginKey = "awsPinpointAnalyticsPlugin"
7521

7622
override func setUp() {
77-
let config = [
78-
"CredentialsProvider": [
79-
"CognitoIdentity": [
80-
"Default": [
81-
"PoolId": "us-west-2:xxx",
82-
"Region": "us-west-2"
83-
]
84-
]
85-
]
86-
]
87-
AWSInfo.configureDefaultAWSInfo(config)
88-
89-
let mobileClientIsInitialized = expectation(description: "AWSMobileClient is initialized")
90-
AWSMobileClient.default().initialize { userState, error in
91-
guard error == nil else {
92-
XCTFail("Error initializing AWSMobileClient. Error: \(error!.localizedDescription)")
93-
return
94-
}
95-
guard let userState = userState else {
96-
XCTFail("userState is unexpectedly empty initializing AWSMobileClient")
97-
return
98-
}
99-
if userState != UserState.signedOut {
100-
AWSMobileClient.default().signOut()
101-
}
102-
mobileClientIsInitialized.fulfill()
103-
}
104-
wait(for: [mobileClientIsInitialized], timeout: 100)
105-
print("AWSMobileClient Initialized")
106-
107-
let analyticsConfig = AnalyticsCategoryConfiguration(
108-
plugins: [
109-
"awsPinpointAnalyticsPlugin": [
110-
"pinpointAnalytics": [
111-
"appId": "xxxxx",
112-
"region": "us-west-2"
113-
],
114-
"pinpointTargeting": [
115-
"region": "us-west-2"
116-
],
117-
"autoFlushEventsInterval": 10,
118-
"trackAppSessions": true,
119-
"autoSessionTrackingInterval": 2
120-
]
121-
])
122-
123-
let amplifyConfig = AmplifyConfiguration(analytics: analyticsConfig)
124-
12523
do {
24+
let config = try TestConfigHelper.retrieveAmplifyConfiguration(
25+
forResource: AWSPinpointAnalyticsPluginIntergrationTests.amplifyConfiguration)
26+
try Amplify.add(plugin: AWSAuthPlugin())
12627
try Amplify.add(plugin: AWSPinpointAnalyticsPlugin())
127-
try Amplify.configure(amplifyConfig)
28+
try Amplify.configure(config)
12829
} catch {
129-
XCTFail("Failed to initialize and configure Amplify")
30+
XCTFail("Failed to initialize and configure Amplify \(error)")
13031
}
131-
132-
print("Amplify initialized")
13332
}
13433

13534
override func tearDown() {
136-
print("Amplify reset")
13735
Amplify.reset()
13836
}
13937

@@ -171,7 +69,7 @@ class AWSPinpointAnalyticsPluginIntergrationTests: XCTestCase {
17169
properties: properties)
17270
Amplify.Analytics.identifyUser(userId, withProfile: userProfile)
17371

174-
wait(for: [identifyUserEvent], timeout: 20)
72+
wait(for: [identifyUserEvent], timeout: TestCommonConstants.networkTimeout)
17573
}
17674

17775
func testRecordEventsAreFlushed() {
@@ -199,12 +97,14 @@ class AWSPinpointAnalyticsPluginIntergrationTests: XCTestCase {
19997
"eventPropertyBoolKey": true] as [String: AnalyticsPropertyValue]
20098
let event = BasicAnalyticsEvent(name: "eventName", properties: properties)
20199
Amplify.Analytics.record(event: event)
100+
Amplify.Analytics.flushEvents()
202101

203-
wait(for: [flushEventsInvoked], timeout: 20)
102+
wait(for: [flushEventsInvoked], timeout: TestCommonConstants.networkTimeout)
204103
}
205104

206105
func testGetEscapeHatch() throws {
207-
let plugin = try Amplify.Analytics.getPlugin(for: analyticsPluginKey)
106+
let plugin = try Amplify.Analytics.getPlugin(
107+
for: AWSPinpointAnalyticsPluginIntergrationTests.analyticsPluginKey)
208108
guard let pinpointAnalyticsPlugin = plugin as? AWSPinpointAnalyticsPlugin else {
209109
XCTFail("Could not get plugin of type AWSPinpointAnalyticsPlugin")
210110
return
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<key>CFBundleName</key>
1414
<string>$(PRODUCT_NAME)</string>
1515
<key>CFBundlePackageType</key>
16-
<string>BNDL</string>
16+
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
1717
<key>CFBundleShortVersionString</key>
1818
<string>1.0</string>
1919
<key>CFBundleVersion</key>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## Analytics Integration Tests
2+
3+
The following steps demonstrate how to set up Analytics. Auth category is also required for signing with AWS Pinpoint service and requesting with IAM credentials to allow unauthenticated and authenticated access.
4+
5+
### Set-up
6+
7+
1. `amplify init`
8+
9+
2. `amplify add analytics`
10+
11+
```perl
12+
? Select an Analytics provider: Amazon Pinpoint`
13+
? Provide your pinpoint resource name `yourPinpointResourceName`
14+
? Apps need autorization to send analytics events. Do you want to allow guests and unauthenticated users to send analytics events? (we recommend you allow this when getting started) `Yes`
15+
```
16+
17+
3. `amplify push`
18+
19+
[temporary step]: Until Amplify CLI supports adding the auth section into amplifyconfiguation.json, copy `awsconfiguration.json`'s auth section over
20+
21+
4. Copy `amplifyconfiguration.json` over to folder `AWSPinpointAnalyticPluginIntegrationTests` as `AWSPinpointAnalyticsPluginIntegrationTests-amplifyconfiguration.json`
22+
23+
5. You can now run all of the integration tests.
24+
25+
6. You can run `amplify console analytics` to check what happens at the backend.

0 commit comments

Comments
 (0)