Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(authenticator): TEST #5796

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,66 +64,84 @@ void main() {
});

asyncTest('can invoke with HTTP client', (_) async {
final username = generateUsername();
final password = generatePassword();
final checkpoints = <String>[];
try {
checkpoints.add('TestTestTest - 1');
final username = generateUsername();
final password = generatePassword();

final signUpRes = await Amplify.Auth.signUp(
username: username,
password: password,
);
expect(signUpRes.isSignUpComplete, isTrue);
checkpoints.add('TestTestTest - 2');
final signUpRes = await Amplify.Auth.signUp(
username: username,
password: password,
);
expect(signUpRes.isSignUpComplete, isTrue);

final signInRes = await Amplify.Auth.signIn(
username: username,
password: password,
);
expect(signInRes.isSignedIn, isTrue);
checkpoints.add('TestTestTest - 3');
final signInRes = await Amplify.Auth.signIn(
username: username,
password: password,
);
expect(signInRes.isSignedIn, isTrue);

final session =
await Amplify.Auth.fetchAuthSession() as CognitoAuthSession;
expect(session.userPoolTokensResult.valueOrNull, isNotNull);
checkpoints.add('TestTestTest - 4');
final session =
await Amplify.Auth.fetchAuthSession() as CognitoAuthSession;
expect(session.userPoolTokensResult.valueOrNull, isNotNull);

final apiUrl = config.api!.awsPlugin!.values
.singleWhere((e) => e.endpointType == EndpointType.rest)
.endpoint;
checkpoints.add('TestTestTest - 5');
final apiUrl = config.api!.awsPlugin!.values
.singleWhere((e) => e.endpointType == EndpointType.rest)
.endpoint;

// Verifies invocation with the ID token. Invocation with an access
// token requires integration with a resource server/OAuth and is, thus,
// not tested.
//
// https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-enable-cognito-user-pool.html
checkpoints.add('TestTestTest - 6');
// Verifies invocation with the ID token. Invocation with an access
// token requires integration with a resource server/OAuth and is, thus,
// not tested.
//
// https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-enable-cognito-user-pool.html

final request = AWSStreamedHttpRequest.post(
Uri.parse(apiUrl).replace(
queryParameters: queryParameters,
),
headers: {
AWSHeaders.accept: 'application/json;charset=utf-8',
AWSHeaders.authorization:
session.userPoolTokensResult.value.idToken.raw,
...customHeaders,
},
body: HttpPayload.json({'request': 'hello'}),
);
final resp = await client.send(request).response;
final body = await resp.decodeBody();
expect(resp.statusCode, 200, reason: body);
expect(
jsonDecode(body),
equals({'response': 'hello'}),
);
customHeaders.forEach((key, value) {
expect(
resp.headers,
containsPair(key, value),
final request = AWSStreamedHttpRequest.post(
Uri.parse(apiUrl).replace(
queryParameters: queryParameters,
),
headers: {
AWSHeaders.accept: 'application/json;charset=utf-8',
AWSHeaders.authorization:
session.userPoolTokensResult.value.idToken.raw,
...customHeaders,
},
body: HttpPayload.json({'request': 'hello'}),
);
});
queryParameters.forEach((key, value) {
checkpoints.add('TestTestTest - 7');
final resp = await client.send(request).response;
checkpoints.add('TestTestTest - 8');
final body = await resp.decodeBody();
checkpoints.add('TestTestTest - 9');
expect(resp.statusCode, 200, reason: body);
expect(
resp.headers,
containsPair('x-query-$key', value),
jsonDecode(body),
equals({'response': 'hello'}),
);
});
checkpoints.add('TestTestTest - 10');
customHeaders.forEach((key, value) {
expect(
resp.headers,
containsPair(key, value),
);
});
checkpoints.add('TestTestTest - 11');
queryParameters.forEach((key, value) {
expect(
resp.headers,
containsPair('x-query-$key', value),
);
});
checkpoints.add('TestTestTest - 12');
} on Object catch (e) {
checkpoints.add('TestTestTest - Error: $e');
throw Exception('$e - CheckPoints1 = $checkpoints');
}
});

asyncTest('can invoke with API plugin', (_) async {
Expand Down Expand Up @@ -207,59 +225,82 @@ void main() {
});

asyncTest('can invoke with HTTP client', (_) async {
final cognitoPlugin = Amplify.Auth.getPlugin(
AmplifyAuthCognito.pluginKey,
);
final session = await cognitoPlugin.fetchAuthSession();
expect(session.credentialsResult.valueOrNull, isNotNull);
final checkpoints = <String>[];
try {
checkpoints.add('TestTestTest - 1');
final cognitoPlugin = Amplify.Auth.getPlugin(
AmplifyAuthCognito.pluginKey,
);
checkpoints.add('TestTestTest - 2');
final session = await cognitoPlugin.fetchAuthSession();
checkpoints.add('TestTestTest - 3');
expect(session.credentialsResult.valueOrNull, isNotNull);
checkpoints.add('TestTestTest - 4');

final restApi = config.api!.awsPlugin!.values
.singleWhere((e) => e.endpointType == EndpointType.rest);
final apiUrl = Uri.parse(restApi.endpoint);
final restApi = config.api!.awsPlugin!.values.singleWhere(
(e) => e.endpointType == EndpointType.rest,
);
checkpoints.add('TestTestTest - 5');
final apiUrl = Uri.parse(restApi.endpoint);
checkpoints.add('TestTestTest - 6');

final payload = jsonEncode({'request': 'hello'});
final request = AWSHttpRequest.post(
apiUrl.replace(
queryParameters: queryParameters,
),
headers: const {
AWSHeaders.accept: 'application/json;charset=utf-8',
...customHeaders,
},
body: utf8.encode(payload),
);
final signer = AWSSigV4Signer(
credentialsProvider: AWSCredentialsProvider(
session.credentialsResult.value,
),
);
final scope = AWSCredentialScope(
region: restApi.region,
service: AWSService.apiGatewayManagementApi,
);
final signedRequest = await signer.sign(
request,
credentialScope: scope,
);
final resp = await client.send(signedRequest).response;
final body = await resp.decodeBody();
expect(resp.statusCode, 200, reason: body);
expect(
jsonDecode(body),
equals({'response': 'hello'}),
);
customHeaders.forEach((key, value) {
expect(
resp.headers,
containsPair(key, value),
final payload = jsonEncode({'request': 'hello'});
final request = AWSHttpRequest.post(
apiUrl.replace(
queryParameters: queryParameters,
),
headers: const {
AWSHeaders.accept: 'application/json;charset=utf-8',
...customHeaders,
},
body: utf8.encode(payload),
);
checkpoints.add('TestTestTest - 7');
final signer = AWSSigV4Signer(
credentialsProvider: AWSCredentialsProvider(
session.credentialsResult.value,
),
);
});
queryParameters.forEach((key, value) {
checkpoints.add('TestTestTest - 8');
final scope = AWSCredentialScope(
region: restApi.region,
service: AWSService.apiGatewayManagementApi,
);
checkpoints.add('TestTestTest - 9');
final signedRequest = await signer.sign(
request,
credentialScope: scope,
);
checkpoints.add('TestTestTest - 10');
final resp = await client.send(signedRequest).response;
checkpoints.add('TestTestTest - 11');
final body = await resp.decodeBody();
checkpoints.add('TestTestTest - 12');
expect(resp.statusCode, 200, reason: body);
checkpoints.add('TestTestTest - 13');
expect(
resp.headers,
containsPair('x-query-$key', value),
jsonDecode(body),
equals({'response': 'hello'}),
);
});
checkpoints.add('TestTestTest - 14');
customHeaders.forEach((key, value) {
expect(
resp.headers,
containsPair(key, value),
);
});
checkpoints.add('TestTestTest - 15');
queryParameters.forEach((key, value) {
expect(
resp.headers,
containsPair('x-query-$key', value),
);
});
checkpoints.add('TestTestTest - 16');
} on Object catch (e) {
checkpoints.add('TestTestTest - Error: $e');
throw Exception('$e - CheckPoints2 = $checkpoints');
}
});

asyncTest('can invoke with API plugin', (_) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ class _SignInTextFieldState extends _SignInFormFieldState<String>
return (v) => state.username = v;
case SignInField.password:
return (v) => state.password = v;
//This is a Test PR
default:
return super.onChanged;
}
Expand Down
Loading