Skip to content

Commit

Permalink
Add SessionManagerMock for testing and update loginQuery tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tsutsu3 committed Dec 11, 2024
1 parent 16f4fd5 commit 86650f5
Showing 1 changed file with 145 additions and 4 deletions.
149 changes: 145 additions & 4 deletions test/gateways/v6/api_gateway_v6_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:mockito/mockito.dart';
Expand All @@ -8,10 +9,56 @@ import 'package:pi_hole_client/gateways/v6/api_gateway_v6.dart';
import 'package:pi_hole_client/models/domain.dart';
import 'package:pi_hole_client/models/gateways.dart';
import 'package:pi_hole_client/models/server.dart';
import 'package:pi_hole_client/services/session_manager.dart';
import './api_gateway_v6_test.mocks.dart';

class SessionManagerMock implements SessionManager {
String? _sid;
String? _password;

SessionManagerMock(this._sid, this._password);

@override
get sid => _sid;

@override
get password async {
try {
return _password;
} catch (e) {
return null;
}
}

@override
Future<bool> save(String sid) async {
_sid = sid;
return true;
}

@override
Future<bool> load() async {
return true;
}

@override
Future<bool> delete() async {
_sid = null;
return true;
}

@override
Future<bool> savePassword(String password) async {
_password = password;
return true;
}
}

@GenerateMocks([http.Client])
void main() {
void main() async {
TestWidgetsFlutterBinding.ensureInitialized();
await dotenv.load(fileName: '.env');

group('loginQuery', () {
late Server server;
final sessinId = 'n9n9f6c3umrumfq2ese1lvu2pg';
Expand All @@ -26,8 +73,8 @@ void main() {
alias: 'example',
defaultServer: true,
apiVersion: SupportedApiVersions.v6,
sm: SessionManagerMock(sessinId, 'xxx123'),
);
server.sm.savePassword('xxx123');
});
test('Return success with valid password', () async {
final mockClient = MockClient();
Expand All @@ -51,12 +98,55 @@ void main() {
}),
200));

int callCount = 0;

when(mockClient.get(
Uri.parse(urls[1]),
headers: anyNamed('headers'),
)).thenAnswer((_) async => http.Response(
)).thenAnswer((_) async {
callCount++;
if (callCount == 1) {
return http.Response(
jsonEncode({
"error": {
"key": "unauthorized",
"message": "Unauthorized",
"hint": null
},
"took": 4.1484832763671875e-05
}),
401,
);
} else {
// 2回目以降の呼び出し: 正常なレスポンスを返す
return http.Response(
jsonEncode({"blocking": "enabled", "timer": null, "took": 0.003}),
200,
);
}
});

final response = await apiGateway.loginQuery();

expect(response.result, APiResponseType.success);
expect(response.sid, sessinId);
expect(response.status, 'enabled');
expect(response.log, isNull);
});

test('Return success with exist sid', () async {
final mockClient = MockClient();
final apiGateway = ApiGatewayV6(server, client: mockClient);

when(mockClient.get(
Uri.parse(urls[1]),
headers: anyNamed('headers'),
)).thenAnswer((_) async {
return http.Response(
jsonEncode({"blocking": "enabled", "timer": null, "took": 0.003}),
200));
200,
);
});

final response = await apiGateway.loginQuery();

Expand All @@ -70,6 +160,23 @@ void main() {
final mockClient = MockClient();
final apiGateway = ApiGatewayV6(server, client: mockClient);

when(mockClient.get(
Uri.parse(urls[1]),
headers: anyNamed('headers'),
)).thenAnswer((_) async {
return http.Response(
jsonEncode({
"error": {
"key": "unauthorized",
"message": "Unauthorized",
"hint": null
},
"took": 4.1484832763671875e-05
}),
401,
);
});

when(mockClient.post(
Uri.parse(urls[0]),
headers: anyNamed('headers'),
Expand Down Expand Up @@ -153,6 +260,23 @@ void main() {
'''
.trimLeft();

when(mockClient.get(
Uri.parse(urls[1]),
headers: anyNamed('headers'),
)).thenAnswer((_) async {
return http.Response(
jsonEncode({
"error": {
"key": "unauthorized",
"message": "Unauthorized",
"hint": null
},
"took": 4.1484832763671875e-05
}),
401,
);
});

when(mockClient.post(
Uri.parse(urls[0]),
headers: anyNamed('headers'),
Expand All @@ -175,6 +299,23 @@ void main() {
final mockClient = MockClient();
final apiGateway = ApiGatewayV6(server, client: mockClient);

when(mockClient.get(
Uri.parse(urls[1]),
headers: anyNamed('headers'),
)).thenAnswer((_) async {
return http.Response(
jsonEncode({
"error": {
"key": "unauthorized",
"message": "Unauthorized",
"hint": null
},
"took": 4.1484832763671875e-05
}),
401,
);
});

when(mockClient.post(
Uri.parse(urls[0]),
headers: anyNamed('headers'),
Expand Down

0 comments on commit 86650f5

Please sign in to comment.