Skip to content

Commit

Permalink
reformat: minor formatting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
srieteja committed Mar 27, 2024
1 parent af4a1d0 commit 930e372
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 72 deletions.
7 changes: 3 additions & 4 deletions packages/at_onboarding_cli/lib/src/register_cli/register.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@ class Register {
}

GetFreeAtsign getFreeAtsignTask = GetFreeAtsign(
apiAccessorInstance: registrarApiAccessor,
);
apiAccessorInstance: registrarApiAccessor, allowRetry: true);

RegisterAtsign registerAtsignTask =
RegisterAtsign(apiAccessorInstance: registrarApiAccessor);
RegisterAtsign(apiAccessorInstance: registrarApiAccessor, allowRetry: true);

ValidateOtp validateOtpTask =
ValidateOtp(apiAccessorInstance: registrarApiAccessor);
ValidateOtp(apiAccessorInstance: registrarApiAccessor, allowRetry: true);

// create a queue of tasks each of type [RegisterTask] and then
// call start on the RegistrationFlow object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class RegistrationFlow {

Future<RegisterTaskResult> start() async {
for (RegisterTask task in processQueue) {
// setting allowRetry to true as this method has logic to retry each
// failed task 3-times and then throw an exception if still failing
try {
_result = await task.run(params);
task.logger.finer('Attempt: ${task.retryCount} | params[$params]');
Expand Down
7 changes: 3 additions & 4 deletions packages/at_onboarding_cli/test/at_register_cli_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ void main() {
apiAccessorInstance: accessorInstance, allowRetry: true);
try {
await RegistrationFlow(params).add(registerAtsignTask).start();
} on Exception catch(e){
} on Exception catch (e) {
expect(e.runtimeType, AtRegisterException);
assert(e.toString().contains('Could not complete the task'));
assert(e.toString().contains('Could not complete the task'));
}
expect(registerAtsignTask.retryCount, registerAtsignTask.maximumRetries);
expect(registerAtsignTask.shouldRetry(), false);
Expand All @@ -78,8 +78,7 @@ void main() {
exceptionFlag = true;
}
expect(exceptionFlag, true);
expect(
registerAtsignTask.retryCount, registerAtsignTask.maximumRetries);
expect(registerAtsignTask.retryCount, registerAtsignTask.maximumRetries);
expect(registerAtsignTask.shouldRetry(), false);
});
});
Expand Down
28 changes: 14 additions & 14 deletions packages/at_register/example/at_register_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ import 'package:at_register/at_register.dart';

