Skip to content

Commit 9f8aeff

Browse files
authored
chore(datastore): Update single auth integration tests to use V2 Transform (#1936)
1 parent 93cdea5 commit 9f8aeff

File tree

9 files changed

+33
-142
lines changed

9 files changed

+33
-142
lines changed

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginAuthIntegrationTests/DefaultAuthCognito/AWSDataStoreCategoryPluginAuthIntegrationTests.swift

Lines changed: 0 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -78,131 +78,6 @@ class AWSDataStoreCategoryPluginAuthIntegrationTests: AWSDataStoreAuthBaseTest {
7878
XCTAssertEqual(user.username, remoteTodoOwner)
7979
}
8080

81-
/// User1, while signed in, creates some data, called "NewUser1Data" in a local store. We wait for this data to be
82-
/// synced to the cloud and then call `DataStore.clear()` so that data will be destroyed in the local data store and
83-
/// the remote sync engine will be halted. After this finishes, we sign out of user1 and sign in with user2, which
84-
/// will restart the sync engine and read all of data from the backend including "NewUser1Data".
85-
///
86-
/// - Given: A DataStore plugin configured with auth enabled TodoExplicitOwnerField model that can be read others.
87-
/// - When:
88-
/// - The owner user is signed in, user saves a todo, syncReceived successfully
89-
/// - Owner signs out, `DataStore.clear`, then retrieving the todo returns nil
90-
/// - The other user signs in, sync engine is started and does a full sync
91-
/// - The other user is able to retrieve the owner's todo
92-
func testOwnerCreatedDataCanBeReadByOtherUsersForReadableModel() throws {
93-
setup(withModels: ModelsRegistration(), testType: .defaultAuthCognito)
94-
95-
signIn(user: user1)
96-
97-
let id = UUID().uuidString
98-
let localTodo = TodoExplicitOwnerField(id: id, content: "owner created content", owner: nil)
99-
let localTodoSaveInvoked = expectation(description: "local note was saved")
100-
101-
let syncReceivedInvoked = expectation(description: "received SyncReceived event")
102-
var remoteTodoOptional: TodoExplicitOwnerField?
103-
let syncReceivedListener = Amplify.Hub.listen(to: .dataStore, eventName: syncReceived) { payload in
104-
guard let mutationEvent = payload.data as? MutationEvent,
105-
let todo = try? mutationEvent.decodeModel() as? TodoExplicitOwnerField else {
106-
XCTFail("Can't cast payload as mutation event")
107-
return
108-
}
109-
if todo.id == localTodo.id, remoteTodoOptional == nil {
110-
remoteTodoOptional = todo
111-
syncReceivedInvoked.fulfill()
112-
}
113-
}
114-
guard try HubListenerTestUtilities.waitForListener(with: syncReceivedListener, timeout: 5.0) else {
115-
XCTFail("syncReceivedListener registered for hub")
116-
return
117-
}
118-
119-
Amplify.DataStore.save(localTodo) { result in
120-
switch result {
121-
case .success(let note):
122-
localTodoSaveInvoked.fulfill()
123-
case .failure(let error):
124-
XCTFail("Failed to save note \(error)")
125-
}
126-
}
127-
wait(for: [localTodoSaveInvoked], timeout: TestCommonConstants.networkTimeout)
128-
wait(for: [syncReceivedInvoked], timeout: TestCommonConstants.networkTimeout)
129-
guard let remoteTodo = remoteTodoOptional else {
130-
XCTFail("Should have received a SyncReceived event with the remote note reconciled to local store")
131-
return
132-
}
133-
guard let owner = remoteTodo.owner else {
134-
XCTFail("Could not retrieve owner value from remote note")
135-
return
136-
}
137-
138-
signOut()
139-
140-
let clearCompletedInvoked = expectation(description: "clear completed")
141-
Amplify.DataStore.clear { result in
142-
switch result {
143-
case .success:
144-
clearCompletedInvoked.fulfill()
145-
case .failure(let error):
146-
XCTFail("Failed to clear \(error)")
147-
}
148-
}
149-
150-
wait(for: [clearCompletedInvoked], timeout: TestCommonConstants.networkTimeout)
151-
152-
let modelSyncedInvoked = expectation(description: "Model fully synced")
153-
Amplify
154-
.Hub
155-
.publisher(for: .dataStore)
156-
.filter { $0.eventName == HubPayload.EventName.DataStore.ready }
157-
.sink { _ in
158-
modelSyncedInvoked.fulfill()
159-
}.store(in: &requests)
160-
161-
let localNoteOptional = queryModel(TodoExplicitOwnerField.self, byId: id)
162-
XCTAssertNil(localNoteOptional)
163-
164-
let syncStartedInvoked2 = expectation(description: "Sync started after other sign in")
165-
let syncStartedListener2 = Amplify.Hub.listen(to: .dataStore, eventName: syncStarted) { _ in
166-
syncStartedInvoked2.fulfill()
167-
}
168-
guard try HubListenerTestUtilities.waitForListener(with: syncStartedListener2, timeout: 5.0) else {
169-
XCTFail("syncStartedListener2 not registered")
170-
return
171-
}
172-
173-
let syncReceivedInvoked2 = expectation(description: "received SyncReceived event for owner")
174-
var remoteNoteOptional2: TodoExplicitOwnerField?
175-
let syncReceivedListener2 = Amplify.Hub.listen(to: .dataStore, eventName: syncReceived) { payload in
176-
guard let mutationEvent = payload.data as? MutationEvent,
177-
let note = try? mutationEvent.decodeModel() as? TodoExplicitOwnerField else {
178-
XCTFail("Can't cast payload as mutation event")
179-
return
180-
}
181-
if note.id == localTodo.id {
182-
remoteNoteOptional2 = note
183-
syncReceivedInvoked2.fulfill()
184-
}
185-
}
186-
guard try HubListenerTestUtilities.waitForListener(with: syncReceivedListener2, timeout: 5.0) else {
187-
XCTFail("syncReceivedListener2 registered for hub")
188-
return
189-
}
190-
signIn(user: user2)
191-
guard let currentUser = Amplify.Auth.getCurrentUser() else {
192-
XCTFail("Could not retrieve current user")
193-
return
194-
}
195-
XCTAssertNotEqual(currentUser.username, owner)
196-
wait(for: [syncStartedInvoked2], timeout: TestCommonConstants.networkTimeout)
197-
wait(for: [syncReceivedInvoked2], timeout: TestCommonConstants.networkTimeout)
198-
guard let ownerRemoteNote = remoteNoteOptional2, let remoteNoteOwner = ownerRemoteNote.owner else {
199-
XCTFail("Should have received a SyncReceived event with the remote note reconciled to local store")
200-
return
201-
}
202-
203-
XCTAssertEqual(owner, remoteNoteOwner)
204-
wait(for: [modelSyncedInvoked], timeout: TestCommonConstants.networkTimeout)
205-
}
20681
}
20782

