2022-08-26 08:11:35 +00:00
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import 'package:hive/hive.dart';
|
|
|
|
import 'package:hive_test/hive_test.dart';
|
2023-03-01 21:52:13 +00:00
|
|
|
import 'package:stackwallet/db/hive/db.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:stackwallet/models/node_model.dart';
|
|
|
|
import 'package:stackwallet/services/node_service.dart';
|
2024-05-22 19:38:49 +00:00
|
|
|
import 'package:stackwallet/app_config.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
|
2024-05-15 21:39:43 +00:00
|
|
|
import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
bool wasRegistered = false;
|
|
|
|
setUp(() async {
|
|
|
|
await setUpTestHive();
|
|
|
|
if (!wasRegistered) {
|
|
|
|
wasRegistered = true;
|
|
|
|
Hive.registerAdapter(NodeModelAdapter());
|
|
|
|
}
|
|
|
|
await Hive.openBox<NodeModel>(DB.boxNameNodeModels);
|
|
|
|
await Hive.openBox<NodeModel>(DB.boxNamePrimaryNodes);
|
|
|
|
});
|
|
|
|
|
|
|
|
group("Empty nodes DB tests", () {
|
|
|
|
test("getPrimaryNodeFor", () {
|
|
|
|
final fakeStore = FakeSecureStorage();
|
|
|
|
final service = NodeService(secureStorageInterface: fakeStore);
|
2024-05-15 21:39:43 +00:00
|
|
|
final node = service.getPrimaryNodeFor(
|
|
|
|
currency: Bitcoin(CryptoCurrencyNetwork.main),
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
expect(node, null);
|
|
|
|
expect(fakeStore.interactions, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("setPrimaryNodeFor", () async {
|
|
|
|
final fakeStore = FakeSecureStorage();
|
|
|
|
final service = NodeService(secureStorageInterface: fakeStore);
|
2024-05-15 21:39:43 +00:00
|
|
|
final node = service.getPrimaryNodeFor(
|
|
|
|
currency: Bitcoin(CryptoCurrencyNetwork.main),
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
expect(node, null);
|
|
|
|
final node1 = NodeModel(
|
|
|
|
host: "host",
|
|
|
|
port: 42,
|
|
|
|
name: "btcnode",
|
|
|
|
id: "pnodeID",
|
|
|
|
useSSL: true,
|
|
|
|
enabled: true,
|
|
|
|
coinName: "bitcoin",
|
|
|
|
isFailover: true,
|
|
|
|
isDown: false,
|
|
|
|
);
|
|
|
|
await service.setPrimaryNodeFor(
|
2024-05-15 21:39:43 +00:00
|
|
|
coin: Bitcoin(CryptoCurrencyNetwork.main),
|
2022-08-26 08:11:35 +00:00
|
|
|
node: node1,
|
|
|
|
shouldNotifyListeners: true,
|
|
|
|
);
|
|
|
|
|
2024-05-15 21:39:43 +00:00
|
|
|
expect(
|
|
|
|
service
|
|
|
|
.getPrimaryNodeFor(currency: Bitcoin(CryptoCurrencyNetwork.main))
|
|
|
|
.toString(),
|
|
|
|
node1.toString(),
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
expect(fakeStore.interactions, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("getNodesFor", () {
|
|
|
|
final fakeStore = FakeSecureStorage();
|
|
|
|
final service = NodeService(secureStorageInterface: fakeStore);
|
2024-05-15 21:39:43 +00:00
|
|
|
final nodes = service.getNodesFor(Bitcoin(CryptoCurrencyNetwork.main));
|
2022-08-26 08:11:35 +00:00
|
|
|
expect(nodes.isEmpty, true);
|
|
|
|
expect(fakeStore.interactions, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("get primary nodes", () {
|
|
|
|
final fakeStore = FakeSecureStorage();
|
|
|
|
final service = NodeService(secureStorageInterface: fakeStore);
|
|
|
|
final nodes = service.primaryNodes;
|
|
|
|
expect(nodes.isEmpty, true);
|
|
|
|
expect(fakeStore.interactions, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("get nodes", () {
|
|
|
|
final fakeStore = FakeSecureStorage();
|
|
|
|
final service = NodeService(secureStorageInterface: fakeStore);
|
|
|
|
final nodes = service.nodes;
|
|
|
|
expect(nodes.isEmpty, true);
|
|
|
|
expect(fakeStore.interactions, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("get failover nodes", () {
|
|
|
|
final fakeStore = FakeSecureStorage();
|
|
|
|
final service = NodeService(secureStorageInterface: fakeStore);
|
2024-05-15 21:39:43 +00:00
|
|
|
final nodes = service.failoverNodesFor(
|
|
|
|
currency: Bitcoin(CryptoCurrencyNetwork.main),
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
expect(nodes.isEmpty, true);
|
|
|
|
expect(fakeStore.interactions, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("get non existing getNodeById", () {
|
|
|
|
final fakeStore = FakeSecureStorage();
|
|
|
|
final service = NodeService(secureStorageInterface: fakeStore);
|
|
|
|
final node = service.getNodeById(id: "Some ID");
|
|
|
|
expect(node, null);
|
|
|
|
expect(fakeStore.interactions, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("updateDefaults", () async {
|
|
|
|
final fakeStore = FakeSecureStorage();
|
|
|
|
final service = NodeService(secureStorageInterface: fakeStore);
|
|
|
|
await service.updateDefaults();
|
2024-05-15 21:39:43 +00:00
|
|
|
expect(
|
|
|
|
service.nodes.length,
|
2024-05-22 19:38:49 +00:00
|
|
|
AppConfig.coins.map((e) => e.defaultNode).length,
|
2024-05-15 21:39:43 +00:00
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
expect(fakeStore.interactions, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group("Defaults populated tests", () {
|
|
|
|
final nodeA = NodeModel(
|
|
|
|
host: "host1",
|
|
|
|
port: 421,
|
|
|
|
name: "btcnode",
|
|
|
|
id: "pnodeID1",
|
|
|
|
useSSL: true,
|
|
|
|
enabled: true,
|
|
|
|
coinName: "bitcoin",
|
|
|
|
isFailover: true,
|
|
|
|
isDown: false,
|
|
|
|
);
|
|
|
|
final nodeB = NodeModel(
|
|
|
|
host: "host2",
|
|
|
|
port: 422,
|
|
|
|
name: "btcnode",
|
|
|
|
id: "pnodeID2",
|
|
|
|
useSSL: true,
|
|
|
|
enabled: true,
|
|
|
|
coinName: "monero",
|
|
|
|
isFailover: true,
|
|
|
|
isDown: false,
|
|
|
|
);
|
|
|
|
final nodeC = NodeModel(
|
|
|
|
host: "host3",
|
|
|
|
port: 423,
|
|
|
|
name: "btcnode",
|
|
|
|
id: "pnodeID3",
|
|
|
|
useSSL: true,
|
|
|
|
enabled: true,
|
|
|
|
coinName: "epicCash",
|
|
|
|
isFailover: true,
|
|
|
|
isDown: false,
|
|
|
|
);
|
|
|
|
|
|
|
|
setUp(() async {
|
2022-11-09 22:43:26 +00:00
|
|
|
await NodeService(secureStorageInterface: FakeSecureStorage())
|
|
|
|
.updateDefaults();
|
2022-08-26 08:11:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test("setPrimaryNodeFor and getPrimaryNodeFor", () async {
|
|
|
|
final fakeStore = FakeSecureStorage();
|
|
|
|
final service = NodeService(secureStorageInterface: fakeStore);
|
2024-05-15 21:39:43 +00:00
|
|
|
expect(
|
|
|
|
service.getPrimaryNodeFor(
|
|
|
|
currency: Bitcoin(CryptoCurrencyNetwork.main),
|
|
|
|
),
|
|
|
|
null,
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
await service.setPrimaryNodeFor(
|
2024-05-15 21:39:43 +00:00
|
|
|
coin: Bitcoin(CryptoCurrencyNetwork.main),
|
|
|
|
node: Bitcoin(CryptoCurrencyNetwork.main).defaultNode,
|
|
|
|
);
|
|
|
|
expect(
|
|
|
|
service
|
|
|
|
.getPrimaryNodeFor(currency: Bitcoin(CryptoCurrencyNetwork.main))
|
|
|
|
.toString(),
|
|
|
|
Bitcoin(CryptoCurrencyNetwork.main).defaultNode.toString(),
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
expect(fakeStore.interactions, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("get primary nodes", () async {
|
|
|
|
final fakeStore = FakeSecureStorage();
|
|
|
|
final service = NodeService(secureStorageInterface: fakeStore);
|
|
|
|
await service.setPrimaryNodeFor(
|
2024-05-15 21:39:43 +00:00
|
|
|
coin: Bitcoin(CryptoCurrencyNetwork.main),
|
|
|
|
node: Bitcoin(CryptoCurrencyNetwork.main).defaultNode,
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
await service.setPrimaryNodeFor(
|
2024-05-15 21:39:43 +00:00
|
|
|
coin: Monero(CryptoCurrencyNetwork.main),
|
|
|
|
node: Monero(CryptoCurrencyNetwork.main).defaultNode,
|
|
|
|
);
|
|
|
|
expect(
|
|
|
|
service.primaryNodes.toString(),
|
|
|
|
[
|
|
|
|
Bitcoin(CryptoCurrencyNetwork.main).defaultNode,
|
|
|
|
Monero(CryptoCurrencyNetwork.main).defaultNode,
|
|
|
|
].toString(),
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
expect(fakeStore.interactions, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("get nodes", () async {
|
|
|
|
final fakeStore = FakeSecureStorage();
|
|
|
|
final service = NodeService(secureStorageInterface: fakeStore);
|
|
|
|
final nodes = service.nodes;
|
2024-05-22 19:38:49 +00:00
|
|
|
final defaults = AppConfig.coins.map((e) => e.defaultNode).toList();
|
2022-08-26 08:11:35 +00:00
|
|
|
|
2024-05-21 18:48:48 +00:00
|
|
|
nodes.sort((a, b) => a.id.compareTo(b.id));
|
|
|
|
defaults.sort((a, b) => a.id.compareTo(b.id));
|
2022-08-26 08:11:35 +00:00
|
|
|
|
|
|
|
expect(nodes.length, defaults.length);
|
|
|
|
expect(nodes.toString(), defaults.toString());
|
|
|
|
expect(fakeStore.interactions, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("add a node without a password", () async {
|
|
|
|
final fakeStore = FakeSecureStorage();
|
|
|
|
final service = NodeService(secureStorageInterface: fakeStore);
|
|
|
|
await service.add(nodeA, null, true);
|
2024-05-15 21:39:43 +00:00
|
|
|
expect(
|
|
|
|
service.nodes.length,
|
2024-05-22 19:38:49 +00:00
|
|
|
AppConfig.coins.map((e) => e.defaultNode).length + 1,
|
2024-05-15 21:39:43 +00:00
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
expect(fakeStore.interactions, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("add a node with a password", () async {
|
|
|
|
final fakeStore = FakeSecureStorage();
|
|
|
|
final service = NodeService(secureStorageInterface: fakeStore);
|
|
|
|
await service.add(nodeA, "some password", true);
|
2024-05-15 21:39:43 +00:00
|
|
|
expect(
|
|
|
|
service.nodes.length,
|
2024-05-22 19:38:49 +00:00
|
|
|
AppConfig.coins.map((e) => e.defaultNode).length + 1,
|
2024-05-15 21:39:43 +00:00
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
expect(fakeStore.interactions, 1);
|
|
|
|
expect(fakeStore.writes, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
group("Additional nodes in storage tests", () {
|
|
|
|
setUp(() async {
|
|
|
|
await DB.instance.put<NodeModel>(
|
2024-05-15 21:39:43 +00:00
|
|
|
boxName: DB.boxNameNodeModels,
|
|
|
|
key: nodeA.id,
|
|
|
|
value: nodeA,
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
await DB.instance.put<NodeModel>(
|
2024-05-15 21:39:43 +00:00
|
|
|
boxName: DB.boxNameNodeModels,
|
|
|
|
key: nodeB.id,
|
|
|
|
value: nodeB,
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
await DB.instance.put<NodeModel>(
|
2024-05-15 21:39:43 +00:00
|
|
|
boxName: DB.boxNameNodeModels,
|
|
|
|
key: nodeC.id,
|
|
|
|
value: nodeC,
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test("edit a node with a password", () async {
|
|
|
|
final fakeStore = FakeSecureStorage();
|
|
|
|
final service = NodeService(secureStorageInterface: fakeStore);
|
|
|
|
final currentLength = service.nodes.length;
|
|
|
|
|
|
|
|
final editedNode = nodeA.copyWith(name: "Some new kind of name");
|
|
|
|
|
|
|
|
await service.edit(editedNode, "123456", true);
|
|
|
|
|
|
|
|
expect(service.nodes.length, currentLength);
|
|
|
|
|
|
|
|
expect(
|
2024-05-15 21:39:43 +00:00
|
|
|
service.getNodeById(id: nodeA.id).toString(),
|
|
|
|
editedNode.toString(),
|
|
|
|
);
|
|
|
|
expect(
|
|
|
|
(await service.getNodeById(id: nodeA.id)!.getPassword(fakeStore))!,
|
|
|
|
"123456",
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
|
|
|
|
expect(fakeStore.interactions, 2);
|
|
|
|
expect(fakeStore.reads, 1);
|
|
|
|
expect(fakeStore.writes, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("delete a node", () async {
|
|
|
|
final fakeStore = FakeSecureStorage();
|
|
|
|
final service = NodeService(secureStorageInterface: fakeStore);
|
|
|
|
|
|
|
|
await service.delete(nodeB.id, true);
|
|
|
|
|
|
|
|
expect(
|
2024-05-15 21:39:43 +00:00
|
|
|
service.nodes.length,
|
2024-05-22 19:38:49 +00:00
|
|
|
AppConfig.coins.map((e) => e.defaultNode).length + 2,
|
2024-05-15 21:39:43 +00:00
|
|
|
);
|
|
|
|
expect(
|
|
|
|
service.nodes.where((element) => element.id == nodeB.id).length,
|
|
|
|
0,
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
|
|
|
|
expect(fakeStore.interactions, 1);
|
|
|
|
expect(fakeStore.deletes, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("set enabled", () async {
|
|
|
|
final fakeStore = FakeSecureStorage();
|
|
|
|
final service = NodeService(secureStorageInterface: fakeStore);
|
|
|
|
|
|
|
|
final preString = nodeC.toString();
|
|
|
|
|
|
|
|
await service.setEnabledState(nodeC.id, false, true);
|
|
|
|
|
|
|
|
final updatedNode = service.getNodeById(id: nodeC.id);
|
|
|
|
|
|
|
|
expect(preString == updatedNode.toString(), false);
|
|
|
|
expect(updatedNode!.enabled, false);
|
|
|
|
|
|
|
|
expect(fakeStore.interactions, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() async {
|
|
|
|
await tearDownTestHive();
|
|
|
|
});
|
|
|
|
}
|