Skip to content

Commit c7c0b0d

Browse files
ekjotmultanityllark
authored andcommitted
chore(canaries): enhance integration test with CRUD coverage and cleanup (#6467)
* chore(canaries): enhance integration test with CRUD coverage and cleanup - Add Auth: fetch session, explicit sign-out, sign-in with credentials - Add Storage: list files, delete files (cleanup) - Add API: query Todo by ID, delete Todo (cleanup) - Add DataStore: query with predicate, delete (cleanup) - Remove redundant Analytics guest tests - Add unique test data naming with UUIDs * Update canaries/integration_test/main_test.dart
1 parent 004d042 commit c7c0b0d

File tree

1 file changed

+80
-17
lines changed

1 file changed

+80
-17
lines changed

canaries/integration_test/main_test.dart

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'dart:math';
55
import 'package:amplified_todo/main.dart' as app;
66
import 'package:amplified_todo/models/ModelProvider.dart';
77
import 'package:amplify_api/amplify_api.dart';
8+
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
89
import 'package:amplify_authenticator/amplify_authenticator.dart';
910
import 'package:amplify_authenticator/src/keys.dart' as keys;
1011
import 'package:amplify_flutter/amplify_flutter.dart';
@@ -58,45 +59,86 @@ void main() {
5859
}
5960

6061
Future<void> performAuthenticatedActions() async {
61-
// Retrieve guest data
62+
// === AUTH: Fetch auth session ===
63+
final authSession = await Amplify.Auth.fetchAuthSession();
64+
expect(authSession.isSignedIn, isTrue);
65+
66+
// === STORAGE: Download guest data ===
6267
final guestData = await Amplify.Storage.downloadData(path: path).result;
6368
expect(utf8.decode(guestData.bytes), data);
6469

65-
// Upload data to Storage
70+
// === STORAGE: Upload to private path ===
71+
final privatePath = StoragePath.fromIdentityId(
72+
(String identityId) => 'private/$identityId/canary-test',
73+
);
6674
await Amplify.Storage.uploadData(
6775
data: StorageDataPayload.string(data),
68-
path: StoragePath.fromIdentityId(
69-
(String identityId) => 'private/$identityId/hello',
70-
),
76+
path: privatePath,
7177
).result;
7278

73-
// Record Analytics event
79+
// === STORAGE: List files ===
80+
final listResult = await Amplify.Storage.list(
81+
path: StoragePath.fromString('public/'),
82+
).result;
83+
expect(listResult.items.isNotEmpty, isTrue);
84+
85+
// === ANALYTICS: Record event ===
7486
await Amplify.Analytics.recordEvent(event: event);
7587
await Amplify.Analytics.flushEvents();
7688

77-
// Perform API mutation
89+
// === AUTH: Get current user ===
7890
final username = (await Amplify.Auth.getCurrentUser()).username;
79-
final todo = Todo(name: 'name', owner: username);
80-
final mutation = ModelMutations.create(todo);
81-
final response = await Amplify.API.mutate(request: mutation).response;
82-
expect(response.hasErrors, isFalse);
83-
expect(response.data, todo);
8491

85-
// Perform DataStore operation
86-
final dsTodo = Todo(name: 'test', owner: username);
92+
// === API: Create Todo ===
93+
final todo = Todo(name: 'canary-test-${uuid()}', owner: username);
94+
final createMutation = ModelMutations.create(todo);
95+
final createResponse =
96+
await Amplify.API.mutate(request: createMutation).response;
97+
expect(createResponse.hasErrors, isFalse);
98+
expect(createResponse.data, isNotNull);
99+
final createdTodo = createResponse.data!;
100+
101+
// === API: Query Todo ===
102+
final queryRequest = ModelQueries.get(Todo.classType, createdTodo.modelIdentifier);
103+
final queryResponse = await Amplify.API.query(request: queryRequest).response;
104+
expect(queryResponse.hasErrors, isFalse);
105+
expect(queryResponse.data?.id, createdTodo.id);
106+
107+
// === API: Delete Todo (cleanup) ===
108+
final deleteMutation = ModelMutations.delete(createdTodo);
109+
final deleteResponse =
110+
await Amplify.API.mutate(request: deleteMutation).response;
111+
expect(deleteResponse.hasErrors, isFalse);
112+
113+
// === DATASTORE: Save and observe ===
114+
final dsTodo = Todo(name: 'canary-ds-test', owner: username);
87115
final subscription = Amplify.DataStore.observe(Todo.classType);
88116
final expectation = expectLater(
89117
subscription,
90118
emitsThrough(
91119
isA<SubscriptionEvent<Todo>>().having(
92120
(event) => event.item.name,
93121
'name',
94-
'test',
122+
'canary-ds-test',
95123
),
96124
),
97125
);
98126
await Amplify.DataStore.save(dsTodo);
99127
await expectation;
128+
129+
// === DATASTORE: Query ===
130+
final dsQueryResult = await Amplify.DataStore.query(
131+
Todo.classType,
132+
where: Todo.NAME.eq('canary-ds-test'),
133+
);
134+
expect(dsQueryResult.isNotEmpty, isTrue);
135+
136+
// === DATASTORE: Delete (cleanup) ===
137+
await Amplify.DataStore.delete(dsTodo);
138+
139+
// === STORAGE: Delete uploaded files (cleanup) ===
140+
await Amplify.Storage.remove(path: path).result;
141+
await Amplify.Storage.remove(path: privatePath).result;
100142
}
101143

102144
testWidgets('canary', (tester) async {
@@ -152,13 +194,34 @@ void main() {
152194

153195
await tester.runAsync(performAuthenticatedActions);
154196

155-
// Clean up
197+
// === AUTH: Sign out ===
198+
await tester.runAsync(() async {
199+
final signOutResult = await Amplify.Auth.signOut();
200+
expect(signOutResult, isA<CognitoCompleteSignOut>());
201+
202+
// Verify user is signed out
203+
final sessionAfterSignOut = await Amplify.Auth.fetchAuthSession();
204+
expect(sessionAfterSignOut.isSignedIn, isFalse);
205+
});
206+
await tester.pumpAndSettle();
207+
expect(signUpTab, findsOneWidget, reason: 'User should be signed out');
208+
209+
// === AUTH: Sign back in for cleanup ===
210+
await tester.runAsync(() async {
211+
final signInResult = await Amplify.Auth.signIn(
212+
username: username,
213+
password: password,
214+
);
215+
expect(signInResult.isSignedIn, isTrue);
216+
});
217+
218+
// === AUTH: Delete user (cleanup) ===
156219
await tester.runAsync(Amplify.Auth.deleteUser);
157220
await expectLater(
158221
hubEventsController.stream,
159222
emitsThrough(AuthHubEvent.userDeleted()),
160223
);
161224
await tester.pumpAndSettle();
162-
expect(signUpTab, findsOneWidget, reason: 'User should be signed out');
225+
expect(signUpTab, findsOneWidget, reason: 'User should be signed out after deletion');
163226
});
164227
}

0 commit comments

Comments
 (0)