20883
extension AWSDataStoreCategoryPluginAuthIntegrationTests {

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginAuthIntegrationTests/DefaultAuthCognito/Models/TodoCustomOwnerExplicit+Schema.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension TodoCustomOwnerExplicit {
2626
let todoCustomOwnerExplicit = TodoCustomOwnerExplicit.keys
2727

2828
model.authRules = [
29-
rule(allow: .owner, ownerField: "dominus", identityClaim: "cognito:username", operations: [.create, .update, .delete, .read])
29+
rule(allow: .owner, ownerField: "dominus", identityClaim: "cognito:username", provider: .userPools, operations: [.create, .update, .delete, .read])
3030
]
3131

3232
model.pluralName = "TodoCustomOwnerExplicits"

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginAuthIntegrationTests/DefaultAuthCognito/Models/TodoCustomOwnerImplicit+Schema.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension TodoCustomOwnerImplicit {
2525
let todoCustomOwnerImplicit = TodoCustomOwnerImplicit.keys
2626

2727
model.authRules = [
28-
rule(allow: .owner, ownerField: "dominus", identityClaim: "cognito:username", operations: [.create, .update, .delete, .read])
28+
rule(allow: .owner, ownerField: "dominus", identityClaim: "cognito:username", provider: .userPools, operations: [.create, .update, .delete, .read])
2929
]
3030

3131
model.pluralName = "TodoCustomOwnerImplicits"

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginAuthIntegrationTests/DefaultAuthCognito/Models/TodoExplicitOwnerField+Schema.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension TodoExplicitOwnerField {
2626
let todoExplicitOwnerField = TodoExplicitOwnerField.keys
2727

2828
model.authRules = [
29-
rule(allow: .owner, ownerField: "owner", identityClaim: "cognito:username", operations: [.create, .update, .delete])
29+
rule(allow: .owner, ownerField: "owner", identityClaim: "cognito:username", provider: .userPools, operations: [.read, .create, .update, .delete])
3030
]
3131

3232
model.pluralName = "TodoExplicitOwnerFields"

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginAuthIntegrationTests/DefaultAuthCognito/Models/TodoImplicitOwnerField+Schema.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension TodoImplicitOwnerField {
2525
let todoImplicitOwnerField = TodoImplicitOwnerField.keys
2626

2727
model.authRules = [
28-
rule(allow: .owner, ownerField: "owner", identityClaim: "cognito:username", operations: [.create, .update, .delete, .read])
28+
rule(allow: .owner, ownerField: "owner", identityClaim: "cognito:username", provider: .userPools, operations: [.create, .update, .delete, .read])
2929
]
3030

3131
model.pluralName = "TodoImplicitOwnerFields"

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginAuthIntegrationTests/DefaultAuthCognito/README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ This configuration is used to run the tests in `AWSDataStoreCategoryPluginAuthIn
77

88
1. `amplify init`
99

10-
2. `amplify add api`
10+
2. Make sure the correct CLI version is used for this test.
11+
12+
- `amplify --v` should be at least 8.5.2
13+
- cli.json "transformerversion": 2
14+
- cli.json "useexperimentalpipelinedtransformer": true
15+
- cli.json "usesubusernamefordefaultidentityclaim": true
16+
17+
3. `amplify add api`
1118

1219
```perl
1320
? Select from one of the below mentioned services: GraphQL
14-
? Here is the GraphQL API that we will create. Select a setting to edit or continue Authorization modes: API key (default, expiration time: 7 days fro
15-
m now)
16-
? Choose the default authorization type for the API: Cognito User Pools
17-
? Configure additional auth types? No
18-
? Here is the GraphQL API that we will create. Select a setting to edit or continue Conflict detection (required for DataStore): Disabled
19-
? Enable conflict detection? Yes
20-
? Select the default resolution strategy Auto Merge
21-
? Here is the GraphQL API that we will create. Select a setting to edit or continue Continue
22-
? Choose a schema template: Blank Schema
21+
Authorization modes: Amazon Cognito User Pool (default)
22+
Conflict detection (required for DataStore): Enabled
23+
Conflict resolution strategy: Auto Merge
24+
? Choose a schema template: Blank Schema
2325
? Do you want to edit the schema now? `Yes`
2426
```
2527

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginAuthIntegrationTests/DefaultAuthCognito/singleauth-cognito-schema.graphql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
type TodoExplicitOwnerField
22
@model
3-
@auth(rules: [{ allow: owner, ownerField: "owner", operations: [create, update, delete] }]) {
3+
@auth(rules: [{ allow: owner, ownerField: "owner", operations: [read, create, update, delete] }]) {
44
id: ID!
55
content: String!
66
owner: String
@@ -25,7 +25,7 @@ type TodoCustomOwnerExplicit @model @auth(rules: [{ allow: owner, ownerField: "d
2525
}
2626

2727
type TodoCognitoExplicitOperations @model
28-
@auth(rules: [{ allow: owner, operations: [create, delete] }]) {
28+
@auth(rules: [{ allow: owner, operations: [read, create, delete] }]) {
2929
id: ID!
3030
title: String!
3131
}

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginAuthIntegrationTests/DefaultAuthIAM/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ This configuration is used to run the tests in `AWSDataStoreCategoryPluginAuthIn
55

66
### Set-up
77

8-
1. `amplify init`
8+
1. `amplify init`.
9+
10+
These tests were provisioned with V1 Transform:
11+
12+
- cli.json "transformerversion": 1
13+
- cli.json "useexperimentalpipelinedtransformer": false
14+
15+
TODO: Update the schema and tests to use V2 Transform.
916

1017
2. `amplify add api`
1118

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginAuthIntegrationTests/MultiAuth/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
1. `amplify init`
1010

11+
These tests were provisioned with V1 Transform:
12+
13+
- cli.json "transformerversion": 1
14+
- cli.json "useexperimentalpipelinedtransformer": false
15+
16+
TODO: Update the schema and tests to use V2 Transform.
17+
1118
2. `amplify add api`
1219

1320
```

0 commit comments

Comments
 (0)