Skip to content

Commit

Permalink
test: add unit tests to validate changes
Browse files Browse the repository at this point in the history
  • Loading branch information
srieteja committed Sep 12, 2024
1 parent cdcea33 commit bf70ac7
Showing 1 changed file with 155 additions and 1 deletion.
156 changes: 155 additions & 1 deletion packages/at_lookup/test/at_lookup_test.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import 'dart:async';
import 'dart:convert';
import 'dart:ffi';
import 'dart:io';

import 'package:at_chops/at_chops.dart';
import 'package:at_commons/at_builders.dart';
import 'package:at_commons/at_commons.dart';
import 'package:at_lookup/at_lookup.dart';
import 'package:at_lookup/src/connection/at_connection.dart';
import 'package:at_lookup/src/connection/outbound_message_listener.dart';
import 'package:test/test.dart';
import 'package:mocktail/mocktail.dart';
Expand Down Expand Up @@ -212,6 +217,7 @@ void main() {
e is UnAuthenticatedException && e.message.contains('AT0401'))));
});
});

group('A group of tests to verify executeCommand method', () {
test('executeCommand - from verb - auth false', () async {
final atLookup = AtLookupImpl('@alice', atServerHost, 64,
Expand All @@ -226,6 +232,7 @@ void main() {
var result = await atLookup.executeCommand('from:@alice\n');
expect(result, fromResponse);
});

test('executeCommand -llookup verb - auth true - auth key not set',
() async {
final atLookup = AtLookupImpl('@alice', atServerHost, 64,
Expand Down Expand Up @@ -292,7 +299,8 @@ void main() {
outboundConnectionFactory: mockOutboundConnectionFactory);
atLookup.atChops = mockAtChops;
final llookupCommand = 'llookup:phone@alice\n';
final llookupResponse = 'error:{"errorCode":"AT0015","errorDescription":"Exception: fubar"}';
final llookupResponse =
'error:{"errorCode":"AT0015","errorDescription":"Exception: fubar"}';
when(() => mockOutBoundConnection.write(llookupCommand))
.thenAnswer((invocation) {
mockSecureSocket.write(llookupCommand);
Expand All @@ -306,4 +314,150 @@ void main() {
e is AtLookUpException && e.errorMessage == 'Exception: fubar')));
});
});

group('Validate executeVerb() behaviour', () {
test('validate EnrollVerbHandler behaviour - request', () async {
final atLookup = AtLookupImpl('@alice', atServerHost, 64,
secondaryAddressFinder: mockSecondaryAddressFinder,
secureSocketFactory: mockSocketFactory,
socketListenerFactory: mockSecureSocketListenerFactory,
outboundConnectionFactory: mockOutboundConnectionFactory);
atLookup.atChops = mockAtChops;

String appName = 'unit_test_1';
String deviceName = 'test_device';
String otp = 'ABCDEF';

EnrollVerbBuilder enrollVerbBuilder = EnrollVerbBuilder()
..operation = EnrollOperationEnum.request
..appName = appName
..deviceName = deviceName
..otp = otp;
String enrollCommand =
'enroll:request:{"appName":"$appName","deviceName":"$deviceName","otp":"$otp"}\n';
final enrollResponse =
'data:{"enrollmentId":"1234567890","status":"pending"}';

when(() => mockOutBoundConnection.write(enrollCommand))
.thenAnswer((invocation) {
mockSecureSocket.write(enrollCommand);
return Future.value();
});
when(() => mockOutboundListener.read())
.thenAnswer((_) => Future.value(enrollResponse));
AtConnectionMetaData? atConnectionMetaData = OutboundConnectionMetadata()
..isAuthenticated = false;
when(() => mockOutBoundConnection.getMetaData())
.thenReturn(atConnectionMetaData);
when(() => mockOutBoundConnection.isInValid()).thenReturn(false);

var result = await atLookup.executeVerb(enrollVerbBuilder);
expect(result, enrollResponse);
});

test('validate behaviour with EnrollVerbHandler - approve', () async {
final atLookup = AtLookupImpl('@alice', atServerHost, 64,
secondaryAddressFinder: mockSecondaryAddressFinder,
secureSocketFactory: mockSocketFactory,
socketListenerFactory: mockSecureSocketListenerFactory,
outboundConnectionFactory: mockOutboundConnectionFactory);
atLookup.atChops = mockAtChops;

String appName = 'unit_test_2';
String deviceName = 'test_device';
String otp = 'GHIJKL';
String enrollmentId = '1357913579';

EnrollVerbBuilder enrollVerbBuilder = EnrollVerbBuilder()
..operation = EnrollOperationEnum.approve
..enrollmentId = '1357913579'
..appName = appName
..deviceName = deviceName;
String enrollCommand =
'enroll:approve:{"enrollmentId":"$enrollmentId","appName":"$appName","deviceName":"$deviceName"}\n';
final enrollResponse =
'data:{"enrollmentId":"1357913579","status":"approved"}';

when(() => mockOutBoundConnection.write(enrollCommand))
.thenAnswer((invocation) {
mockSecureSocket.write(enrollCommand);
return Future.value();
});
when(() => mockOutboundListener.read())
.thenAnswer((_) => Future.value(enrollResponse));
AtConnectionMetaData? atConnectionMetaData = OutboundConnectionMetadata()
..isAuthenticated = true;
when(() => mockOutBoundConnection.getMetaData())
.thenReturn(atConnectionMetaData);
when(() => mockOutBoundConnection.isInValid()).thenReturn(false);

expect(await atLookup.executeVerb(enrollVerbBuilder), enrollResponse);
});

test('validate behaviour with EnrollVerbHandler - revoke', () async {
final atLookup = AtLookupImpl('@alice', atServerHost, 64,
secondaryAddressFinder: mockSecondaryAddressFinder,
secureSocketFactory: mockSocketFactory,
socketListenerFactory: mockSecureSocketListenerFactory,
outboundConnectionFactory: mockOutboundConnectionFactory);
atLookup.atChops = mockAtChops;
String enrollmentId = '89213647826348';

EnrollVerbBuilder enrollVerbBuilder = EnrollVerbBuilder()
..operation = EnrollOperationEnum.revoke
..enrollmentId = enrollmentId;
String enrollCommand =
'enroll:revoke:{"enrollmentId":"$enrollmentId"}\n';
String enrollResponse =
'data:{"enrollmentId":"$enrollmentId","status":"revoked"}';

when(() => mockOutBoundConnection.write(enrollCommand))
.thenAnswer((invocation) {
mockSecureSocket.write(enrollCommand);
return Future.value();
});
when(() => mockOutboundListener.read())
.thenAnswer((_) => Future.value(enrollResponse));
AtConnectionMetaData? atConnectionMetaData = OutboundConnectionMetadata()
..isAuthenticated = true;
when(() => mockOutBoundConnection.getMetaData())
.thenReturn(atConnectionMetaData);
when(() => mockOutBoundConnection.isInValid()).thenReturn(false);

expect(await atLookup.executeVerb(enrollVerbBuilder), enrollResponse);
});

test('validate behaviour with EnrollVerbHandler - deny', () async {
final atLookup = AtLookupImpl('@alice', atServerHost, 64,
secondaryAddressFinder: mockSecondaryAddressFinder,
secureSocketFactory: mockSocketFactory,
socketListenerFactory: mockSecureSocketListenerFactory,
outboundConnectionFactory: mockOutboundConnectionFactory);
atLookup.atChops = mockAtChops;
String enrollmentId = '5754765754';

EnrollVerbBuilder enrollVerbBuilder = EnrollVerbBuilder()
..operation = EnrollOperationEnum.deny
..enrollmentId = enrollmentId;
String enrollCommand =
'enroll:deny:{"enrollmentId":"$enrollmentId"}\n';
String enrollResponse =
'data:{"enrollmentId":"$enrollmentId","status":"denied"}';

when(() => mockOutBoundConnection.write(enrollCommand))
.thenAnswer((invocation) {
mockSecureSocket.write(enrollCommand);
return Future.value();
});
when(() => mockOutboundListener.read())
.thenAnswer((_) => Future.value(enrollResponse));
AtConnectionMetaData? atConnectionMetaData = OutboundConnectionMetadata()
..isAuthenticated = true;
when(() => mockOutBoundConnection.getMetaData())
.thenReturn(atConnectionMetaData);
when(() => mockOutBoundConnection.isInValid()).thenReturn(false);

expect(await atLookup.executeVerb(enrollVerbBuilder), enrollResponse);
});
});
}

0 comments on commit bf70ac7

Please sign in to comment.