void main() async {
String email = '<enter your email here>';
RegisterParams params = RegisterParams()..email = email;
RegisterParams registerParams = RegisterParams()..email = email;

GetFreeAtsign getFreeAtsignTask = GetFreeAtsign();
RegisterTaskResult result = await getFreeAtsignTask.run(params);
params.addFromJson(result.data);
RegisterTaskResult result = await getFreeAtsignTask.run(registerParams);
registerParams.addFromJson(result.data);

RegisterAtsign registerAtsignTask = RegisterAtsign();
result = await registerAtsignTask.run(params);
params.addFromJson(result.data);
result = await registerAtsignTask.run(registerParams);
registerParams.addFromJson(result.data);

// verification code sent to email provided in the beginning
// check the same email and enter that verification code through terminal/stdin
params.otp = ApiUtil.readCliVerificationCode();
registerParams.otp = ApiUtil.readCliVerificationCode();
ValidateOtp validateOtpTask = ValidateOtp();
result = await validateOtpTask.run(params);
if(result.apiCallStatus == ApiCallStatus.success){
result = await validateOtpTask.run(registerParams);
if (result.apiCallStatus == ApiCallStatus.success) {
print(result.data[RegistrarConstants.cramKeyName]);
} else {
// this is the case where the email has existing atsigns
// set task.params.confirmation to true, select an atsign (existing/new) from
// the
// set task.params.confirmation to true, select an atsign (existing/new)
// from the list of atsigns returned in the previous call(ValidateOtp with confirmation set to false)
String newAtsign = result.data[RegistrarConstants.newAtsignName];
params.atsign = newAtsign;
params.confirmation = true;
result = await validateOtpTask.run(params);
registerParams.atsign = newAtsign;
registerParams.confirmation = true;
result = await validateOtpTask.run(registerParams);
print(result.data[RegistrarConstants.cramKeyName]);
}
}
}
8 changes: 5 additions & 3 deletions packages/at_register/example/at_register_usage_explained.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ Future<void> main() async {
print(getFreeAtsignResult.data);

// this step is optional
// Can be used to propagates the data received in the current task to the next
// Can be used to propagate the data received in the current task to the next
params.addFromJson(getFreeAtsignResult.data);
// ----------------------------------------------------

/// Example for RegisterAtsign task
RegisterAtsign registerAtsignTask =
RegisterAtsign(apiAccessorInstance: accessorInstance);
RegisterTaskResult registerAtsignResult = await registerAtsignTask.run(params);
RegisterTaskResult registerAtsignResult =
await registerAtsignTask.run(params);
// registerAtsignResult.data should have a key named 'otpSent' which contains
// true/false reg the status of verificationCodeSent to provided email
print(registerAtsignResult.data[RegistrarConstants.otpSentName]);
Expand Down Expand Up @@ -49,12 +50,13 @@ Future<void> main() async {
// now this will return a result with the cram key in result.data
List<String> fetchedAtsignList =
validateOtpResult.data[RegistrarConstants.fetchedAtsignListName];
// selecting the first atsign from the fetchedAtsignList for demonstration
params.atsign = fetchedAtsignList[0];
validateOtpResult = await validateOtpTask.run(params);
print(validateOtpResult.data[RegistrarConstants.cramKeyName]);

// CASE 3: if the otp is incorrect, fetch the correct otp from user and re-run
// the validateOtpTask
params.otp = 'AB14'; // correct otp
validateOtpResult = await validateOtpTask.run(params);
validateOtpResult = await validateOtpTask.retry(params);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:at_commons/at_commons.dart';

import '../../at_register.dart';
import 'package:at_register/at_register.dart';

/// A [RegisterTask] that fetches a list of free atsigns.
/// A [RegisterTask] that fetches a free atsign
///
/// Throws an [AtException] with the concerned message encountered in the
/// HTTP GET/POST request.
Expand All @@ -12,7 +12,7 @@ import '../../at_register.dart';
/// GetFreeAtsign getFreeAtsignInstance = GetFreeAtsign();
/// RegisterTaskResult result = await getFreeAtsignInstance.run(registerParams);
/// ```
/// The fetched atsign will be stored in result.data[[RegistrarConstants.atsignName]]
/// The fetched atsign will be stored in RegisterTaskResult.data[[RegistrarConstants.atsignName]]
class GetFreeAtsign extends RegisterTask {
GetFreeAtsign(
{RegistrarApiAccessor? apiAccessorInstance, bool allowRetry = false})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:at_commons/at_commons.dart';

import '../../at_register.dart';
import 'package:at_register/at_register.dart';

/// User selects an atsign from the list fetched in [GetFreeAtsign].
///
Expand Down Expand Up @@ -36,7 +36,7 @@ class RegisterAtsign extends RegisterTask {
params.atsign!, params.email!,
authority: RegistrarConstants.authority);
result.data[RegistrarConstants.otpSentName] = otpSent;
if(otpSent) {
if (otpSent) {
logger.info('Verification code sent to: ${params.email}');
result.apiCallStatus = ApiCallStatus.success;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:at_commons/at_commons.dart';
import 'package:at_utils/at_logger.dart';
import 'package:http/http.dart' as http;

import '../../at_register.dart';
import 'package:at_register/at_register.dart';

/// Contains methods that actually perform the RegistrarAPI calls
/// and handle/process the response
Expand Down Expand Up @@ -48,6 +48,7 @@ class RegistrarApiAccessor {
});
if (response.statusCode == 200) {
final jsonDecoded = jsonDecode(response.body) as Map<String, dynamic>;
// will be set to true if API response contains message with 'success'
bool sentSuccessfully =
jsonDecoded['message'].toLowerCase().contains('success');
return sentSuccessfully;
Expand Down Expand Up @@ -143,16 +144,14 @@ class RegistrarApiAccessor {
final response = await ApiUtil.postRequest(authority,
RegistrarConstants.requestAuthenticationOtpPath, {'atsign': atsign});
final apiResponseMessage = jsonDecode(response.body)['message'];
if (response.statusCode == 200) {
if (apiResponseMessage.contains('Sent Successfully')) {
logger.info(
'Successfully sent verification code to your registered e-mail');
return;
}
throw AtRegisterException(
'Unable to send verification code for authentication. | Cause: $apiResponseMessage');
if (response.statusCode == 200 &&
apiResponseMessage.contains('Sent Successfully')) {
logger.info(
'Successfully sent verification code to your registered e-mail');
return;
}
throw AtRegisterException(apiResponseMessage);
throw AtRegisterException(
'Unable to send verification code for authentication. | Cause: $apiResponseMessage');
}

/// Returns the cram key for an atsign by fetching it from the registrar API
Expand All @@ -175,7 +174,9 @@ class RegistrarApiAccessor {
logger.info('CRAM Key fetched successfully');
return cram;
}
throw InvalidDataException(
// If API call status is HTTP.OK / 200, but the response message does not
// contain 'Verified', that indicates incorrect verification provided by user
throw InvalidVerificationCodeException(
'Invalid verification code. Please enter a valid verification code');
}
throw InvalidDataException(jsonDecodedBody['message']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ class ValidateOtp extends RegisterTask {
break;

case ValidateOtpStatus.failure:
result.apiCallStatus = ApiCallStatus.failure;
result.exception = validateOtpApiResult.exception;
break;

case null:

default:
result.apiCallStatus = ApiCallStatus.failure;
result.exception = validateOtpApiResult.exception;
break;
Expand Down
52 changes: 26 additions & 26 deletions packages/at_register/test/at_register_allow_retry_false_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ void main() {
group('Validate individual task behaviour with invalid params', () {
test('validate behaviour of GetFreeAtsign', () async {
RegisterParams params = RegisterParams()..email = '';
var getFreeAtsignTask = GetFreeAtsign(
apiAccessorInstance: mockRegistrarApiAccessor);
var getFreeAtsignTask =
GetFreeAtsign(apiAccessorInstance: mockRegistrarApiAccessor);
expect(() => getFreeAtsignTask.run(params),
throwsA(predicate((e) => e is IllegalArgumentException)));
});
Expand All @@ -23,8 +23,8 @@ void main() {
..email = 'email@email'
..atsign = null;

var registerAtsignTask = RegisterAtsign(
apiAccessorInstance: mockRegistrarApiAccessor);
var registerAtsignTask =
RegisterAtsign(apiAccessorInstance: mockRegistrarApiAccessor);
expect(() => registerAtsignTask.run(params),
throwsA(predicate((e) => e is IllegalArgumentException)));
});
Expand All @@ -34,8 +34,8 @@ void main() {
..email = 'email@email'
..otp = null;

var validateOtpTask = ValidateOtp(
apiAccessorInstance: mockRegistrarApiAccessor);
var validateOtpTask =
ValidateOtp(apiAccessorInstance: mockRegistrarApiAccessor);
expect(() => validateOtpTask.run(params),
throwsA(predicate((e) => e is IllegalArgumentException)));
});
Expand All @@ -46,8 +46,8 @@ void main() {
..email = 'email@email'
..atsign = '@abcd'
..otp = null;
var validateOtpTask = ValidateOtp(
apiAccessorInstance: mockRegistrarApiAccessor);
var validateOtpTask =
ValidateOtp(apiAccessorInstance: mockRegistrarApiAccessor);
expect(() => validateOtpTask.run(params),
throwsA(predicate((e) => e is InvalidVerificationCodeException)));
});
Expand All @@ -61,8 +61,8 @@ void main() {
.thenAnswer((invocation) => Future.value('@alice'));

RegisterParams params = RegisterParams()..email = '[email protected]';
GetFreeAtsign getFreeAtsign = GetFreeAtsign(
apiAccessorInstance: mockRegistrarApiAccessor);
GetFreeAtsign getFreeAtsign =
GetFreeAtsign(apiAccessorInstance: mockRegistrarApiAccessor);
final result = await getFreeAtsign.run(params);

expect(result.data[RegistrarConstants.atsignName], '@alice');
Expand All @@ -74,8 +74,8 @@ void main() {
.thenThrow(Exception('random exception'));

RegisterParams params = RegisterParams()..email = '[email protected]';
GetFreeAtsign getFreeAtsign = GetFreeAtsign(
apiAccessorInstance: mockRegistrarApiAccessor);
GetFreeAtsign getFreeAtsign =
GetFreeAtsign(apiAccessorInstance: mockRegistrarApiAccessor);
bool exceptionFlag = false;
try {
await getFreeAtsign.run(params);
Expand All @@ -102,8 +102,8 @@ void main() {
RegisterParams params = RegisterParams()
..atsign = atsign
..email = email;
RegisterAtsign registerAtsignTask = RegisterAtsign(
apiAccessorInstance: mockRegistrarApiAccessor);
RegisterAtsign registerAtsignTask =
RegisterAtsign(apiAccessorInstance: mockRegistrarApiAccessor);
RegisterTaskResult result = await registerAtsignTask.run(params);

expect(result.apiCallStatus, ApiCallStatus.success);
Expand All @@ -120,8 +120,8 @@ void main() {
RegisterParams params = RegisterParams()
..atsign = atsign
..email = email;
RegisterAtsign registerAtsignTask = RegisterAtsign(
apiAccessorInstance: mockRegistrarApiAccessor);
RegisterAtsign registerAtsignTask =
RegisterAtsign(apiAccessorInstance: mockRegistrarApiAccessor);
RegisterTaskResult result = await registerAtsignTask.run(params);

expect(result.apiCallStatus, ApiCallStatus.retry);
Expand All @@ -139,8 +139,8 @@ void main() {
..atsign = atsign
..email = email;

RegisterAtsign registerAtsignTask = RegisterAtsign(
apiAccessorInstance: mockRegistrarApiAccessor);
RegisterAtsign registerAtsignTask =
RegisterAtsign(apiAccessorInstance: mockRegistrarApiAccessor);
bool exceptionFlag = false;
try {
await registerAtsignTask.run(params);
Expand All @@ -164,8 +164,8 @@ void main() {
RegisterParams params = RegisterParams()
..atsign = atsign
..email = email;
RegisterAtsign registerAtsignTask = RegisterAtsign(
apiAccessorInstance: mockRegistrarApiAccessor);
RegisterAtsign registerAtsignTask =
RegisterAtsign(apiAccessorInstance: mockRegistrarApiAccessor);
bool exceptionFlag = false;
try {
await registerAtsignTask.run(params);
Expand Down Expand Up @@ -203,8 +203,8 @@ void main() {
..email = email
..otp = otp;

ValidateOtp validateOtpTask = ValidateOtp(
apiAccessorInstance: mockRegistrarApiAccessor);
ValidateOtp validateOtpTask =
ValidateOtp(apiAccessorInstance: mockRegistrarApiAccessor);
RegisterTaskResult result = await validateOtpTask.run(params);

expect(result.data[RegistrarConstants.cramKeyName], cram);
Expand Down Expand Up @@ -245,8 +245,8 @@ void main() {
..email = email
..otp = otp;

ValidateOtp validateOtpTask = ValidateOtp(
apiAccessorInstance: mockRegistrarApiAccessor);
ValidateOtp validateOtpTask =
ValidateOtp(apiAccessorInstance: mockRegistrarApiAccessor);
RegisterTaskResult result = await validateOtpTask.run(params);
expect(params.confirmation, true); // confirmation set to true by the Task
expect(result.apiCallStatus, ApiCallStatus.retry);
Expand Down Expand Up @@ -308,8 +308,8 @@ void main() {
.thenThrow(
MaximumAtsignQuotaException('maximum free atsign limit reached'));

ValidateOtp validateOtpTask = ValidateOtp(
apiAccessorInstance: mockRegistrarApiAccessor);
ValidateOtp validateOtpTask =
ValidateOtp(apiAccessorInstance: mockRegistrarApiAccessor);

expect(() async => await validateOtpTask.run(params),
throwsA(predicate((e) => e is MaximumAtsignQuotaException)));
Expand Down

0 comments on commit 930e372

Please sign in to comment.